https://mooseframework.inl.gov
SystemInfo.C
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://mooseframework.inl.gov
3 //*
4 //* All rights reserved, see COPYRIGHT for full restrictions
5 //* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6 //*
7 //* Licensed under LGPL 2.1, please see LICENSE for details
8 //* https://www.gnu.org/licenses/lgpl-2.1.html
9 
10 #include "SystemInfo.h"
11 #include "ExecutablePath.h"
12 #include "MooseRevision.h"
13 
14 #include "libmesh/libmesh_config.h"
15 
16 #include <sstream>
17 #include <sys/stat.h>
18 #include <iomanip>
19 #ifdef LIBMESH_HAVE_LOCALE
20 #include <locale>
21 #endif
22 
23 #ifdef LIBTORCH_ENABLED
24 #include <torch/version.h>
25 #endif
26 
27 SystemInfo::SystemInfo(int argc, char * argv[]) : _argc(argc), _argv(argv) {}
28 
29 std::string
31 {
32  std::stringstream oss;
33  oss << std::left << "Framework Information:\n"
34  << std::setw(25) << "MOOSE Version: " << MOOSE_REVISION << '\n'
35  << std::setw(25) << "LibMesh Version: " << LIBMESH_BUILD_VERSION << '\n';
36 #ifdef LIBMESH_DETECTED_PETSC_VERSION_MAJOR
37  oss << std::setw(25) << "PETSc Version: " << LIBMESH_DETECTED_PETSC_VERSION_MAJOR << '.'
38  << LIBMESH_DETECTED_PETSC_VERSION_MINOR << '.' << LIBMESH_DETECTED_PETSC_VERSION_SUBMINOR
39  << '\n';
40 #endif
41 #ifdef LIBMESH_DETECTED_SLEPC_VERSION_MAJOR
42  oss << std::setw(25) << "SLEPc Version: " << LIBMESH_DETECTED_SLEPC_VERSION_MAJOR << '.'
43  << LIBMESH_DETECTED_SLEPC_VERSION_MINOR << '.' << LIBMESH_DETECTED_SLEPC_VERSION_SUBMINOR
44  << '\n';
45 #endif
46 #ifdef LIBTORCH_ENABLED
47  oss << std::setw(25) << "Libtorch Version: " << TORCH_VERSION << '\n';
48 #endif
49 
50  // Current Time
51  oss << std::setw(25) << "Current Time: " << getTimeStamp() << "\n";
52 
53  // Executable Timestamp
54  std::string executable_path = getExecutable();
55  std::string executable_time = getExecutableTimeStamp(executable_path);
56  if (!executable_time.empty())
57  oss << std::setw(25) << "Executable Timestamp: " << executable_time << "\n";
58 
59  oss << std::endl;
60  return oss.str();
61 }
62 
63 // TODO: Update libmesh to handle this function "timestamp.h"
64 std::string
65 SystemInfo::getTimeStamp(std::time_t * time_stamp) const
66 {
67  struct tm * tm_struct;
68  std::time_t local_time;
69 
70 #ifdef LIBMESH_HAVE_LOCALE
71  // Create time_put "facet"
72  std::locale loc;
73  const std::time_put<char> & tp = std::use_facet<std::time_put<char>>(loc);
74 
75  if (!time_stamp)
76  {
77  // Call C-style time getting functions
78  local_time = time(NULL);
79  time_stamp = &local_time;
80  }
81  tm_struct = std::localtime(time_stamp);
82 
83  // Date will eventually be stored in this ostringstream's string
84  std::ostringstream date_stream;
85 
86  // See below for documentation on the use of the
87  // std::time_put::put() function
88  tp.put(date_stream, /*s*/
89  date_stream, /*str*/
90  date_stream.fill(), /*fill*/
91  tm_struct, /*tm*/
92  'c'); /*format*/
93 
94  // Another way to use it is to totally customize the format...
95  // char pattern[]="%d %B %Y %I:%M:%S %p";
96  // tp.put(date_stream, /*s*/
97  // date_stream, /*str*/
98  // date_stream.fill(), /*fill*/
99  // tm_struct, /*tm*/
100  // pattern, /*format begin*/
101  // pattern+sizeof(pattern)-1); /*format end */
102 
103  return date_stream.str();
104 #else
105  // C-stye code originally found here:
106  // http://people.sc.fsu.edu/~burkardt/cpp_src/timestamp/timestamp.C
107  // Author: John Burkardt, 24 September 2003
108  const unsigned int time_size = 40;
109  char time_buffer[time_size];
110 
111  if (!time_stamp)
112  {
113  local_time = time(NULL);
114  time_stamp = &local_time;
115  }
116  tm_struct = std::localtime(time_stamp);
117 
118  // No more than time_size characters will be placed into the array. If the
119  // total number of resulting characters, including the terminating
120  // NUL character, is not more than time_size, strftime() returns the
121  // number of characters in the array, not counting the terminating
122  // NUL. Otherwise, zero is returned and the buffer contents are
123  // indeterminate.
124  size_t len = strftime(time_buffer, time_size, "%c", tm_struct);
125 
126  if (len != 0)
127  return std::string(time_buffer);
128  else
129  {
130  libMesh::out << "Error formatting time buffer, returning empty string!" << std::endl;
131  return std::string("");
132  }
133 
134 #endif // LIBMESH_HAVE_LOCALE
135 }
136 
137 std::string
139 {
140  std::string executable(_argv[0]);
141  size_t last_slash = executable.find_last_of("/");
142  if (last_slash != std::string::npos)
143  executable = executable.substr(last_slash + 1);
144  std::string exe(Moose::getExecutablePath() + executable);
145  return exe;
146 }
147 
148 std::string
150 {
151  const std::string exe = getExecutable();
152  return getExecutableTimeStamp(exe);
153 }
154 
155 std::string
156 SystemInfo::getExecutableTimeStamp(const std::string & exe) const
157 {
158  struct stat attrib;
159  if (!stat(exe.c_str(), &attrib))
160  return getTimeStamp(&(attrib.st_mtime));
161  return "";
162 }
SystemInfo(int argc, char *argv[])
Definition: SystemInfo.C:27
std::string getExecutable() const
Definition: SystemInfo.C:138
std::string getExecutablePath()
This function returns the PATH of the running executable.
std::string getTimeStamp(std::time_t *time_stamp=NULL) const
Definition: SystemInfo.C:65
char ** _argv
Definition: SystemInfo.h:31
std::string getExecutableTimeStamp() const
Definition: SystemInfo.C:149
std::string getInfo() const
Definition: SystemInfo.C:30
OStreamProxy out(std::cout)