Skip to content

Commit

Permalink
Add escaped message to simplify parsing of logs (#25)
Browse files Browse the repository at this point in the history
* Add escaped message to simplify parsing of logs

Signed-off-by: Dima Dorezyuk <[email protected]>

---------

Signed-off-by: Dima Dorezyuk <[email protected]>
Co-authored-by: Dima Dorezyuk <[email protected]>
  • Loading branch information
dorezyuk and Dima Dorezyuk authored Oct 14, 2024
1 parent 3eab950 commit 763a56a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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
)
Expand Down
33 changes: 33 additions & 0 deletions lib/logging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
#include <boost/log/attributes/current_process_id.hpp>
#include <boost/log/attributes/current_process_name.hpp>
#include <boost/log/attributes/current_thread_id.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/expressions/attr.hpp>
#include <boost/log/expressions/formatter.hpp>
#include <boost/log/expressions/formatters/c_decorator.hpp>
#include <boost/log/expressions/formatters/format.hpp>
#include <boost/log/expressions/formatters/stream.hpp>
#include <boost/log/sources/record_ostream.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -100,13 +107,39 @@ 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<char> {
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, "");
}

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<escaped_message_formatter_factory>());

// add useful attributes
logging::add_common_attributes();

Expand Down

0 comments on commit 763a56a

Please sign in to comment.