nidas  v1.2-1520
Public Member Functions | Private Member Functions | Private Attributes | Friends | List of all members
nidas::util::LogContext Class Reference

The LogContext is created at a point in the application code and filled in with details about that log point, such as the file and line number, and the function name of the containing function, and the log level of that log point, such as LOGGER_DEBUG or LOGGER_ERROR. More...

#include <Logger.h>

Public Member Functions

 LogContext (int level, const char *file, const char *function, int line, const char *tags=0)
 The LogContext constructor initializes all the static context information, adds this context to the global registry of log points, then sets the active() status according to the current log configuration. More...
 
 ~LogContext ()
 
bool active () const
 Return true if log messages from this context would be accepted. More...
 
const char * filename () const
 
const char * function () const
 
int line () const
 
int level () const
 
const char * tags () const
 
std::string threadName () const
 Return the name of the thread which created this context. More...
 
std::string levelName () const
 
void log (const std::string &msg) const
 Convenience method which writes the given message to the current Logger instance, passing this object as the LogContext instance. More...
 
LogMessage log () const
 Return a LogMessage associated with this LogContext, so the message will be logged through this context when the LogMessage goes out of scope. More...
 

Private Member Functions

 LogContext (const LogContext &)
 No copying. More...
 
LogContextoperator= (const LogContext &)
 No assignment. More...
 

Private Attributes

int _level
 
const char * _file
 
const char * _function
 
int _line
 
const char * _tags
 
bool _active
 
pthread_t _threadId
 

Friends

class nidas::util::Logger
 
class nidas::util::LoggerPrivate
 

Detailed Description

The LogContext is created at a point in the application code and filled in with details about that log point, such as the file and line number, and the function name of the containing function, and the log level of that log point, such as LOGGER_DEBUG or LOGGER_ERROR.

Except for the log level and tags, the rest of the LogContext can be filled in with CPP macros and predefined compiler symbols like PRETTY_FUNCTION. The LogContext holds its own active state, so a simple, inline boolean test determines whether a log point has been activated and thus needs to generate and log its message. Otherwise the log point can be skipped with minimal time overhead. Where further discrimination of log points is needed, a log point can be associated with a string of tags, and a LogConfig can match the log point by one of its tags. The tags string is empty unless specified at creation.

The LogContext effectively stashes context info for a log point so that info can be used in the log message output, and so that info can be used as criteria for selecting which messages are logged. Thus a LogContext registers itself with the Logger class, and the Logger class can enable or disable a log point according to the current configuration scheme, Logging Configuration Schemes. The LogContext contains a flag which is false if the log point is not active, meaning almost all overhead of generating a log message can be avoided by testing the active() flag first. The typical usage of a LogContext will be as a static object, and the log macros DLOG, ELOG, and so on test the active() flag before passing a message.

The ultimate goal is to make all log messages always available in the runtime while incurring as little performance overhead as possible when they are not needed, and without requiring alternate compile configurations. However, if it should ever be necessary, then the log macros can still be defined as completely empty.

See LogContext::log() for using a LogContext instance to enclose more complicated blocks of logging output.

A LogContext has no support for concurrency. If it might be shared among multiple threads, then it should be guarded with a lock or created in thread-local storage. However, a LogContext does record the thread which created it, and the name of that thread is returned by the threadName() method.

Constructor & Destructor Documentation

LogContext::LogContext ( int  level,
const char *  file,
const char *  function,
int  line,
const char *  tags = 0 
)

The LogContext constructor initializes all the static context information, adds this context to the global registry of log points, then sets the active() status according to the current log configuration.

The constructor also records the ID of the current thread, so if this LogContext is instantiated in thread-local storage, all messages logged through it will be associated with the correct thread name.

References _active, current_scheme, nidas::util::LoggerPrivate::get_active_flag(), nidas::util::LogScheme::getShowLogPoints(), log_points, nidas::util::Logger::mutex, and nidas::util::LogScheme::show_log_point().

LogContext::~LogContext ( )
nidas::util::LogContext::LogContext ( const LogContext )
private

No copying.

Member Function Documentation

bool nidas::util::LogContext::active ( ) const
inline
const char* nidas::util::LogContext::filename ( ) const
inline
const char* nidas::util::LogContext::function ( ) const
inline
int nidas::util::LogContext::level ( ) const
inline
std::string nidas::util::LogContext::levelName ( ) const
inline
int nidas::util::LogContext::line ( ) const
inline
LogContext& nidas::util::LogContext::operator= ( const LogContext )
private

No assignment.

const char* nidas::util::LogContext::tags ( ) const
inline
std::string LogContext::threadName ( ) const

Return the name of the thread which created this context.

This is not necessarily the name of the currently running thread, so it may not be the same as the thread named in a log message when LogScheme::ThreadField is enabled in the LogScheme.

References _threadId, nidas::util::Thread::getName(), and nidas::util::Thread::lookupThread().

Friends And Related Function Documentation

friend class nidas::util::Logger
friend
friend class nidas::util::LoggerPrivate
friend

Member Data Documentation

bool nidas::util::LogContext::_active
private

Referenced by active(), and LogContext().

const char* nidas::util::LogContext::_file
private
const char* nidas::util::LogContext::_function
private
int nidas::util::LogContext::_level
private
int nidas::util::LogContext::_line
private
const char* nidas::util::LogContext::_tags
private

Referenced by tags().

pthread_t nidas::util::LogContext::_threadId
private

Referenced by threadName().


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