LSST Applications g04c3c9f7ca+2075667efa,g1e125bf412+5f448d5fcf,g2079a07aa2+3e9fd84d81,g2305ad1205+b635cf1488,g2bbee38e9b+6c6beb4891,g337abbeb29+6c6beb4891,g33d1c0ed96+6c6beb4891,g3a166c0a6a+6c6beb4891,g50ff169b8f+96c6868917,g52b1c1532d+585e252eca,g591dd9f2cf+42f171e1e6,g5c3423f6d4+d536b04327,g607f77f49a+d536b04327,g6f43f06aed+ca1339dc19,g858d7b2824+d536b04327,g8ee334c5b4+d7f9608c2f,g9963eaa53e+b3dc1655d3,g998f4353bf+d536b04327,g99cad8db69+8ef2408349,g9ddcbc5298+9a081db1e4,ga1e77700b3+2cbb763275,gadfd92a7e4+aec2f3b930,gae0086650b+585e252eca,gb0e22166c9+0e73c8378f,gb3b7280ab2+cb5fdb229e,gbb8dafda3b+a327199e22,gc120e1dc64+88074880ea,gc28159a63d+6c6beb4891,gcdd4ae20e8+bd241b2308,gcde1bda545+903e937d91,gcf0d15dbbd+bd241b2308,gdaeeff99f8+f9a426f77a,gddc38dedce+585e252eca,ge79ae78c31+6c6beb4891,gfbcc870c63+b310236976,w.2024.23
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.
 
bool isErrorEnabled () const
 Check whether the logger is enabled for the ERROR Level.
 
bool isFatalEnabled () const
 Check whether the logger is enabled for the FATAL Level.
 
bool isInfoEnabled () const
 Check whether the logger is enabled for the INFO Level.
 
bool isTraceEnabled () const
 Check whether the logger is enabled for the TRACE Level.
 
bool isWarnEnabled () const
 Check whether the logger is enabled for the WARN Level.
 
std::string getName () const
 Get the logger name associated with the Log object.
 
void setLevel (int level)
 Set the logging threshold to LEVEL.
 
int getLevel () const
 Retrieve the logging threshold.
 
int getEffectiveLevel () const
 Retrieve the effective logging threshold.
 
bool isEnabledFor (int level) const
 Return whether the logging threshold of the logger is less than or equal to LEVEL.
 
Log getChild (std::string const &suffix) const
 Return a logger which is a descendant to this logger.
 
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.
 
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.
 

Static Public Member Functions

static Log getDefaultLogger ()
 Return default logger instance, same as default constructor.
 
static void configure ()
 Explicitly configures log4cxx and initializes logging system.
 
static void configure (std::string const &filename)
 Configures log4cxx from specified file.
 
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.
 
static Log getLogger (Log const &logger)
 
static Log getLogger (std::string const &loggername)
 Returns logger object for a given name.
 
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.
 
static void MDCRemove (std::string const &key)
 Remove the value associated with KEY within the MDC.
 
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 }
373 std::string name = getName();
374 if (name.empty()) {
375 name = suffix.substr(pos);
376 } else {
377 name += '.';
378 name += suffix.substr(pos);
379 }
380 return getLogger(name);
381}
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}
table::Key< std::string > name
Definition Amplifier.cc:116

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

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