|
cppthread 1.1.16
C++ Thread Library
|
The cppthread logger. More...
Public Member Functions | |
| logger () | |
| Initialize the logger. | |
| logger & | end () |
| 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. | |
| logger & | operator<< (log_level_t const &level) |
| Save the level at which to log this message. | |
| logger & | operator<< (logger &(*func)(logger &)) |
| Execute a function. | |
| template<typename T > | |
| logger & | operator<< (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. | |
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.
| 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().

| 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.
Definition at line 554 of file log.cpp.
References f_level, f_log, cppthread::to_string(), and unlock().
Referenced by cppthread::end().


| 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.
| [in] | level | The level to get the counter from. |
| 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.
| std::uint32_t cppthread::logger::get_warnings | ( | ) | const |
This function returns the number of warnings that were sent to the cppthread logger.
|
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.
Definition at line 238 of file log.cpp.
Referenced by operator<<(), and operator<<().

| 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:
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().
| [in] | level | The level this message represents. |
Definition at line 407 of file log.cpp.
References f_level, f_log, lock(), and unlock().

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.
| [in] | func | The function to call with this logger as parameter. |
|
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.
| [in] | v | The value to be output to this log message. |
Definition at line 68 of file log.h.

| void cppthread::logger::reset_counters | ( | ) |
|
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<<().

|
private |
|
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<<().
|
private |
This variable member is a stringstream which holds the current message. Once the end() is sent, it outputs the f_log.str() message.
Definition at line 85 of file log.h.
Referenced by end(), operator<<(), and operator<<().
This document is part of the Snap! Websites Project.
Copyright by Made to Order Software Corp.