nidas  v1.2-1520
Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | Static Protected Attributes | Private Member Functions | Static Private Member Functions | Static Private Attributes | 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 (va_alist)
 Build a message from a printf-format string and variable args, and log the message. More...
 
void msg (const LogContext &lc, const std::string &msg)
 Send a log message for the given LogContext lc. More...
 
void msg (const LogContext &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. More...
 
static LoggercreateInstance (std::ostream *out=0)
 Create a logger to the given output stream. More...
 
static LoggergetInstance ()
 Retrieve the current Logger singleton instance. More...
 
static void destroyInstance ()
 Destroy any existing Logger instance. More...
 
static void setScheme (const std::string &name)
 Set the current scheme. More...
 
static void setScheme (const LogScheme &scheme)
 Set the current scheme to the given scheme. More...
 
static void updateScheme (const LogScheme &scheme)
 Update or insert this scheme in the collection of log schemes. More...
 
static LogScheme getScheme (const std::string &name)
 Get the scheme with the given name. More...
 
static LogScheme getScheme ()
 Return the current LogScheme, creating the default LogScheme if it has not been set yet. More...
 
static bool knownScheme (const std::string &name)
 Return true if the scheme with the given name is known, false otherwise. More...
 
static void clearSchemes ()
 Erase all known schemes and reset the active scheme to the name initialized at application start. More...
 

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. More...
 

Protected Attributes

std::ostream * output
 
bool syslogit
 
char * loggerTZ
 
char * saveTZ
 
char * _ident
 

Static Protected Attributes

static Logger_instance = 0
 

Private Member Functions

void msg_locked (const nidas::util::LogContext &lc, const std::string &msg)
 Other Log classes call this method to log a message when the log mutex is already locked. More...
 
 ~Logger ()
 
 Logger (const Logger &)
 No copying. More...
 
Loggeroperator= (const Logger &)
 No assignment. More...
 

Static Private Member Functions

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

Static Private Attributes

static nidas::util::Mutex mutex
 

Friends

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

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::Logger ( const std::string &  ident,
int  logopt,
int  facility,
const char *  TZ = 0 
)
protected

References _ident, and setTZ().

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

References _ident, loggerTZ, output, saveTZ, and syslogit.

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

No copying.

Member Function Documentation

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
ident,:see syslog parameter
logopt,:see syslog parameter
facility,:see syslog parameter
TZ,:string containing timezone for syslog time strings If NULL(0), use default timezone.

References _instance, Logger(), and mutex.

Referenced by getInstance(), main(), TeeTTy::run(), and TeeI2C::run().

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, get_instance_locked(), and mutex.

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, and mutex.

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(), and nidas::util::LogScheme::show_log_point().

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::core::SampleClock::addSampleDate(), nidas::core::DSMSensor::addSampleTag(), nidas::dynld::isff::GOESOutput::addSourceSampleTag(), nidas::core::DSMService::cancel(), nidas::dynld::isff::GOESOutput::cancelThread(), nidas::dynld::raf::IRIGSensor::checkClock(), nidas::core::DSMService::checkSubThreads(), nidas::dynld::UDPSampleOutput::clone(), nidas::dynld::raf::PPT_Serial::close(), nidas::core::TCPSocketIODevice::close(), nidas::core::UDPSocketIODevice::close(), nidas::core::ServerSocketIODevice::close(), nidas::core::DSMEngine::closeOutputs(), nidas::dynld::RawSampleService::connect(), nidas::core::DSMEngine::connect(), nidas::dynld::XMLConfigService::connected(), nidas::dynld::psql::PSQLProcessor::connected(), nidas::dynld::isff::GOESOutput::connected(), nidas::dynld::StatisticsProcessor::connectSource(), nidas::dynld::raf::AlicatSDI::derivedDataNotify(), nidas::dynld::SampleProcessor::disconnect(), nidas::dynld::RawSampleService::disconnect(), nidas::dynld::raf::SyncRecordGenerator::disconnect(), nidas::dynld::raf::CVIProcessor::disconnect(), nidas::core::SampleArchiver::disconnect(), nidas::dynld::StatisticsProcessor::disconnect(), nidas::core::DSMEngine::disconnect(), nidas::core::RemoteSerialConnection::doEscCmds(), nidas::core::GetAdsFileName::execute(), nidas::core::Project::findDSM(), nidas::core::Project::findDSMFromHostname(), nidas::core::MultipleUDPSockets::fromDOMElement(), nidas::core::DSMService::fromDOMElement(), nidas::core::McSocketUDP::fromDOMElement(), nidas::core::McSocket::fromDOMElement(), nidas::core::DSMConfig::fromDOMElement(), nidas::core::DSMSensor::fromDOMElement(), nidas::core::DSMSensor::getLooper(), nidas::core::Socket::getRemoteInet4Address(), nidas::core::Project::getUniqueSampleId(), nidas::dynld::isff::GOESOutput::GOESOutput(), nidas::dynld::isff::PacketInputStream::init(), nidas::dynld::psql::PSQLSampleOutput::init(), nidas::dynld::raf::TwoD64_USB::init_parameters(), nidas::core::DSMService::interrupt(), nidas::core::DSMService::join(), nidas::util::McSocket< SocketT >::joinMulticaster(), nidas::dynld::isff::GOESOutput::joinThread(), nidas::dynld::isff::GOESOutput::killThread(), nidas::core::TimetagAdjuster::log(), nidas::util::LogContext::log(), LogSchemeFromDOMElement(), nidas::dynld::isff::MOSMote::MOS_TimeSyncer::looperNotify(), nidas::core::SerialSensor::Prompter::looperNotify(), main(), nidas::dynld::raf::VCSEL2_Serial::open(), nidas::dynld::raf::AlicatSDI::open(), nidas::dynld::raf::VCSEL_Serial::open(), nidas::dynld::raf::DSMAnalogSensor::open(), nidas::dynld::raf::TwoD_USB::open(), nidas::dynld::raf::LamsSensor::open(), nidas::dynld::raf::PIP_Serial::open(), nidas::core::DSMEngine::openSensors(), PacketDecode::parseRunstring(), nidas::dynld::raf::LamsSensor::printStatus(), nidas::dynld::DSC_A2DSensor::printStatus(), nidas::dynld::raf::DSMAnalogSensor::printStatus(), nidas::dynld::DSC_FreqCounter::printStatus(), nidas::dynld::DSC_PulseCounter::printStatus(), nidas::dynld::DSC_Event::printStatus(), nidas::dynld::raf::IRIGSensor::printStatus(), nidas::dynld::raf::TwoD_USB::printStatus(), nidas::dynld::raf::DSMArincSensor::printStatus(), nidas::core::SerialSensor::printStatus(), nidas::dynld::raf::PSI9116_Sensor::process(), nidas::core::DatagramSampleScanner::readBuffer(), nidas::dynld::ParoSci_202BG_Calibration::readCalFile(), nidas::dynld::raf::DSMAnalogSensor::readCalFile(), nidas::dynld::raf::A2D_Serial::readCalFile(), nidas::dynld::raf::DSMAnalogSensor::readFilterFile(), nidas::dynld::isff::Wind3D::readOffsetsAnglesCalFile(), nidas::dynld::isff::PacketInputStream::readSamples(), nidas::dynld::raf::CVIOutput::receive(), nidas::dynld::UDPSampleOutput::receive(), nidas::dynld::AsciiOutput::receive(), nidas::dynld::psql::PSQLSampleOutput::receive(), nidas::dynld::raf::CVIProcessor::receive(), nidas::dynld::isff::GOESOutput::receive(), nidas::core::Looper::removeClient(), SensorExtract::run(), TeeI2C::run(), nidas::dynld::isff::GOESOutput::run(), nidas::core::SensorHandler::run(), nidas::dynld::RawSampleService::Worker::run(), nidas::core::Socket::ConnectionThread::run(), nidas::core::SampleTracer::SampleTracer(), nidas::dynld::RawSampleService::schedule(), nidas::dynld::raf::SyncRecordSource::selectVariablesFromSensor(), nidas::dynld::raf::TwoD_USB::sendTrueAirspeed(), nidas::dynld::isff::GOESOutput::setIOChannel(), nidas::dynld::UDPSampleOutput::UDPSampleOutput(), nidas::core::CharacterSensor::validateSscanfs(), nidas::dynld::raf::CVIProcessor::~CVIProcessor(), nidas::core::SampleArchiver::~SampleArchiver(), nidas::core::SampleBuffer::~SampleBuffer(), nidas::dynld::SampleProcessor::~SampleProcessor(), nidas::core::ServerSocket::~ServerSocket(), nidas::dynld::StatisticsProcessor::~StatisticsProcessor(), and nidas::dynld::raf::SyncRecordGenerator::~SyncRecordGenerator().

void Logger::log ( va_alist  )

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);

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 nidas::util::LogContext::active(), buffer, and fillError().

