nidas v1.2.3
Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | Static Protected Attributes | Private Member Functions | Static Private Member Functions | Friends | List of all members
nidas::util::Logger Class Reference

Simple logging class, based on UNIX syslog interface. More...

#include <Logger.h>

Public Member Functions

void log (int severity, const char *file, const char *fn, int line, const char *fmt,...)
 Build a message from a printf-format string and variable args, and log the message.
 
void msg (const LogContextState &lc, const std::string &msg)
 Send a log message for the given LogContext lc.
 
void msg (const LogContextState &lc, const LogMessage &m)
 

Static Public Member Functions

static LoggercreateInstance (const std::string &ident, int logopt, int facility, const char *TZ=0)
 Create a syslog-type Logger.
 
static LoggercreateInstance (std::ostream *out=0)
 Create a logger to the given output stream.
 
static LoggergetInstance ()
 Retrieve the current Logger singleton instance.
 
static void destroyInstance ()
 Destroy any existing Logger instance.
 
static void init ()
 Initializes the logging implementation.
 
static void setScheme (const std::string &name)
 Set the current scheme.
 
static void setScheme (const LogScheme &scheme)
 Set the current scheme to the given scheme.
 
static void updateScheme (const LogScheme &scheme)
 Update or insert this scheme in the collection of log schemes.
 
static LogScheme getScheme (const std::string &name)
 Get the scheme with the given name.
 
static LogScheme getScheme ()
 Return the current LogScheme, creating the default LogScheme if it has not been set yet.
 
static bool knownScheme (const std::string &name)
 Return true if the scheme with the given name is known, false otherwise.
 
static void clearSchemes ()
 Erase all known schemes and reset the active scheme to the name initialized at application start.
 

Protected Member Functions

 Logger (const std::string &ident, int logopt, int facility, const char *TZ=0)
 
 Logger (std::ostream *)
 
 Logger ()
 
void setTZ (const char *val)
 Set the timezone to be used in the log messages, whether passed to syslog or formatted to include the current time.
 

Protected Attributes

std::ostream * _output
 
bool _syslogit
 
char * _loggerTZ
 
char * _saveTZ
 
char * _ident
 

Static Protected Attributes

static Logger_instance = 0
 

Private Member Functions

 ~Logger ()
 
 Logger (const Logger &)
 No copying.
 
Loggeroperator= (const Logger &)
 No assignment.
 

Static Private Member Functions

static Loggerget_instance_locked (std::ostream *out=0)
 Return the current instance or create a default, without locking.
 

Friends

class nidas::util::LogContext
 
class nidas::util::LogScheme
 
class nidas::util::LoggerPrivate
 

Detailed Description

Simple logging class, based on UNIX syslog interface.

The Logger is a singleton instance through which all log messages are sent. It determines whether messages are sent to syslog or written to an output stream provided by the application, such as std::cerr, or a file output stream, or an ostringstream. The Logger class contains static methods to manage active and available LogSchemes, but those are completely separate from the current Logger instance. Replacing the Logger instance does not change the active LogScheme.

Constructor & Destructor Documentation

◆ Logger() [1/4]

Logger::Logger ( const std::string & ident,
int logopt,
int facility,
const char * TZ = 0 )
protected

References _ident, and setTZ().

◆ Logger() [2/4]

Logger::Logger ( std::ostream * out)
protected

◆ Logger() [3/4]

Logger::Logger ( )
protected

◆ ~Logger()

Logger::~Logger ( )
private

References _ident, _loggerTZ, _output, _saveTZ, and _syslogit.

◆ Logger() [4/4]

nidas::util::Logger::Logger ( const Logger & )
private

No copying.

Member Function Documentation

◆ createInstance() [1/2]

Logger * Logger::createInstance ( const std::string & ident,
int logopt,
int facility,
const char * TZ = 0 )
static

Create a syslog-type Logger.

See syslog(2) man page.

Parameters
identsee syslog parameter
logoptsee syslog parameter
facilitysee syslog parameter
TZstring containing timezone for syslog time strings If NULL(0), use default timezone.

References _instance, and Logger().

Referenced by getInstance().

◆ createInstance() [2/2]

Logger * Logger::createInstance ( std::ostream * out = 0)
static

Create a logger to the given output stream.

The output stream should remain valid as long as this logger exists. This Logger does not take responsibility for closing or destroying the stream.

Parameters
outPointer to the output stream, or null to default to cerr.

References _instance, and get_instance_locked().

◆ destroyInstance()

void Logger::destroyInstance ( )
static

Destroy any existing Logger instance.

New log messages will create a default Logger instance unless one is created explicitly with createInstance(). The Logger singleon no longer has a public destructor, it must be destroyed through this static method.

References _instance.

◆ get_instance_locked()

Logger * Logger::get_instance_locked ( std::ostream * out = 0)
staticprivate

