add logging

This commit is contained in:
Andy Pack 2023-04-22 20:59:39 +01:00
parent e4dc6593a5
commit 43d9a9f35a
Signed by: sarsoo
GPG Key ID: A55BA3536A5E0ED7
2 changed files with 35 additions and 1 deletions

View File

@ -15,4 +15,4 @@ FetchContent_Declare(
) )
FetchContent_MakeAvailable(Boost) FetchContent_MakeAvailable(Boost)
target_link_libraries(cppScratch PRIVATE nlohmann_json::nlohmann_json Boost::program_options dal library) 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)

View File

@ -7,15 +7,49 @@
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
#include <boost/program_options.hpp> #include <boost/program_options.hpp>
#include <boost/log/core.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/utility/setup/file.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
using namespace std; using namespace std;
namespace po = boost::program_options; 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<logging::trivial::severity_level, char>("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) void on_age(int age)
{ {
std::cout << "On age: " << age << '\n'; std::cout << "On age: " << age << '\n';
} }
int main(int argc, const char *argv[]) { int main(int argc, const char *argv[]) {
init_logging();
BOOST_LOG_TRIVIAL(info) << "log test";
//////////// ////////////
// DECLARE // DECLARE
//////////// ////////////