GNU Radio Manual and C++ API Reference 3.10.5.1
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>
27namespace gr {
28using 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
47namespace gr {
48using log_level = spdlog::level::level_enum;
49
51{
52public:
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
72
73 //! \brief set the debug logging 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
83 //! \brief add a default-constructed console sink to the debugging logger
85
86 static constexpr const char* default_pattern = "%n :%l: %v";
87
88private:
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{
120private:
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
125public:
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;
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};
225using logger_ptr = std::shared_ptr<logger>;
226
227/*!
228 * Function to use the GR prefs files to get and setup the two
229 * default loggers defined there. The loggers are unique to the
230 * class in which they are called, and we pass it the \p name to
231 * identify where the log message originates from. For a GNU Radio
232 * block, we use 'alias()' for this value, and this is set up for us
233 * automatically in gr::block.
234 */
237
238} /* namespace gr */
239
240// global logging shorthands
241
242#define GR_LOG_TRACE(log, msg) \
243 { \
244 log->d_logger->trace(msg); \
245 }
246
247#define GR_LOG_DEBUG(log, msg) \
248 { \
249 log->d_logger->debug(msg); \
250 }
251
252#define GR_LOG_INFO(log, msg) \
253 { \
254 log->d_logger->info(msg); \
255 }
256
257#define GR_LOG_NOTICE(log, msg) \
258 { \
259 log->d_logger->info(msg); \
260 }
261
262
263#define GR_LOG_WARN(log, msg) \
264 { \
265 log->d_logger->warn(msg); \
266 }
267
268#define GR_LOG_ERROR(log, msg) \
269 { \
270 log->d_logger->error(msg); \
271 }
272
273#define GR_LOG_CRIT(log, msg) \
274 { \
275 log->d_logger->critical(msg); \
276 }
277
278#define GR_LOG_ALERT(log, msg) \
279 { \
280 log->d_logger->critical(msg); \
281 }
282
283#define GR_LOG_FATAL(log, msg) \
284 { \
285 log->d_logger->critical(msg); \
286 }
287
288#define GR_LOG_EMERG(log, msg) \
289 { \
290 log->d_logger->critical(msg); \
291 }
292
293// Helper class to allow passing of boost::format to fmt
294template <>
295struct fmt::formatter<boost::format> : formatter<string_view> {
296 // parse is inherited from formatter<string_view>.
297 template <typename FormatContext>
298 auto format(const boost::format& bfmt, FormatContext& ctx)
299 -> decltype(formatter<string_view>::format(bfmt.str(), ctx))
300 {
301 return formatter<string_view>::format(bfmt.str(), ctx);
302 }
303};
304
305#endif
306
307#endif /* INCLUDED_GR_LOGGER_H */
GR_LOG macros.
Definition: logger.h:119
void crit(const spdlog::string_view_t &msg, const Args &... args)
inline function, wrapper for CRITICAL message
Definition: logger.h:199
void set_name(const std::string &name)
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:220
void alert(const spdlog::string_view_t &msg, const Args &... args)
inline function, wrapper for CRITICAL message, DEPRECATED
Definition: logger.h:206
~logger()=default
Destructor.
underlying_logger_ptr d_logger
Definition: logger.h:140
void debug(const spdlog::string_view_t &msg, const Args &... args)
inline function, wrapper for DEBUG message
Definition: logger.h:164
void fatal(const spdlog::string_view_t &msg, const Args &... args)
inline function, wrapper for CRITICAL message, DEPRECATED
Definition: logger.h:213
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:157
const std::string & name() const
void error(const spdlog::string_view_t &msg, const Args &... args)
inline function, wrapper for ERROR message
Definition: logger.h:192
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:178
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:185
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:171
Definition: logger.h:51
void set_default_level(log_level level)
set the default logging level
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
static logging & singleton()
void set_debug_level(log_level level)
set the debug logging level
log_level debug_level() const
get the debug logging level
Definition: logger.h:68
void add_debug_sink(const spdlog::sink_ptr &sink)
adds a debugging sink
log_level default_level() const
get the default logging level
Definition: logger.h:65
#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:225
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:48
auto format(const boost::format &bfmt, FormatContext &ctx) -> decltype(formatter< string_view >::format(bfmt.str(), ctx))
Definition: logger.h:298