LSST Applications  21.0.0-147-g0e635eb1+1acddb5be5,22.0.0+052faf71bd,22.0.0+1ea9a8b2b2,22.0.0+6312710a6c,22.0.0+729191ecac,22.0.0+7589c3a021,22.0.0+9f079a9461,22.0.1-1-g7d6de66+b8044ec9de,22.0.1-1-g87000a6+536b1ee016,22.0.1-1-g8e32f31+6312710a6c,22.0.1-10-gd060f87+016f7cdc03,22.0.1-12-g9c3108e+df145f6f68,22.0.1-16-g314fa6d+c825727ab8,22.0.1-19-g93a5c75+d23f2fb6d8,22.0.1-19-gb93eaa13+aab3ef7709,22.0.1-2-g8ef0a89+b8044ec9de,22.0.1-2-g92698f7+9f079a9461,22.0.1-2-ga9b0f51+052faf71bd,22.0.1-2-gac51dbf+052faf71bd,22.0.1-2-gb66926d+6312710a6c,22.0.1-2-gcb770ba+09e3807989,22.0.1-20-g32debb5+b8044ec9de,22.0.1-23-gc2439a9a+fb0756638e,22.0.1-3-g496fd5d+09117f784f,22.0.1-3-g59f966b+1e6ba2c031,22.0.1-3-g849a1b8+f8b568069f,22.0.1-3-gaaec9c0+c5c846a8b1,22.0.1-32-g5ddfab5d3+60ce4897b0,22.0.1-4-g037fbe1+64e601228d,22.0.1-4-g8623105+b8044ec9de,22.0.1-5-g096abc9+d18c45d440,22.0.1-5-g15c806e+57f5c03693,22.0.1-7-gba73697+57f5c03693,master-g6e05de7fdc+c1283a92b8,master-g72cdda8301+729191ecac,w.2021.39
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...
 
int getEffectiveLevel () const
 Retrieve the effective 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 724 of file Log.h.

Constructor & Destructor Documentation

◆ Log()

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

Definition at line 730 of file Log.h.

730 : _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:61

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

366  {
367  // strip leading dots and spaces from suffix
368  auto pos = suffix.find_first_not_of(" .");
369  if (pos == std::string::npos) {
370  // empty, just return myself
371  return *this;
372  }
374  if (name.empty()) {
375  name = suffix.substr(pos);
376  } else {
377  name += '.';
378  name += suffix.substr(pos);
379  }
380  return getLogger(name);
381 }
table::Key< std::string > name
Definition: Amplifier.cc:116
static Log getLogger(Log const &logger)
Definition: Log.h:772
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 766 of file Log.h.

766 { return Log(); }

◆ getEffectiveLevel()

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

Retrieve the effective logging threshold.

Returns
int Indicating the effective logging threshold.

Definition at line 330 of file Log.cc.

330  {
331  log4cxx::LevelPtr level = _logger->getEffectiveLevel();
332  int levelno = -1;
333  if (level != NULL) {
334  levelno = level->toInt();
335  }
336  return levelno;
337 }

◆ 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 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 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:766
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 735 of file Log.h.

735 { 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 345 of file Log.cc.

345  {
346  if (_logger->isEnabledFor(log4cxx::Level::toLevel(level))) {
347  return true;
348  } else {
349  return false;
350  }
351 }

◆ isErrorEnabled()

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

Check whether the logger is enabled for the ERROR Level.

Definition at line 739 of file Log.h.

739 { return _logger->isErrorEnabled(); }

◆ isFatalEnabled()

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

Check whether the logger is enabled for the FATAL Level.

Definition at line 743 of file Log.h.

743 { return _logger->isFatalEnabled(); }

◆ isInfoEnabled()

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

Check whether the logger is enabled for the INFO Level.

Definition at line 747 of file Log.h.

747 { return _logger->isInfoEnabled(); }

◆ isTraceEnabled()

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

Check whether the logger is enabled for the TRACE Level.

Definition at line 751 of file Log.h.

751 { return _logger->isTraceEnabled(); }

◆ isWarnEnabled()

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

Check whether the logger is enabled for the WARN Level.

Definition at line 755 of file Log.h.

755 { 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 386 of file Log.cc.

390  {
391  va_list args;
392  va_start(args, fmt);
393  char msg[MAX_LOG_MSG_LEN];
394  vsnprintf(msg, MAX_LOG_MSG_LEN, fmt, args);
395  logMsg(level, location, msg);
396 }
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:400
#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 400 of file Log.cc.

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

◆ 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: