GNU Radio C++ API Reference: dictionary_logger_backend.h Source File

| GNU Radio C++ API Reference 3.10.12.0 The Free & Open Software Radio Ecosystem |

Loading...
Searching...
No Matches
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 <memory>
17#include <mutex>
18#include <regex>
19#include <set>
20#include <utility>
21
22#include <gnuradio/api.h>
23namespace gr {
24/*! \brief In-Memory Logger
25 *
26 * Logs the messages passing by, sorted by the name of the logger logging them.
27 *
28 * Use by adding a `std::shared_ptr` to this to the logging system, i.e.,
29 * make a logger `auto new_backend = std::make_shared<gr::dictionary_logger_backend>();`
30 * and use it through: `gr::logging.singleton()->add_default_backend(new_backend);`
31 *
32 * After running your work load, get the map and fetch the logged messages from that.
33 */
34 class GR_RUNTIME_API dictionary_logger_backend final
35 : public spdlog::sinks::base_sink<spdlog::details::null_mutex>
36{
37public:
38 using log_entry = std::pair<spdlog::log_clock::time_point, std::string>;
39 using log_map = std::unordered_map<std::string, std::set<log_entry>>;
40
41 //! \brief Create unfiltered logger
42 dictionary_logger_backend();
43
44 //! \brief Create logger that filters according to the specified regex.
45 dictionary_logger_backend(std::regex src_regex);
46
47 /* \brief retrieve a copy of the map containing all logs
48 * The individual logs are accessible through the names! of the respective loggers.
49 */
50 log_map get_map() const { return log_entries; };
51
52 ~dictionary_logger_backend() override{};
53
54protected:
55 void sink_it_(const spdlog::details::log_msg& message) override;
56 void flush_() override{};
57
58private:
59 mutable std::mutex map_mutex;
60 bool has_regex = false;
61 std::regex src_regex;
62 log_map log_entries;
63};
64} // namespace gr
65#endif
gr::dictionary_logger_backend::log_map
std::unordered_map< std::string, std::set< log_entry > > log_map
Definition dictionary_logger_backend.h:39
gr::dictionary_logger_backend::flush_
void flush_() override
Definition dictionary_logger_backend.h:56
gr::dictionary_logger_backend::get_map
log_map get_map() const
Definition dictionary_logger_backend.h:50
gr::dictionary_logger_backend::dictionary_logger_backend
dictionary_logger_backend(std::regex src_regex)
Create logger that filters according to the specified regex.
gr::dictionary_logger_backend::sink_it_
void sink_it_(const spdlog::details::log_msg &message) override
gr::dictionary_logger_backend::dictionary_logger_backend
dictionary_logger_backend()
Create unfiltered logger.
gr::dictionary_logger_backend::~dictionary_logger_backend
~dictionary_logger_backend() override
Definition dictionary_logger_backend.h:52
gr::dictionary_logger_backend::log_entry
std::pair< spdlog::log_clock::time_point, std::string > log_entry
Definition dictionary_logger_backend.h:38
gr::message
Message class.
Definition message.h:28
api.h
GR_RUNTIME_API
#define GR_RUNTIME_API
Definition gnuradio-runtime/include/gnuradio/api.h:18
gr
GNU Radio logging wrapper.
Definition basic_block.h:29