LSSTApplications  18.1.0
LSSTDataManagementBasePackage
Public Member Functions | Static Public Member Functions | List of all members
lsst::log::Log Class Reference

This static class includes a variety of methods for interacting with the the logging module. More...

#include <Log.h>

Public Member Functions

 Log ()
 
bool isDebugEnabled () const
 Check whether the logger is enabled for the DEBUG Level. More...
 
bool isErrorEnabled () const
 Check whether the logger is enabled for the ERROR Level. More...
 
bool isFatalEnabled () const
 Check whether the logger is enabled for the FATAL Level. More...
 
bool isInfoEnabled () const
 Check whether the logger is enabled for the INFO Level. More...
 
bool isTraceEnabled () const
 Check whether the logger is enabled for the TRACE Level. More...
 
bool isWarnEnabled () const
 Check whether the logger is enabled for the WARN Level. More...
 
std::string getName () const
 Get the logger name associated with the Log object. More...
 
void setLevel (int level)
 Set the logging threshold to LEVEL. More...
 
int getLevel () const
 Retrieve the logging threshold. More...
 
bool isEnabledFor (int level) const
 Return whether the logging threshold of the logger is less than or equal to LEVEL. More...
 
Log getChild (std::string const &suffix) const
 Return a logger which is a descendant to this logger. More...
 
void log (log4cxx::LevelPtr level, log4cxx::spi::LocationInfo const &location, char const *fmt,...)
 Method used by LOG_INFO and similar macros to process a log message with variable arguments along with associated metadata. More...
 
void logMsg (log4cxx::LevelPtr level, log4cxx::spi::LocationInfo const &location, std::string const &msg)
 Method used by LOGS_INFO and similar macros to process a log message. More...
 

Static Public Member Functions

static Log getDefaultLogger ()
 Return default logger instance, same as default constructor. More...
 
static void configure ()
 Explicitly configures log4cxx and initializes logging system. More...
 
static void configure (std::string const &filename)
 Configures log4cxx from specified file. More...
 
static void configure_prop (std::string const &properties)
 Configures log4cxx using a string containing the list of properties, equivalent to configuring from a file containing the same content but without creating temporary files. More...
 
static Log getLogger (Log const &logger)
 
static Log getLogger (std::string const &loggername)
 Returns logger object for a given name. More...
 
static void MDC (std::string const &key, std::string const &value)
 Places a KEY/VALUE pair in the Mapped Diagnostic Context (MDC) for the current thread. More...
 
static void MDCRemove (std::string const &key)
 Remove the value associated with KEY within the MDC. More...
 
static int MDCRegisterInit (std::function< void()> function)
 

Detailed Description

This static class includes a variety of methods for interacting with the the logging module.

These methods are not meant for direct use. Rather, they are used by the LOG* macros and the SWIG interface declared in logInterface.h.

Definition at line 698 of file Log.h.

Constructor & Destructor Documentation

◆ Log()

lsst::log::Log::Log ( )
inline

Definition at line 704 of file Log.h.

704 : _logger(_defaultLogger()) { }

Member Function Documentation

◆ configure() [1/2]

void lsst::log::Log::configure ( )
static

Explicitly configures log4cxx and initializes logging system.

Configuration can be specified via environment variable LSST_LOG_CONFIG, if it is set and specifies existing file name then this file name is used for configuration. Otherwise pre-defined configuration is used, which is hardwired to add to the root logger a ConsoleAppender. In this case, the output will be formatted using a PatternLayout set to the pattern "%c %p: %m%n".

Definition at line 177 of file Log.cc.

177  {
178  std::lock_guard<std::mutex> lock(::configMutex);
179 
180  // Make sure other threads know that default configuration is not needed
181  ::initialized = true;
182 
183  // This removes all defined appenders, resets level to DEBUG,
184  // existing loggers are not deleted, only reset.
185  log4cxx::BasicConfigurator::resetConfiguration();
186 
187  // Do default configuration (only if not configured already?)
188  ::defaultConfig();
189 }
T lock(T... args)

◆ configure() [2/2]

void lsst::log::Log::configure ( std::string const &  filename)
static

Configures log4cxx from specified file.

If file name ends with ".xml", it is passed to log4cxx::xml::DOMConfigurator::configure(). Otherwise, it assumed to be a log4j Java properties file and is passed to log4cxx::PropertyConfigurator::configure(). See http://logging.apache.org/log4cxx/usage.html for additional details.

Parameters
filenamePath to configuration file.

Definition at line 201 of file Log.cc.

201  {
202  std::lock_guard<std::mutex> lock(::configMutex);
203 
204  // Make sure other threads know that default configuration is not needed
205  ::initialized = true;
206 
207  // This removes all defined appenders, resets level to DEBUG,
208  // existing loggers are not deleted, only reset.
209  log4cxx::BasicConfigurator::resetConfiguration();
210 
211  ::configFromFile(filename);
212 }
T lock(T... args)

◆ configure_prop()

void lsst::log::Log::configure_prop ( std::string const &  properties)
static

