From f77dae2f2830299597195b50f3ef2edb0d8cc450 Mon Sep 17 00:00:00 2001 From: Ravin Perera <33562092+ravinsp@users.noreply.github.com> Date: Mon, 21 Oct 2019 13:29:52 +0530 Subject: [PATCH] Added file log. --- CMakeLists.txt | 1 + src/conf.cpp | 1 + src/conf.hpp | 1 + src/hplog.cpp | 44 +++++++++++++++++++++++++++++++++++--------- src/hplog.hpp | 1 + src/main.cpp | 23 +++++++++++++++++++++-- 6 files changed, 60 insertions(+), 11 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9cc36b48..26244697 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -33,6 +33,7 @@ target_link_libraries(hpcore libsodium.a ${Boost_SYSTEM_LIBRARY} ${Boost_LOG_LIBRARY} + ${Boost_LOG_SETUP_LIBRARY} stdc++fs pthread protobuf.a diff --git a/src/conf.cpp b/src/conf.cpp index 395405cc..df6841b7 100644 --- a/src/conf.cpp +++ b/src/conf.cpp @@ -123,6 +123,7 @@ void set_contract_dir_paths(std::string basedir) ctx.configFile = ctx.configDir + "/hp.cfg"; ctx.histDir = basedir + "/hist"; ctx.stateDir = basedir + "/state"; + ctx.logDir = basedir + "/log"; } /** diff --git a/src/conf.hpp b/src/conf.hpp index 81495792..6dab8305 100644 --- a/src/conf.hpp +++ b/src/conf.hpp @@ -20,6 +20,7 @@ struct contract_ctx std::string contractDir; // Contract base directory std::string histDir; // Contract history dir std::string stateDir; // Contract state dir + std::string logDir; // Contract log dir std::string configDir; // Contract config dir std::string configFile; // Full path to the contract config file }; diff --git a/src/hplog.cpp b/src/hplog.cpp index 583d57f1..dc824a77 100644 --- a/src/hplog.cpp +++ b/src/hplog.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include "conf.hpp" @@ -25,7 +26,7 @@ namespace hplog { /** - * Stream operator overload for converting integer severity vaue to text. + * Stream operator overload for converting integer severity value to text. */ std::ostream &operator<<(std::ostream &os, LOG_SEVERITY level) { @@ -50,27 +51,52 @@ void init() else if (conf::cfg.loglevel == "error") severity = LOG_SEVERITY::ERROR; + // Log line format expression. + auto format_expr = (expr::stream + << expr::format_date_time("TimeStamp", "%Y-%m-%d %H:%M:%S") + //<< ":" << expr::attr("ThreadID") + << " [" << expr::attr("Channel") + << "] [" << expr::attr("Severity") + << "] " << expr::smessage); + if (conf::cfg.loggers.count("console") == 1) { logging::add_console_log( std::clog, keywords::filter = (a_severity >= severity), - keywords::format = - (expr::stream - << expr::format_date_time("TimeStamp", "%Y-%m-%d %H:%M:%S") - //<< ":" << expr::attr("ThreadID") - << " [" << expr::attr("Channel") - << "] [" << expr::attr("Severity") - << "] " << expr::smessage)); + keywords::format = format_expr); } if (conf::cfg.loggers.count("file") == 1) { - // TODO: Add file logger. + logging::add_file_log( + keywords::target = conf::ctx.logDir, // Log file directory. + keywords::file_name = conf::ctx.logDir + "/hp_%N.log", // File name pattern "hp_1.log". + keywords::rotation_size = 10 * 1024 * 1024, // Rotate files every 10 MB. + keywords::max_size = 500 * 1024 * 1024, // Do not exceed 500 MB total logs. + keywords::filter = (a_severity >= severity), + keywords::format = format_expr, + + // This will make every new launch of Hot Pocket to start a new log file number. + // It will scan existing log files matching the pattern and find the next number. + keywords::scan_method = sinks::file::scan_matching, + +#ifndef NDEBUG + // We enable auto_flush to immediately get the logs onto the file. Otherwise it takes time + // for buffered logs to reach the file. This impacts performance. So enabled only in debug build. + keywords::auto_flush = true +#endif + ); } // Add Boost Log built-in fields for log entries. logging::add_common_attributes(); } +void deinit() +{ + // This will make all buffered logs to be flushed to the sink. + logging::core::get()->remove_all_sinks(); +} + } // namespace hplog \ No newline at end of file diff --git a/src/hplog.hpp b/src/hplog.hpp index efd3019b..b498d98c 100644 --- a/src/hplog.hpp +++ b/src/hplog.hpp @@ -28,6 +28,7 @@ enum LOG_SEVERITY BOOST_LOG_ATTRIBUTE_KEYWORD(a_severity, "Severity", hplog::LOG_SEVERITY); void init(); +void deinit(); } // namespace hplog diff --git a/src/main.cpp b/src/main.cpp index 54107662..759fd5f3 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -62,6 +62,22 @@ int parse_cmd(int argc, char **argv) return -1; } +/** + * Performs any cleanup on graceful application termination. + */ +void deinit() +{ + usr::deinit(); + hplog::deinit(); +} + +void signal_handler(int signum) +{ + LOG_WARN << "Interrupt signal (" << signum << ") received."; + deinit(); + exit(signum); +} + int main(int argc, char **argv) { // Extract the CLI args @@ -101,7 +117,7 @@ int main(int argc, char **argv) else if (conf::ctx.command == "run") { // In order to host the contract we should init some required sub systems. - + if (conf::init() != 0) return -1; @@ -110,6 +126,9 @@ int main(int argc, char **argv) if (usr::init() != 0) return -1; + // After initializing primary subsystems, register the SIGINT handler. + signal(SIGINT, signal_handler); + // This will start hosting the contract and start consensus rounds. // TODO @@ -156,7 +175,7 @@ int main(int argc, char **argv) } // Free resources. - usr::deinit(); + deinit(); } } }