cppthread 1.1.16
C++ Thread Library
Public Member Functions | Static Private Member Functions | Private Attributes | List of all members
cppthread::logger Class Referencefinal

The cppthread logger. More...

Public Member Functions

 logger ()
 Initialize the logger.
 
loggerend ()
 End the logger's message.
 
std::uint32_t get_counter (log_level_t level) const
 Get one of the level counters.
 
std::uint32_t get_errors () const
 Get the number of errors that occurred so far.
 
std::uint32_t get_warnings () const
 Get the number of warnings that occurred so far.
 
loggeroperator<< (log_level_t const &level)
 Save the level at which to log this message.
 
loggeroperator<< (logger &(*func)(logger &))
 Execute a function.
 
template<typename T >
loggeroperator<< (T const &v)
 Send any type of data to the logger.
 
void reset_counters ()
 Reset all the log message counters to zero.
 

Static Private Member Functions

static void lock ()
 Lock the system so a log can be emitted properly.
 
static void unlock ()
 Unlock the logger once we are done with it.
 

Private Attributes

std::uint32_t f_counters [static_cast< int >(log_level_t::LOG_LEVEL_SIZE)] = {}
 
log_level_t f_level = log_level_t::error
 The level of this message.
 
std::stringstream f_log = std::stringstream()
 The log message.
 

Detailed Description

The cppthread is a dependency of the snaplogger so we have to have our own logger implementation in this class.

This class is very basic. It only supports 5 severity levels and a simple mechanism to write messages to a log file or a callback.

Note
If you can use the snaplogger library, then you should avoid using this cppthread implementation. The snaplogger will automatically provide a callback to this cppthread implementation and output the logs as required. It's just that the snaplogger is way more powerful.
This is actually the main reason why the class is marked final.

Definition at line 58 of file log.h.

Constructor & Destructor Documentation

◆ logger()

cppthread::logger::logger ( )

The logger makes sure that the system mutex gets allocated. This way if an error occurs in that initialization process, it will happen after the logger gets initialized. Although it can happen before the logger initialization returns, the object initialization is already complete when we reach the call to the create_system_mutex() function.

Definition at line 207 of file log.cpp.

References cppthread::create_system_mutex().

Here is the call graph for this function:

Member Function Documentation

◆ end()

logger & cppthread::logger::end ( )

This function is called whenever you apply the end() function to the logger. It processes the message and sends it to the log callback function or prints it to std::cerr.

If no callback was setup, the function throws away any debug messages and prints out the other messages to std::cerr.

Note
There is only one logger for the cppthread log system. It gets locked whenever you send the message level, which is expected to be done first. The end() function is in charge of unlocking before printing out the message or calling your callback.
Returns
A reference to the logger object.

Definition at line 554 of file log.cpp.

References f_level, f_log, cppthread::to_string(), and unlock().

Referenced by cppthread::end().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_counter()

std::uint32_t cppthread::logger::get_counter ( log_level_t  level) const

Whenever a log is sent to the cppthread logger, one of its counter gets incremented by 1. This is useful if you want to know whether error messages were sent to the logger, see the get_errors() function too as it includes a total of all the errors that happened.

This function is useful to check the number of debug and info messages that were processed.

Todo:
The counter functions are not thread safe.
Parameters
[in]levelThe level to get the counter from.
Returns
The number of times that level received a log message.
See also
get_errors()
get_warnings()

Definition at line 483 of file log.cpp.

◆ get_errors()

std::uint32_t cppthread::logger::get_errors ( ) const

This function returns the total number of errors and fatal errors that were sent to the cppthread logger.

Todo:
The counter functions are not thread safe.
Returns
The number of errors generated so far.
See also
get_counter()
get_warnings()

Definition at line 511 of file log.cpp.

◆ get_warnings()

std::uint32_t cppthread::logger::get_warnings ( ) const

This function returns the number of warnings that were sent to the cppthread logger.

Todo:
The counter functions are not thread safe.
Returns
The number of warnings generated so far.
See also
get_counter()
get_errors()

Definition at line 531 of file log.cpp.

◆ lock()

void cppthread::logger::lock ( )
staticprivate

This function allows the locking of the logger. This ensures that we can use a simple object even in a multithread environment. The only potential problem now is a deadlock.

