LSST Applications g070148d5b3+33e5256705,g0d53e28543+25c8b88941,g0da5cf3356+2dd1178308,g1081da9e2a+62d12e78cb,g17e5ecfddb+7e422d6136,g1c76d35bf8+ede3a706f7,g295839609d+225697d880,g2e2c1a68ba+cc1f6f037e,g2ffcdf413f+853cd4dcde,g38293774b4+62d12e78cb,g3b44f30a73+d953f1ac34,g48ccf36440+885b902d19,g4b2f1765b6+7dedbde6d2,g5320a0a9f6+0c5d6105b6,g56b687f8c9+ede3a706f7,g5c4744a4d9+ef6ac23297,g5ffd174ac0+0c5d6105b6,g6075d09f38+66af417445,g667d525e37+2ced63db88,g670421136f+2ced63db88,g71f27ac40c+2ced63db88,g774830318a+463cbe8d1f,g7876bc68e5+1d137996f1,g7985c39107+62d12e78cb,g7fdac2220c+0fd8241c05,g96f01af41f+368e6903a7,g9ca82378b8+2ced63db88,g9d27549199+ef6ac23297,gabe93b2c52+e3573e3735,gb065e2a02a+3dfbe639da,gbc3249ced9+0c5d6105b6,gbec6a3398f+0c5d6105b6,gc9534b9d65+35b9f25267,gd01420fc67+0c5d6105b6,geee7ff78d7+a14128c129,gf63283c776+ede3a706f7,gfed783d017+0c5d6105b6,w.2022.47
LSST Data Management Base Package
Loading...
Searching...
No Matches
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);
234 log4cxx::PropertyConfigurator::configure(prop);
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.

◆ 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

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