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