LSSTApplications  11.0-13-gbb96280,12.1.rc1,12.1.rc1+1,12.1.rc1+2,12.1.rc1+5,12.1.rc1+8,12.1.rc1-1-g06d7636+1,12.1.rc1-1-g253890b+5,12.1.rc1-1-g3d31b68+7,12.1.rc1-1-g3db6b75+1,12.1.rc1-1-g5c1385a+3,12.1.rc1-1-g83b2247,12.1.rc1-1-g90cb4cf+6,12.1.rc1-1-g91da24b+3,12.1.rc1-2-g3521f8a,12.1.rc1-2-g39433dd+4,12.1.rc1-2-g486411b+2,12.1.rc1-2-g4c2be76,12.1.rc1-2-gc9c0491,12.1.rc1-2-gda2cd4f+6,12.1.rc1-3-g3391c73+2,12.1.rc1-3-g8c1bd6c+1,12.1.rc1-3-gcf4b6cb+2,12.1.rc1-4-g057223e+1,12.1.rc1-4-g19ed13b+2,12.1.rc1-4-g30492a7
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

#include <Log.h>

Public Member Functions

 Log ()
 
bool isDebugEnabled () const
 
bool isErrorEnabled () const
 
bool isFatalEnabled () const
 
bool isInfoEnabled () const
 
bool isTraceEnabled () const
 
bool isWarnEnabled () const
 
std::string getName () const
 
void setLevel (int level)
 
int getLevel () const
 
bool isEnabledFor (int level) const
 
void log (log4cxx::LevelPtr level, log4cxx::spi::LocationInfo const &location, char const *fmt,...)
 
void logMsg (log4cxx::LevelPtr level, log4cxx::spi::LocationInfo const &location, std::string const &msg)
 

Static Public Member Functions

static Log getDefaultLogger ()
 Return default logger instance, same as default constructor. More...
 
static std::string getDefaultLoggerName ()
 
static void configure ()
 
static void configure (std::string const &filename)
 
static void configure_prop (std::string const &properties)
 
static Log getLogger (Log const &logger)
 
static Log getLogger (std::string const &loggername)
 
static void pushContext (std::string const &name)
 
static void popContext ()
 
static void MDC (std::string const &key, std::string const &value)
 
static void MDCRemove (std::string const &key)
 
static int MDCRegisterInit (std::function< void()> function)
 
static void log (Log logger, log4cxx::LevelPtr level, log4cxx::spi::LocationInfo const &location, char const *fmt,...)
 
static void logMsg (Log logger, log4cxx::LevelPtr level, log4cxx::spi::LocationInfo const &location, std::string const &msg)
 

Private Member Functions

 Log (log4cxx::LoggerPtr const &logger)
 

Static Private Member Functions

static log4cxx::LoggerPtr & _defaultLogger ()
 

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()) { }
static log4cxx::LoggerPtr & _defaultLogger()
Definition: Log.cc:149
log4cxx::LoggerPtr _logger
Definition: Log.h:803
lsst::log::Log::Log ( log4cxx::LoggerPtr const &  logger)
inlineprivate

Definition at line 801 of file Log.h.

801 : _logger(logger) { }
log4cxx::LoggerPtr _logger
Definition: Log.h:803

Member Function Documentation

log4cxx::LoggerPtr & lsst::log::Log::_defaultLogger ( )
staticprivate

Returns default LOG4CXX logger.

Definition at line 149 of file Log.cc.

149  {
150  static log4cxx::LoggerPtr _default(::log4cxxInit());
151  return _default;
152 }
void lsst::log::Log::configure ( )
static

Explicitly configures log4cxx and initializes logging system.

Uses either default configuration or log4cxx basic configuration. Default 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 log4cxx BasicConfigurator class is used to configure, 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 "%-4r [%t] %-5p %c %x - %m%n".

Definition at line 165 of file Log.cc.

