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

Generated by: LCOV version 1.14