Return the current instance or create a default, without locking.

Parameters
outIf null, use std::cerr as the output stream.
Returns
Logger*

References _instance, and Logger().

Referenced by createInstance().

◆ getInstance()

Logger * Logger::getInstance ( )
static

Retrieve the current Logger singleton instance.

Return a pointer to the currently active Logger singleton, and create a default if it does not exist. The default writes log messages to std::cerr.

Note this method is not thread-safe. The createInstance() and destroyInstance() methods are thread-safe, in that the pointer to the singleton instance is modified only while a mutex is locked. However, once an application has the instance pointer, nothing prevents that instance from being destroyed and recreated. The application should take care to not change the Logger instance while other threads might be writing log messages. Typically the Logger instance is created at the beginning of the application and then never changed.

Every log message being written must call this method, so the locking overhead is not worth it, and it would not be effective without also locking all existing code which calls getInstance() directly.
A more correct approach would not provide access to the global singleton pointer outside logging methods which have locked the logging mutex.

References _instance, and createInstance().

Referenced by nidas::util::McSocket< SocketT >::joinMulticaster(), nidas::util::LogContextState::log(), nidas::core::TimetagAdjuster::log(), and nidas::core::SampleTracer::SampleTracer().

◆ init()

void Logger::init ( )
static

Initializes the logging implementation.

◆ log()

void Logger::log ( int severity,
const char * file,
const char * fn,
int line,
const char * fmt,
... )

Build a message from a printf-format string and variable args, and log the message.

The severity should be passed using one of the syslog macros, such as LOG_DEBUG or LOG_EMERG. Those macros are redefined such that they fill in the first four parameters automatically:

Logger::getInstance()->log (LOG_DEBUG, "pi=%f", 3.14159);
static Logger * getInstance()
Retrieve the current Logger singleton instance.
Definition Logger.cc:318
#define LOG_DEBUG
Definition Logger.h:220

This method is mostly for backwards compatibility. The newer log method is to use the log context macros DLOG, ILOG, ELOG, and so on, since those macros will not waste time generating the message if the message will not be logged.

References buffer, fillError(), and msg().

◆ msg() [1/2]

void nidas::util::Logger::msg ( const LogContextState & lc,
const LogMessage & m )
inline

References msg().

◆ msg() [2/2]

void Logger::msg ( const LogContextState & lc,
const std::string & msg )

Send a log message for the given LogContext lc.

The Logger double-checks that the LogContext is active, to guard against (or allow) code which logs messages that are not guarded by a test of lc.active(). If active, the message is just immediately sent to the current log output, formatted according to the context. For syslog output, the message and severity level are passed, but VERBOSE messages are never passed to syslog. For all other output, the message includes the current time and the log context info, such as filename, line number, function name, and thread name.

References _loggerTZ, _output, _saveTZ, _syslogit, current_scheme, nidas::util::Thread::currentName(), nidas::util::LogScheme::FileField, nidas::util::LogScheme::FunctionField, nidas::util::LogScheme::LevelField, nidas::util::LOGGER_VERBOSE, nidas::util::LogScheme::MessageField, msg(), nidas::util::UTime::setUTC(), nidas::util::LogScheme::ThreadField, and nidas::util::LogScheme::TimeField.

Referenced by log(), msg(), and msg().

◆ operator=()

Logger & nidas::util::Logger::operator= ( const Logger & )
private

No assignment.

◆ setTZ()

void Logger::setTZ ( const char * val)
protected

Set the timezone to be used in the log messages, whether passed to syslog or formatted to include the current time.

To be careful, don't make this public. The user should set the TZ once in the constructor. Otherwise, in a multithreaded app the log() method could have problems, since this is a singleton shared by multiple threads, and we don't provide a locking mechanism for _loggerTZ.

References _loggerTZ, and _saveTZ.

Referenced by Logger().

Friends And Related Symbol Documentation

◆ nidas::util::LogContext

friend class nidas::util::LogContext
friend

◆ nidas::util::LoggerPrivate

friend class nidas::util::LoggerPrivate
friend

◆ nidas::util::LogScheme

friend class nidas::util::LogScheme
friend

Member Data Documentation

◆ _ident

char* nidas::util::Logger::_ident
protected

Referenced by Logger(), and ~Logger().

◆ _instance

Logger * Logger::_instance = 0
staticprotected

◆ _loggerTZ

char* nidas::util::Logger::_loggerTZ
protected

Referenced by msg(), setTZ(), and ~Logger().

◆ _output

std::ostream* nidas::util::Logger::_output
protected

Referenced by msg(), and ~Logger().

◆ _saveTZ

char* nidas::util::Logger::_saveTZ
protected

Referenced by msg(), setTZ(), and ~Logger().

◆ _syslogit

bool nidas::util::Logger::_syslogit
protected

Referenced by msg(), and ~Logger().


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