From 37c810f6fac39c60e34b695f3dbb62bcf53d9be1 Mon Sep 17 00:00:00 2001 From: Brandon Kong Date: Fri, 17 Jun 2022 07:43:15 -0700 Subject: [PATCH] Added log rotation feature and console/file logging config options (#181) Fixes an issue that occurred when rebasing the previous log rotation PR. Updated config to allow log rotation size, log rotation interval, and log directory max size specification Updated file size base unit to Mb, added documentation for logging The file size base unit is now in Mb, with detailed description of logging configurations in readme.md Updated CMake install script to correctly set path in production mode Co-authored-by: Brandon Kong --- src/main/main.cpp | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/src/main/main.cpp b/src/main/main.cpp index 182033713..540c9ac00 100644 --- a/src/main/main.cpp +++ b/src/main/main.cpp @@ -110,7 +110,7 @@ initLogging(boost::json::object const& config) namespace trivial = boost::log::trivial; boost::log::add_common_attributes(); std::string format = "[%TimeStamp%] [%ThreadID%] [%Severity%] %Message%"; - if (config.contains("log_to_console") && + if (!config.contains("log_to_console") || config.at("log_to_console").as_bool()) { boost::log::add_console_log(std::cout, keywords::format = format); @@ -124,16 +124,25 @@ initLogging(boost::json::object const& config) config.at("log_directory").as_string().c_str()}; if (!boost::filesystem::exists(dirPath)) boost::filesystem::create_directories(dirPath); - const uint64_t rotationSize = config.contains("log_rotation_size") - ? config.at("log_rotation_size").as_uint64() * 1024 * 1024 + const int64_t rotationSize = config.contains("log_rotation_size") + ? config.at("log_rotation_size").as_int64() * 1024 * 1024u : 2 * 1024 * 1024 * 1024u; - const uint64_t rotationPeriod = + if (rotationSize <= 0) + throw std::runtime_error( + "log rotation size must be greater than 0"); + const int64_t rotationPeriod = config.contains("log_rotation_hour_interval") - ? config.at("log_rotation_hour_interval").as_uint64() + ? config.at("log_rotation_hour_interval").as_int64() : 12u; - const uint64_t dirSize = config.contains("log_directory_max_size") - ? config.at("log_directory_max_size").as_uint64() * 1024 * 1024 + if (rotationPeriod <= 0) + throw std::runtime_error( + "log rotation time interval must be greater than 0"); + const int64_t dirSize = config.contains("log_directory_max_size") + ? config.at("log_directory_max_size").as_int64() * 1024 * 1024u : 50 * 1024 * 1024 * 1024u; + if (dirSize <= 0) + throw std::runtime_error( + "log rotation directory max size must be greater than 0"); auto fileSink = boost::log::add_file_log( keywords::file_name = dirPath / "clio.log", keywords::target_file_name = dirPath / "clio_%Y-%m-%d_%H-%M-%S.log",