GNU Radio Manual and C++ API Reference  3.10.0.0
The Free & Open Software Radio Ecosystem
logger.h
Go to the documentation of this file.
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2012-2013 Free Software Foundation, Inc.
4  * Copyright 2021 Marcus Müller
5  *
6  * This file is part of GNU Radio
7  *
8  * SPDX-License-Identifier: GPL-3.0-or-later
9  *
10  */
11 
12 #ifndef INCLUDED_GR_LOGGER_H
13 #define INCLUDED_GR_LOGGER_H
14 
15 /*!
16  * \ingroup logging
17  * \brief GNU Radio logging wrapper
18  *
19  */
20 #ifdef DISABLE_LOGGER_H
21 // pygccxml as of v2.2.1 has a difficult time parsing headers that
22 // include spdlog or format
23 // Since it only needs the top level header info, this is a hack to not
24 // transitively include anything logger related when parsing the
25 // headers
26 #include <memory>
27 namespace gr {
28 using logger_ptr = std::shared_ptr<void>;
29 }
30 #else
31 
32 // Since this file is included in *all* gr::blocks, please make sure this list of includes
33 // keeps as short as possible; if anything is needed only by the implementation in
34 // buffer.cc, then only include it there
35 #include <gnuradio/api.h>
36 #include <spdlog/common.h>
37 #include <spdlog/fmt/fmt.h>
38 #include <memory>
39 
40 #include <spdlog/spdlog.h>
41 
42 #include <spdlog/sinks/dist_sink.h>
43 
44 #include <boost/format.hpp>
45 
46 namespace gr {
47 using log_level = spdlog::level::level_enum;
48 
50 {
51 public:
52  logging(logging const&) = delete; // delete copy ctor, this is a singleton class
53  void operator=(logging const&) = delete; // can't assign to singleton class
54  static logging& singleton(); //! \brief get the singleton
55 
56  //! \brief get the default logging level
57  inline log_level default_level() const { return _default_level; }
58 
59  //! \brief get the debug logging level
60  inline log_level debug_level() const { return _debug_level; }
61  spdlog::sink_ptr default_backend() const;
62  //! \brief adds a logging sink
63  void add_default_sink(const spdlog::sink_ptr& sink);
64  //! \brief adds a debugging sink
65  void add_debug_sink(const spdlog::sink_ptr& sink);
66  //! \brief add a default-constructed console sink to the default logger
68  //! \brief add a default-constructed console sink to the debugging logger
70 
71  static constexpr const char* default_pattern = "%n :%l: %v";
72 
73 private:
74  logging();
75  const log_level _default_level, _debug_level;
76  std::shared_ptr<spdlog::sinks::dist_sink_mt> _default_backend, _debug_backend;
77 };
78 
79 /*!
80  * \brief GR_LOG macros
81  * \ingroup logging
82  *
83  * These macros wrap the standard LOG4CPP_LEVEL macros. The available macros
84  * are:
85  * LOG_DEBUG
86  * LOG_INFO
87  * LOG_WARN
88  * LOG_TRACE
89  * LOG_ERROR
90  * LOG_ALERT
91  * LOG_CRIT
92  * LOG_FATAL
93  * LOG_EMERG
94  */
95 
96 /********************* Start Classes and Methods for Python ******************/
97 /*!
98  * \brief Logger class for referencing loggers in python. Not
99  * needed in C++ (use macros) Wraps and manipulates loggers for
100  * python as python has no macros
101  * \ingroup logging
102  *
103  */
105 {
106 private:
107  /*! \brief pointer to logger associated with this wrapper class */
108  std::string _name;
109  using underlying_logger_ptr = std::shared_ptr<spdlog::logger>;
110 
111 public:
112  /*!
113  * \brief constructor Provide name of logger to associate with this class
114  * \param logger_name Name of logger associated with class
115  */
116  logger(const std::string& logger_name);
117 
118  /*! \brief Destructor */
119  // FIXME implement or = default
120  ~logger() = default;
121 
122  underlying_logger_ptr d_logger;
123 
124  // Wrappers for logging macros
125  /*! \brief inline function, wrapper to set the logger level */
126  void set_level(const std::string& level);
127  void set_level(const log_level level);
128 
129  /*! \brief inline function, wrapper to get the logger level */
130  void get_level(std::string& level) const;
131  const std::string get_string_level() const;
133 
134  const std::string& name() const;
135  void set_name(const std::string& name);
136 
137  /*! \brief inline function, wrapper for TRACE message */
138  template <typename... Args>
139  inline void trace(const spdlog::string_view_t& msg, const Args&... args)
140  {
141  d_logger->trace(msg, args...);
142  }
143 
144  /*! \brief inline function, wrapper for DEBUG message */
145  template <typename... Args>
146  inline void debug(const spdlog::string_view_t& msg, const Args&... args)
147  {
148  d_logger->debug(msg, args...);
149  }
150 
151  /*! \brief inline function, wrapper for INFO message */
152  template <typename... Args>
153  inline void info(const spdlog::string_view_t& msg, const Args&... args)
154  {
155  d_logger->info(msg, args...);
156  }
157 
158  /*! \brief inline function, wrapper for INFO message, DEPRECATED */
159  template <typename... Args>
160  inline void notice(const spdlog::string_view_t& msg, const Args&... args)
161  {
162  d_logger->info(msg, args...);
163  }
164 
165  /*! \brief inline function, wrapper for WARN message */
166  template <typename... Args>
167  inline void warn(const spdlog::string_view_t& msg, const Args&... args)
168  {
169  d_logger->warn(msg, args...);
170  }
171 
172  /*! \brief inline function, wrapper for ERROR message */
173  template <typename... Args>
174  inline void error(const spdlog::string_view_t& msg, const Args&... args)
175  {
176  d_logger->error(msg, args...);
177  }
178 
179  /*! \brief inline function, wrapper for CRITICAL message */
180  template <typename... Args>
181  inline void crit(const spdlog::string_view_t& msg, const Args&... args)
182  {
183  d_logger->critical(msg, args...);
184  }
185 
186  /*! \brief inline function, wrapper for CRITICAL message, DEPRECATED */
187  template <typename... Args>
188  inline void alert(const spdlog::string_view_t& msg, const Args&... args)
189  {
190  d_logger->critical(msg, args...);
191  }
192 
193  /*! \brief inline function, wrapper for CRITICAL message, DEPRECATED */
194  template <typename... Args>
195  inline void fatal(const spdlog::string_view_t& msg, const Args&... args)
196  {
197  d_logger->critical(msg, args...);
198  }
199 
200  /*! \brief inline function, wrapper for CRITICAL message, DEPRECATED */
201  template <typename... Args>
202  inline void emerg(const spdlog::string_view_t& msg, const Args&... args)
203  {
204  d_logger->critical(msg, args...);
205  }
206 };
207 using logger_ptr = std::shared_ptr<logger>;
208 
209 /*!
210  * Function to use the GR prefs files to get and setup the two
211  * default loggers defined there. The loggers are unique to the
212  * class in which they are called, and we pass it the \p name to
213  * identify where the log message originates from. For a GNU Radio
214  * block, we use 'alias()' for this value, and this is set up for us
215  * automatically in gr::block.
216  */
217 GR_RUNTIME_API bool
218 configure_default_loggers(gr::logger_ptr& l, gr::logger_ptr& d, const std::string& name);
219 
220 } /* namespace gr */
221 
222 // global logging shorthands
223 
224 #define GR_LOG_TRACE(log, msg) \
225  { \
226  log->d_logger->trace(msg); \
227  }
228 
229 #define GR_LOG_DEBUG(log, msg) \
230  { \
231  log->d_logger->debug(msg); \
232  }
233 
234 #define GR_LOG_INFO(log, msg) \
235  { \
236  log->d_logger->info(msg); \
237  }
238 
239 #define GR_LOG_NOTICE(log, msg) \
240  { \
241  log->d_logger->info(msg); \
242  }
243 
244 
245 #define GR_LOG_WARN(log, msg) \
246  { \
247  log->d_logger->warn(msg); \
248  }
249 
250 #define GR_LOG_ERROR(log, msg) \
251  { \
252  log->d_logger->error(msg); \
253  }
254 
255 #define GR_LOG_CRIT(log, msg) \
256  { \
257  log->d_logger->critical(msg); \
258  }
259 
260 #define GR_LOG_ALERT(log, msg) \
261  { \
262  log->d_logger->critical(msg); \
263  }
264 
265 #define GR_LOG_FATAL(log, msg) \
266  { \
267  log->d_logger->critical(msg); \
268  }
269 
270 #define GR_LOG_EMERG(log, msg) \
271  { \
272  log->d_logger->critical(msg); \
273  }
274 
275 // Helper class to allow passing of boost::format to fmt
276 template <>
277 struct fmt::formatter<boost::format> : formatter<string_view> {
278  // parse is inherited from formatter<string_view>.
279  template <typename FormatContext>
280  auto format(const boost::format& bfmt, FormatContext& ctx)
281  -> decltype(formatter<string_view>::format(bfmt.str(), ctx))
282  {
283  return formatter<string_view>::format(bfmt.str(), ctx);
284  }
285 };
286 
287 #endif
288 
289 #endif /* INCLUDED_GR_LOGGER_H */
GR_LOG macros.
Definition: logger.h:105
void crit(const spdlog::string_view_t &msg, const Args &... args)
inline function, wrapper for CRITICAL message
Definition: logger.h:181
void set_name(const std::string &name)
const std::string & name() const
void get_level(std::string &level) const
inline function, wrapper to get the logger level
void emerg(const spdlog::string_view_t &msg, const Args &... args)
inline function, wrapper for CRITICAL message, DEPRECATED
Definition: logger.h:202
void alert(const spdlog::string_view_t &msg, const Args &... args)
inline function, wrapper for CRITICAL message, DEPRECATED
Definition: logger.h:188
~logger()=default
Destructor.
underlying_logger_ptr d_logger
Definition: logger.h:122
void debug(const spdlog::string_view_t &msg, const Args &... args)
inline function, wrapper for DEBUG message
Definition: logger.h:146
void fatal(const spdlog::string_view_t &msg, const Args &... args)
inline function, wrapper for CRITICAL message, DEPRECATED
Definition: logger.h:195
logger(const std::string &logger_name)
constructor Provide name of logger to associate with this class
void trace(const spdlog::string_view_t &msg, const Args &... args)
inline function, wrapper for TRACE message
Definition: logger.h:139
void error(const spdlog::string_view_t &msg, const Args &... args)
inline function, wrapper for ERROR message
Definition: logger.h:174
void set_level(const log_level level)
void set_level(const std::string &level)
inline function, wrapper to set the logger level
void notice(const spdlog::string_view_t &msg, const Args &... args)
inline function, wrapper for INFO message, DEPRECATED
Definition: logger.h:160
log_level get_level() const
void warn(const spdlog::string_view_t &msg, const Args &... args)
inline function, wrapper for WARN message
Definition: logger.h:167
const std::string get_string_level() const
void info(const spdlog::string_view_t &msg, const Args &... args)
inline function, wrapper for INFO message
Definition: logger.h:153
Definition: logger.h:50
void add_default_sink(const spdlog::sink_ptr &sink)
adds a logging sink
spdlog::sink_ptr default_backend() const
void operator=(logging const &)=delete
void add_default_console_sink()
add a default-constructed console sink to the default logger
logging(logging const &)=delete
void add_debug_console_sink()
add a default-constructed console sink to the debugging logger
log_level debug_level() const
get the debug logging level
Definition: logger.h:60
static logging & singleton()
void add_debug_sink(const spdlog::sink_ptr &sink)
adds a debugging sink
log_level default_level() const
get the singleton
Definition: logger.h:57
#define GR_RUNTIME_API
Definition: gnuradio-runtime/include/gnuradio/api.h:18
GR_RUNTIME_API const pmt::pmt_t msg()
GNU Radio logging wrapper.
Definition: basic_block.h:29
std::shared_ptr< logger > logger_ptr
Definition: logger.h:207
GR_RUNTIME_API bool configure_default_loggers(gr::logger_ptr &l, gr::logger_ptr &d, const std::string &name)
spdlog::level::level_enum log_level
Definition: logger.h:47
auto format(const boost::format &bfmt, FormatContext &ctx) -> decltype(formatter< string_view >::format(bfmt.str(), ctx))
Definition: logger.h:280