From 43d9a9f35a6b9b8ed9a6074479f916a92c6156e4 Mon Sep 17 00:00:00 2001 From: Andy Pack Date: Sat, 22 Apr 2023 20:59:39 +0100 Subject: [PATCH] add logging --- src/CMakeLists.txt | 2 +- src/main.cpp | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 14af7a7..d779ff1 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -15,4 +15,4 @@ FetchContent_Declare( ) FetchContent_MakeAvailable(Boost) -target_link_libraries(cppScratch PRIVATE nlohmann_json::nlohmann_json Boost::program_options dal library) \ No newline at end of file +target_link_libraries(cppScratch PRIVATE nlohmann_json::nlohmann_json Boost::program_options Boost::log Boost::date_time Boost::filesystem Boost::system Boost::thread Boost::log_setup Boost::chrono Boost::atomic dal library) \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 7e41748..3f930b4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -7,15 +7,49 @@ #include #include +#include +#include +#include +#include +#include + using namespace std; namespace po = boost::program_options; +namespace logging = boost::log; +namespace src = boost::log::sources; +namespace sinks = boost::log::sinks; +namespace keywords = boost::log::keywords; + +void init_logging() +{ + logging::register_simple_formatter_factory("Severity"); + + logging::add_file_log + ( + keywords::file_name = "sample_%N.log", + keywords::time_based_rotation = sinks::file::rotation_at_time_point(0, 0, 0), + keywords::format = "[%TimeStamp%] [%ThreadID%] [%Severity%] %Message%" + ); + + logging::core::get()->set_filter + ( + logging::trivial::severity >= logging::trivial::info + ); + + logging::add_common_attributes(); +} + void on_age(int age) { std::cout << "On age: " << age << '\n'; } int main(int argc, const char *argv[]) { + init_logging(); + + BOOST_LOG_TRIVIAL(info) << "log test"; + //////////// // DECLARE ////////////