Logger supporting message levels and multiple output streams received from multiple threads. More...
#include <Logger.hpp>
Classes | |
struct | ThreadBuffer |
Buffer for a thread of execution. More... | |
Public Member Functions | |
Logger () | |
Creates a logger object. | |
~Logger () | |
Destructor. | |
void | AddStream (std::ostream &stream, LogLevel level) |
Adds a handler to this logger at the given level. | |
void | ClearStreams () |
Removes all output streams. | |
void | SetLevel (LogLevel level) |
Sets the level of all messages this logger receives from now on. | |
void | Flush () |
Flushes the log. | |
template<typename TYPE > | |
Logger & | operator<< (const TYPE &type) |
Pipes text into the log. | |
Static Public Member Functions | |
static Logger & | Global () |
Returns the global Logger object. | |
Private Member Functions | |
ThreadBuffer & | GetThreadBuffer () |
Returns the buffer for the current thread. | |
LogLevel | GetThreadLevel () |
Returns the current loglevel for the thread. | |
Private Attributes | |
std::vector< std::ostream * > | m_streams |
Streams for this log. | |
std::vector< LogLevel > | m_levels |
Level for each stream. | |
pthread_mutex_t | m_buffer_mutex |
Threads must grab this mutex before modifying m_thread_buffer or printing output. | |
ThreadBuffer | m_thread_buffer [MAX_THREADS] |
Buffers for up to MAX_THREADS active threads. | |
pthread_mutex_t | m_map_mutex |
Threads must grab this mutex before modifying m_thread_level. | |
std::map< pthread_t, LogLevel > | m_thread_level |
Current log level for each thread. | |
Static Private Attributes | |
static const int | MAX_THREADS = 16 |
Maximum number of threads handled at once. |
Logger supporting message levels and multiple output streams received from multiple threads.
A Logger can have several streams. Each stream is assigned a level. When the Logger receives a message at level L, all streams with level at least L will be sent a copy of the message.
Logger handles messages coming from multiple threads internally. A LogLevel for each thread is maintained, and separate buffers are used for messages being formed simulataneously in different threads. Blocking is used to grant a thread exclusive access to the streams when printing; this potentially could cause slowdown if several threads are dumping lots of text simultaneously.
Definition at line 91 of file Logger.hpp.
Logger::Logger | ( | ) |
Creates a logger object.
By default, log outputs to std::cerr at LOG_LEVEL_INFO.
Definition at line 12 of file Logger.cpp.
References AddStream(), LOG_LEVEL_INFO, m_thread_buffer, and MAX_THREADS.
Logger::~Logger | ( | ) |
Destructor.
Definition at line 21 of file Logger.cpp.
void Logger::AddStream | ( | std::ostream & | stream, | |
LogLevel | level | |||
) |
Adds a handler to this logger at the given level.
Definition at line 33 of file Logger.cpp.
References m_levels, and m_streams.
Referenced by HexProgram::InitLog(), and Logger().
void Logger::ClearStreams | ( | ) |
Removes all output streams.
Definition at line 39 of file Logger.cpp.
References m_levels, and m_streams.
Referenced by HexProgram::InitLog().
void Logger::Flush | ( | ) |
Flushes the log.
Definition at line 94 of file Logger.cpp.
References Logger::ThreadBuffer::buffer, GetThreadBuffer(), GetThreadLevel(), Logger::ThreadBuffer::id, m_buffer_mutex, m_levels, m_streams, and HexColorUtil::toString().
Referenced by operator<<(), and HexProgram::ShutdownLog().
Logger::ThreadBuffer & Logger::GetThreadBuffer | ( | ) | [private] |
Returns the buffer for the current thread.
Definition at line 45 of file Logger.cpp.
References Logger::ThreadBuffer::id, m_buffer_mutex, m_thread_buffer, and MAX_THREADS.
Referenced by Flush(), and operator<<().
LogLevel Logger::GetThreadLevel | ( | ) | [private] |
Returns the current loglevel for the thread.
Definition at line 85 of file Logger.cpp.
References m_map_mutex, and m_thread_level.
Referenced by Flush().
Logger & Logger::Global | ( | ) | [static] |
Returns the global Logger object.
Definition at line 25 of file Logger.cpp.
Referenced by HexProgram::InitLog(), LogConfig(), LogFine(), LogInfo(), LogSevere(), LogWarning(), and HexProgram::ShutdownLog().
Logger & Logger::operator<< | ( | const TYPE & | type | ) | [inline] |
Pipes text into the log.
If text ends in a '
', log is flushed.
Definition at line 163 of file Logger.hpp.
References Logger::ThreadBuffer::buffer, Flush(), and GetThreadBuffer().
void Logger::SetLevel | ( | LogLevel | level | ) |
Sets the level of all messages this logger receives from now on.
Definition at line 78 of file Logger.cpp.
References m_map_mutex, and m_thread_level.
Referenced by LogConfig(), LogFine(), LogInfo(), LogSevere(), and LogWarning().
pthread_mutex_t Logger::m_buffer_mutex [private] |
Threads must grab this mutex before modifying m_thread_buffer or printing output.
Definition at line 149 of file Logger.hpp.
Referenced by Flush(), and GetThreadBuffer().
std::vector<LogLevel> Logger::m_levels [private] |
Level for each stream.
Definition at line 145 of file Logger.hpp.
Referenced by AddStream(), ClearStreams(), and Flush().
pthread_mutex_t Logger::m_map_mutex [private] |
Threads must grab this mutex before modifying m_thread_level.
Definition at line 156 of file Logger.hpp.
Referenced by GetThreadLevel(), and SetLevel().
std::vector<std::ostream*> Logger::m_streams [private] |
Streams for this log.
Definition at line 142 of file Logger.hpp.
Referenced by AddStream(), ClearStreams(), and Flush().
ThreadBuffer Logger::m_thread_buffer[MAX_THREADS] [private] |
Buffers for up to MAX_THREADS active threads.
Definition at line 152 of file Logger.hpp.
Referenced by GetThreadBuffer(), and Logger().
std::map<pthread_t, LogLevel> Logger::m_thread_level [private] |
Current log level for each thread.
Definition at line 159 of file Logger.hpp.
Referenced by GetThreadLevel(), and SetLevel().
const int Logger::MAX_THREADS = 16 [static, private] |
Maximum number of threads handled at once.
Definition at line 126 of file Logger.hpp.
Referenced by GetThreadBuffer(), and Logger().