|
cppthread 1.1.16
C++ Thread Library
|
Implementation of the logging facility. More...
#include "cppthread/log.h"#include "cppthread/exception.h"#include <cstring>#include <iostream>#include <snapdev/poison.h>
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. | |
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.
Definition in file log.cpp.
|
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.
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.
You could even create a function which returns a reference to that local mutex (just make sure you don't copy that mutex).
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().


| 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).
| [in] | callback | The 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().


| 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.
| cppthread_invalid_error | If the log level is not one of the know log levels, then the function raises this exception. |
| [in] | level | The message log level to convert to a string. |
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().


| logger cppthread::log |
This object is used to send data to the logger.
Definition at line 90 of file log.cpp.
Referenced by cppthread::mutex::mutex(), cppthread::guard::~guard(), cppthread::mutex::~mutex(), cppthread::runner::~runner(), cppthread::thread::~thread(), cppthread::mutex::broadcast(), cppthread::mutex::dated_wait(), cppthread::runner::enter(), cppthread::thread::init(), cppthread::thread::internal_enter(), cppthread::thread::internal_leave(), cppthread::thread::internal_run(), cppthread::thread::internal_thread(), cppthread::runner::leave(), cppthread::mutex::lock(), cppthread::mutex::safe_broadcast(), cppthread::mutex::safe_signal(), cppthread::mutex::signal(), cppthread::thread::start(), cppthread::mutex::timed_wait(), cppthread::mutex::try_lock(), cppthread::mutex::unlock(), and cppthread::mutex::wait().
This document is part of the Snap! Websites Project.
Copyright by Made to Order Software Corp.