Note that you may call the lock() function any number of times. It will not count the number of calls. It locks only once. If already locked, it is like a passthrough. If another thread already acquired the lock, then the function blocks until the other thread is done with the logger.

Todo:
This locking system is not exception safe. If one of the functions called to generate the log message throws, then we will stay locked.

Definition at line 238 of file log.cpp.

Referenced by operator<<(), and operator<<().

Here is the caller graph for this function:

◆ operator<<() [1/3]

logger & cppthread::logger::operator<< ( log_level_t const &  level)

This function gets called whenever you apply a level. This is expected as the very first parameter of the log. It is very important to specify the level first because this function locks the cppthread logger which is otherwise shared between all the threads. Thus not doing so first is likely to create mangled messages if not crashes of your application.

The end() function unlock()'s the logger.

The supported levels are defined in the log_level_t enumeration:

  • log_level_t::debug
  • log_level_t::info
  • log_level_t::warning
  • log_level_t::error
  • log_level_t::fatal

The fatal error level has no specific effect. It just displays a level of "fatal". If you want to stop the software, you are in charge of calling std::terminate() or throw an exception that won't be caught such as the advgetopt::getopt_exit().

Note
The snaplogger captures these logs and it has a special handler you can setup to capture fatal errors. This allows your application to exit your software when such an error occurs. It uses an exception for the purpose.
Although calling this function multiple times is safe, it has the side effect of counting each level so it really should be called only once.
Parameters
[in]levelThe level this message represents.
Returns
A reference to this logger.

Definition at line 407 of file log.cpp.

References f_level, f_log, lock(), and unlock().

Here is the call graph for this function:

◆ operator<<() [2/3]

logger & cppthread::logger::operator<< ( logger &(*)(logger &)  func)

This call accepts a special value representing a logger function which gets called with the logger as a reference parameter.

This is currently used by the end() function.

Parameters
[in]funcThe function to call with this logger as parameter.
Returns
A reference to this logger.

Definition at line 441 of file log.cpp.

◆ operator<<() [3/3]

template<typename T >
cppthread::logger::operator<< ( T const &  v)
inline

This operator allows to send data of type T to the logger.

The logger makes use of a stringstream to generate the final message. The type T must be a type that the stringstream supports. In most cases, these are the types that the iostream supports.

Parameters
[in]vThe value to be output to this log message.
Returns
A reference to this logger.

Definition at line 68 of file log.h.

References f_log, and lock().

Here is the call graph for this function:

◆ reset_counters()

void cppthread::logger::reset_counters ( )

This function resets all the log message counters to zero. This is useful if you run in a server and want to count the logs for one run of a process opposed to forever while running.

Todo:
The counter functions are not thread safe.

Definition at line 457 of file log.cpp.

◆ unlock()

void cppthread::logger::unlock ( )
staticprivate

Whenever the end() function gets called, the logger reacts by sending the collected message to the user defined callback or to std::cerr.

Note that the number of calls to the lock are not limited. However, the unlock() function is called only once in the end() function. Attempting to unlock the logger more than once is a bug and undefined behavior may ensue. The class tries to emit an error when that happens, but it is unlikely to catch the error each time.

Definition at line 346 of file log.cpp.

Referenced by end(), and operator<<().

Here is the caller graph for this function:

Member Data Documentation

◆ f_counters

std::uint32_t cppthread::logger::f_counters[static_cast< int >(log_level_t::LOG_LEVEL_SIZE)] = {}
private

Definition at line 86 of file log.h.

◆ f_level

cppthread::logger::f_level = log_level_t::error
private

This variable member holds the level of the message. The level can be specified within the set of << operators.

The default is to display informational, warning, error, and fatal error messages to the error output (stderr). Other messages (debug) get dropped. If you have a callback assigned (i.e. for example, if you use snaplogger in your application), then all the messages get redirected to that callback.

Definition at line 84 of file log.h.

Referenced by end(), and operator<<().

◆ f_log

cppthread::logger::f_log = std::stringstream()
private

This variable member is a stringstream which holds the current message. Once the end() is sent, it outputs the f_log.str() message.

Note
In order for messages to not criss-cross each other, the implementation makes use of a global lock. That means only one thread can be sending a log message at a time. The unlock() happens when the end() function gets called.

Definition at line 85 of file log.h.

Referenced by end(), operator<<(), and operator<<().


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

This document is part of the Snap! Websites Project.

Copyright by Made to Order Software Corp.