GNU Radio Manual and C++ API Reference  3.10.7.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,2022 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 <spdlog/fmt/ostr.h>
39 #include <memory>
40 
41 #include <spdlog/spdlog.h>
42 
43 #include <spdlog/sinks/dist_sink.h>
44 
45 #include <boost/format.hpp>
46 
47 namespace gr {
48 using log_level = spdlog::level::level_enum;
49 
51 {
52 public:
53  /* \brief deleted copy constructor
54  * get your own logging system, or, more likely, use the singleton.
55  */
56  logging(logging const&) = delete;
57 
58  // \brief deleted assignment operator
59  void operator=(logging const&) = delete;
60 
61  // \brief singleton to access the one logging system
62  static logging& singleton();
63 
64  //! \brief get the default logging level
65  inline log_level default_level() const { return _default_backend->level(); }
66 
67  //! \brief get the debug logging level
68  inline log_level debug_level() const { return _debug_backend->level(); }
69 
70  //! \brief set the default logging level
71  void set_default_level(log_level level);
72 
73  //! \brief set the debug logging level
74  void set_debug_level(log_level level);
75 
76  spdlog::sink_ptr default_backend() const;
77  //! \brief adds a logging sink
78  void add_default_sink(const spdlog::sink_ptr& sink);
79  //! \brief adds a debugging sink
80  void add_debug_sink(const spdlog::sink_ptr& sink);
81  //! \brief add a default-constructed console sink to the default logger
82  void add_default_console_sink();
83  //! \brief add a default-constructed console sink to the debugging logger
84  void add_debug_console_sink();
85 
86  static constexpr const char* default_pattern = "%n :%l: %v";
87 
88 private:
89  logging();
90  std::shared_ptr<spdlog::sinks::dist_sink_mt> _default_backend, _debug_backend;
91 };
92 
93 /*!
94  * \brief GR_LOG macros
95  * \ingroup logging
96  *
97  * These macros wrap the standard LOG4CPP_LEVEL macros. The available macros
98  * are:
99  * LOG_DEBUG
100  * LOG_INFO
101  * LOG_WARN
102  * LOG_TRACE
103  * LOG_ERROR
104  * LOG_ALERT
105  * LOG_CRIT
106  * LOG_FATAL
107  * LOG_EMERG
108  */
109 
110 /********************* Start Classes and Methods for Python ******************/
111 /*!
112  * \brief Logger class for referencing loggers in python. Not
113  * needed in C++ (use macros) Wraps and manipulates loggers for
114  * python as python has no macros
115  * \ingroup logging
116  *
117  */
119 {
120 private:
121  /*! \brief pointer to logger associated with this wrapper class */
122  std::string _name;
123  using underlying_logger_ptr = std::shared_ptr<spdlog::logger>;
124 
125 public:
126  /*!
127  * \brief constructor Provide name of logger to associate with this class
128  * \param logger_name Name of logger associated with class
129  *
130  * Creates a new logger. Loggers inherit the logging level (through `gr.prefs` or
131  * through `gr::logging::singleton().set_default_level()`) that is set at the time of
132  * their creation.
133  */
134  logger(const std::string& logger_name);
135 
136  /*! \brief Destructor */
137  // FIXME implement or = default
138  ~logger() = default;
139 
140  underlying_logger_ptr d_logger;
141 
142  // Wrappers for logging macros
143  /*! \brief inline function, wrapper to set the logger level */
144  void set_level(const std::string& level);
145  void set_level(const log_level level);
146 
147  /*! \brief inline function, wrapper to get the logger level */
148  void get_level(std::string& level) const;
149  const std::string get_string_level() const;
150  log_level get_level() const;
151 
152  const std::string& name() const;
153  void set_name(const std::string& name);
154 
155  /*! \brief inline function, wrapper for TRACE message */
156  template <typename... Args>
157  inline void trace(const spdlog::string_view_t& msg, const Args&... args)
158  {
159  d_logger->trace(msg, args...);
160  }
161 
162  /*! \brief inline function, wrapper for DEBUG message */
163  template <typename... Args>
164  inline void debug(const spdlog::string_view_t& msg, const Args&... args)
165  {
166  d_logger->debug(msg, args...);
167  }
168 
169  /*! \brief inline function, wrapper for INFO message */
170  template <typename... Args>
171  inline void info(const spdlog::string_view_t& msg, const Args&... args)
172  {
173  d_logger->info(msg, args...);
174  }
175 
176  /*! \brief inline function, wrapper for INFO message, DEPRECATED */
177  template <typename... Args>
178  inline void notice(const spdlog::string_view_t& msg, const Args&... args)
179  {
180  d_logger->info(msg, args...);
181  }
182 
183  /*! \brief inline function, wrapper for WARN message */
184  template <typename... Args>
185  inline void warn(const spdlog::string_view_t& msg, const Args&... args)
186  {
187  d_logger->warn(msg, args...);
188  }
189 
190  /*! \brief inline function, wrapper for ERROR message */
191  template <typename... Args>
192  inline void error(const spdlog::string_view_t& msg, const Args&... args)
193  {
194  d_logger->error(msg, args...);
195  }
196 
197  /*! \brief inline function, wrapper for CRITICAL message */
198  template <typename... Args>
199  inline void crit(const spdlog::string_view_t& msg, const Args&... args)
200  {
201  d_logger->critical(msg, args...);
202  }
203 
204  /*! \brief inline function, wrapper for CRITICAL message, DEPRECATED */
205  template <typename... Args>
206  inline void alert(const spdlog::string_view_t& msg, const Args&... args)
207  {
208  d_logger->critical(msg, args...);
209  }
210 
211  /*! \brief inline function, wrapper for CRITICAL message, DEPRECATED */
212  template <typename... Args>
213  inline void fatal(const spdlog::string_view_t& msg, const Args&... args)
214  {
215  d_logger->critical(msg, args...);
216  }
217 
218  /*! \brief inline function, wrapper for CRITICAL message, DEPRECATED */
219  template <typename... Args>
220  inline void emerg(const spdlog::string_view_t& msg, const Args&... args)
221  {
222  d_logger->critical(msg, args...);
223  }
224  /*! \brief inline function, wrapper for logging with ad-hoc adjustable level*/
225  template <typename... Args>
226  inline void log(spdlog::level::level_enum level,
227  const spdlog::string_view_t& msg,
228  const Args&... args)
229  {
230  d_logger->log(level, msg, args...);
231  }
232 };
233 using logger_ptr = std::shared_ptr<logger>;
234 
235 /*!
236  * Function to use the GR prefs files to get and setup the two
237  * default loggers defined there. The loggers are unique to the
238  * class in which they are called, and we pass it the \p name to
239  * identify where the log message originates from. For a GNU Radio
240  * block, we use 'alias()' for this value, and this is set up for us
241  * automatically in gr::block.
242  */
243 GR_RUNTIME_API bool
244 configure_default_loggers(gr::logger_ptr& l, gr::logger_ptr& d, const std::string& name);
245 
246 } /* namespace gr */
247 
248 // global logging shorthands
249 
250 #define GR_LOG_TRACE(log, msg) \
251  { \
252  log->d_logger->trace(msg); \
253  }
254 
255 #define GR_LOG_DEBUG(log, msg) \
256  { \
257  log->d_logger->debug(msg); \
258  }
259 
260 #define GR_LOG_INFO(log, msg) \
261  { \
262  log->d_logger->info(msg); \
263  }
264 
265 #define GR_LOG_NOTICE(log, msg) \
266  { \
267  log->d_logger->info(msg); \
268  }
269 
270 
271 #define GR_LOG_WARN(log, msg) \
272  { \
273  log->d_logger->warn(msg); \
274  }
275 
276 #define GR_LOG_ERROR(log, msg) \
277  { \
278  log->d_logger->error(msg); \
279  }
280 
281 #define GR_LOG_CRIT(log, msg) \
282  { \
283  log->d_logger->critical(msg); \
284  }
285 
286 #define GR_LOG_ALERT(log, msg) \
287  { \
288  log->d_logger->critical(msg); \
289  }
290 
291 #define GR_LOG_FATAL(log, msg) \
292  { \
293  log->d_logger->critical(msg); \
294  }
295 
296 #define GR_LOG_EMERG(log, msg) \
297  { \
298  log->d_logger->critical(msg); \
299  }
300 
301 // Helper class to allow passing of boost::format to fmt
302 template <>
303 struct fmt::formatter<boost::format> : formatter<string_view> {
304  // parse is inherited from formatter<string_view>.
305  template <typename FormatContext>
306  auto format(const boost::format& bfmt, FormatContext& ctx)
307  -> decltype(formatter<string_view>::format(bfmt.str(), ctx))
308  {
309  return formatter<string_view>::format(bfmt.str(), ctx);
310  }
311 };
312 
313 #endif
314 
315 #endif /* INCLUDED_GR_LOGGER_H */
Definition: logger.h:50
void emerg(const spdlog::string_view_t &msg, const Args &... args)
inline function, wrapper for CRITICAL message, DEPRECATED
Definition: logger.h:220
GR_RUNTIME_API const pmt::pmt_t msg()
void fatal(const spdlog::string_view_t &msg, const Args &... args)
inline function, wrapper for CRITICAL message, DEPRECATED
Definition: logger.h:213
log_level default_level() const
get the default logging level
Definition: logger.h:65
std::shared_ptr< logger > logger_ptr
Definition: logger.h:233
void trace(const spdlog::string_view_t &msg, const Args &... args)
inline function, wrapper for TRACE message
Definition: logger.h:157
auto format(const boost::format &bfmt, FormatContext &ctx) -> decltype(formatter< string_view >::format(bfmt.str(), ctx))
Definition: logger.h:306
void error(const spdlog::string_view_t &msg, const Args &... args)
inline function, wrapper for ERROR message
Definition: logger.h:192
#define GR_RUNTIME_API
Definition: gnuradio-runtime/include/gnuradio/api.h:18
GR_RUNTIME_API bool configure_default_loggers(gr::logger_ptr &l, gr::logger_ptr &d, const std::string &name)
void warn(const spdlog::string_view_t &msg, const Args &... args)
inline function, wrapper for WARN message
Definition: logger.h:185
GNU Radio logging wrapper.
Definition: basic_block.h:29
log_level debug_level() const
get the debug logging level
Definition: logger.h:68
void info(const spdlog::string_view_t &msg, const Args &... args)
inline function, wrapper for INFO message
Definition: logger.h:171
void log(spdlog::level::level_enum level, const spdlog::string_view_t &msg, const Args &... args)
inline function, wrapper for logging with ad-hoc adjustable level
Definition: logger.h:226
void crit(const spdlog::string_view_t &msg, const Args &... args)
inline function, wrapper for CRITICAL message
Definition: logger.h:199
underlying_logger_ptr d_logger
Definition: logger.h:140
GR_LOG macrosThese macros wrap the standard LOG4CPP_LEVEL macros. The available macros are: LOG_DEBUG...
Definition: logger.h:118
void alert(const spdlog::string_view_t &msg, const Args &... args)
inline function, wrapper for CRITICAL message, DEPRECATED
Definition: logger.h:206
void debug(const spdlog::string_view_t &msg, const Args &... args)
inline function, wrapper for DEBUG message
Definition: logger.h:164
void notice(const spdlog::string_view_t &msg, const Args &... args)
inline function, wrapper for INFO message, DEPRECATED
Definition: logger.h:178
spdlog::level::level_enum log_level
Definition: logger.h:48