LCOV - code coverage report
Current view: top level - libMagAOX/logger/types - software_log.hpp (source / functions) Coverage Total Hit
Test: MagAOX Lines: 52.7 % 110 58
Test Date: 2026-01-03 21:03:39 Functions: 53.3 % 15 8

            Line data    Source code
       1              : /** \file software_log.hpp
       2              :  * \brief The MagAO-X logger software_log log type.
       3              :  * \author Jared R. Males (jaredmales@gmail.com)
       4              :  *
       5              :  * \ingroup logger_types_files
       6              :  *
       7              :  * History:
       8              :  * - 2018-08-18 created by JRM
       9              :  */
      10              : #ifndef logger_types_software_log_hpp
      11              : #define logger_types_software_log_hpp
      12              : 
      13              : #include <source_location>
      14              : 
      15              : #include "generated/software_log_generated.h"
      16              : #include "flatbuffer_log.hpp"
      17              : 
      18              : namespace MagAOX
      19              : {
      20              : namespace logger
      21              : {
      22              : 
      23              : /// Base class for software logs
      24              : /** Such logs are used to log software status, warnings, and errors. Does not have defaultLevel, so this can not be used
      25              :  * as a log type in logger.
      26              :  *
      27              :  * \includedoc sw_logs.dox.inc
      28              :  *
      29              :  *
      30              :  * \ingroup logger_types__basic
      31              :  */
      32              : struct software_log : public flatbuffer_log
      33              : {
      34              :     /// The event code
      35              :     static const flatlogs::eventCodeT eventCode = eventCodes::SOFTWARE_LOG;
      36              : 
      37              :     /// The type of the message
      38              :     struct messageT : public fbMessage
      39              :     {
      40              :         /// C'tor with full specification.
      41            2 :         messageT( const char    *file,      ///< [in] The file of the error, should always be \c \_\_FILE\_\_
      42              :                   const uint32_t line,      ///< [in] The line number of the error, should always be \c \_\_LINE\_\_
      43              :                   const int32_t  errnoCode, ///< [in] The errno code at the time of the log entry. Only errno should be
      44              :                                             ///< passed here, so strerror can be used later.
      45              :                   const int32_t otherCode,  ///< [in] Some other error code, such as a return value or library code.
      46              :                   const char   *explanation ///< [in] explanatory text about the software event
      47              :         )
      48            2 :         {
      49            2 :             auto _file = builder.CreateString( file );
      50            2 :             auto _expl = builder.CreateString( explanation );
      51              : 
      52            2 :             auto gs = CreateSoftware_log_fb( builder, _file, line, errnoCode, otherCode, _expl );
      53            2 :             builder.Finish( gs );
      54            2 :         }
      55              : 
      56              :         /// C'tor with full specification, overloaded for a std::string in explanation.
      57              :         /** \overload
      58              :          */
      59            6 :         messageT( const char    *file,      ///< [in] The file of the error, should always be \c \_\_FILE\_\_
      60              :                   const uint32_t line,      ///< [in] The line number of the error, should always be \c \_\_LINE\_\_
      61              :                   const int32_t  errnoCode, ///< [in] The errno code at the time of the log entry. Only errno should be
      62              :                                             ///< passed here, so strerror can be used later.
      63              :                   const int32_t      otherCode, ///< [in] Some other error code, such as a return value or library code.
      64              :                   const std::string &explanation ///< [in] explanatory text about the software event
      65              :         )
      66            6 :         {
      67            6 :             auto _file = builder.CreateString( file );
      68            6 :             auto _expl = builder.CreateString( explanation );
      69              : 
      70            6 :             auto gs = CreateSoftware_log_fb( builder, _file, line, errnoCode, otherCode, _expl );
      71            6 :             builder.Finish( gs );
      72            6 :         }
      73              : 
      74              :         /// C'tor for errno only -- code explanation can be looked up later.
      75            0 :         messageT( const char    *file,     ///< [in] The file of the error, should always be \c \_\_FILE\_\_
      76              :                   const uint32_t line,     ///< [in] The line number of the error, should always be \c \_\_LINE\_\_
      77              :                   const int32_t  errnoCode ///< [in] The errno code at the time of the log entry. Only errno should be
      78              :                                            ///< passed here, so strerror can be used later.
      79              :         )
      80            0 :         {
      81            0 :             auto _file = builder.CreateString( file );
      82              : 
      83            0 :             auto gs = CreateSoftware_log_fb( builder, _file, line, errnoCode, 0, 0 );
      84            0 :             builder.Finish( gs );
      85            0 :         }
      86              : 
      87              :         /// C'tor for errno with additional explanation.
      88            9 :         messageT( const char    *file,      ///< [in] The file of the error, should always be \c \_\_FILE\_\_
      89              :                   const uint32_t line,      ///< [in] The line number of the error, should always be \c \_\_LINE\_\_
      90              :                   const int32_t  errnoCode, ///< [in] The errno code at the time of the log entry. Only errno should be
      91              :                                             ///< passed here, so strerror can be used later.
      92              :                   const char *explanation   ///< [in] explanatory text about the software event
      93              :         )
      94            9 :         {
      95            9 :             auto _file = builder.CreateString( file );
      96            9 :             auto _expl = builder.CreateString( explanation );
      97              : 
      98            9 :             auto gs = CreateSoftware_log_fb( builder, _file, line, errnoCode, 0, _expl );
      99            9 :             builder.Finish( gs );
     100            9 :         }
     101              : 
     102              :         /// C'tor for errno with additional explanation, std::string overload.
     103            0 :         messageT( const char    *file,      ///< [in] The file of the error, should always be \c \_\_FILE\_\_
     104              :                   const uint32_t line,      ///< [in] The line number of the error, should always be \c \_\_LINE\_\_
     105              :                   const int32_t  errnoCode, ///< [in] The errno code at the time of the log entry. Only errno should be
     106              :                                             ///< passed here, so strerror can be used later.
     107              :                   const std::string &explanation ///< [in] explanatory text about the software event
     108              :         )
     109            0 :         {
     110            0 :             auto _file = builder.CreateString( file );
     111            0 :             auto _expl = builder.CreateString( explanation );
     112              : 
     113            0 :             auto gs = CreateSoftware_log_fb( builder, _file, line, errnoCode, 0, _expl );
     114            0 :             builder.Finish( gs );
     115            0 :         }
     116              : 
     117              :         /// C'tor with no codes, just the explanation.
     118           31 :         messageT( const char        *file, ///< [in] The file of the error, should always be \c \_\_FILE\_\_
     119              :                   const uint32_t     line, ///< [in] The line number of the error, should always be \c \_\_LINE\_\_
     120              :                   const std::string &explanation ///< [in] explanatory text about the software event
     121              :         )
     122           31 :         {
     123           31 :             auto _file = builder.CreateString( file );
     124           31 :             auto _expl = builder.CreateString( explanation );
     125              : 
     126           31 :             auto gs = CreateSoftware_log_fb( builder, _file, line, 0, 0, _expl );
     127           31 :             builder.Finish( gs );
     128           31 :         }
     129              : 
     130              :         /// C'tor for a trace log, only the file and line.
     131            1 :         messageT( const char    *file, ///< [in] The file of the error, should always be \c \_\_FILE\_\_
     132              :                   const uint32_t line  ///< [in] The line number of the error, should always be \c \_\_LINE\_\_
     133              :         )
     134            1 :         {
     135            1 :             auto _file = builder.CreateString( file );
     136              : 
     137            1 :             auto gs = CreateSoftware_log_fb( builder, _file, line, 0, 0, 0 );
     138            1 :             builder.Finish( gs );
     139            1 :         }
     140              : 
     141              :         // constructors using source location
     142              : 
     143            0 :         messageT( const int32_t  errnoCode, /**< [in] The errno code at the time of the log entry. Only errno should be
     144              :                                                        passed here, so strerror can be used later.*/
     145              :                   const int32_t otherCode,  ///< [in] Some other error code, such as a return value or library code.
     146              :                   const char   *explanation, ///< [in] explanatory text about the software event
     147              :                   const std::source_location &loc /**< [in] [opt] source location */
     148            0 :                   = std::source_location::current()
     149              :         )
     150            0 :         {
     151            0 :             auto _file = builder.CreateString( loc.file_name() );
     152            0 :             auto _expl = builder.CreateString( explanation );
     153              : 
     154            0 :             auto gs = CreateSoftware_log_fb( builder, _file, loc.line(), errnoCode, otherCode, _expl );
     155            0 :             builder.Finish( gs );
     156            0 :         }
     157              : 
     158              :         /// C'tor for errno only -- code explanation can be looked up later.
     159              :         messageT( const int32_t errnoCode, /**< [in] The errno code at the time of the log entry. Only errno should be
     160              :                                                     passed here, so strerror can be used later.*/
     161              :                   const std::source_location &loc /**< [in] [opt] source location */
     162              :                   = std::source_location::current() )
     163              :         {
     164              :             auto _file = builder.CreateString( loc.file_name() );
     165              : 
     166              :             auto gs = CreateSoftware_log_fb( builder, _file, loc.line(), errnoCode, 0, 0 );
     167              :             builder.Finish( gs );
     168              :         }
     169              : 
     170              :         /// C'tor for errno with additional explanation.
     171            0 :         messageT( const int32_t errnoCode, /**< [in] The errno code at the time of the log entry. Only errno should be
     172              :                                                      passed here, so strerror can be used later.*/
     173              :                   const char *explanation, ///< [in] explanatory text about the software event
     174              :                   const std::source_location &loc /**< [in] [opt] source location */
     175            0 :                   = std::source_location::current()
     176              :         )
     177            0 :         {
     178            0 :             auto _file = builder.CreateString( loc.file_name() );
     179            0 :             auto _expl = builder.CreateString( explanation );
     180              : 
     181            0 :             auto gs = CreateSoftware_log_fb( builder, _file, loc.line(), errnoCode, 0, _expl );
     182            0 :             builder.Finish( gs );
     183            0 :         }
     184              : 
     185              :         /// C'tor for errno with additional explanation.
     186            0 :         messageT( const int32_t errnoCode, /**< [in] The errno code at the time of the log entry. Only errno should be
     187              :                                                      passed here, so strerror can be used later.*/
     188              :                   const std::string & explanation, ///< [in] explanatory text about the software event
     189              :                   const std::source_location &loc /**< [in] [opt] source location */
     190            0 :                   = std::source_location::current()
     191              :         )
     192            0 :         {
     193            0 :             auto _file = builder.CreateString( loc.file_name() );
     194            0 :             auto _expl = builder.CreateString( explanation );
     195              : 
     196            0 :             auto gs = CreateSoftware_log_fb( builder, _file, loc.line(), errnoCode, 0, _expl );
     197            0 :             builder.Finish( gs );
     198            0 :         }
     199              : 
     200              :         /// C'tor with no codes, just the explanation.
     201            5 :         messageT( const std::string          &explanation, /**< [in] explanatory text about the software event */
     202              :                   const std::source_location &loc          /**< [in] [opt] source location */
     203           10 :                   = std::source_location::current() )
     204            5 :         {
     205            5 :             auto _file = builder.CreateString( loc.file_name() );
     206            5 :             auto _expl = builder.CreateString( explanation );
     207              : 
     208            5 :             auto gs = CreateSoftware_log_fb( builder, _file, loc.line(), 0, 0, _expl );
     209              : 
     210            5 :             builder.Finish( gs );
     211            5 :         }
     212              : 
     213              :         /// C'tor for a trace log, only the file and line.
     214            0 :         messageT( const std::source_location &loc /**< [in] [opt] source location */
     215              :                   = std::source_location::current() )
     216            0 :         {
     217            0 :             auto _file = builder.CreateString( loc.file_name() );
     218              : 
     219            0 :             auto gs = CreateSoftware_log_fb( builder, _file, loc.line(), 0, 0, 0 );
     220            0 :             builder.Finish( gs );
     221            0 :         }
     222              :     };
     223              : 
     224            0 :     static bool verify( flatlogs::bufferPtrT &logBuff, ///< [in] Buffer containing the flatbuffer serialized message.
     225              :                         flatlogs::msgLenT     len      ///< [in] length of msgBuffer.
     226              :     )
     227              :     {
     228            0 :         auto verifier = flatbuffers::Verifier( static_cast<uint8_t *>( flatlogs::logHeader::messageBuffer( logBuff ) ),
     229            0 :                                                static_cast<size_t>( len ) );
     230            0 :         return VerifySoftware_log_fbBuffer( verifier );
     231              :     }
     232              : 
     233              :     /// Get the message formatted for human consumption.
     234           54 :     static std::string msgString( void *msgBuffer,      /**< [in] Buffer containing the flatbuffer serialized message.*/
     235              :                                   flatlogs::msgLenT len /**< [in] [unused] length of msgBuffer.*/
     236              :     )
     237              :     {
     238              : 
     239              :         static_cast<void>( len );
     240              : 
     241           54 :         auto rgs = GetSoftware_log_fb( msgBuffer );
     242              : 
     243           54 :         std::string ret = "SW FILE: ";
     244           54 :         if( rgs->file() != nullptr )
     245              :         {
     246           54 :             ret += rgs->file()->c_str();
     247              :         }
     248              :         else
     249              :         {
     250            0 :             ret += "????";
     251              :         }
     252              : 
     253           54 :         ret += std::format( " LINE: {}", rgs->line() );
     254              : 
     255           54 :         if( rgs->errnoCode() )
     256              :         {
     257           17 :             ret += std::format( "  ERRNO: {} [{}]", rgs->errnoCode(), rgs->errnoCode() );
     258              :         }
     259           54 :         if( rgs->otherCode() )
     260              :         {
     261            0 :             ret += std::format( "  CODE: {}", rgs->otherCode() );
     262            0 :             if( rgs->explanation() )
     263              :             {
     264            0 :                 ret += std::format( " [{}]", rgs->explanation()->c_str() );
     265              :             }
     266              :         }
     267           54 :         else if( rgs->explanation() )
     268              :         {
     269           53 :             ret += " >";
     270           53 :             ret += rgs->explanation()->c_str();
     271              :         }
     272           54 :         return ret;
     273            0 :     }
     274              : 
     275              :     /// Get an empty logMetaDetail because meta data doesn't make sense for this log
     276              :     /**
     277              :      * \returns an empty logMetaDetail
     278              :      */
     279            1 :     static logMetaDetail getAccessor( const std::string &member /**< [in] the name of the member */ )
     280              :     {
     281              :         static_cast<void>( member );
     282              : 
     283            1 :         std::cerr << "meta data doesn't make sense for software_log.\n";
     284            1 :         return logMetaDetail();
     285              :     }
     286              : };
     287              : 
     288              : /// Software EMERGENCY log entry
     289              : /** This should only be used for a system-wide emergency requiring operator or automatic shutdown.  Not for a process
     290              :  * specific problem.
     291              :  * \includedoc sw_logs.dox.inc
     292              :  * \ingroup logger_types
     293              :  */
     294              : struct software_emergency : public software_log
     295              : {
     296              :     /// The default level
     297              :     static const flatlogs::logPrioT defaultLevel = flatlogs::logPrio::LOG_EMERGENCY;
     298              : };
     299              : 
     300              : /// Software ALERT log entry
     301              : /** This should only be used for a system-wide emergency requiring operator or automatic action.  Not for a process
     302              :  * specific problem.
     303              :  * \includedoc sw_logs.dox.inc
     304              :  * \ingroup logger_types
     305              :  */
     306              : struct software_alert : public software_log
     307              : {
     308              :     /// The default level
     309              :     static const flatlogs::logPrioT defaultLevel = flatlogs::logPrio::LOG_ALERT;
     310              : };
     311              : 
     312              : /// Software CRITICAL log entry
     313              : /** This should only be used if the process is going to shutdown.
     314              :  * \includedoc sw_logs.dox.inc
     315              :  * \ingroup logger_types
     316              :  */
     317              : struct software_critical : public software_log
     318              : {
     319              :     /// The default level
     320              :     static const flatlogs::logPrioT defaultLevel = flatlogs::logPrio::LOG_CRITICAL;
     321              : };
     322              : 
     323              : /// Software ERR log entry
     324              : /** Used to record and error that the process will attempt to recover from.
     325              :  * \includedoc sw_logs.dox.inc
     326              :  * \ingroup logger_types
     327              :  */
     328              : struct software_error : public software_log
     329              : {
     330              :     /// The default level
     331              :     static const flatlogs::logPrioT defaultLevel = flatlogs::logPrio::LOG_ERROR;
     332              : };
     333              : 
     334              : /// Software WARN log entry
     335              : /** Used to record an abnormal condition.
     336              :  * \includedoc sw_logs.dox.inc
     337              :  * \ingroup logger_types
     338              :  */
     339              : struct software_warning : public software_log
     340              : {
     341              :     /// The default level
     342              :     static const flatlogs::logPrioT defaultLevel = flatlogs::logPrio::LOG_WARNING;
     343              : };
     344              : 
     345              : /// Software NOTICE log entry
     346              : /** Used to record a normal but signficant event or condition.
     347              :  * \includedoc sw_logs.dox.inc
     348              :  * \ingroup logger_types
     349              :  */
     350              : struct software_notice : public software_log
     351              : {
     352              :     /// The default level
     353              :     static const flatlogs::logPrioT defaultLevel = flatlogs::logPrio::LOG_NOTICE;
     354              : };
     355              : 
     356              : /// Software INFO log entry
     357              : /** \includedoc sw_logs.dox.inc
     358              :  * Used to record a normal event or condition.  This is the lowest priority used in normal operations.
     359              :  * \ingroup logger_types
     360              :  */
     361              : struct software_info : public software_log
     362              : {
     363              :     /// The default level
     364              :     static const flatlogs::logPrioT defaultLevel = flatlogs::logPrio::LOG_INFO;
     365              : };
     366              : 
     367              : /// Software DEBUG log entry
     368              : /** \includedoc sw_logs.dox.inc
     369              :  * \ingroup logger_types
     370              :  */
     371              : struct software_debug : public software_log
     372              : {
     373              :     /// The default level
     374              :     static const flatlogs::logPrioT defaultLevel = flatlogs::logPrio::LOG_DEBUG;
     375              : };
     376              : 
     377              : /// Software DEBUG2 log entry
     378              : /** \includedoc sw_logs.dox.inc
     379              :  * \ingroup logger_types
     380              :  */
     381              : struct software_debug2 : public software_log
     382              : {
     383              :     /// The default level
     384              :     static const flatlogs::logPrioT defaultLevel = flatlogs::logPrio::LOG_DEBUG2;
     385              : };
     386              : 
     387              : } // namespace logger
     388              : } // namespace MagAOX
     389              : 
     390              : #endif // logger_types_software_log_hpp
        

Generated by: LCOV version 2.0-1