nidas
v1.2-1520
|
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... | |
LogContext & | operator= (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 |
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.
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 | ( | ) |
References log_points, and nidas::util::Logger::mutex.
|
private |
No copying.
|
inline |
Return true if log messages from this context would be accepted.
If this returns false, then there is no point in generating and submitting a log message.
References _active.
Referenced by nidas::core::SampleTracer::active(), nidas::dynld::raf::SyncRecordSource::allocateRecord(), nidas::dynld::StatisticsCruncher::computeStats(), nidas::core::SamplePipeline::connect(), nidas::dynld::raf::TwoD_USB::countParticle(), nidas::dynld::StatisticsCruncher::createCombinations(), nidas::core::BadSampleFilter::invalidSampleHeader(), nidas::util::Logger::log(), nidas::util::Logger::msg_locked(), nidas::core::NidasApp::parseInputs(), NidsMerge::parseRunstring(), StatsProcess::parseRunstring(), nidas::dynld::raf::SyncRecordSource::preLoadCalibrations(), nidas::dynld::raf::TwoD64_USB::process(), nidas::dynld::iss::TiltSensor::process(), nidas::dynld::isff::Wind2D::process(), nidas::dynld::raf::TwoD64_USB::processImageRecord(), nidas::core::CalFile::readCFInclude(), nidas::dynld::StatisticsCruncher::receive(), nidas::util::SocketImpl::receive(), DataPrep::run(), nidas::core::SampleSorter::run(), nidas::dynld::SampleInputStream::sampleFromHeader(), nidas::core::CharacterSensor::searchSampleScanners(), nidas::dynld::raf::SyncRecordSource::sendSyncRecord(), nidas::util::LogScheme::show_log_point(), nidas::dynld::isff::NCAR_TRH::validate(), and nidas::dynld::SampleOutputStream::write().
|
inline |
References _file.
Referenced by nidas::util::LogConfig::matches(), nidas::util::Logger::msg_locked(), and nidas::util::LogScheme::show_log_point().
|
inline |
References _function.
Referenced by nidas::util::LogConfig::matches(), nidas::util::Logger::msg_locked(), and nidas::util::LogScheme::show_log_point().
|
inline |
References _level.
Referenced by nidas::util::LogConfig::matches(), and nidas::util::Logger::msg_locked().
|
inline |
References _level, and nidas::util::logLevelToString().
Referenced by nidas::util::Logger::msg_locked(), and nidas::util::LogScheme::show_log_point().
|
inline |
References _line.
Referenced by nidas::util::LogConfig::matches(), nidas::util::Logger::msg_locked(), and nidas::util::LogScheme::show_log_point().
|
private |
No assignment.
|
inline |
References _tags.
Referenced by nidas::util::LogConfig::matches(), and nidas::util::LogScheme::show_log_point().
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().
|
friend |
|
friend |
|
private |
Referenced by active(), and LogContext().
|
private |
Referenced by filename(), and nidas::util::LoggerPrivate::get_active_flag().
|
private |
Referenced by function(), and nidas::util::LoggerPrivate::get_active_flag().
|
private |
Referenced by nidas::util::LoggerPrivate::get_active_flag(), level(), and levelName().
|
private |
Referenced by nidas::util::LoggerPrivate::get_active_flag(), and line().
|
private |
Referenced by tags().
|
private |
Referenced by threadName().