INSTINCT Code Coverage Report


Directory: src/
File: util/Logger/dist_filter_sink.hpp
Date: 2025-02-07 16:54:41
Exec Total Coverage
Lines: 0 10 0.0%
Functions: 0 3 0.0%
Branches: 0 14 0.0%

Line Branch Exec Source
1 // This file is part of INSTINCT, the INS Toolkit for Integrated
2 // Navigation Concepts and Training by the Institute of Navigation of
3 // the University of Stuttgart, Germany.
4 //
5 // This Source Code Form is subject to the terms of the Mozilla Public
6 // License, v. 2.0. If a copy of the MPL was not distributed with this
7 // file, You can obtain one at https://mozilla.org/MPL/2.0/.
8
9 /// @file dist_filter_sink.hpp
10 /// @brief Distribution sink with filter option. Stores a vector of sinks which get called when log is called
11 /// @author T. Topp (topp@ins.uni-stuttgart.de)
12 /// @date 2023-03-27
13
14 #pragma once
15
16 #include "spdlog/sinks/dist_sink.h"
17 #include <regex>
18
19 namespace spdlog::sinks
20 {
21
22 /// Distribution sink (mux) with filter option
23 template<typename Mutex>
24 class dist_filter_sink : public spdlog::sinks::dist_sink<Mutex> // NOLINT(cppcoreguidelines-virtual-class-destructor)
25 {
26 public:
27 /// @brief Default constructor
28 /// @param[in] filter Filter string
29 explicit dist_filter_sink(const std::string& filter)
30 : filter_(filter, std::regex_constants::ECMAScript){};
31 /// @brief Destructor
32 ~dist_filter_sink() override = default;
33 /// @brief Copy constructor
34 dist_filter_sink(const dist_filter_sink&) = delete;
35 /// @brief Move constructor
36 dist_filter_sink(dist_filter_sink&&) noexcept = default;
37 /// @brief Copy assignment operator
38 dist_filter_sink& operator=(const dist_filter_sink&) = delete;
39 /// @brief Move assignment operator
40 dist_filter_sink& operator=(dist_filter_sink&&) noexcept = default;
41
42 protected:
43 /// String to filter messages for
44 std::regex filter_;
45
46 /// @brief Function called to process the log message
47 /// @param msg Log message struct
48 void sink_it_(const spdlog::details::log_msg& msg) override
49 {
50 spdlog::memory_buf_t formatted_buf;
51 spdlog::sinks::base_sink<Mutex>::formatter_->format(msg, formatted_buf);
52 std::string formatted = fmt::to_string(formatted_buf);
53
54 if (std::regex_search(formatted, filter_))
55 {
56 spdlog::sinks::dist_sink<Mutex>::sink_it_(msg);
57 }
58 }
59 };
60
61 #ifndef DOXYGEN_IGNORE
62
63 #include "spdlog/details/null_mutex.h"
64 #include <mutex>
65 using dist_filter_sink_mt = dist_filter_sink<std::mutex>;
66 using dist_filter_sink_st = dist_filter_sink<spdlog::details::null_mutex>;
67
68 #endif
69
70 } // namespace spdlog::sinks
71