cppthread 1.1.16
C++ Thread Library
Functions | Variables
log.cpp File Reference

Implementation of the logging facility. More...

#include "cppthread/log.h"
#include "cppthread/exception.h"
#include <cstring>
#include <iostream>
#include <snapdev/poison.h>
Include dependency graph for log.cpp:

Go to the source code of this file.

Functions

void cppthread::create_system_mutex ()
 Function used to initialize the system mutex.
 
void cppthread::set_log_callback (log_callback callback)
 Set a callback function.
 
std::string cppthread::to_string (log_level_t level)
 Convert a log level to a string.
 

Variables

logger cppthread::log
 The logger object used to send logs out.
 

Detailed Description

The library is very often used by daemons meaning that it will be running on its own in the background. For this reason, all the output is done through the log facility.

This interface defines a function which you are expected to call to setup a callback. By default, the callback is set to a function that simply prints errors to std::cerr.

Note
Note that the log facility is used only in extreme cases. It was made fully thread safe, however, that implementation is considered slow. Especially, if two different threads attempt to log a message simultaneously, only one of them will be allowed to build their message and the other one will be blocked for the entire time. Our snaplogger, on the other hand, allows any number of threads to generate errors in parallel, only the processing at the end, which can be done asynchronously, requires serialization.

Definition in file log.cpp.

Function Documentation

◆ create_system_mutex()

void cppthread::create_system_mutex ( )
extern

This is an internal function called by the logger constructor.

This is unfortunate, but the system mutex create relies on the log object to exist and be properly initialized before it gets created. So we call this function from the constructor of the logger.

Note
This means it's not exact properly initialized since the constructor did not yet return. However, it is guaranteed to work properly since we do not allow derivations or virtual tables.

The logger constructor is called whenever the cppthread library is loaded and that happens in your main thread at initialization time. This means any of your code can access the g_system_mutex as required.

This function is not defined externally so that other users can't call it from the outside (there would be no need anyway). Just in case, if called a second time, the function writes an error message and fails with a call to std::terminate().

Another way to safely create a new mutex is to use a static variable in a function. The initialization of that static variable will always be safe so that mutex will be available to all your threads even if you already had multiple threads the first time that function was called.

void myfunc()
{
// here "m" is a safe to use mutex
...do atomic work here...
}
Lock a mutex in an RAII manner.
Definition guard.h:42
A mutex object to ensures atomicity.
Definition mutex.h:55

You could even create a function which returns a reference to that local mutex (just make sure you don't copy that mutex).

Note
Although this function gets called from the logger contructor, it is not used by the logger at all.

Definition at line 926 of file mutex.cpp.

References cppthread::create_system_mutex(), and cppthread::g_system_mutex.

Referenced by cppthread::logger::logger(), and cppthread::create_system_mutex().

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

◆ set_log_callback()

void cppthread::set_log_callback ( log_callback  callback)

Set a callback function used to redirect the logs generated by the cppthread library and any library that makes use of this log facility (i.e. the advgetopt project does so).

Note
You should not use this facility unless you do not have access to the snaplogger, somehow. The snaplogger is a much more advanced and better interface especially in a multithreaded application.
Parameters
[in]callbackThe function to call whenever a log is generated.

Definition at line 170 of file log.cpp.

References cppthread::set_log_callback().

Referenced by cppthread::set_log_callback().

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

◆ to_string()

std::string cppthread::to_string ( log_level_t  level)

This function transforms a log_level_t value to a string which can then be used in a log message.

Exceptions
cppthread_invalid_errorIf the log level is not one of the know log levels, then the function raises this exception.
Parameters
[in]levelThe message log level to convert to a string.
Returns
A string representing the log level.

Definition at line 610 of file log.cpp.

References cppthread::to_string().

Referenced by cppthread::pool< W, A >::worker_thread_t::worker_thread_t(), cppthread::logger::end(), and cppthread::to_string().

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

Variable Documentation

◆ log

logger cppthread::log

This document is part of the Snap! Websites Project.

Copyright by Made to Order Software Corp.