LSSTApplications  18.0.0+106,18.0.0+50,19.0.0,19.0.0+1,19.0.0+10,19.0.0+11,19.0.0+13,19.0.0+17,19.0.0+2,19.0.0-1-g20d9b18+6,19.0.0-1-g425ff20,19.0.0-1-g5549ca4,19.0.0-1-g580fafe+6,19.0.0-1-g6fe20d0+1,19.0.0-1-g7011481+9,19.0.0-1-g8c57eb9+6,19.0.0-1-gb5175dc+11,19.0.0-1-gdc0e4a7+9,19.0.0-1-ge272bc4+6,19.0.0-1-ge3aa853,19.0.0-10-g448f008b,19.0.0-12-g6990b2c,19.0.0-2-g0d9f9cd+11,19.0.0-2-g3d9e4fb2+11,19.0.0-2-g5037de4,19.0.0-2-gb96a1c4+3,19.0.0-2-gd955cfd+15,19.0.0-3-g2d13df8,19.0.0-3-g6f3c7dc,19.0.0-4-g725f80e+11,19.0.0-4-ga671dab3b+1,19.0.0-4-gad373c5+3,19.0.0-5-ga2acb9c+2,19.0.0-5-gfe96e6c+2,w.2020.01
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
static Log getDefaultLogger()
Return default logger instance, same as default constructor.
Definition: Log.h:754
bool isWarnEnabled() const
Check whether the logger is enabled for the WARN Level.
Definition: Log.h:744
bool isErrorEnabled() const
Check whether the logger is enabled for the ERROR Level.
Definition: Log.h:728
static void configure()
Explicitly configures log4cxx and initializes logging system.
Definition: Log.cc:177
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
Log getChild(std::string const &suffix) const
Return a logger which is a descendant to this logger.
Definition: Log.cc:354
This static class includes a variety of methods for interacting with the the logging module...
Definition: Log.h:713
LogMDCScope(std::string const &key, std::string const &value)
Constructor adds KEY/VALUE pair to current thread MDC.
Definition: Log.h:802
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
bool isInfoEnabled() const
Check whether the logger is enabled for the INFO Level.
Definition: Log.h:736
ItemVariant const * other
Definition: Schema.cc:56
bool isDebugEnabled() const
Check whether the logger is enabled for the DEBUG Level.
Definition: Log.h:724
STL class.
std::string getName() const
Get the logger name associated with the Log object.
Definition: Log.cc:240
A base class for image defects.
static void MDCRemove(std::string const &key)
Remove the value associated with KEY within the MDC.
Definition: Log.cc:288
int getLevel() const
Retrieve the logging threshold.
Definition: Log.cc:318
LogMDCScope & operator=(LogMDCScope &&other)
Definition: Log.h:818
LogMDCScope(LogMDCScope &&other)
Definition: Log.h:812
Key< U > key
Definition: Schema.cc:281
void setLevel(int level)
Set the logging threshold to LEVEL.
Definition: Log.cc:311
bool isTraceEnabled() const
Check whether the logger is enabled for the TRACE Level.
Definition: Log.h:740
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
~LogMDCScope()
Destructor restores old key value in MDC.
Definition: Log.h:835
bool isEnabledFor(int level) const
Return whether the logging threshold of the logger is less than or equal to LEVEL.
Definition: Log.cc:333
static Log getLogger(Log const &logger)
Definition: Log.h:760
unsigned lwpID()
Function which returns LWP ID on platforms which support it.
Definition: Log.cc:413
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
bool isFatalEnabled() const
Check whether the logger is enabled for the FATAL Level.
Definition: Log.h:732
static int MDCRegisterInit(std::function< void()> function)
Definition: Log.cc:292