Belle II Software  release-08-01-10
LogSystem Class Reference

Class for logging debug, info and error messages. More...

#include <LogSystem.h>

Collaboration diagram for LogSystem:

Public Member Functions

void addLogConnection (LogConnectionBase *logConnection)
 Adds a log connection object which is used to the send the logging messages. More...
 
void resetLogConnections ()
 Removes all log connections.
 
void resetLogging ()
 Reset logging system to defaults: empty all log messages and reset connections to the default.
 
LogConfiggetLogConfig ()
 Returns global log system configuration.
 
void addPackageLogConfig (const std::string &package, const LogConfig &logConfig)
 Add the per package log configuration. More...
 
LogConfiggetPackageLogConfig (const std::string &package)
 Get the log configuration for the package with the given name. More...
 
bool isLevelEnabled (LogConfig::ELogLevel level, int debugLevel=0, const char *package=nullptr) const
 Returns true if the given log level is allowed by the log system (i.e. More...
 
bool sendMessage (LogMessage &&message)
 Sends a log message using the log connection object. More...
 
void resetMessageCounter ()
 Resets the message counter and error log by setting all message counts to 0.
 
int getMessageCounter (LogConfig::ELogLevel logLevel) const
 Returns the number of logging calls per log level. More...
 
const LogConfiggetCurrentLogConfig (const char *package=nullptr) const
 Returns the current LogConfig object used by the logging system. More...
 
LogConfig::ELogLevel getCurrentLogLevel (const char *package=nullptr) const
 Returns the current log level used by the logging system. More...
 
int getCurrentDebugLevel (const char *package=nullptr) const
 Returns the current debug level used by the logging system. More...
 
unsigned getMaxMessageRepetitions () const
 Get maximum number of repetitions before silencing "identical" log messages. More...
 
void setMaxMessageRepetitions (unsigned repetitions)
 Set maximum number of repetitions before silencing "identical" log messages. More...
 
void printErrorSummary ()
 Print error/warning summary at end of execution. More...
 
void enableErrorSummary (bool on)
 enable/disable error/warning summary after successful execution and B2FATAL. More...
 
void updateModule (const LogConfig *moduleLogConfig=nullptr, const std::string &moduleName="")
 Sets the log configuration to the given module log configuration and sets the module name This method should only be called by the EventProcessor. More...
 

Static Public Member Functions

static LogSystemInstance ()
 Static method to get a reference to the LogSystem instance. More...
 
static void enableDebug ()
 Enable debug output.
 
static bool debugEnabled ()
 Is debug output enabled?
 

Static Public Attributes

static const unsigned int c_errorSummaryMaxLines = 10000
 Error log will contain at most this many lines.
 

Private Member Functions

 LogSystem ()
 The constructor is hidden to avoid that someone creates an instance of this class.
 
 LogSystem (const LogSystem &)=delete
 Disable/Hide the copy constructor.
 
LogSystemoperator= (const LogSystem &)=delete
 Disable/Hide the copy assignment operator.
 
 ~LogSystem ()
 The LogSystem destructor.
 
void incMessageCounter (LogConfig::ELogLevel logLevel)
 Increases the counter for the called message level by one. More...
 
bool deliverMessageToConnections (const LogMessage &msg)
 Do nothing else than to send the message to all connected connections. More...
 
void showText (LogConfig::ELogLevel level, const std::string &txt, int info=LogConfig::c_Message)
 Send a custom message which looks like a log message but should not be counted as such. More...
 

Private Attributes

std::vector< LogConnectionBase * > m_logConnections
 Stores the pointers to the log connection objects. More...
 
LogConfig m_logConfig
 The global log system configuration.
 
const LogConfigm_moduleLogConfig
 log config of current module
 
std::string m_moduleName
 The current module name.
 
std::map< std::string, LogConfigm_packageLogConfigs
 Stores the log configuration objects for packages.
 
bool m_printErrorSummary
 Wether to re-print errors-warnings encountered during execution at the end.
 
std::unordered_map< LogMessage, int, LogMessage::TextHasher, LogMessage::TextHasherm_messageLog {100}
 Count of previous log messages for the summary and to suppress repetitive messages.
 
unsigned int m_maxErrorRepetition {0}
 Maximum number to show the same message. More...
 
unsigned int m_suppressedMessages {0}
 The amount of messages we have suppressed so far just to get an indication we print this from time to time.
 
int m_messageCounter [LogConfig::c_Default]
 Counts the number of messages sent per message level.
 

Static Private Attributes

static bool s_debugEnabled = false
 Global flag for fast checking if debug output is enabled.
 

Detailed Description

Class for logging debug, info and error messages.

For log messages the following preprocessor macros should be used: B2DEBUG for code development B2INFO for informational messages, e.g. about opened files, processed events, etc. B2RESULT for informational summary messages, e.g. about number of selected events B2WARNING for potential problems that the user should pay attention to B2ERROR for things that went wrong and have to be fixed B2FATAL for situations were the program execution can not be continued

By default the Logger sends messages to std::cout.

Using preprocessor macros has two advantages.

  • the log statements can be removed from the code completely by redefining the macros.
  • they allow to add information about the location (package, function, file, line).

This class is designed as a singleton.

Definition at line 46 of file LogSystem.h.

Member Function Documentation

◆ addLogConnection()

void addLogConnection ( LogConnectionBase logConnection)

Adds a log connection object which is used to the send the logging messages.

Note
The LogSystem class takes ownership of the log connection object.
Parameters
logConnectionPointer to the logging connection object. The LogSystem takes ownership of this object.

Definition at line 38 of file LogSystem.cc.

39 {
40  m_logConnections.push_back(logConnection);
41 }
std::vector< LogConnectionBase * > m_logConnections
Stores the pointers to the log connection objects.
Definition: LogSystem.h:205

◆ addPackageLogConfig()

void addPackageLogConfig ( const std::string &  package,
const LogConfig logConfig 
)
inline

Add the per package log configuration.

Adds a new log configuration for the given package name.

Parameters
packageThe name of the package whose log configuration should be added.
logConfigThe log configuration which should be assigned to the given package.

Definition at line 87 of file LogSystem.h.

◆ deliverMessageToConnections()

bool deliverMessageToConnections ( const LogMessage msg)
private

Do nothing else than to send the message to all connected connections.

No bookkeeping, just and and be done

Definition at line 53 of file LogSystem.cc.

◆ enableErrorSummary()

void enableErrorSummary ( bool  on)
inline

enable/disable error/warning summary after successful execution and B2FATAL.

During normal basf2 execution, this is turned on by the Framework object. For unit tests (i.e. without a Framework object), the default is used instead (off).

Definition at line 181 of file LogSystem.h.

◆ getCurrentDebugLevel()

int getCurrentDebugLevel ( const char *  package = nullptr) const
inline

Returns the current debug level used by the logging system.

See also
getCurrentLogConfig()

Definition at line 153 of file LogSystem.h.

◆ getCurrentLogLevel()

LogConfig::ELogLevel getCurrentLogLevel ( const char *  package = nullptr) const
inline

Returns the current log level used by the logging system.

See also
getCurrentLogConfig()

Definition at line 146 of file LogSystem.h.

◆ getMaxMessageRepetitions()

unsigned getMaxMessageRepetitions ( ) const
inline

Get maximum number of repetitions before silencing "identical" log messages.

Identity only checks for log level and message content (excluding variables)

Definition at line 160 of file LogSystem.h.

◆ getMessageCounter()

int getMessageCounter ( LogConfig::ELogLevel  logLevel) const

Returns the number of logging calls per log level.

Parameters
logLevelThe logging level which should be returned.
Returns
The number of message calls for the given log level.

Definition at line 161 of file LogSystem.cc.

◆ getPackageLogConfig()

LogConfig& getPackageLogConfig ( const std::string &  package)
inline

Get the log configuration for the package with the given name.

If no package specific configuration exists a new one is created.

Parameters
packageThe name of the package whose log configuration should be returned
Returns
The log configuration of the given package

Definition at line 96 of file LogSystem.h.

◆ incMessageCounter()

void incMessageCounter ( LogConfig::ELogLevel  logLevel)
private

Increases the counter for the called message level by one.

Parameters
logLevelThe logging level which should be increased by one.

Definition at line 297 of file LogSystem.cc.

◆ Instance()

LogSystem & Instance ( )
static

Static method to get a reference to the LogSystem instance.

Returns
A reference to an instance of this class.

Definition at line 31 of file LogSystem.cc.

◆ printErrorSummary()

void printErrorSummary ( )

Print error/warning summary at end of execution.

Does nothing if m_printErrorSummary is false.

Definition at line 206 of file LogSystem.cc.

◆ sendMessage()

bool sendMessage ( LogMessage &&  message)

Sends a log message using the log connection object.

This method should only be called by the logger macros.

Parameters
messageThe log message object. The message might be moved into internal storage and is not valid any more after the method call.
Returns
Returns true if the message could be send.

Definition at line 69 of file LogSystem.cc.

◆ setMaxMessageRepetitions()

void setMaxMessageRepetitions ( unsigned  repetitions)
inline

Set maximum number of repetitions before silencing "identical" log messages.

Identity only checks for log level and message content (excluding variables)

Definition at line 167 of file LogSystem.h.

◆ showText()

void showText ( LogConfig::ELogLevel  level,
const std::string &  txt,
int  info = LogConfig::c_Message 
)
private

Send a custom message which looks like a log message but should not be counted as such.

This is used in a few places to emit descriptive messages related to other messages without danger of being suppressed

Definition at line 62 of file LogSystem.cc.

◆ updateModule()

void updateModule ( const LogConfig moduleLogConfig = nullptr,
const std::string &  moduleName = "" 
)
inline

Sets the log configuration to the given module log configuration and sets the module name This method should only be called by the EventProcessor.

Parameters
moduleLogConfigPointer to the logging configuration object of the module. Set to NULL to use the global log configuration.
moduleNameName of the module.

Definition at line 191 of file LogSystem.h.

Member Data Documentation

◆ m_logConnections

std::vector<LogConnectionBase*> m_logConnections
private

Stores the pointers to the log connection objects.

(owned by us)

Definition at line 205 of file LogSystem.h.

◆ m_maxErrorRepetition

unsigned int m_maxErrorRepetition {0}
private

Maximum number to show the same message.

If zero we don't suppress repeated messages

Definition at line 219 of file LogSystem.h.


The documentation for this class was generated from the following files: