|
static Logger * | createInstance (const std::string &ident, int logopt, int facility, const char *TZ=0) |
| Create a syslog-type Logger.
|
|
static Logger * | createInstance (std::ostream *out=0) |
| Create a logger to the given output stream.
|
|
static Logger * | getInstance () |
| 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.
|
|
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.
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().
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:
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().
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().
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().