LSSTApplications  16.0-11-g09ed895+2,16.0-11-g12e47bd,16.0-11-g9bb73b2+6,16.0-12-g5c924a4+6,16.0-14-g9a974b3+1,16.0-15-g1417920+1,16.0-15-gdd5ca33+1,16.0-16-gf0259e2,16.0-17-g31abd91+7,16.0-17-g7d7456e+7,16.0-17-ga3d2e9f+13,16.0-18-ga4d4bcb+1,16.0-18-gd06566c+1,16.0-2-g0febb12+21,16.0-2-g9d5294e+69,16.0-2-ga8830df+6,16.0-20-g21842373+7,16.0-24-g3eae5ec,16.0-28-gfc9ea6c+4,16.0-29-ge8801f9,16.0-3-ge00e371+34,16.0-4-g18f3627+13,16.0-4-g5f3a788+20,16.0-4-ga3eb747+10,16.0-4-gabf74b7+29,16.0-4-gb13d127+6,16.0-49-g42e581f7+6,16.0-5-g27fb78a+7,16.0-5-g6a53317+34,16.0-5-gb3f8a4b+87,16.0-6-g9321be7+4,16.0-6-gcbc7b31+42,16.0-6-gf49912c+29,16.0-7-gd2eeba5+51,16.0-71-ge89f8615e,16.0-8-g21fd5fe+29,16.0-8-g3a9f023+20,16.0-8-g4734f7a+1,16.0-8-g5858431+3,16.0-9-gf5c1f43+8,master-gd73dc1d098+1,w.2019.01
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...
 
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 std::string getDefaultLoggerName ()
 Get the current default logger name. 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 pushContext (std::string const &name)
 Pushes NAME onto the global hierarchical default logger name. More...
 
static void popContext ()
 Pops the last pushed name off the global hierarchical default logger 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 723 of file Log.h.

Constructor & Destructor Documentation

◆ Log()

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

Definition at line 732 of file Log.h.

732 : _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 173 of file Log.cc.

173  {
174 
175  // This removes all defined appenders, resets level to DEBUG,
176  // existing loggers are not deleted, only reset.
177  log4cxx::BasicConfigurator::resetConfiguration();
178 
179  // Do default configuration (only if not configured already?)
180  ::defaultConfig();
181 
182  // reset default logger to the root logger
183  _defaultLogger(log4cxx::Logger::getRootLogger());
184 }

◆ 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 196 of file Log.cc.

196  {
197  // This removes all defined appenders, resets level to DEBUG,
198  // existing loggers are not deleted, only reset.
199  log4cxx::BasicConfigurator::resetConfiguration();
200 
201  ::configFromFile(filename);
202 
203  // reset default logger to the root logger
204  _defaultLogger(log4cxx::Logger::getRootLogger());
205 }

◆ 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 213 of file Log.cc.

213  {
214  // This removes all defined appenders, resets level to DEBUG,
215  // existing loggers are not deleted, only reset.
216  log4cxx::BasicConfigurator::resetConfiguration();
217 
218  std::vector<unsigned char> data(properties.begin(), properties.end());
219  log4cxx::helpers::InputStreamPtr inStream(new log4cxx::helpers::ByteArrayInputStream(data));
220  log4cxx::helpers::Properties prop;
221  prop.load(inStream);
223 
224  // reset default logger to the root logger
225  _defaultLogger(log4cxx::Logger::getRootLogger());
226 }
char * data
Definition: BaseTable.cc:205
STL class.

◆ getDefaultLogger()

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

Return default logger instance, same as default constructor.

Definition at line 765 of file Log.h.

765 { return Log(_defaultLogger()); }

◆ getDefaultLoggerName()

std::string lsst::log::Log::getDefaultLoggerName ( )
static

Get the current default logger name.

Returns
String containing the default logger name.

Definition at line 231 of file Log.cc.

231  {
232  return getDefaultLogger().getName();
233 }
static Log getDefaultLogger()
Return default logger instance, same as default constructor.
Definition: Log.h:765
std::string getName() const
Get the logger name associated with the Log object.
Definition: Log.cc:238

◆ getLevel()

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

Retrieve the logging threshold.

Returns
int Indicating the logging threshold.

Definition at line 354 of file Log.cc.

354  {
355  log4cxx::LevelPtr level = _logger->getLevel();
356  int levelno = -1;
357  if (level != NULL) {
358  levelno = level->toInt();
359  }
360  return levelno;
361 }

◆ getLogger() [1/2]

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

Definition at line 772 of file Log.h.

