Main   Namespaces   Classes   Hierarchy   Annotated   Files   Compound   Global   Pages  

Logger Class Reference

Logger supporting message levels and multiple output streams received from multiple threads. More...

#include <Logger.hpp>

List of all members.

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 >
Loggeroperator<< (const TYPE &type)
 Pipes text into the log.

Static Public Member Functions

static LoggerGlobal ()
 Returns the global Logger object.

Private Member Functions

ThreadBufferGetThreadBuffer ()
 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< LogLevelm_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, LogLevelm_thread_level
 Current log level for each thread.

Static Private Attributes

static const int MAX_THREADS = 16
 Maximum number of threads handled at once.

Detailed Description

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.


Constructor & Destructor Documentation

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.


Member Function Documentation

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 (  ) 
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().

template<typename TYPE >
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().


Member Data Documentation

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().

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().


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


6 Jan 2011 Doxygen 1.6.3