Referenced by nidas::core::SampleClock::addSampleDate(), nidas::core::DSMSensor::addSampleTag(), nidas::dynld::isff::GOESOutput::addSourceSampleTag(), nidas::core::DSMService::cancel(), nidas::dynld::isff::GOESOutput::cancelThread(), nidas::dynld::raf::IRIGSensor::checkClock(), nidas::core::DSMService::checkSubThreads(), nidas::dynld::UDPSampleOutput::clone(), nidas::dynld::raf::PPT_Serial::close(), nidas::core::TCPSocketIODevice::close(), nidas::core::UDPSocketIODevice::close(), nidas::core::ServerSocketIODevice::close(), nidas::core::DSMEngine::closeOutputs(), nidas::dynld::RawSampleService::connect(), nidas::core::DSMEngine::connect(), nidas::dynld::XMLConfigService::connected(), nidas::dynld::psql::PSQLProcessor::connected(), nidas::dynld::isff::GOESOutput::connected(), nidas::dynld::StatisticsProcessor::connectSource(), nidas::dynld::raf::AlicatSDI::derivedDataNotify(), nidas::dynld::SampleProcessor::disconnect(), nidas::dynld::RawSampleService::disconnect(), nidas::dynld::raf::SyncRecordGenerator::disconnect(), nidas::dynld::raf::CVIProcessor::disconnect(), nidas::core::SampleArchiver::disconnect(), nidas::dynld::StatisticsProcessor::disconnect(), nidas::core::DSMEngine::disconnect(), nidas::core::RemoteSerialConnection::doEscCmds(), nidas::core::GetAdsFileName::execute(), nidas::core::Project::findDSM(), nidas::core::Project::findDSMFromHostname(), nidas::core::MultipleUDPSockets::fromDOMElement(), nidas::dynld::psql::PSQLChannel::fromDOMElement(), nidas::core::DSMService::fromDOMElement(), nidas::core::McSocketUDP::fromDOMElement(), nidas::core::McSocket::fromDOMElement(), nidas::core::DSMConfig::fromDOMElement(), nidas::core::DSMSensor::fromDOMElement(), nidas::core::DSMSensor::getLooper(), nidas::core::Socket::getRemoteInet4Address(), nidas::core::Project::getUniqueSampleId(), nidas::dynld::isff::GOESOutput::GOESOutput(), nidas::dynld::isff::PacketInputStream::init(), nidas::dynld::psql::PSQLSampleOutput::init(), nidas::dynld::raf::TwoD64_USB::init_parameters(), nidas::core::DSMService::interrupt(), nidas::core::DSMService::join(), nidas::util::McSocket< SocketT >::joinMulticaster(), nidas::dynld::isff::GOESOutput::joinThread(), nidas::dynld::isff::GOESOutput::killThread(), nidas::core::TimetagAdjuster::log(), nidas::dynld::isff::MOSMote::MOS_TimeSyncer::looperNotify(), nidas::core::SerialSensor::Prompter::looperNotify(), nidas::dynld::raf::VCSEL2_Serial::open(), nidas::dynld::raf::AlicatSDI::open(), nidas::dynld::raf::VCSEL_Serial::open(), nidas::dynld::raf::DSMAnalogSensor::open(), nidas::dynld::raf::TwoD_USB::open(), nidas::dynld::raf::LamsSensor::open(), nidas::dynld::raf::PIP_Serial::open(), nidas::core::DSMEngine::openSensors(), nidas::dynld::raf::LamsSensor::printStatus(), nidas::dynld::raf::DSMAnalogSensor::printStatus(), nidas::dynld::DSC_A2DSensor::printStatus(), nidas::dynld::DSC_FreqCounter::printStatus(), nidas::dynld::DSC_Event::printStatus(), nidas::dynld::DSC_PulseCounter::printStatus(), nidas::dynld::raf::IRIGSensor::printStatus(), nidas::dynld::raf::TwoD_USB::printStatus(), nidas::dynld::raf::DSMArincSensor::printStatus(), nidas::core::SerialSensor::printStatus(), nidas::dynld::raf::PSI9116_Sensor::process(), nidas::core::DatagramSampleScanner::readBuffer(), nidas::dynld::ParoSci_202BG_Calibration::readCalFile(), nidas::dynld::raf::DSMAnalogSensor::readCalFile(), nidas::dynld::raf::A2D_Serial::readCalFile(), nidas::dynld::raf::DSMAnalogSensor::readFilterFile(), nidas::dynld::isff::Wind3D::readOffsetsAnglesCalFile(), nidas::dynld::isff::PacketInputStream::readSamples(), nidas::dynld::raf::CVIOutput::receive(), nidas::dynld::UDPSampleOutput::receive(), nidas::dynld::AsciiOutput::receive(), nidas::dynld::psql::PSQLSampleOutput::receive(), nidas::dynld::raf::CVIProcessor::receive(), nidas::dynld::isff::GOESOutput::receive(), nidas::core::Looper::removeClient(), SensorExtract::run(), nidas::dynld::isff::GOESOutput::run(), nidas::core::SensorHandler::run(), nidas::dynld::RawSampleService::Worker::run(), nidas::core::Socket::ConnectionThread::run(), nidas::dynld::RawSampleService::schedule(), nidas::dynld::raf::SyncRecordSource::selectVariablesFromSensor(), nidas::dynld::raf::TwoD_USB::sendTrueAirspeed(), nidas::dynld::isff::GOESOutput::setIOChannel(), nidas::dynld::UDPSampleOutput::UDPSampleOutput(), nidas::core::CharacterSensor::validateSscanfs(), nidas::dynld::raf::CVIProcessor::~CVIProcessor(), nidas::core::SampleArchiver::~SampleArchiver(), nidas::core::SampleBuffer::~SampleBuffer(), nidas::dynld::SampleProcessor::~SampleProcessor(), nidas::core::ServerSocket::~ServerSocket(), nidas::dynld::StatisticsProcessor::~StatisticsProcessor(), and nidas::dynld::raf::SyncRecordGenerator::~SyncRecordGenerator().

void Logger::msg ( const LogContext 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 msg_locked(), and mutex.

Referenced by nidas::util::LogContext::log(), msg(), and msg_locked().

void nidas::util::Logger::msg ( const LogContext lc,
const LogMessage m 
)
inline
void Logger::msg_locked ( const nidas::util::LogContext lc,
const std::string &  msg 
)
private
Logger& nidas::util::Logger::operator= ( const Logger )
private

No assignment.

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 Function Documentation

friend class nidas::util::LogContext
friend
friend class nidas::util::LogScheme
friend

Member Data Documentation

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

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

Logger * Logger::_instance = 0
staticprotected
char* nidas::util::Logger::loggerTZ
protected

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

Mutex Logger::mutex
staticprivate
std::ostream* nidas::util::Logger::output
protected

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

char* nidas::util::Logger::saveTZ
protected

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

bool nidas::util::Logger::syslogit
protected

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


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