165  {
166 
167  // in case log4cxxInit() was not called yet tell it to ignore basic config
168  ::doBasicConfig = false;
169 
170  // TODO: does resetConfiguration() remove existing appenders?
171  log4cxx::BasicConfigurator::resetConfiguration();
172 
173  // if LSST_LOG_CONFIG is set then use that file
174  if (const char* env = getenv(::configEnv)) {
175  if (env[0] and access(env, R_OK) == 0) {
176  configure(env);
177  return;
178  }
179  }
180 
181  // Do basic configuration (only if not configured already?)
182  log4cxx::LoggerPtr rootLogger = log4cxx::Logger::getRootLogger();
183  if (rootLogger->getAllAppenders().size() == 0) {
185  }
186 
187  // reset default logger to the root logger
188  _defaultLogger() = rootLogger;
189 }
static void configure()
Definition: Log.cc:165
static log4cxx::LoggerPtr & _defaultLogger()
Definition: Log.cc:149
def configure
Recursively configure a package using ups/.cfg files.
Definition: dependencies.py:39
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  // in case log4cxxInit() was not called yet tell it to ignore basic config
203  ::doBasicConfig = false;
204  // TODO: does resetConfiguration() remove existing appenders?
205  log4cxx::BasicConfigurator::resetConfiguration();
206  if (getFileExtension(filename).compare(".xml") == 0) {
208  } else {
210  }
211  // reset default logger to the root logger
212  _defaultLogger() = log4cxx::Logger::getRootLogger();
213 }
static log4cxx::LoggerPtr & _defaultLogger()
Definition: Log.cc:149
def configure
Recursively configure a package using ups/.cfg files.
Definition: dependencies.py:39
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 221 of file Log.cc.

221  {
222  // in case log4cxxInit() was not called yet tell it to ignore basic config
223  ::doBasicConfig = false;
224 
225  std::vector<unsigned char> data(properties.begin(), properties.end());
226  log4cxx::helpers::InputStreamPtr inStream(new log4cxx::helpers::ByteArrayInputStream(data));
227  log4cxx::helpers::Properties prop;
228  prop.load(inStream);
230 
231  // reset default logger to the root logger
232  _defaultLogger() = log4cxx::Logger::getRootLogger();
233 }
static log4cxx::LoggerPtr & _defaultLogger()
Definition: Log.cc:149
def configure
Recursively configure a package using ups/.cfg files.
Definition: dependencies.py:39
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 & _defaultLogger()
Definition: Log.cc:149
std::string lsst::log::Log::getDefaultLoggerName ( )
static

Get the current default logger name.

Returns
String containing the default logger name.

Definition at line 238 of file Log.cc.

238  {
239  return getDefaultLogger().getName();
240 }
static Log getDefaultLogger()
Return default logger instance, same as default constructor.
Definition: Log.h:765
std::string getName() const
Definition: Log.cc:245
int lsst::log::Log::getLevel ( ) const

Retrieve the logging threshold.

Returns
int Indicating the logging threshold.

Definition at line 356 of file Log.cc.

356  {
357  log4cxx::LevelPtr level = _logger->getLevel();
358  int levelno = -1;
359  if (level != NULL) {
360  levelno = level->toInt();
361  }
362  return levelno;
363 }
log4cxx::LoggerPtr _logger
Definition: Log.h:803
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 261 of file Log.cc.

261  {
262  if (loggername.empty()){
263  return getDefaultLogger();
264  } else {
265  return Log(log4cxx::Logger::getLogger(loggername));
266  }
267 }
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 245 of file Log.cc.

245  {
246  std::string name = _logger->getName();
247  if (name == "root") {
248  name.clear();
249  }
250  return name;
251 }
table::Key< std::string > name
Definition: ApCorrMap.cc:71
log4cxx::LoggerPtr _logger
Definition: Log.h:803
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:803
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 371 of file Log.cc.

371  {
372  if (_logger->isEnabledFor(log4cxx::Level::toLevel(level))) {
373  return true;
374  } else {
375  return false;
376  }
377 }
log4cxx::LoggerPtr _logger
Definition: Log.h:803
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:803
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:803
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:803
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:803
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:803
void lsst::log::Log::log ( Log  logger,
log4cxx::LevelPtr  level,
log4cxx::spi::LocationInfo const &  location,
char const *  fmt,
  ... 
)
static

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

Parameters
loggerthe logger
levelmessage level
locationmessage origin location
fmtmessage format string

Definition at line 397 of file Log.cc.

