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