Configures log4cxx using a string containing the list of properties, equivalent to configuring from a file containing the same content but without creating temporary files.

Parameters
propertiesConfiguration properties.

Definition at line 220 of file Log.cc.

220  {
221  std::lock_guard<std::mutex> lock(::configMutex);
222 
223  // Make sure other threads know that default configuration is not needed
224  ::initialized = true;
225 
226  // This removes all defined appenders, resets level to DEBUG,
227  // existing loggers are not deleted, only reset.
228  log4cxx::BasicConfigurator::resetConfiguration();
229 
230  std::vector<unsigned char> data(properties.begin(), properties.end());
231  log4cxx::helpers::InputStreamPtr inStream(new log4cxx::helpers::ByteArrayInputStream(data));
232  log4cxx::helpers::Properties prop;
233  prop.load(inStream);
235 }
char * data
Definition: BaseRecord.cc:62
T lock(T... args)
STL class.

◆ getChild()

Log lsst::log::Log::getChild ( std::string const &  suffix) const

Return a logger which is a descendant to this logger.

If for example name of this logger is "main.task" and suffix is "subtask1.algorithm" then this method will return logger with the name "main.task.subtask1.algorithm". If this logger is root logger then suffix name is used for returned logger name. If suffix is empty then this instance is returned.

Parameters
suffixSuffix for tha name of returned logger, can include dot (but not at leading position) and can be empty.
Returns
Log instance.

Definition at line 351 of file Log.cc.

351  {
352  // strip leading dots and spaces from suffix
353  auto pos = suffix.find_first_not_of(" .");
354  if (pos == std::string::npos) {
355  // empty, just return myself
356  return *this;
357  }
359  if (name.empty()) {
360  name = suffix.substr(pos);
361  } else {
362  name += '.';
363  name += suffix.substr(pos);
364  }
365  return getLogger(name);
366 }
T empty(T... args)
STL class.
std::string getName() const
Get the logger name associated with the Log object.
Definition: Log.cc:240
static Log getLogger(Log const &logger)
Definition: Log.h:745
T substr(T... args)

◆ getDefaultLogger()

static Log lsst::log::Log::getDefaultLogger ( )
inlinestatic

Return default logger instance, same as default constructor.

Definition at line 739 of file Log.h.

739 { return Log(); }

◆ getLevel()

int lsst::log::Log::getLevel ( ) const

Retrieve the logging threshold.

Returns
int Indicating the logging threshold.

Definition at line 315 of file Log.cc.

315  {
316  log4cxx::LevelPtr level = _logger->getLevel();
317  int levelno = -1;
318  if (level != NULL) {
319  levelno = level->toInt();
320  }
321  return levelno;
322 }

◆ getLogger() [1/2]

static Log lsst::log::Log::getLogger ( Log const &  logger)
inlinestatic

Definition at line 745 of file Log.h.

745 { return logger; }

◆ getLogger() [2/2]

Log lsst::log::Log::getLogger ( std::string const &  loggername)
static

Returns logger object for a given name.

If name is empty then current logger is returned and not a root logger.

Parameters
loggernameName of logger to return.
Returns
Log instance corresponding to logger name.

Definition at line 256 of file Log.cc.

256  {
257  if (loggername.empty()){
258  return getDefaultLogger();
259  } else {
260  return Log(log4cxx::Logger::getLogger(loggername));
261  }
262 }
static Log getDefaultLogger()
Return default logger instance, same as default constructor.
Definition: Log.h:739
def getLogger(loggername)

◆ getName()

std::string lsst::log::Log::getName ( ) const

Get the logger name associated with the Log object.

Returns
String containing the logger name.

Definition at line 240 of file Log.cc.

240  {
241  std::string name = _logger->getName();
242  if (name == "root") {
243  name.clear();
244  }
245  return name;
246 }
STL class.
T clear(T... args)

◆ isDebugEnabled()

bool lsst::log::Log::isDebugEnabled ( ) const
inline

Check whether the logger is enabled for the DEBUG Level.

Definition at line 709 of file Log.h.

709 { return _logger->isDebugEnabled(); }

◆ isEnabledFor()

bool lsst::log::Log::isEnabledFor ( int  level) const

Return whether the logging threshold of the logger is less than or equal to LEVEL.

Returns
Bool indicating whether or not logger is enabled.
Parameters
levelLogging threshold to check.

Definition at line 330 of file Log.cc.

330  {
331  if (_logger->isEnabledFor(log4cxx::Level::toLevel(level))) {
332  return true;
333  } else {
334  return false;
335  }
336 }

◆ isErrorEnabled()

bool lsst::log::Log::isErrorEnabled ( ) const
inline

Check whether the logger is enabled for the ERROR Level.

Definition at line 713 of file Log.h.

713 { return _logger->isErrorEnabled(); }

◆ isFatalEnabled()

bool lsst::log::Log::isFatalEnabled ( ) const
inline

Check whether the logger is enabled for the FATAL Level.