402  {
403  va_list args;
404  va_start(args, fmt);
405  char msg[MAX_LOG_MSG_LEN];
406  vsnprintf(msg, MAX_LOG_MSG_LEN, fmt, args);
407  logger.logMsg(level, location, msg);
408 }
#define MAX_LOG_MSG_LEN
Definition: Log.cc:56
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 382 of file Log.cc.

386  {
387  va_list args;
388  va_start(args, fmt);
389  char msg[MAX_LOG_MSG_LEN];
390  vsnprintf(msg, MAX_LOG_MSG_LEN, fmt, args);
391  logMsg(level, location, msg);
392 }
#define MAX_LOG_MSG_LEN
Definition: Log.cc:56
static void logMsg(Log logger, log4cxx::LevelPtr level, log4cxx::spi::LocationInfo const &location, std::string const &msg)
Definition: Log.cc:439
void lsst::log::Log::logMsg ( Log  logger,
log4cxx::LevelPtr  level,
log4cxx::spi::LocationInfo const &  location,
std::string const &  msg 
)
static

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

Parameters
loggerthe logger
levelmessage level
locationmessage origin location
msgmessage string

Definition at line 439 of file Log.cc.

443  {
444  logger.logMsg(level, location, msg);
445 }
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 412 of file Log.cc.

415  {
416 
417  // do one-time per-thread initialization, this was implemented
418  // with thread_local initially but clang on OS X did not support it
419  void *ptr = pthread_getspecific(::pthreadKey.key);
420  if (ptr == nullptr) {
421 
422  // use pointer value as a flag, don't care where it points to
423  ptr = static_cast<void*>(&::pthreadKey);
424  pthread_setspecific(::pthreadKey.key, ptr);
425 
426  std::lock_guard<std::mutex> lock(mdcInitMutex);
427  // call all functions in the MDC init list
428  for (auto& fun: mdcInitFunctions) {
429  fun();
430  }
431  }
432 
433  // forward everything to logger
434  _logger->forcedLog(level, msg, location);
435 }
log4cxx::LoggerPtr _logger
Definition: Log.h:803
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 318 of file Log.cc.

318  {
319  log4cxx::MDC::put(key, value);
320 }
int lsst::log::Log::MDCRegisterInit ( std::function< void()>  function)
static

Definition at line 330 of file Log.cc.

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

326  {
327  log4cxx::MDC::remove(key);
328 }
void lsst::log::Log::popContext ( )
static

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

Definition at line 300 of file Log.cc.

300  {
301  // switch to parent logger, this assumes that loggers are not
302  // re-parented between calls to push and pop
303  log4cxx::LoggerPtr parent = _defaultLogger()->getParent();
304  // root logger does not have parent, stay at root instead
305  if (parent) {
306  _defaultLogger() = parent;
307  }
308 }
static log4cxx::LoggerPtr & _defaultLogger()
Definition: Log.cc:149
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 273 of file Log.cc.

273  {
274  // can't handle empty names
275  if (name.empty()) {
276  throw std::invalid_argument("lsst::log::Log::pushContext(): "
277  "empty context name is not allowed");
278  }
279  // we do not allow multi-level context (logger1.logger2)
280  if (name.find('.') != std::string::npos) {
281  throw std::invalid_argument("lsst::log::Log::pushContext(): "
282  "multi-level contexts are not allowed: " + name);
283  }
284 
285  // Construct new default logger name
286  std::string newName = _defaultLogger()->getName();
287  if (newName == "root") {
288  newName = name;
289  } else {
290  newName += ".";
291  newName += name;
292  }
293  // Update defaultLogger
294  _defaultLogger() = log4cxx::Logger::getLogger(newName);
295 }
table::Key< std::string > name
Definition: ApCorrMap.cc:71
static log4cxx::LoggerPtr & _defaultLogger()
Definition: Log.cc:149
void lsst::log::Log::setLevel ( int  level)

Set the logging threshold to LEVEL.

Parameters
levelNew logging threshold.

Definition at line 349 of file Log.cc.

349  {
350  _logger->setLevel(log4cxx::Level::toLevel(level));
351 }
log4cxx::LoggerPtr _logger
Definition: Log.h:803

Member Data Documentation

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

Definition at line 803 of file Log.h.


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