Derecho  0.9
Distributed systems toolkit for RDMA
logger.hpp
Go to the documentation of this file.
1 #ifndef LOGGER_HPP
2 #define LOGGER_HPP
3 
4 #include <string>
5 #include <atomic>
6 #include "spdlog/spdlog.h"
7 
8 #ifndef NDEBUG
9 #undef NOLOG
10 #endif
11 
13 private:
14  static std::atomic<uint32_t> _initialize_state;
15  static std::shared_ptr<spdlog::details::thread_pool> _thread_pool_holder;
16  static std::shared_ptr<spdlog::logger> _default_logger;
17  static void _initialize();
18  static std::shared_ptr<spdlog::logger> _create_logger(
19  const std::string &logger_name,
20  spdlog::level::level_enum log_level);
21 public:
22  // create the logger
23  // @PARAM logger_name
24  // Name of the logger. The log file would be created as
25  // "<logger_name>.log"
26  // @PARAM log_level
27  // The level of the logger.
28  // @RETURN
29  // The created logger.
30  static std::shared_ptr<spdlog::logger> createLogger(
31  const std::string &logger_name,
32  spdlog::level::level_enum log_level = spdlog::level::info);
33  // get the default logger
34  static std::shared_ptr<spdlog::logger>& getDefaultLogger();
35 };
36 
37 #ifndef NOLOG
38  // Heavy logging version
39  #define dbg_trace(logger, ...) logger->trace(__VA_ARGS__)
40  #define dbg_default_trace(...) dbg_trace(LoggerFactory::getDefaultLogger(), __VA_ARGS__)
41  #define dbg_debug(logger, ...) logger->debug(__VA_ARGS__)
42  #define dbg_default_debug(...) dbg_debug(LoggerFactory::getDefaultLogger(), __VA_ARGS__)
43  #define dbg_info(logger, ...) logger->info(__VA_ARGS__)
44  #define dbg_default_info(...) dbg_info(LoggerFactory::getDefaultLogger(), __VA_ARGS__)
45  #define dbg_warn(logger, ...) logger->warn(__VA_ARGS__)
46  #define dbg_default_warn(...) dbg_warn(LoggerFactory::getDefaultLogger(), __VA_ARGS__)
47  #define dbg_error(logger, ...) logger->error(__VA_ARGS__)
48  #define dbg_default_error(...) dbg_error(LoggerFactory::getDefaultLogger(), __VA_ARGS__)
49  #define dbg_crit(logger, ...) logger->critical(__VA_ARGS__)
50  #define dbg_default_crit(...) dbg_crit(LoggerFactory::getDefaultLogger(), __VA_ARGS__)
51  #define dbg_flush(logger) logger->flush()
52  #define dbg_default_flush() LoggerFactory::getDefaultLogger()->flush()
53 #else
54  // Log-disabled version
55  #define dbg_trace(logger, ...)
56  #define dbg_default_trace(...)
57  #define dbg_debug(logger, ...)
58  #define dbg_default_debug(...)
59  #define dbg_info(logger, ...)
60  #define dbg_default_info(...)
61  #define dbg_warn(logger, ...)
62  #define dbg_default_warn(...)
63  #define dbg_error(logger, ...)
64  #define dbg_default_error(...)
65  #define dbg_crit(logger, ...)
66  #define dbg_default_crit(...)
67  #define dbg_flush(logger)
68  #define dbg_default_flush()
69 #endif
70 
71 // Log-in-release macros. These will not be compiled out in benchmark mode, so use carefully.
72 #define rls_info(logger, ...) logger->info(__VA_ARGS__)
73 #define rls_default_info(...) rls_info(LoggerFactory::getDefaultLogger(), __VA_ARGS__)
74 #define rls_warn(logger, ...) logger->warn(__VA_ARGS__)
75 #define rls_default_warn( ... ) rls_warn(LoggerFactory::getDefaultLogger(), __VA_ARGS__)
76 #define rls_error(logger, ...) logger->error(__VA_ARGS__)
77 #define rls_default_error( ... ) rls_error(LoggerFactory::getDefaultLogger(), __VA_ARGS__)
78 #define rls_crit(logger, ...) logger->critical(__VA_ARGS__)
79 #define rls_default_crit( ... ) rls_crit(LoggerFactory::getDefaultLogger(), __VA_ARGS__)
80 #define rls_flush(logger) logger->flush()
81 #define rls_default_flush() LoggerFactory::getDefaultLogger()->flush()
82 
83 #endif//LOGGER_HPP
static std::shared_ptr< spdlog::logger > _default_logger
Definition: logger.hpp:16
static std::shared_ptr< spdlog::logger > createLogger(const std::string &logger_name, spdlog::level::level_enum log_level=spdlog::level::info)
Definition: logger.cpp:61
static void _initialize()
Definition: logger.cpp:37
static std::shared_ptr< spdlog::details::thread_pool > _thread_pool_holder
Definition: logger.hpp:15
static std::shared_ptr< spdlog::logger > & getDefaultLogger()
Definition: logger.cpp:68
static std::atomic< uint32_t > _initialize_state
Definition: logger.hpp:14
static std::shared_ptr< spdlog::logger > _create_logger(const std::string &logger_name, spdlog::level::level_enum log_level)
Definition: logger.cpp:17