From c7236d5bed9445b34927d2b7fcf54c2a4d33d706 Mon Sep 17 00:00:00 2001 From: nobodyczcz Date: Tue, 21 Nov 2023 10:50:11 +1100 Subject: [PATCH 1/5] logger use client code namespace. --- inc/Logger.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inc/Logger.h b/inc/Logger.h index b120e0f..1f96105 100644 --- a/inc/Logger.h +++ b/inc/Logger.h @@ -19,7 +19,7 @@ class Logger Logger(){init();}; ~Logger(){}; - boost::shared_ptr> sink; + boost::shared_ptr> sink; void set_logfile(std::string filename); void init(); From f345e0eabff98c058b9453deec85b21e1e649bd0 Mon Sep 17 00:00:00 2001 From: nobodyczcz Date: Tue, 21 Nov 2023 12:52:35 +1100 Subject: [PATCH 2/5] fix #48 program crash on log to file not set. --- Changelog.md | 5 +++++ inc/Logger.h | 8 ++++---- version.txt | 2 +- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/Changelog.md b/Changelog.md index 1d8154a..9d6d01e 100644 --- a/Changelog.md +++ b/Changelog.md @@ -1,5 +1,10 @@ # Changelog +Version 1.1.5 - 2023-11-21 +---------------------------- +Fixed: +- A emergency fix on program crash if log file is not set. + Version 1.1.4 - 2023-11-18 ---------------------------- Fixed: diff --git a/inc/Logger.h b/inc/Logger.h index 1f96105..1cb1524 100644 --- a/inc/Logger.h +++ b/inc/Logger.h @@ -19,7 +19,6 @@ class Logger Logger(){init();}; ~Logger(){}; - boost::shared_ptr> sink; void set_logfile(std::string filename); void init(); @@ -30,7 +29,8 @@ class Logger void log_fatal(std::string input, int timestep); void log_warning(std::string input); void log_warning(std::string input, int timestep); - void flush(){this->sink->flush();}; - // void log_preprocessing(bool succ); - // void log_plan(bool succ,int time); + void flush(){ if (this->sink != nullptr) this->sink->flush();}; +private: + boost::shared_ptr> sink = nullptr; + }; diff --git a/version.txt b/version.txt index 65087b4..e25d8d9 100644 --- a/version.txt +++ b/version.txt @@ -1 +1 @@ -1.1.4 +1.1.5 From a18b074bdae6804fe83755b38e83f538ffea7d0f Mon Sep 17 00:00:00 2001 From: Zhe Chen <43833617+nobodyczcz@users.noreply.github.com> Date: Tue, 21 Nov 2023 12:54:28 +1100 Subject: [PATCH 3/5] Update Changelog.md --- Changelog.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Changelog.md b/Changelog.md index 9d6d01e..e193a69 100644 --- a/Changelog.md +++ b/Changelog.md @@ -3,7 +3,7 @@ Version 1.1.5 - 2023-11-21 ---------------------------- Fixed: -- A emergency fix on program crash if log file is not set. +- An emergency fix on program crash if a log file is not set with CLI. Version 1.1.4 - 2023-11-18 ---------------------------- From 0912e7663c9d59b07a20520e0c1057cdd03cb021 Mon Sep 17 00:00:00 2001 From: nobodyczcz Date: Tue, 21 Nov 2023 13:36:00 +1100 Subject: [PATCH 4/5] fix with a more explicit way --- inc/Logger.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/inc/Logger.h b/inc/Logger.h index 1cb1524..bba2099 100644 --- a/inc/Logger.h +++ b/inc/Logger.h @@ -29,8 +29,8 @@ class Logger void log_fatal(std::string input, int timestep); void log_warning(std::string input); void log_warning(std::string input, int timestep); - void flush(){ if (this->sink != nullptr) this->sink->flush();}; + void flush(){ if (!this->sink) this->sink->flush();}; private: - boost::shared_ptr> sink = nullptr; + boost::shared_ptr> sink; }; From 5f97f8b7ca0fbeb264a8a03043431f260d623a2a Mon Sep 17 00:00:00 2001 From: nobodyczcz Date: Tue, 21 Nov 2023 22:04:06 +1100 Subject: [PATCH 5/5] simplified the logger implementation --- inc/Logger.h | 11 ++++------- src/Logger.cpp | 43 +++++++++++++++++-------------------------- src/driver.cpp | 6 ++---- 3 files changed, 23 insertions(+), 37 deletions(-) diff --git a/inc/Logger.h b/inc/Logger.h index bba2099..8940306 100644 --- a/inc/Logger.h +++ b/inc/Logger.h @@ -16,21 +16,18 @@ namespace keywords = boost::log::keywords; class Logger { public: - Logger(){init();}; + Logger(std::string filename, int severity=logging::trivial::info); ~Logger(){}; - - void set_logfile(std::string filename); - void init(); - void log_info(std::string input); void log_info(std::string input, int timestep); void log_fatal(std::string input); void log_fatal(std::string input, int timestep); void log_warning(std::string input); void log_warning(std::string input, int timestep); - void flush(){ if (!this->sink) this->sink->flush();}; + void flush(); private: - boost::shared_ptr> sink; + logging::core_ptr core; + }; diff --git a/src/Logger.cpp b/src/Logger.cpp index ad1c9e3..c3673b4 100644 --- a/src/Logger.cpp +++ b/src/Logger.cpp @@ -4,37 +4,34 @@ namespace logging = boost::log; namespace keywords = boost::log::keywords; namespace src = boost::log::sources; namespace sinks = boost::log::sinks; +using namespace logging::trivial; +src::severity_logger< severity_level > lg; -void Logger::set_logfile(std::string filename) -{ - this->sink = logging::add_file_log +Logger::Logger(std::string filename, int severity){ + this->core = logging::core::get(); + logging::add_common_attributes(); + logging::core::get()->set_filter + ( + logging::trivial::severity >= severity + ); + + if (filename != "") + logging::add_file_log ( - keywords::file_name = filename, - keywords::format = "[%TimeStamp%]: *%Severity%* %Message%" - ); + keywords::file_name = filename, + keywords::format = "[%TimeStamp%]: *%Severity%* %Message%" + ); } - -void Logger::init() -{ - // logging::add_file_log - // ( - // keywords::format = "[%TimeStamp%]: %Message%" - // ); - logging::core::get()->set_filter - ( - logging::trivial::severity >= logging::trivial::info - ); +void Logger::flush(){ + this->core->flush(); } void Logger::log_info(std::string input) { - logging::add_common_attributes(); - using namespace logging::trivial; - src::severity_logger< severity_level > lg; BOOST_LOG_SEV(lg, info) << input; } @@ -53,20 +50,14 @@ void Logger::log_fatal(std::string input, int timestep) void Logger::log_fatal(std::string input) { - logging::add_common_attributes(); - using namespace logging::trivial; - src::severity_logger< severity_level > lg; BOOST_LOG_SEV(lg, fatal) << input; } void Logger::log_warning(std::string input) { - logging::add_common_attributes(); - using namespace logging::trivial; - src::severity_logger< severity_level > lg; BOOST_LOG_SEV(lg, warning) << input; } diff --git a/src/driver.cpp b/src/driver.cpp index 9edacb5..344a5ec 100644 --- a/src/driver.cpp +++ b/src/driver.cpp @@ -54,7 +54,7 @@ int main(int argc, char **argv) ("fileStoragePath", po::value()->default_value(""), "the path to the storage path") ("planTimeLimit", po::value()->default_value(INT_MAX), "the time limit for planner in seconds") ("preprocessTimeLimit", po::value()->default_value(INT_MAX), "the time limit for preprocessing in seconds") - ("logFile,l", po::value(), "issue log file name"); + ("logFile,l", po::value()->default_value(""), "issue log file name"); clock_t start_time = clock(); po::store(po::parse_command_line(argc, argv, desc), vm); @@ -75,9 +75,7 @@ int main(int argc, char **argv) base_folder += "/"; } - Logger *logger = new Logger(); - if (vm.count("logFile")) - logger->set_logfile(vm["logFile"].as()); + Logger *logger = new Logger(vm["logFile"].as()); MAPFPlanner *planner = nullptr; // Planner is inited here, but will be managed and deleted by system_ptr deconstructor