nidas v1.2.3
|
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) | |
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. | |
LogContext & | operator= (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 |
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.
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 | ( | ) |
Since the constructor registers this context with the global log points, the destructor unregisters it.
References log_points.
|
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.
|
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().
|
inlineinherited |
References nidas::util::LogContextState::_file.
|
inlineinherited |
References nidas::util::LogContextState::_function.
|
inlineinherited |
References nidas::util::LogContextState::_level.
|
inlineinherited |
References nidas::util::LogContextState::_level, and nidas::util::logLevelToString().
|
inlineinherited |
References nidas::util::LogContextState::_line.
|
delete |
|
inlineinherited |
Enable this log point according to active
.
References nidas::util::LogContextState::_active, and nidas::util::LogContextState::active().
|
inlineinherited |
References nidas::util::LogContextState::_tags.
|
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().
|
friend |
|
friend |
|
protectedinherited |
Referenced by nidas::util::LogContextState::active(), and nidas::util::LogContextState::setActive().
|
protectedinherited |
Referenced by nidas::util::LogContextState::filename().
|
protectedinherited |
Referenced by nidas::util::LogContextState::function().
|
protectedinherited |
Referenced by nidas::util::LogContextState::level(), and nidas::util::LogContextState::levelName().
|
protectedinherited |
Referenced by nidas::util::LogContextState::line().
|
protectedinherited |
Referenced by nidas::util::LogContextState::tags().
|
protectedinherited |
Referenced by nidas::util::LogContextState::threadName().