LSSTApplications  17.0+11,17.0+34,17.0+56,17.0+57,17.0+59,17.0+7,17.0-1-g377950a+33,17.0.1-1-g114240f+2,17.0.1-1-g4d4fbc4+28,17.0.1-1-g55520dc+49,17.0.1-1-g5f4ed7e+52,17.0.1-1-g6dd7d69+17,17.0.1-1-g8de6c91+11,17.0.1-1-gb9095d2+7,17.0.1-1-ge9fec5e+5,17.0.1-1-gf4e0155+55,17.0.1-1-gfc65f5f+50,17.0.1-1-gfc6fb1f+20,17.0.1-10-g87f9f3f+1,17.0.1-11-ge9de802+16,17.0.1-16-ga14f7d5c+4,17.0.1-17-gc79d625+1,17.0.1-17-gdae4c4a+8,17.0.1-2-g26618f5+29,17.0.1-2-g54f2ebc+9,17.0.1-2-gf403422+1,17.0.1-20-g2ca2f74+6,17.0.1-23-gf3eadeb7+1,17.0.1-3-g7e86b59+39,17.0.1-3-gb5ca14a,17.0.1-3-gd08d533+40,17.0.1-30-g596af8797,17.0.1-4-g59d126d+4,17.0.1-4-gc69c472+5,17.0.1-6-g5afd9b9+4,17.0.1-7-g35889ee+1,17.0.1-7-gc7c8782+18,17.0.1-9-gc4bbfb2+3,w.2019.22
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 
127 #define LOG_MDC_INIT(func) lsst::log::Log::MDCRegisterInit(std::function<void()>(func))
128 
136 #define LOG_SET_LVL(logger, level) \
137  lsst::log::Log::getLogger(logger).setLevel(level)
138 
147 #define LOG_GET_LVL(logger) \
148  lsst::log::Log::getLogger(logger).getLevel()
149 
159 #define LOG_CHECK_LVL(logger, level) \
160  lsst::log::Log::getLogger(logger).isEnabledFor(level)
161 
168 #define LOG_CHECK_TRACE() \
169  LOG4CXX_UNLIKELY(lsst::log::Log::getDefaultLogger().isTraceEnabled())
170 
177 #define LOG_CHECK_DEBUG() \
178  LOG4CXX_UNLIKELY(lsst::log::Log::getDefaultLogger().isDebugEnabled())
179 
186 #define LOG_CHECK_INFO() \
187  lsst::log::Log::getDefaultLogger().isInfoEnabled()
188 
195 #define LOG_CHECK_WARN() \
196  lsst::log::Log::getDefaultLogger().isWarnEnabled()
197 
204 #define LOG_CHECK_ERROR() \
205  lsst::log::Log::getDefaultLogger().isErrorEnabled()
206 
213 #define LOG_CHECK_FATAL() \
214  lsst::log::Log::getDefaultLogger().isFatalEnabled()
215 
225 #define LOG(logger, level, message...) \
226  do { \
227  lsst::log::Log log(lsst::log::Log::getLogger(logger)); \
228  if (log.isEnabledFor(level)) { \
229  log.log(log4cxx::Level::toLevel(level), LOG4CXX_LOCATION, message); } \
230  } while (false)
231 
240 #define LOG_TRACE(message...) \
241  do { \
242  lsst::log::Log log; \
243  if (LOG4CXX_UNLIKELY(log.isTraceEnabled())) { \
244  log.log(log4cxx::Level::getTrace(), LOG4CXX_LOCATION, message); } \
245  } while (false)
246 
255 #define LOG_DEBUG(message...) \
256  do { \
257  lsst::log::Log log; \
258  if (LOG4CXX_UNLIKELY(log.isDebugEnabled())) { \
259  log.log(log4cxx::Level::getDebug(), LOG4CXX_LOCATION, message); } \
260  } while (false)
261 
270 #define LOG_INFO(message...) \
271  do { \
272  lsst::log::Log log; \
273  if (log.isInfoEnabled()) { \
274  log.log(log4cxx::Level::getInfo(), LOG4CXX_LOCATION, message); } \
275  } while (false)
276 
285 #define LOG_WARN(message...) \
286  do { \
287  lsst::log::Log log; \
288  if (log.isWarnEnabled()) { \
289  log.log(log4cxx::Level::getWarn(), LOG4CXX_LOCATION, message); } \
290  } while (false)
291 
300 #define LOG_ERROR(message...) \
301  do { \
302  lsst::log::Log log; \
303  if (log.isErrorEnabled()) { \
304  log.log(log4cxx::Level::getError(), LOG4CXX_LOCATION, message); } \
305  } while (false)
306 
315 #define LOG_FATAL(message...) \
316  do { \
317  lsst::log::Log log; \
318  if (log.isFatalEnabled()) { \
319  log.log(log4cxx::Level::getFatal(), LOG4CXX_LOCATION, message); } \
320  } while (false)
321 
322 
323 // small internal utility macro, not for regular clients
324 #define LOG_MESSAGE_VIA_STREAM_(logger, level, message) \
325  std::ostringstream stream_; \
326  stream_ << message; \
327  logger.logMsg(level, LOG4CXX_LOCATION, stream_.str())
328 
343 #define LOGS(logger, level, message) \
344  do { \
345  lsst::log::Log log(lsst::log::Log::getLogger(logger)); \
346  if (log.isEnabledFor(level)) { \
347  LOG_MESSAGE_VIA_STREAM_(log, log4cxx::Level::toLevel(level), message); \
348  } \
349  } while (false)
350 
362 #define LOGS_TRACE(message) \
363  do { \
364  lsst::log::Log log; \
365  if (LOG4CXX_UNLIKELY(log.isTraceEnabled())) { \
366  LOG_MESSAGE_VIA_STREAM_(log, log4cxx::Level::getTrace(), message); \
367  } \
368  } while (false)
369 
381 #define LOGS_DEBUG(message) \
382  do { \
383  lsst::log::Log log; \
384  if (LOG4CXX_UNLIKELY(log.isDebugEnabled())) { \
385  LOG_MESSAGE_VIA_STREAM_(log, log4cxx::Level::getDebug(), message); \
386  } \
387  } while (false)
388 
400 #define LOGS_INFO(message) \
401  do { \
402  lsst::log::Log log; \
403  if (log.isInfoEnabled()) { \
404  LOG_MESSAGE_VIA_STREAM_(log, log4cxx::Level::getInfo(), message); \
405  } \
406  } while (false)
407 
419 #define LOGS_WARN(message) \
420  do { \
421  lsst::log::Log log; \
422  if (log.isWarnEnabled()) { \
423  LOG_MESSAGE_VIA_STREAM_(log, log4cxx::Level::getWarn(), message); \
424  } \
425  } while (false)
426 
438 #define LOGS_ERROR(message) \
439  do { \
440  lsst::log::Log log; \
441  if (log.isErrorEnabled()) { \
442  LOG_MESSAGE_VIA_STREAM_(log, log4cxx::Level::getError(), message); \
443  } \
444  } while (false)
445 
457 #define LOGS_FATAL(message) \
458  do { \
459  lsst::log::Log log; \
460  if (log.isFatalEnabled()) { \
461  LOG_MESSAGE_VIA_STREAM_(log, log4cxx::Level::getFatal(), message); \
462  } \
463  } while (false)
464 
473 #define LOGL_TRACE(logger, message...) \
474  do { \
475  lsst::log::Log log(lsst::log::Log::getLogger(logger)); \
476  if (LOG4CXX_UNLIKELY(log.isTraceEnabled())) { \
477  log.log(log4cxx::Level::getTrace(), LOG4CXX_LOCATION, message);\
478  } \
479  } while (false)
480 
489 #define LOGL_DEBUG(logger, message...) \
490  do { \
491  lsst::log::Log log(lsst::log::Log::getLogger(logger)); \
492  if (LOG4CXX_UNLIKELY(log.isDebugEnabled())) { \
493  log.log(log4cxx::Level::getDebug(), LOG4CXX_LOCATION, message); \
494  } \
495  } while (false)
496 
505 #define LOGL_INFO(logger, message...) \
506  do { \
507  lsst::log::Log log(lsst::log::Log::getLogger(logger)); \
508  if (log.isInfoEnabled()) { \
509  log.log(log4cxx::Level::getInfo(), LOG4CXX_LOCATION, message); \
510  } \
511  } while (false)
512 
521 #define LOGL_WARN(logger, message...) \
522  do { \
523  lsst::log::Log log(lsst::log::Log::getLogger(logger)); \
524  if (log.isWarnEnabled()) { \
525  log.log(log4cxx::Level::getWarn(), LOG4CXX_LOCATION, message); \
526  } \
527  } while (false)
528 
537 #define LOGL_ERROR(logger, message...) \
538  do { \
539  lsst::log::Log log(lsst::log::Log::getLogger(logger)); \
540  if (log.isErrorEnabled()) { \
541  log.log(log4cxx::Level::getError(), LOG4CXX_LOCATION, message); \
542  } \
543  } while (false)
544 
553 #define LOGL_FATAL(logger, message...) \
554  do { \
555  lsst::log::Log log(lsst::log::Log::getLogger(logger)); \
556  if (log.isFatalEnabled()) { \
557  log.log(log4cxx::Level::getFatal(), LOG4CXX_LOCATION, message); \
558  } \
559  } while (false)
560 
573 #define LOGLS_TRACE(logger, message) \
574  do { \
575  lsst::log::Log log(lsst::log::Log::getLogger(logger)); \
576  if (LOG4CXX_UNLIKELY(log.isTraceEnabled())) { \
577  LOG_MESSAGE_VIA_STREAM_(log, log4cxx::Level::getTrace(), message); \
578  } \
579  } while (false)
580 
593 #define LOGLS_DEBUG(logger, message) \
594  do { \
595  lsst::log::Log log(lsst::log::Log::getLogger(logger)); \
596  if (LOG4CXX_UNLIKELY(log.isDebugEnabled())) { \
597  LOG_MESSAGE_VIA_STREAM_(log, log4cxx::Level::getDebug(), message); \
598  } \
599  } while (false)
600 
613 #define LOGLS_INFO(logger, message) \
614  do { \
615  lsst::log::Log log(lsst::log::Log::getLogger(logger)); \
616  if (log.isInfoEnabled()) { \
617  LOG_MESSAGE_VIA_STREAM_(log, log4cxx::Level::getInfo(), message); \
618  } \
619  } while (false)
620 
633 #define LOGLS_WARN(logger, message) \
634  do { \
635  lsst::log::Log log(lsst::log::Log::getLogger(logger)); \
636  if (log.isWarnEnabled()) { \
637  LOG_MESSAGE_VIA_STREAM_(log, log4cxx::Level::getWarn(), message); \
638  } \
639  } while (false)
640 
653 #define LOGLS_ERROR(logger, message) \
654  do { \
655  lsst::log::Log log(lsst::log::Log::getLogger(logger)); \
656  if (log.isErrorEnabled()) { \
657  LOG_MESSAGE_VIA_STREAM_(log, log4cxx::Level::getError(), message); \
658  } \
659  } while (false)
660 
673 #define LOGLS_FATAL(logger, message) \
674  do { \
675  lsst::log::Log log(lsst::log::Log::getLogger(logger)); \
676  if (log.isFatalEnabled()) { \
677  LOG_MESSAGE_VIA_STREAM_(log, log4cxx::Level::getFatal(), message); \
678  } \
679  } while (false)
680 
681 #define LOG_LVL_TRACE static_cast<int>(log4cxx::Level::TRACE_INT)
682 #define LOG_LVL_DEBUG static_cast<int>(log4cxx::Level::DEBUG_INT)
683 #define LOG_LVL_INFO static_cast<int>(log4cxx::Level::INFO_INT)
684 #define LOG_LVL_WARN static_cast<int>(log4cxx::Level::WARN_INT)
685 #define LOG_LVL_ERROR static_cast<int>(log4cxx::Level::ERROR_INT)
686 #define LOG_LVL_FATAL static_cast<int>(log4cxx::Level::FATAL_INT)
687 
688 #define LOG_LOGGER lsst::log::Log
689 
690 namespace lsst {
691 namespace log {
692 
698 class Log {
699 public:
700 
701  /***
702  * Default constructor creates an instance of root logger.
703  */
704  Log() : _logger(_defaultLogger()) { }
705 
709  bool isDebugEnabled() const { return _logger->isDebugEnabled(); }
713  bool isErrorEnabled() const { return _logger->isErrorEnabled(); }
717  bool isFatalEnabled() const { return _logger->isFatalEnabled(); }
721  bool isInfoEnabled() const { return _logger->isInfoEnabled(); }
725  bool isTraceEnabled() const { return _logger->isTraceEnabled(); }
729  bool isWarnEnabled() const { return _logger->isWarnEnabled(); }
730 
731  std::string getName() const;
732  void setLevel(int level);
733  int getLevel() const;
734  bool isEnabledFor(int level) const;
735 
736  Log getChild(std::string const& suffix) const;
737 
739  static Log getDefaultLogger() { return Log(); }
740 
741  static void configure();
742  static void configure(std::string const& filename);
743  static void configure_prop(std::string const& properties);
744 
745  static Log getLogger(Log const& logger) { return logger; }
746  static Log getLogger(std::string const& loggername);
747 
748  static void MDC(std::string const& key, std::string const& value);
749  static void MDCRemove(std::string const& key);
750  static int MDCRegisterInit(std::function<void()> function);
751 
752  void log(log4cxx::LevelPtr level,
753  log4cxx::spi::LocationInfo const& location,
754  char const* fmt, ...);
755  void logMsg(log4cxx::LevelPtr level,
756  log4cxx::spi::LocationInfo const& location,
757  std::string const& msg);
758 
759 private:
760 
767  static log4cxx::LoggerPtr const& _defaultLogger();
768 
775  Log(log4cxx::LoggerPtr const& logger) : Log() { _logger = logger; }
776 
777  log4cxx::LoggerPtr _logger;
778 };
779 
787 unsigned lwpID();
788 
789 }} // namespace lsst::log
790 
791 #endif // LSST_LOG_LOG_H
static Log getDefaultLogger()
Return default logger instance, same as default constructor.
Definition: Log.h:739
bool isWarnEnabled() const
Check whether the logger is enabled for the WARN Level.
Definition: Log.h:729
bool isErrorEnabled() const
Check whether the logger is enabled for the ERROR Level.
Definition: Log.h:713
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:385
Log getChild(std::string const &suffix) const
Return a logger which is a descendant to this logger.
Definition: Log.cc:351
This static class includes a variety of methods for interacting with the the logging module...
Definition: Log.h:698
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:371
bool isInfoEnabled() const
Check whether the logger is enabled for the INFO Level.
Definition: Log.h:721
static void 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:273
bool isDebugEnabled() const
Check whether the logger is enabled for the DEBUG Level.
Definition: Log.h:709
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:285
int getLevel() const
Retrieve the logging threshold.
Definition: Log.cc:315
void setLevel(int level)
Set the logging threshold to LEVEL.
Definition: Log.cc:308
bool isTraceEnabled() const
Check whether the logger is enabled for the TRACE Level.
Definition: Log.h:725
bool isEnabledFor(int level) const
Return whether the logging threshold of the logger is less than or equal to LEVEL.
Definition: Log.cc:330
static Log getLogger(Log const &logger)
Definition: Log.h:745
unsigned lwpID()
Function which returns LWP ID on platforms which support it.
Definition: Log.cc:410
Key< U > key
Definition: Schema.cc:281
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:717
static int MDCRegisterInit(std::function< void()> function)
Definition: Log.cc:289