Definition at line 717 of file Log.h.

717 { return _logger->isFatalEnabled(); }

◆ isInfoEnabled()

bool lsst::log::Log::isInfoEnabled ( ) const
inline

Check whether the logger is enabled for the INFO Level.

Definition at line 721 of file Log.h.

721 { return _logger->isInfoEnabled(); }

◆ isTraceEnabled()

bool lsst::log::Log::isTraceEnabled ( ) const
inline

Check whether the logger is enabled for the TRACE Level.

Definition at line 725 of file Log.h.

725 { return _logger->isTraceEnabled(); }

◆ isWarnEnabled()

bool lsst::log::Log::isWarnEnabled ( ) const
inline

Check whether the logger is enabled for the WARN Level.

Definition at line 729 of file Log.h.

729 { return _logger->isWarnEnabled(); }

◆ log()

void lsst::log::Log::log ( log4cxx::LevelPtr  level,
log4cxx::spi::LocationInfo const &  location,
char const *  fmt,
  ... 
)

Method used by LOG_INFO and similar macros to process a log message with variable arguments along with associated metadata.

Parameters
levelmessage level
locationmessage origin location
fmtmessage format string

Definition at line 371 of file Log.cc.

375  {
376  va_list args;
377  va_start(args, fmt);
378  char msg[MAX_LOG_MSG_LEN];
379  vsnprintf(msg, MAX_LOG_MSG_LEN, fmt, args);
380  logMsg(level, location, msg);
381 }
void logMsg(log4cxx::LevelPtr level, log4cxx::spi::LocationInfo const &location, std::string const &msg)
Method used by LOGS_INFO and similar macros to process a log message.
Definition: Log.cc:385
T vsnprintf(T... args)
#define MAX_LOG_MSG_LEN
Definition: Log.cc:55

◆ logMsg()

void lsst::log::Log::logMsg ( log4cxx::LevelPtr  level,
log4cxx::spi::LocationInfo const &  location,
std::string const &  msg 
)

Method used by LOGS_INFO and similar macros to process a log message.

Parameters
levelmessage level
locationmessage origin location
msgmessage string

Definition at line 385 of file Log.cc.

388  {
389 
390  // do one-time per-thread initialization, this was implemented
391  // with thread_local initially but clang on OS X did not support it
392  void *ptr = pthread_getspecific(::pthreadKey.key);
393  if (ptr == nullptr) {
394 
395  // use pointer value as a flag, don't care where it points to
396  ptr = static_cast<void*>(&::pthreadKey);
397  pthread_setspecific(::pthreadKey.key, ptr);
398 
399  std::lock_guard<std::mutex> lock(mdcInitMutex);
400  // call all functions in the MDC init list
401  for (auto& fun: mdcInitFunctions) {
402  fun();
403  }
404  }
405 
406  // forward everything to logger
407  _logger->forcedLog(level, msg, location);
408 }
uint64_t * ptr
Definition: RangeSet.cc:88
T lock(T... args)

◆ MDC()

void lsst::log::Log::MDC ( std::string const &  key,
std::string const &  value 
)
static

Places a KEY/VALUE pair in the Mapped Diagnostic Context (MDC) for the current thread.

The VALUE may then be included in log messages by using the following the X conversion character within a pattern layout as X{KEY}. Note that unlike log4cxx::MDC::put() this method overwrites any previously existing mapping.

Parameters
keyUnique key.
valueString value.

Definition at line 273 of file Log.cc.

273  {
274  // put() does not remove existing mapping, to make it less confusing
275  // for clients which expect that MDC() always overwrites existing mapping
276  // we explicitly remove it first if it exists.
277  log4cxx::MDC::remove(key);
278  log4cxx::MDC::put(key, value);
279 }
Key< U > key
Definition: Schema.cc:281

◆ MDCRegisterInit()

int lsst::log::Log::MDCRegisterInit ( std::function< void()>  function)
static

Definition at line 289 of file Log.cc.

289  {
290 
291  std::lock_guard<std::mutex> lock(mdcInitMutex);
292 
293  // logMsg may have been called already in this thread, to make sure that
294  // this function is executed in this thread call it explicitly
295  function();
296 
297  // store function for later use
298  ::mdcInitFunctions.push_back(std::move(function));
299 
300  // return arbitrary number
301  return 1;
302 }
T lock(T... args)
T move(T... args)

◆ MDCRemove()

void lsst::log::Log::MDCRemove ( std::string const &  key)
static

Remove the value associated with KEY within the MDC.

Parameters
keyKey identifying value to remove.

Definition at line 285 of file Log.cc.

285  {
286  log4cxx::MDC::remove(key);
287 }
Key< U > key
Definition: Schema.cc:281

◆ setLevel()

void lsst::log::Log::setLevel ( int  level)

Set the logging threshold to LEVEL.

Parameters
levelNew logging threshold.

Definition at line 308 of file Log.cc.

308  {
309  _logger->setLevel(log4cxx::Level::toLevel(level));
310 }

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