LSSTApplications  11.0-13-gbb96280,12.1+18,12.1+7,12.1-1-g14f38d3+72,12.1-1-g16c0db7+5,12.1-1-g5961e7a+84,12.1-1-ge22e12b+23,12.1-11-g06625e2+4,12.1-11-g0d7f63b+4,12.1-19-gd507bfc,12.1-2-g7dda0ab+38,12.1-2-gc0bc6ab+81,12.1-21-g6ffe579+2,12.1-21-gbdb6c2a+4,12.1-24-g941c398+5,12.1-3-g57f6835+7,12.1-3-gf0736f3,12.1-37-g3ddd237,12.1-4-gf46015e+5,12.1-5-g06c326c+20,12.1-5-g648ee80+3,12.1-5-gc2189d7+4,12.1-6-ga608fc0+1,12.1-7-g3349e2a+5,12.1-7-gfd75620+9,12.1-9-g577b946+5,12.1-9-gc4df26a+10
LSSTDataManagementBasePackage
Public Member Functions | Static Public Member Functions | Private Member Functions | Static Private Member Functions | Private Attributes | 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)
 

Private Member Functions

 Log (log4cxx::LoggerPtr const &logger)
 Construct a Log using a LOG4CXX logger. More...
 

Static Private Member Functions

static log4cxx::LoggerPtr const & _defaultLogger (log4cxx::LoggerPtr const &newDefault=log4cxx::LoggerPtr())
 Returns default LOG4CXX logger. More...
 

Private Attributes

log4cxx::LoggerPtr _logger
 

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

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

Definition at line 732 of file Log.h.

732 : _logger(_defaultLogger()) { }
log4cxx::LoggerPtr _logger
Definition: Log.h:804
static log4cxx::LoggerPtr const & _defaultLogger(log4cxx::LoggerPtr const &newDefault=log4cxx::LoggerPtr())
Returns default LOG4CXX logger.
Definition: Log.cc:152
lsst::log::Log::Log ( log4cxx::LoggerPtr const &  logger)
inlineprivate

Construct a Log using a LOG4CXX logger.

The default constructor is called to ensure the default logger is initialized and LOG4CXX is configured.

Definition at line 802 of file Log.h.

802 : Log() { _logger = logger; }
log4cxx::LoggerPtr _logger
Definition: Log.h:804

Member Function Documentation

log4cxx::LoggerPtr const & lsst::log::Log::_defaultLogger ( log4cxx::LoggerPtr const &  newDefault = log4cxx::LoggerPtr())
staticprivate

Returns default LOG4CXX logger.

Parameters
newDefaultif non-zero then default is set to this value first.

Definition at line 152 of file Log.cc.

152  {
153  bool isNull = newDefault == log4cxx::LoggerPtr();
154 
155  // initialize on the first call (skip initialization if someone else did that)
156  static log4cxx::LoggerPtr _default(::log4cxxInit(!isNull));
157 
158  if (!isNull) {
159  _default = newDefault;
160  }
161  return _default;
162 }
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 }
static log4cxx::LoggerPtr const & _defaultLogger(log4cxx::LoggerPtr const &newDefault=log4cxx::LoggerPtr())
Returns default LOG4CXX logger.
Definition: Log.cc:152
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 }
static log4cxx::LoggerPtr const & _defaultLogger(log4cxx::LoggerPtr const &newDefault=log4cxx::LoggerPtr())
Returns default LOG4CXX logger.
Definition: Log.cc:152
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 }
def configure
Recursively configure a package using ups/.cfg files.
Definition: dependencies.py:39
static log4cxx::LoggerPtr const & _defaultLogger(log4cxx::LoggerPtr const &newDefault=log4cxx::LoggerPtr())
Returns default LOG4CXX logger.
Definition: Log.cc:152
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()); }
static log4cxx::LoggerPtr const & _defaultLogger(log4cxx::LoggerPtr const &newDefault=log4cxx::LoggerPtr())
Returns default LOG4CXX logger.
Definition: Log.cc:152
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
int lsst::log::Log::getLevel ( ) const

Retrieve the logging threshold.

Returns
int Indicating the logging threshold.

Definition at line 349 of file Log.cc.

349  {
350  log4cxx::LevelPtr level = _logger->getLevel();
351  int levelno = -1;
352  if (level != NULL) {
353  levelno = level->toInt();
354  }
355  return levelno;
356 }
log4cxx::LoggerPtr _logger
Definition: Log.h:804
static Log lsst::log::Log::getLogger ( Log const &  logger)
inlinestatic

Definition at line 772 of file Log.h.

772 { return logger; }
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
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 }
table::Key< std::string > name
Definition: ApCorrMap.cc:71
log4cxx::LoggerPtr _logger
Definition: Log.h:804
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(); }
log4cxx::LoggerPtr _logger
Definition: Log.h:804
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 364 of file Log.cc.

364  {
365  if (_logger->isEnabledFor(log4cxx::Level::toLevel(level))) {
366  return true;
367  } else {
368  return false;
369  }
370 }
log4cxx::LoggerPtr _logger
Definition: Log.h:804
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(); }
log4cxx::LoggerPtr _logger
Definition: Log.h:804
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(); }
log4cxx::LoggerPtr _logger
Definition: Log.h:804
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(); }
log4cxx::LoggerPtr _logger
Definition: Log.h:804
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(); }
log4cxx::LoggerPtr _logger
Definition: Log.h:804
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(); }
log4cxx::LoggerPtr _logger
Definition: Log.h:804
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 375 of file Log.cc.

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

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

Parameters
keyUnique key.
valueString value.

Definition at line 311 of file Log.cc.

311  {
312  log4cxx::MDC::put(key, value);
313 }
int lsst::log::Log::MDCRegisterInit ( std::function< void()>  function)
static

Definition at line 323 of file Log.cc.

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

319  {
320  log4cxx::MDC::remove(key);
321 }
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 }
static log4cxx::LoggerPtr const & _defaultLogger(log4cxx::LoggerPtr const &newDefault=log4cxx::LoggerPtr())
Returns default LOG4CXX logger.
Definition: Log.cc:152
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 }
table::Key< std::string > name
Definition: ApCorrMap.cc:71
static log4cxx::LoggerPtr const & _defaultLogger(log4cxx::LoggerPtr const &newDefault=log4cxx::LoggerPtr())
Returns default LOG4CXX logger.
Definition: Log.cc:152
void lsst::log::Log::setLevel ( int  level)

Set the logging threshold to LEVEL.

Parameters
levelNew logging threshold.

Definition at line 342 of file Log.cc.

342  {
343  _logger->setLevel(log4cxx::Level::toLevel(level));
344 }
log4cxx::LoggerPtr _logger
Definition: Log.h:804

Member Data Documentation

log4cxx::LoggerPtr lsst::log::Log::_logger
private

Definition at line 804 of file Log.h.


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