GNU Radio C++ API Reference  gcd20ee2
The Free & Open Software Radio Ecosystem
dictionary_logger_backend.h
Go to the documentation of this file.
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2022 Marcus Müller
4  *
5  * This file is part of GNU Radio
6  *
7  * SPDX-License-Identifier: GPL-3.0-or-later
8  *
9  */
10 #ifndef INCLUDED_DICTIONARY_LOGGER_BACKEND_H
11 #define INCLUDED_DICTIONARY_LOGGER_BACKEND_H
12 #include <spdlog/common.h>
13 #include <spdlog/details/null_mutex.h>
14 #include <spdlog/sinks/base_sink.h>
15 #include <unordered_map>
16 #include <mutex>
17 #include <regex>
18 #include <set>
19 #include <utility>
20 
21 #include <gnuradio/api.h>
22 namespace gr {
23 /*! \brief In-Memory Logger
24  *
25  * Logs the messages passing by, sorted by the name of the logger logging them.
26  *
27  * Use by adding a `std::shared_ptr` to this to the logging system, i.e.,
28  * make a logger `auto new_backend = std::make_shared<gr::dictionary_logger_backend>();`
29  * and use it through: `gr::logging.singleton()->add_default_backend(new_backend);`
30  *
31  * After running your work load, get the map and fetch the logged messages from that.
32  */
34  : public spdlog::sinks::base_sink<spdlog::details::null_mutex>
35 {
36 public:
37  using log_entry = std::pair<spdlog::log_clock::time_point, std::string>;
38  using log_map = std::unordered_map<std::string, std::set<log_entry>>;
39 
40  //! \brief Create unfiltered logger
42 
43  //! \brief Create logger that filters according to the specified regex.
44  dictionary_logger_backend(std::regex src_regex);
45 
46  /* \brief retrieve a copy of the map containing all logs
47  * The individual logs are accessible through the names! of the respective loggers.
48  */
49  log_map get_map() const { return log_entries; };
50 
52 
53 protected:
54  void sink_it_(const spdlog::details::log_msg& message) override;
55  void flush_() override{};
56 
57 private:
58  mutable std::mutex map_mutex;
59  bool has_regex = false;
60  std::regex src_regex;
61  log_map log_entries;
62 };
63 } // namespace gr
64 #endif
In-Memory Logger.
Definition: dictionary_logger_backend.h:35
void flush_() override
Definition: dictionary_logger_backend.h:55
log_map get_map() const
Definition: dictionary_logger_backend.h:49
dictionary_logger_backend(std::regex src_regex)
Create logger that filters according to the specified regex.
void sink_it_(const spdlog::details::log_msg &message) override
std::unordered_map< std::string, std::set< log_entry > > log_map
Definition: dictionary_logger_backend.h:38
dictionary_logger_backend()
Create unfiltered logger.
~dictionary_logger_backend() override
Definition: dictionary_logger_backend.h:51
std::pair< spdlog::log_clock::time_point, std::string > log_entry
Definition: dictionary_logger_backend.h:37
Message class.
Definition: message.h:28
#define GR_RUNTIME_API
Definition: gnuradio-runtime/include/gnuradio/api.h:18
boost::mutex mutex
Definition: thread.h:34
GNU Radio logging wrapper.
Definition: basic_block.h:29