From 763a56a4bf194e7cf1cab7446e993c376ce933fa Mon Sep 17 00:00:00 2001 From: Dima Dorezyuk Date: Mon, 14 Oct 2024 11:07:26 +0200 Subject: [PATCH] Add escaped message to simplify parsing of logs (#25) * Add escaped message to simplify parsing of logs Signed-off-by: Dima Dorezyuk --------- Signed-off-by: Dima Dorezyuk Co-authored-by: Dima Dorezyuk --- CMakeLists.txt | 2 +- lib/logging.cpp | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 3946e0e..78a9f19 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,7 +1,7 @@ cmake_minimum_required(VERSION 3.11) project(everest-log - VERSION 0.2.1 + VERSION 0.2.3 DESCRIPTION "EVerest logging library" LANGUAGES CXX C ) diff --git a/lib/logging.cpp b/lib/logging.cpp index ada2e51..b4c4cfe 100644 --- a/lib/logging.cpp +++ b/lib/logging.cpp @@ -8,6 +8,12 @@ #include #include #include +#include +#include +#include +#include +#include +#include #include #include #include @@ -37,6 +43,7 @@ namespace fs = std::filesystem; #endif namespace logging = boost::log::BOOST_LOG_VERSION_NAMESPACE; namespace attrs = logging::attributes; +namespace expr = logging::expressions; namespace Everest { namespace Logging { @@ -100,6 +107,29 @@ std::istream& operator>>(std::istream& strm, severity_level& level) { return strm; } +/// Custom formatter for escaped messages. +/// +/// Not really clear but just a wrapper around the c_decor formatter. +struct escaped_message_formatter { + explicit escaped_message_formatter(logging::attribute_name const& name) : + f_{expr::stream << expr::c_decor[expr::stream << expr::smessage]} { + } + void operator()(logging::record_view const& rec, logging::formatting_ostream& strm) const { + f_(rec, strm); + } + +private: + /// @brief The formatter itself. + boost::log::formatter f_; +}; + +/// The factory for the EscMessage formatter. +struct escaped_message_formatter_factory : public logging::formatter_factory { + formatter_type create_formatter(logging::attribute_name const& attr_name, args_map const& args) { + return formatter_type(escaped_message_formatter(attr_name)); + } +}; + void init(const std::string& logconf) { init(logconf, ""); } @@ -107,6 +137,9 @@ void init(const std::string& logconf) { void init(const std::string& logconf, std::string process_name) { BOOST_LOG_FUNCTION(); + // First thing - register the custom formatter for EscMessage + logging::register_formatter_factory("EscapedMessage", boost::make_shared()); + // add useful attributes logging::add_common_attributes();