772 { 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 254 of file Log.cc.

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

◆ 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 238 of file Log.cc.

238  {
239  std::string name = _logger->getName();
240  if (name == "root") {
241  name.clear();
242  }
243  return name;
244 }
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 737 of file Log.h.

737 { 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 369 of file Log.cc.

369  {
370  if (_logger->isEnabledFor(log4cxx::Level::toLevel(level))) {
371  return true;
372  } else {
373  return false;
374  }
375 }

◆ isErrorEnabled()

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

Check whether the logger is enabled for the ERROR Level.

Definition at line 741 of file Log.h.

741 { return _logger->isErrorEnabled(); }

◆ isFatalEnabled()

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

Check whether the logger is enabled for the FATAL Level.

Definition at line 745 of file Log.h.

745 { return _logger->isFatalEnabled(); }

◆ isInfoEnabled()

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

Check whether the logger is enabled for the INFO Level.

Definition at line 749 of file Log.h.

749 { return _logger->isInfoEnabled(); }

◆ isTraceEnabled()

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

Check whether the logger is enabled for the TRACE Level.

Definition at line 753 of file Log.h.

753 { return _logger->isTraceEnabled(); }

◆ isWarnEnabled()

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

Check whether the logger is enabled for the WARN Level.

Definition at line 757 of file Log.h.

757 { 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 380 of file Log.cc.

384  {
385  va_list args;
386  va_start(args, fmt);
387  char msg[MAX_LOG_MSG_LEN];
388  vsnprintf(msg, MAX_LOG_MSG_LEN, fmt, args);
389  logMsg(level, location, msg);
390 }
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:394
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 394 of file Log.cc.

397  {
398 
399  // do one-time per-thread initialization, this was implemented
400  // with thread_local initially but clang on OS X did not support it
401  void *ptr = pthread_getspecific(::pthreadKey.key);
402  if (ptr == nullptr) {
403 
404  // use pointer value as a flag, don't care where it points to
405  ptr = static_cast<void*>(&::pthreadKey);
406  pthread_setspecific(::pthreadKey.key, ptr);
407 
408  std::lock_guard<std::mutex> lock(mdcInitMutex);
409  // call all functions in the MDC init list
410  for (auto& fun: mdcInitFunctions) {
411  fun();
412  }
413  }
414 
415  // forward everything to logger
416  _logger->forcedLog(level, msg, location);
417 }
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 312 of file Log.cc.

312  {
313  // put() does not remove existing mapping, to make it less confusing
314  // for clients which expect that MDC() always overwrites existing mapping
315  // we explicitly remove it first if it exists.
316  log4cxx::MDC::remove(key);
317  log4cxx::MDC::put(key, value);
318 }
Key< U > key
Definition: Schema.cc:281

◆ MDCRegisterInit()

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

Definition at line 328 of file Log.cc.

328  {
329 
330  std::lock_guard<std::mutex> lock(mdcInitMutex);
331 
332  // logMsg may have been called already in this thread, to make sure that
333  // this function is executed in this thread call it explicitly
334  function();
335 
336  // store function for later use
337  ::mdcInitFunctions.push_back(std::move(function));
338 
339  // return arbitrary number
340  return 1;
341 }
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 324 of file Log.cc.

324  {
325  log4cxx::MDC::remove(key);
326 }
Key< U > key
Definition: Schema.cc:281

◆ popContext()

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

Pops the last pushed name off the global hierarchical default logger name.

Definition at line 293 of file Log.cc.

293  {
294  // switch to parent logger, this assumes that loggers are not
295  // re-parented between calls to push and pop
296  log4cxx::LoggerPtr parent = _defaultLogger()->getParent();
297  // root logger does not have parent, stay at root instead
298  if (parent) {
299  _defaultLogger(parent);
300  }
301 }

◆ pushContext()

void lsst::log::Log::pushContext ( std::string const &  name)
static

Pushes NAME onto the global hierarchical default logger name.

Parameters
nameString to push onto logging context.

Definition at line 266 of file Log.cc.

266  {
267  // can't handle empty names
268  if (name.empty()) {
269  throw std::invalid_argument("lsst::log::Log::pushContext(): "
270  "empty context name is not allowed");
271  }
272  // we do not allow multi-level context (logger1.logger2)
273  if (name.find('.') != std::string::npos) {
274  throw std::invalid_argument("lsst::log::Log::pushContext(): "
275  "multi-level contexts are not allowed: " + name);
276  }
277 
278  // Construct new default logger name
279  std::string newName = _defaultLogger()->getName();
280  if (newName == "root") {
281  newName = name;
282  } else {
283  newName += ".";
284  newName += name;
285  }
286  // Update defaultLogger
287  _defaultLogger(log4cxx::Logger::getLogger(newName));
288 }
STL class.

◆ setLevel()

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

Set the logging threshold to LEVEL.

Parameters
levelNew logging threshold.

Definition at line 347 of file Log.cc.

347  {
348  _logger->setLevel(log4cxx::Level::toLevel(level));
349 }

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