LSST Applications  21.0.0+04719a4bac,21.0.0-1-ga51b5d4+f5e6047307,21.0.0-11-g2b59f77+a9c1acf22d,21.0.0-11-ga42c5b2+86977b0b17,21.0.0-12-gf4ce030+76814010d2,21.0.0-13-g1721dae+760e7a6536,21.0.0-13-g3a573fe+768d78a30a,21.0.0-15-g5a7caf0+f21cbc5713,21.0.0-16-g0fb55c1+b60e2d390c,21.0.0-19-g4cded4ca+71a93a33c0,21.0.0-2-g103fe59+bb20972958,21.0.0-2-g45278ab+04719a4bac,21.0.0-2-g5242d73+3ad5d60fb1,21.0.0-2-g7f82c8f+8babb168e8,21.0.0-2-g8f08a60+06509c8b61,21.0.0-2-g8faa9b5+616205b9df,21.0.0-2-ga326454+8babb168e8,21.0.0-2-gde069b7+5e4aea9c2f,21.0.0-2-gecfae73+1d3a86e577,21.0.0-2-gfc62afb+3ad5d60fb1,21.0.0-25-g1d57be3cd+e73869a214,21.0.0-3-g357aad2+ed88757d29,21.0.0-3-g4a4ce7f+3ad5d60fb1,21.0.0-3-g4be5c26+3ad5d60fb1,21.0.0-3-g65f322c+e0b24896a3,21.0.0-3-g7d9da8d+616205b9df,21.0.0-3-ge02ed75+a9c1acf22d,21.0.0-4-g591bb35+a9c1acf22d,21.0.0-4-g65b4814+b60e2d390c,21.0.0-4-gccdca77+0de219a2bc,21.0.0-4-ge8a399c+6c55c39e83,21.0.0-5-gd00fb1e+05fce91b99,21.0.0-6-gc675373+3ad5d60fb1,21.0.0-64-g1122c245+4fb2b8f86e,21.0.0-7-g04766d7+cd19d05db2,21.0.0-7-gdf92d54+04719a4bac,21.0.0-8-g5674e7b+d1bd76f71f,master-gac4afde19b+a9c1acf22d,w.2021.13
LSST Data Management Base Package
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 std::string 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 713 of file Log.h.

Constructor & Destructor Documentation

◆ Log()

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

Definition at line 719 of file Log.h.

719 : _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 }

◆ 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

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

354  {
355  // strip leading dots and spaces from suffix
356  auto pos = suffix.find_first_not_of(" .");
357  if (pos == std::string::npos) {
358  // empty, just return myself
359  return *this;
360  }
362  if (name.empty()) {
363  name = suffix.substr(pos);
364  } else {
365  name += '.';
366  name += suffix.substr(pos);
367  }
368  return getLogger(name);
369 }
table::Key< std::string > name
Definition: Amplifier.cc:116
static Log getLogger(Log const &logger)
Definition: Log.h:760
std::string getName() const
Get the logger name associated with the Log object.
Definition: Log.cc:240

◆ getDefaultLogger()

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

Return default logger instance, same as default constructor.

Definition at line 754 of file Log.h.

754 { return Log(); }

◆ getLevel()

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

Retrieve the logging threshold.

Returns
int Indicating the logging threshold.

Definition at line 318 of file Log.cc.

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

◆ getLogger() [1/2]

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

Definition at line 760 of file Log.h.

760 { 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:754
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 }

◆ isDebugEnabled()

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

Check whether the logger is enabled for the DEBUG Level.

Definition at line 724 of file Log.h.

724 { 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 333 of file Log.cc.

333  {
334  if (_logger->isEnabledFor(log4cxx::Level::toLevel(level))) {
335  return true;
336  } else {
337  return false;
338  }
339 }

◆ isErrorEnabled()

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

Check whether the logger is enabled for the ERROR Level.

Definition at line 728 of file Log.h.

728 { return _logger->isErrorEnabled(); }

◆ isFatalEnabled()

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

Check whether the logger is enabled for the FATAL Level.

Definition at line 732 of file Log.h.

732 { return _logger->isFatalEnabled(); }

◆ isInfoEnabled()

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

Check whether the logger is enabled for the INFO Level.

Definition at line 736 of file Log.h.

736 { return _logger->isInfoEnabled(); }

◆ isTraceEnabled()

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

Check whether the logger is enabled for the TRACE Level.

Definition at line 740 of file Log.h.

740 { return _logger->isTraceEnabled(); }

◆ isWarnEnabled()

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

Check whether the logger is enabled for the WARN Level.

Definition at line 744 of file Log.h.

744 { 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
...message arguments

Definition at line 374 of file Log.cc.

378  {
379  va_list args;
380  va_start(args, fmt);
381  char msg[MAX_LOG_MSG_LEN];
382  vsnprintf(msg, MAX_LOG_MSG_LEN, fmt, args);
383  logMsg(level, location, msg);
384 }
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:388
#define MAX_LOG_MSG_LEN
Definition: Log.cc:55
T vsnprintf(T... args)

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

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

◆ MDC()

std::string 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.
Returns
Previous value for the key in the MDC.

Definition at line 274 of file Log.cc.

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

◆ MDCRegisterInit()

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

Definition at line 292 of file Log.cc.

292  {
293 
294  std::lock_guard<std::mutex> lock(mdcInitMutex);
295 
296  // logMsg may have been called already in this thread, to make sure that
297  // this function is executed in this thread call it explicitly
298  function();
299 
300  // store function for later use
301  ::mdcInitFunctions.push_back(std::move(function));
302 
303  // return arbitrary number
304  return 1;
305 }
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 288 of file Log.cc.

288  {
289  log4cxx::MDC::remove(key);
290 }

◆ setLevel()

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

Set the logging threshold to LEVEL.

Parameters
levelNew logging threshold.

Definition at line 311 of file Log.cc.

311  {
312  _logger->setLevel(log4cxx::Level::toLevel(level));
313 }

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