LSSTApplications  19.0.0-14-gb0260a2+72efe9b372,20.0.0+7927753e06,20.0.0+8829bf0056,20.0.0+995114c5d2,20.0.0+b6f4b2abd1,20.0.0+bddc4f4cbe,20.0.0-1-g253301a+8829bf0056,20.0.0-1-g2b7511a+0d71a2d77f,20.0.0-1-g5b95a8c+7461dd0434,20.0.0-12-g321c96ea+23efe4bbff,20.0.0-16-gfab17e72e+fdf35455f6,20.0.0-2-g0070d88+ba3ffc8f0b,20.0.0-2-g4dae9ad+ee58a624b3,20.0.0-2-g61b8584+5d3db074ba,20.0.0-2-gb780d76+d529cf1a41,20.0.0-2-ged6426c+226a441f5f,20.0.0-2-gf072044+8829bf0056,20.0.0-2-gf1f7952+ee58a624b3,20.0.0-20-geae50cf+e37fec0aee,20.0.0-25-g3dcad98+544a109665,20.0.0-25-g5eafb0f+ee58a624b3,20.0.0-27-g64178ef+f1f297b00a,20.0.0-3-g4cc78c6+e0676b0dc8,20.0.0-3-g8f21e14+4fd2c12c9a,20.0.0-3-gbd60e8c+187b78b4b8,20.0.0-3-gbecbe05+48431fa087,20.0.0-38-ge4adf513+a12e1f8e37,20.0.0-4-g97dc21a+544a109665,20.0.0-4-gb4befbc+087873070b,20.0.0-4-gf910f65+5d3db074ba,20.0.0-5-gdfe0fee+199202a608,20.0.0-5-gfbfe500+d529cf1a41,20.0.0-6-g64f541c+d529cf1a41,20.0.0-6-g9a5b7a1+a1cd37312e,20.0.0-68-ga3f3dda+5fca18c6a4,20.0.0-9-g4aef684+e18322736b,w.2020.45
LSSTDataManagementBasePackage
Log.h
Go to the documentation of this file.
1 /*
2  * LSST Data Management System
3  * Copyright 2013-2014 LSST Corporation.
4  *
5  * This product includes software developed by the
6  * LSST Project (http://www.lsst.org/).
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the LSST License Statement and
19  * the GNU General Public License along with this program. If not,
20  * see <http://www.lsstcorp.org/LegalNotices/>.
21  */
22 
32 #ifndef LSST_LOG_LOG_H
33 #define LSST_LOG_LOG_H
34 
35 
36 // System headers
37 #include <functional>
38 #include <sstream>
39 #include <stdarg.h>
40 #include <string>
41 
42 // Third-party headers
43 #include <log4cxx/logger.h>
44 #include <boost/format.hpp>
45 
52 #define LOG_CONFIG(filename) lsst::log::Log::configure(filename)
53 
66 #define LOG_CONFIG_PROP(string) lsst::log::Log::configure_prop(string)
67 
75 #define LOG_GET(logger) lsst::log::Log::getLogger(logger)
76 
85 #define LOG_GET_CHILD(logger, suffix) lsst::log::Log::getLogger(logger).getChild(suffix)
86 
97 #define LOG_MDC(key, value) lsst::log::Log::MDC(key, value)
98 
105 #define LOG_MDC_REMOVE(key) lsst::log::Log::MDCRemove(key)
106 
115 // These two macros generate unique variable name using __COUNTER__ built-in
116 // macro. Two levels of calls is needed to use __COUNTER__ macro value instead
117 // of the macro name in concatenation.
118 #define LOG_CONCAT_IMPL(a, b) a ## b
119 #define LOG_CONCAT_IMPL2(a, b) LOG_CONCAT_IMPL(a, b)
120 #define LOG_MDC_SCOPE(key, value) lsst::log::LogMDCScope LOG_CONCAT_IMPL2(_log_mdc_scope_, __COUNTER__)(key, value);
121 
142 #define LOG_MDC_INIT(func) lsst::log::Log::MDCRegisterInit(std::function<void()>(func))
143 
151 #define LOG_SET_LVL(logger, level) \
152  lsst::log::Log::getLogger(logger).setLevel(level)
153 
162 #define LOG_GET_LVL(logger) \
163  lsst::log::Log::getLogger(logger).getLevel()
164 
174 #define LOG_CHECK_LVL(logger, level) \
175  lsst::log::Log::getLogger(logger).isEnabledFor(level)
176 
183 #define LOG_CHECK_TRACE() \
184  LOG4CXX_UNLIKELY(lsst::log::Log::getDefaultLogger().isTraceEnabled())
185 
192 #define LOG_CHECK_DEBUG() \
193  LOG4CXX_UNLIKELY(lsst::log::Log::getDefaultLogger().isDebugEnabled())
194 
201 #define LOG_CHECK_INFO() \
202  lsst::log::Log::getDefaultLogger().isInfoEnabled()
203 
210 #define LOG_CHECK_WARN() \
211  lsst::log::Log::getDefaultLogger().isWarnEnabled()
212 
219 #define LOG_CHECK_ERROR() \
220  lsst::log::Log::getDefaultLogger().isErrorEnabled()
221 
228 #define LOG_CHECK_FATAL() \
229  lsst::log::Log::getDefaultLogger().isFatalEnabled()
230 
240 #define LOG(logger, level, message...) \
241  do { \
242  lsst::log::Log log(lsst::log::Log::getLogger(logger)); \
243  if (log.isEnabledFor(level)) { \
244  log.log(log4cxx::Level::toLevel(level), LOG4CXX_LOCATION, message); } \
245  } while (false)
246 
255 #define LOG_TRACE(message...) \
256  do { \
257  lsst::log::Log log; \
258  if (LOG4CXX_UNLIKELY(log.isTraceEnabled())) { \
259  log.log(log4cxx::Level::getTrace(), LOG4CXX_LOCATION, message); } \
260  } while (false)
261 
270 #define LOG_DEBUG(message...) \
271  do { \
272  lsst::log::Log log; \
273  if (LOG4CXX_UNLIKELY(log.isDebugEnabled())) { \
274  log.log(log4cxx::Level::getDebug(), LOG4CXX_LOCATION, message); } \
275  } while (false)
276 
285 #define LOG_INFO(message...) \
286  do { \
287  lsst::log::Log log; \
288  if (log.isInfoEnabled()) { \
289  log.log(log4cxx::Level::getInfo(), LOG4CXX_LOCATION, message); } \
290  } while (false)
291 
300 #define LOG_WARN(message...) \
301  do { \
302  lsst::log::Log log; \
303  if (log.isWarnEnabled()) { \
304  log.log(log4cxx::Level::getWarn(), LOG4CXX_LOCATION, message); } \
305  } while (false)
306 
315 #define LOG_ERROR(message...) \
316  do { \
317  lsst::log::Log log; \
318  if (log.isErrorEnabled()) { \
319  log.log(log4cxx::Level::getError(), LOG4CXX_LOCATION, message); } \
320  } while (false)
321 
330 #define LOG_FATAL(message...) \
331  do { \
332  lsst::log::Log log; \
333  if (log.isFatalEnabled()) { \
334  log.log(log4cxx::Level::getFatal(), LOG4CXX_LOCATION, message); } \
335  } while (false)
336 
337 
338 // small internal utility macro, not for regular clients
339 #define LOG_MESSAGE_VIA_STREAM_(logger, level, message) \
340  std::ostringstream stream_; \
341  stream_ << message; \
342  logger.logMsg(level, LOG4CXX_LOCATION, stream_.str())
343 
358 #define LOGS(logger, level, message) \
359  do { \
360  lsst::log::Log log(lsst::log::Log::getLogger(logger)); \
361  if (log.isEnabledFor(level)) { \
362  LOG_MESSAGE_VIA_STREAM_(log, log4cxx::Level::toLevel(level), message); \
363  } \
364  } while (false)
365 
377 #define LOGS_TRACE(message) \
378  do { \
379  lsst::log::Log log; \
380  if (LOG4CXX_UNLIKELY(log.isTraceEnabled())) { \
381  LOG_MESSAGE_VIA_STREAM_(log, log4cxx::Level::getTrace(), message); \
382  } \
383  } while (false)
384 
396 #define LOGS_DEBUG(message) \
397  do { \
398  lsst::log::Log log; \
399  if (LOG4CXX_UNLIKELY(log.isDebugEnabled())) { \
400  LOG_MESSAGE_VIA_STREAM_(log, log4cxx::Level::getDebug(), message); \
401  } \
402  } while (false)
403 
415 #define LOGS_INFO(message) \
416  do { \
417  lsst::log::Log log; \
418  if (log.isInfoEnabled()) { \
419  LOG_MESSAGE_VIA_STREAM_(log, log4cxx::Level::getInfo(), message); \
420  } \
421  } while (false)
422 
434 #define LOGS_WARN(message) \
435  do { \
436  lsst::log::Log log; \
437  if (log.isWarnEnabled()) { \
438  LOG_MESSAGE_VIA_STREAM_(log, log4cxx::Level::getWarn(), message); \
439  } \
440  } while (false)
441 
453 #define LOGS_ERROR(message) \
454  do { \
455  lsst::log::Log log; \
456  if (log.isErrorEnabled()) { \
457  LOG_MESSAGE_VIA_STREAM_(log, log4cxx::Level::getError(), message); \
458  } \
459  } while (false)
460 
472 #define LOGS_FATAL(message) \
473  do { \
474  lsst::log::Log log; \
475  if (log.isFatalEnabled()) { \
476  LOG_MESSAGE_VIA_STREAM_(log, log4cxx::Level::getFatal(), message); \
477  } \
478  } while (false)
479 
488 #define LOGL_TRACE(logger, message...) \
489  do { \
490  lsst::log::Log log(lsst::log::Log::getLogger(logger)); \
491  if (LOG4CXX_UNLIKELY(log.isTraceEnabled())) { \
492  log.log(log4cxx::Level::getTrace(), LOG4CXX_LOCATION, message);\
493  } \
494  } while (false)
495 
504 #define LOGL_DEBUG(logger, message...) \
505  do { \
506  lsst::log::Log log(lsst::log::Log::getLogger(logger)); \
507  if (LOG4CXX_UNLIKELY(log.isDebugEnabled())) { \
508  log.log(log4cxx::Level::getDebug(), LOG4CXX_LOCATION, message); \
509  } \
510  } while (false)
511 
520 #define LOGL_INFO(logger, message...) \
521  do { \
522  lsst::log::Log log(lsst::log::Log::getLogger(logger)); \
523  if (log.isInfoEnabled()) { \
524  log.log(log4cxx::Level::getInfo(), LOG4CXX_LOCATION, message); \
525  } \
526  } while (false)
527 
536 #define LOGL_WARN(logger, message...) \
537  do { \
538  lsst::log::Log log(lsst::log::Log::getLogger(logger)); \
539  if (log.isWarnEnabled()) { \
540  log.log(log4cxx::Level::getWarn(), LOG4CXX_LOCATION, message); \
541  } \
542  } while (false)
543 
552 #define LOGL_ERROR(logger, message...) \
553  do { \
554  lsst::log::Log log(lsst::log::Log::getLogger(logger)); \
555  if (log.isErrorEnabled()) { \
556  log.log(log4cxx::Level::getError(), LOG4CXX_LOCATION, message); \
557  } \
558  } while (false)
559 
568 #define LOGL_FATAL(logger, message...) \
569  do { \
570  lsst::log::Log log(lsst::log::Log::getLogger(logger)); \
571  if (log.isFatalEnabled()) { \
572  log.log(log4cxx::Level::getFatal(), LOG4CXX_LOCATION, message); \
573  } \
574  } while (false)
575 
588 #define LOGLS_TRACE(logger, message) \
589  do { \
590  lsst::log::Log log(lsst::log::Log::getLogger(logger)); \
591  if (LOG4CXX_UNLIKELY(log.isTraceEnabled())) { \
592  LOG_MESSAGE_VIA_STREAM_(log, log4cxx::Level::getTrace(), message); \
593  } \
594  } while (false)
595 
608 #define LOGLS_DEBUG(logger, message) \
609  do { \
610  lsst::log::Log log(lsst::log::Log::getLogger(logger)); \
611  if (LOG4CXX_UNLIKELY(log.isDebugEnabled())) { \
612  LOG_MESSAGE_VIA_STREAM_(log, log4cxx::Level::getDebug(), message); \
613  } \
614  } while (false)
615 
628 #define LOGLS_INFO(logger, message) \
629  do { \
630  lsst::log::Log log(lsst::log::Log::getLogger(logger)); \
631  if (log.isInfoEnabled()) { \
632  LOG_MESSAGE_VIA_STREAM_(log, log4cxx::Level::getInfo(), message); \
633  } \
634  } while (false)
635 
648 #define LOGLS_WARN(logger, message) \
649  do { \
650  lsst::log::Log log(lsst::log::Log::getLogger(logger)); \
651  if (log.isWarnEnabled()) { \
652  LOG_MESSAGE_VIA_STREAM_(log, log4cxx::Level::getWarn(), message); \
653  } \
654  } while (false)
655 
668 #define LOGLS_ERROR(logger, message) \
669  do { \
670  lsst::log::Log log(lsst::log::Log::getLogger(logger)); \
671  if (log.isErrorEnabled()) { \
672  LOG_MESSAGE_VIA_STREAM_(log, log4cxx::Level::getError(), message); \
673  } \
674  } while (false)
675 
688 #define LOGLS_FATAL(logger, message) \
689  do { \
690  lsst::log::Log log(lsst::log::Log::getLogger(logger)); \
691  if (log.isFatalEnabled()) { \
692  LOG_MESSAGE_VIA_STREAM_(log, log4cxx::Level::getFatal(), message); \
693  } \
694  } while (false)
695 
696 #define LOG_LVL_TRACE static_cast<int>(log4cxx::Level::TRACE_INT)
697 #define LOG_LVL_DEBUG static_cast<int>(log4cxx::Level::DEBUG_INT)
698 #define LOG_LVL_INFO static_cast<int>(log4cxx::Level::INFO_INT)
699 #define LOG_LVL_WARN static_cast<int>(log4cxx::Level::WARN_INT)
700 #define LOG_LVL_ERROR static_cast<int>(log4cxx::Level::ERROR_INT)
701 #define LOG_LVL_FATAL static_cast<int>(log4cxx::Level::FATAL_INT)
702 
703 #define LOG_LOGGER lsst::log::Log
704 
705 namespace lsst {
706 namespace log {
707 
713 class Log {
714 public:
715 
716  /***
717  * Default constructor creates an instance of root logger.
718  */
719  Log() : _logger(_defaultLogger()) { }
720 
724  bool isDebugEnabled() const { return _logger->isDebugEnabled(); }
728  bool isErrorEnabled() const { return _logger->isErrorEnabled(); }
732  bool isFatalEnabled() const { return _logger->isFatalEnabled(); }
736  bool isInfoEnabled() const { return _logger->isInfoEnabled(); }
740  bool isTraceEnabled() const { return _logger->isTraceEnabled(); }
744  bool isWarnEnabled() const { return _logger->isWarnEnabled(); }
745 
746  std::string getName() const;
747  void setLevel(int level);
748  int getLevel() const;
749  bool isEnabledFor(int level) const;
750 
751  Log getChild(std::string const& suffix) const;
752 
754  static Log getDefaultLogger() { return Log(); }
755 
756  static void configure();
757  static void configure(std::string const& filename);
758  static void configure_prop(std::string const& properties);
759 
760  static Log getLogger(Log const& logger) { return logger; }
761  static Log getLogger(std::string const& loggername);
762 
763  static std::string MDC(std::string const& key, std::string const& value);
764  static void MDCRemove(std::string const& key);
765  static int MDCRegisterInit(std::function<void()> function);
766 
767  void log(log4cxx::LevelPtr level,
768  log4cxx::spi::LocationInfo const& location,
769  char const* fmt, ...);
770  void logMsg(log4cxx::LevelPtr level,
771  log4cxx::spi::LocationInfo const& location,
772  std::string const& msg);
773 
774 private:
775 
782  static log4cxx::LoggerPtr const& _defaultLogger();
783 
790  Log(log4cxx::LoggerPtr const& logger) : Log() { _logger = logger; }
791 
792  log4cxx::LoggerPtr _logger;
793 };
794 
795 class LogMDCScope {
796 public:
797 
802  LogMDCScope(std::string const& key, std::string const& value)
803  : _key(key)
804  {
805  _oldValue = Log::MDC(key, value);
806  }
807 
808  // no copy allowed
809  LogMDCScope(LogMDCScope const&) = delete;
810  LogMDCScope& operator=(LogMDCScope const&) = delete;
811 
813  _key = other._key;
814  _oldValue = other._oldValue;
815  other._key.clear();
816  }
817 
819  if (not _key.empty()) {
820  if (_oldValue.empty()) {
821  Log::MDCRemove(_key);
822  } else {
823  Log::MDC(_key, _oldValue);
824  }
825  }
826  _key = other._key;
827  _oldValue = other._oldValue;
828  other._key.clear();
829  return *this;
830  }
831 
836  if (not _key.empty()) {
837  if (_oldValue.empty()) {
838  Log::MDCRemove(_key);
839  } else {
840  Log::MDC(_key, _oldValue);
841  }
842  }
843  }
844 
845 private:
846  std::string _key;
847  std::string _oldValue;
848 };
849 
857 unsigned lwpID();
858 
859 }} // namespace lsst::log
860 
861 #endif // LSST_LOG_LOG_H
lsst::log::LogMDCScope
Definition: Log.h:795
lsst::log::LogMDCScope::LogMDCScope
LogMDCScope(LogMDCScope &&other)
Definition: Log.h:812
std::string
STL class.
lsst::log::Log::MDCRemove
static void MDCRemove(std::string const &key)
Remove the value associated with KEY within the MDC.
Definition: Log.cc:288
lsst::log::Log::getLevel
int getLevel() const
Retrieve the logging threshold.
Definition: Log.cc:318
lsst::afw.display.interface.logger
logger
Definition: interface.py:39
lsst::log::LogMDCScope::~LogMDCScope
~LogMDCScope()
Destructor restores old key value in MDC.
Definition: Log.h:835
lsst::log::Log::isEnabledFor
bool isEnabledFor(int level) const
Return whether the logging threshold of the logger is less than or equal to LEVEL.
Definition: Log.cc:333
lsst::log::LogMDCScope::operator=
LogMDCScope & operator=(LogMDCScope const &)=delete
lsst::log::Log::getLogger
static Log getLogger(Log const &logger)
Definition: Log.h:760
std::function
lsst::log::LogMDCScope::operator=
LogMDCScope & operator=(LogMDCScope &&other)
Definition: Log.h:818
lsst::log::Log::logMsg
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:388
lsst::log::Log::configure_prop
static void configure_prop(std::string const &properties)
Configures log4cxx using a string containing the list of properties, equivalent to configuring from a...
Definition: Log.cc:220
lsst::log::Log::isWarnEnabled
bool isWarnEnabled() const
Check whether the logger is enabled for the WARN Level.
Definition: Log.h:744
lsst::log::Log::isTraceEnabled
bool isTraceEnabled() const
Check whether the logger is enabled for the TRACE Level.
Definition: Log.h:740
lsst::log::Log::isFatalEnabled
bool isFatalEnabled() const
Check whether the logger is enabled for the FATAL Level.
Definition: Log.h:732
lsst::log::LogMDCScope::LogMDCScope
LogMDCScope(LogMDCScope const &)=delete
lsst::base.packages.log
log
Definition: packages.py:38
lsst::log::LogMDCScope::LogMDCScope
LogMDCScope(std::string const &key, std::string const &value)
Constructor adds KEY/VALUE pair to current thread MDC.
Definition: Log.h:802
lsst::log::lwpID
unsigned lwpID()
Function which returns LWP ID on platforms which support it.
Definition: Log.cc:413
other
ItemVariant const * other
Definition: Schema.cc:56
lsst::log::Log::configure
static void configure()
Explicitly configures log4cxx and initializes logging system.
Definition: Log.cc:177
lsst::log::Log::getName
std::string getName() const
Get the logger name associated with the Log object.
Definition: Log.cc:240
lsst::log::Log::getDefaultLogger
static Log getDefaultLogger()
Return default logger instance, same as default constructor.
Definition: Log.h:754
lsst::log::Log::Log
Log()
Definition: Log.h:719
lsst::log::Log::isDebugEnabled
bool isDebugEnabled() const
Check whether the logger is enabled for the DEBUG Level.
Definition: Log.h:724
lsst
A base class for image defects.
Definition: imageAlgorithm.dox:1
lsst::log::Log::log
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 wit...
Definition: Log.cc:374
lsst::log::Log::MDCRegisterInit
static int MDCRegisterInit(std::function< void()> function)
Definition: Log.cc:292
key
Key< U > key
Definition: Schema.cc:281
std::string::empty
T empty(T... args)
lsst::log::Log::isInfoEnabled
bool isInfoEnabled() const
Check whether the logger is enabled for the INFO Level.
Definition: Log.h:736
lsst::log::Log::MDC
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.
Definition: Log.cc:274
lsst::log::Log
This static class includes a variety of methods for interacting with the the logging module.
Definition: Log.h:713
lsst::log::Log::getChild
Log getChild(std::string const &suffix) const
Return a logger which is a descendant to this logger.
Definition: Log.cc:354
lsst::log::Log::setLevel
void setLevel(int level)
Set the logging threshold to LEVEL.
Definition: Log.cc:311
lsst::log::Log::isErrorEnabled
bool isErrorEnabled() const
Check whether the logger is enabled for the ERROR Level.
Definition: Log.h:728