nidas v1.2.3
Public Member Functions | Protected 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>

Inheritance diagram for nidas::util::LogContext:
Inheritance graph
[legend]

Public Member Functions

 LogContext (int level, const char *file, const char *function, int line, const char *tags=0)
 Initialize LogContextState with the static context information, then add this context to the global registry of log points and set the active() status according to the current log configuration.
 
 ~LogContext ()
 Since the constructor registers this context with the global log points, the destructor unregisters it.
 
 LogContext (const LogContext &)=delete
 LogContext allocates a resource: it's registration with the global log points.
 
LogContextoperator= (const LogContext &)=delete
 
bool active () const
 Return true if log messages from this context have been enabled.
 
void setActive (bool active)
 Enable this log point according to active.
 
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.
 
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 LogContextState.
 
LogMessage log () const
 Return a LogMessage associated with this LogContextState, so the message will be logged through this context when the LogMessage goes out of scope.
 

Protected 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 LogContextState 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 LogContextState::log() for using a LogContext instance to enclose more complicated blocks of logging output.

A LogContext itself is not thread-safe except for updating the global registry of log points. Rather than guard the _active flag against concurrent access, it is assumed the flag will be "consistent enough". In other words, it will be mostly read, and it is not critical that the read state be immediately updated on each logging config change.

Thread-safe access can be assured for most cases by constructing a LogContext in thread-local storage. (See NIDAS_LOOGER_THREADLOCAL.) A LogContext always records the thread which created it, and the name of that thread is returned by the threadName() method. When the LogContext is used to send a log message, then the name of the current thread is used in the log message, even if different from the creating thread.

Constructor & Destructor Documentation

◆ LogContext() [1/2]

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

Initialize LogContextState with the static context information, then add this context to the global registry of log points and set the active() status according to the current log configuration.

References current_scheme, log_points, and nidas::util::LoggerPrivate::reconfig().

◆ ~LogContext()

LogContext::~LogContext ( )

Since the constructor registers this context with the global log points, the destructor unregisters it.

References log_points.

◆ LogContext() [2/2]

nidas::util::LogContext::LogContext ( const LogContext & )
delete

LogContext allocates a resource: it's registration with the global log points.

The global logging modifies the active flag of this instance through a pointer to this instance, so only one instance can own that pointer and be responsible for deallocating it (unregistering). Thus copy and assignment are prohibited.

Member Function Documentation

◆ active()

bool nidas::util::LogContextState::active ( ) const
inlineinherited

Return true if log messages from this context have been enabled.

If this returns false, then there is no point in generating and submitting a log message.

References nidas::util::LogContextState::_active.

Referenced by nidas::core::SampleTracer::active(), nidas::core::SampleTracer::active(), nidas::dynld::SampleInputStream::sampleFromHeader(), and nidas::util::LogContextState::setActive().

◆ filename()

const char * nidas::util::LogContextState::filename ( ) const
inlineinherited

◆ function()

const char * nidas::util::LogContextState::function ( ) const
inlineinherited

◆ level()

int nidas::util::LogContextState::level ( ) const
inlineinherited

◆ levelName()

std::string nidas::util::LogContextState::levelName ( ) const
inlineinherited

◆ line()

int nidas::util::LogContextState::line ( ) const
inlineinherited

◆ operator=()

LogContext & nidas::util::LogContext::operator= ( const LogContext & )
delete

◆ setActive()

void nidas::util::LogContextState::setActive ( bool active)
inlineinherited

Enable this log point according to active.

References nidas::util::LogContextState::_active, and nidas::util::LogContextState::active().

◆ tags()

const char * nidas::util::LogContextState::tags ( ) const
inlineinherited

◆ threadName()

std::string LogContextState::threadName ( ) const
inherited

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 nidas::util::LogContextState::_threadId, and nidas::util::Thread::lookupThread().

Friends And Related Symbol Documentation

◆ nidas::util::Logger

friend class nidas::util::Logger
friend

◆ nidas::util::LoggerPrivate

friend class nidas::util::LoggerPrivate
friend

Member Data Documentation

◆ _active

bool nidas::util::LogContextState::_active
protectedinherited

◆ _file

const char* nidas::util::LogContextState::_file
protectedinherited

◆ _function

const char* nidas::util::LogContextState::_function
protectedinherited

◆ _level

int nidas::util::LogContextState::_level
protectedinherited

◆ _line

int nidas::util::LogContextState::_line
protectedinherited

◆ _tags

const char* nidas::util::LogContextState::_tags
protectedinherited

◆ _threadId

pthread_t nidas::util::LogContextState::_threadId
protectedinherited

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