LCOV - code coverage report
Current view: top level - src/utils - SystemInfo.C (source / functions) Hit Total Coverage
Test: idaholab/moose framework: 2bf808 Lines: 43 48 89.6 %
Date: 2025-07-17 01:28:37 Functions: 5 6 83.3 %
Legend: Lines: hit not hit

          Line data    Source code
       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" ///< This file is auto-generated and contains the revision
      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       51361 : SystemInfo::SystemInfo(int argc, char * argv[]) : _argc(argc), _argv(argv) {}
      28             : 
      29             : std::string
      30       68897 : SystemInfo::getInfo() const
      31             : {
      32       68897 :   std::stringstream oss;
      33       68897 :   oss << std::left << "Framework Information:\n"
      34             :       << std::setw(25) << "MOOSE Version: " << MOOSE_REVISION << '\n'
      35       68897 :       << std::setw(25) << "LibMesh Version: " << LIBMESH_BUILD_VERSION << '\n';
      36             : #ifdef LIBMESH_DETECTED_PETSC_VERSION_MAJOR
      37       68897 :   oss << std::setw(25) << "PETSc Version: " << LIBMESH_DETECTED_PETSC_VERSION_MAJOR << '.'
      38       68897 :       << LIBMESH_DETECTED_PETSC_VERSION_MINOR << '.' << LIBMESH_DETECTED_PETSC_VERSION_SUBMINOR
      39       68897 :       << '\n';
      40             : #endif
      41             : #ifdef LIBMESH_DETECTED_SLEPC_VERSION_MAJOR
      42       68897 :   oss << std::setw(25) << "SLEPc Version: " << LIBMESH_DETECTED_SLEPC_VERSION_MAJOR << '.'
      43       68897 :       << LIBMESH_DETECTED_SLEPC_VERSION_MINOR << '.' << LIBMESH_DETECTED_SLEPC_VERSION_SUBMINOR
      44       68897 :       << '\n';
      45             : #endif
      46             : #ifdef LIBTORCH_ENABLED
      47        6909 :   oss << std::setw(25) << "Libtorch Version: " << TORCH_VERSION << '\n';
      48             : #endif
      49             : 
      50             :   // Current Time
      51       68897 :   oss << std::setw(25) << "Current Time: " << getTimeStamp() << "\n";
      52             : 
      53             :   // Executable Timestamp
      54       68897 :   std::string executable_path = getExecutable();
      55       68897 :   std::string executable_time = getExecutableTimeStamp(executable_path);
      56       68897 :   if (!executable_time.empty())
      57       68897 :     oss << std::setw(25) << "Executable Timestamp: " << executable_time << "\n";
      58             : 
      59       68897 :   oss << std::endl;
      60      137794 :   return oss.str();
      61       68897 : }
      62             : 
      63             : // TODO: Update libmesh to handle this function "timestamp.h"
      64             : std::string
      65      139150 : 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      139150 :   std::locale loc;
      73      139150 :   const std::time_put<char> & tp = std::use_facet<std::time_put<char>>(loc);
      74             : 
      75      139150 :   if (!time_stamp)
      76             :   {
      77             :     // Call C-style time getting functions
      78       69575 :     local_time = time(NULL);
      79       69575 :     time_stamp = &local_time;
      80             :   }
      81      139150 :   tm_struct = std::localtime(time_stamp);
      82             : 
      83             :   // Date will eventually be stored in this ostringstream's string
      84      139150 :   std::ostringstream date_stream;
      85             : 
      86             :   // See below for documentation on the use of the
      87             :   // std::time_put::put() function
      88      139150 :   tp.put(date_stream,        /*s*/
      89             :          date_stream,        /*str*/
      90      139150 :          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      278300 :   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      139150 : }
     136             : 
     137             : std::string
     138       69575 : SystemInfo::getExecutable() const
     139             : {
     140       69575 :   std::string executable(_argv[0]);
     141       69575 :   size_t last_slash = executable.find_last_of("/");
     142       69575 :   if (last_slash != std::string::npos)
     143       69575 :     executable = executable.substr(last_slash + 1);
     144       69575 :   std::string exe(Moose::getExecutablePath() + executable);
     145      139150 :   return exe;
     146       69575 : }
     147             : 
     148             : std::string
     149           0 : SystemInfo::getExecutableTimeStamp() const
     150             : {
     151           0 :   const std::string exe = getExecutable();
     152           0 :   return getExecutableTimeStamp(exe);
     153           0 : }
     154             : 
     155             : std::string
     156       69575 : SystemInfo::getExecutableTimeStamp(const std::string & exe) const
     157             : {
     158             :   struct stat attrib;
     159       69575 :   if (!stat(exe.c_str(), &attrib))
     160       69575 :     return getTimeStamp(&(attrib.st_mtime));
     161           0 :   return "";
     162             : }

Generated by: LCOV version 1.14