From 5833d66f6540e7b34e10ddef37c2b67bd501994b Mon Sep 17 00:00:00 2001 From: moneromooo-monero Date: Sun, 1 Jan 2017 16:34:23 +0000 Subject: Change logging to easylogging++ This replaces the epee and data_loggers logging systems with a single one, and also adds filename:line and explicit severity levels. Categories may be defined, and logging severity set by category (or set of categories). epee style 0-4 log level maps to a sensible severity configuration. Log files now also rotate when reaching 100 MB. To select which logs to output, use the MONERO_LOGS environment variable, with a comma separated list of categories (globs are supported), with their requested severity level after a colon. If a log matches more than one such setting, the last one in the configuration string applies. A few examples: This one is (mostly) silent, only outputting fatal errors: MONERO_LOGS=*:FATAL This one is very verbose: MONERO_LOGS=*:TRACE This one is totally silent (logwise): MONERO_LOGS="" This one outputs all errors and warnings, except for the "verify" category, which prints just fatal errors (the verify category is used for logs about incoming transactions and blocks, and it is expected that some/many will fail to verify, hence we don't want the spam): MONERO_LOGS=*:WARNING,verify:FATAL Log levels are, in decreasing order of priority: FATAL, ERROR, WARNING, INFO, DEBUG, TRACE Subcategories may be added using prefixes and globs. This example will output net.p2p logs at the TRACE level, but all other net* logs only at INFO: MONERO_LOGS=*:ERROR,net*:INFO,net.p2p:TRACE Logs which are intended for the user (which Monero was using a lot through epee, but really isn't a nice way to go things) should use the "global" category. There are a few helper macros for using this category, eg: MGINFO("this shows up by default") or MGINFO_RED("this is red"), to try to keep a similar look and feel for now. Existing epee log macros still exist, and map to the new log levels, but since they're used as a "user facing" UI element as much as a logging system, they often don't map well to log severities (ie, a log level 0 log may be an error, or may be something we want the user to see, such as an important info). In those cases, I tried to use the new macros. In other cases, I left the existing macros in. When modifying logs, it is probably best to switch to the new macros with explicit levels. The --log-level options and set_log commands now also accept category settings, in addition to the epee style log levels. --- src/daemon/CMakeLists.txt | 1 - src/daemon/command_line_args.h | 4 +-- src/daemon/command_parser_executor.cpp | 23 ++++++------ src/daemon/command_parser_executor.h | 2 ++ src/daemon/command_server.cpp | 5 ++- src/daemon/core.h | 11 +++--- src/daemon/daemon.cpp | 9 +++-- src/daemon/daemon.h | 3 ++ src/daemon/executor.cpp | 8 +++-- src/daemon/executor.h | 3 ++ src/daemon/main.cpp | 66 ++++++++++++---------------------- src/daemon/p2p.h | 15 ++++---- src/daemon/protocol.h | 11 +++--- src/daemon/rpc.h | 17 +++++---- src/daemon/rpc_command_executor.cpp | 31 ++++++++++++++++ src/daemon/rpc_command_executor.h | 5 +++ 16 files changed, 130 insertions(+), 84 deletions(-) (limited to 'src/daemon') diff --git a/src/daemon/CMakeLists.txt b/src/daemon/CMakeLists.txt index 0f4baf932..1b6363f7b 100644 --- a/src/daemon/CMakeLists.txt +++ b/src/daemon/CMakeLists.txt @@ -89,7 +89,6 @@ target_link_libraries(daemon cryptonote_core crypto common - otshell_utils p2p cryptonote_protocol daemonizer diff --git a/src/daemon/command_line_args.h b/src/daemon/command_line_args.h index 1fe2ddcf6..cb9fb6014 100644 --- a/src/daemon/command_line_args.h +++ b/src/daemon/command_line_args.h @@ -46,10 +46,10 @@ namespace daemon_args , "Specify log file" , "" }; - const command_line::arg_descriptor arg_log_level = { + const command_line::arg_descriptor arg_log_level = { "log-level" , "" - , LOG_LEVEL_0 + , "" }; const command_line::arg_descriptor> arg_command = { "daemon_command" diff --git a/src/daemon/command_parser_executor.cpp b/src/daemon/command_parser_executor.cpp index 9d28cd41e..27f9d0fd7 100644 --- a/src/daemon/command_parser_executor.cpp +++ b/src/daemon/command_parser_executor.cpp @@ -29,6 +29,9 @@ #include "common/dns_utils.h" #include "daemon/command_parser_executor.h" +#undef MONERO_DEFAULT_LOG_CATEGORY +#define MONERO_DEFAULT_LOG_CATEGORY "daemon" + namespace daemonize { t_command_parser_executor::t_command_parser_executor( @@ -117,24 +120,24 @@ bool t_command_parser_executor::set_log_level(const std::vector& ar { if(args.size() != 1) { - std::cout << "use: set_log " << std::endl; + std::cout << "use: set_log [ | ]" << std::endl; return true; } uint16_t l = 0; - if(!epee::string_tools::get_xtype_from_string(l, args[0])) + if(epee::string_tools::get_xtype_from_string(l, args[0])) { - std::cout << "wrong number format, use: set_log " << std::endl; - return true; + if(4 < l) + { + std::cout << "wrong number range, use: set_log " << std::endl; + return true; + } + return m_executor.set_log_level(l); } - - if(LOG_LEVEL_4 < l) + else { - std::cout << "wrong number range, use: set_log " << std::endl; - return true; + return m_executor.set_log_categories(args.front()); } - - return m_executor.set_log_level(l); } bool t_command_parser_executor::print_height(const std::vector& args) diff --git a/src/daemon/command_parser_executor.h b/src/daemon/command_parser_executor.h index 7763ebed0..15293ade9 100644 --- a/src/daemon/command_parser_executor.h +++ b/src/daemon/command_parser_executor.h @@ -72,6 +72,8 @@ public: bool set_log_level(const std::vector& args); + bool set_log_categories(const std::vector& args); + bool print_height(const std::vector& args); bool print_block(const std::vector& args); diff --git a/src/daemon/command_server.cpp b/src/daemon/command_server.cpp index 086478a47..95fd3178c 100644 --- a/src/daemon/command_server.cpp +++ b/src/daemon/command_server.cpp @@ -30,6 +30,9 @@ #include "version.h" #include "daemon/command_server.h" +#undef MONERO_DEFAULT_LOG_CATEGORY +#define MONERO_DEFAULT_LOG_CATEGORY "daemon" + namespace daemonize { namespace p = std::placeholders; @@ -133,7 +136,7 @@ t_command_server::t_command_server( m_command_lookup.set_handler( "set_log" , std::bind(&t_command_parser_executor::set_log_level, &m_parser, p::_1) - , "set_log - Change current log detalization level, is a number 0-4" + , "set_log | - Change current loglevel, is a number 0-4" ); m_command_lookup.set_handler( "diff" diff --git a/src/daemon/core.h b/src/daemon/core.h index 2b7f0d177..23f7a9f63 100644 --- a/src/daemon/core.h +++ b/src/daemon/core.h @@ -33,6 +33,9 @@ #include "misc_log_ex.h" #include "daemon/command_line_args.h" +#undef MONERO_DEFAULT_LOG_CATEGORY +#define MONERO_DEFAULT_LOG_CATEGORY "daemon" + namespace daemonize { @@ -68,12 +71,12 @@ public: bool run() { //initialize core here - LOG_PRINT_L0("Initializing core..."); + MGINFO("Initializing core..."); if (!m_core.init(m_vm_HACK)) { return false; } - LOG_PRINT_L0("Core initialized OK"); + MGINFO("Core initialized OK"); return true; } @@ -84,12 +87,12 @@ public: ~t_core() { - LOG_PRINT_L0("Deinitializing core..."); + MGINFO("Deinitializing core..."); try { m_core.deinit(); m_core.set_cryptonote_protocol(nullptr); } catch (...) { - LOG_PRINT_L0("Failed to deinitialize core..."); + MERROR("Failed to deinitialize core..."); } } }; diff --git a/src/daemon/daemon.cpp b/src/daemon/daemon.cpp index 74875bfb0..287c30cb4 100644 --- a/src/daemon/daemon.cpp +++ b/src/daemon/daemon.cpp @@ -46,6 +46,9 @@ using namespace epee; #include +#undef MONERO_DEFAULT_LOG_CATEGORY +#define MONERO_DEFAULT_LOG_CATEGORY "daemon" + namespace daemonize { struct t_internals { @@ -136,17 +139,17 @@ bool t_daemon::run(bool interactive) } mp_internals->rpc.stop(); - LOG_PRINT("Node stopped.", LOG_LEVEL_0); + MGINFO("Node stopped."); return true; } catch (std::exception const & ex) { - LOG_ERROR("Uncaught exception! " << ex.what()); + MFATAL("Uncaught exception! " << ex.what()); return false; } catch (...) { - LOG_ERROR("Uncaught exception!"); + MFATAL("Uncaught exception!"); return false; } } diff --git a/src/daemon/daemon.h b/src/daemon/daemon.h index 655ca8a3d..c8fae5c28 100644 --- a/src/daemon/daemon.h +++ b/src/daemon/daemon.h @@ -29,6 +29,9 @@ #pragma once #include +#undef MONERO_DEFAULT_LOG_CATEGORY +#define MONERO_DEFAULT_LOG_CATEGORY "daemon" + namespace daemonize { struct t_internals; diff --git a/src/daemon/executor.cpp b/src/daemon/executor.cpp index 9583fd056..ac5803cfb 100644 --- a/src/daemon/executor.cpp +++ b/src/daemon/executor.cpp @@ -26,16 +26,19 @@ // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -#include "daemon/executor.h" - #include "misc_log_ex.h" +#include "daemon/executor.h" + #include "common/command_line.h" #include "cryptonote_config.h" #include "version.h" #include +#undef MONERO_DEFAULT_LOG_CATEGORY +#define MONERO_DEFAULT_LOG_CATEGORY "daemon" + namespace daemonize { std::string const t_executor::NAME = "Monero Daemon"; @@ -64,7 +67,6 @@ namespace daemonize boost::program_options::variables_map const & vm ) { - epee::log_space::log_singletone::add_logger(LOGGER_CONSOLE, NULL, NULL); return t_daemon{vm}.run(true); } } diff --git a/src/daemon/executor.h b/src/daemon/executor.h index 37b4970f4..a6b47b93d 100644 --- a/src/daemon/executor.h +++ b/src/daemon/executor.h @@ -34,6 +34,9 @@ #include #include +#undef MONERO_DEFAULT_LOG_CATEGORY +#define MONERO_DEFAULT_LOG_CATEGORY "daemon" + namespace daemonize { class t_executor final diff --git a/src/daemon/main.cpp b/src/daemon/main.cpp index 0895e1bf1..e08065ccd 100644 --- a/src/daemon/main.cpp +++ b/src/daemon/main.cpp @@ -47,6 +47,9 @@ #include "common/stack_trace.h" #endif // STACK_TRACE +#undef MONERO_DEFAULT_LOG_CATEGORY +#define MONERO_DEFAULT_LOG_CATEGORY "daemon" + namespace po = boost::program_options; namespace bf = boost::filesystem; @@ -54,7 +57,6 @@ int main(int argc, char const * argv[]) { try { - _note_c("dbg/main", "Begin of main()"); // TODO parse the debug options like set log level right here at start tools::sanitize_locale(); @@ -79,7 +81,6 @@ int main(int argc, char const * argv[]) bf::path default_conf = default_data_dir / std::string(CRYPTONOTE_NAME ".conf"); command_line::add_arg(visible_options, daemon_args::arg_config_file, default_conf.string()); command_line::add_arg(visible_options, command_line::arg_test_dbg_lock_sleep); - cryptonote::core::init_options(core_settings); // Settings bf::path default_log = default_data_dir / std::string(CRYPTONOTE_NAME ".log"); @@ -196,6 +197,23 @@ int main(int argc, char const * argv[]) } po::notify(vm); + // log_file_path + // default: /.log + // if log-file argument given: + // absolute path + // relative path: relative to data_dir + bf::path log_file_path {data_dir / std::string(CRYPTONOTE_NAME ".log")}; + if (! vm["log-file"].defaulted()) + log_file_path = command_line::get_arg(vm, daemon_args::arg_log_file); + log_file_path = bf::absolute(log_file_path, relative_path_base); + mlog_configure(log_file_path.string(), true); + + // Set log level + if (!vm["log-level"].defaulted()) + { + mlog_set_log(command_line::get_arg(vm, daemon_args::arg_log_level).c_str()); + } + // If there are positional options, we're running a daemon command { auto command = command_line::get_arg(vm, daemon_args::arg_command); @@ -236,55 +254,17 @@ int main(int argc, char const * argv[]) } } - // Start with log level 0 - epee::log_space::get_set_log_detalisation_level(true, LOG_LEVEL_0); - - // Set log level - { - int new_log_level = command_line::get_arg(vm, daemon_args::arg_log_level); - if(new_log_level < LOG_LEVEL_MIN || new_log_level > LOG_LEVEL_MAX) - { - LOG_PRINT_L0("Wrong log level value: " << new_log_level); - } - else if (epee::log_space::get_set_log_detalisation_level(false) != new_log_level) - { - epee::log_space::get_set_log_detalisation_level(true, new_log_level); - int otshell_utils_log_level = 100 - (new_log_level * 20); - gCurrentLogger.setDebugLevel(otshell_utils_log_level); - LOG_PRINT_L0("LOG_LEVEL set to " << new_log_level); - } - } - - // log_file_path - // default: /.log - // if log-file argument given: - // absolute path - // relative path: relative to data_dir - - // Set log file - { - bf::path log_file_path {data_dir / std::string(CRYPTONOTE_NAME ".log")}; - if (! vm["log-file"].defaulted()) - log_file_path = command_line::get_arg(vm, daemon_args::arg_log_file); - log_file_path = bf::absolute(log_file_path, relative_path_base); - - epee::log_space::log_singletone::add_logger( - LOGGER_FILE - , log_file_path.filename().string().c_str() - , log_file_path.parent_path().string().c_str() - ); #ifdef STACK_TRACE - tools::set_stack_trace_log(log_file_path.filename().string()); + tools::set_stack_trace_log(log_file_path.filename().string()); #endif // STACK_TRACE - } if (command_line::has_arg(vm, daemon_args::arg_max_concurrency)) tools::set_max_concurrency(command_line::get_arg(vm, daemon_args::arg_max_concurrency)); // logging is now set up - LOG_PRINT_L0("Monero '" << MONERO_RELEASE_NAME << "' (v" << MONERO_VERSION_FULL << ")"); + MGINFO("Monero '" << MONERO_RELEASE_NAME << "' (v" << MONERO_VERSION_FULL << ")"); - _note_c("dbg/main", "Moving from main() into the daemonize now."); + MINFO("Moving from main() into the daemonize now."); return daemonizer::daemonize(argc, argv, daemonize::t_executor{}, vm); } diff --git a/src/daemon/p2p.h b/src/daemon/p2p.h index 3858989ce..f29c2d822 100644 --- a/src/daemon/p2p.h +++ b/src/daemon/p2p.h @@ -34,6 +34,9 @@ #include "p2p/net_node.h" #include "daemon/protocol.h" +#undef MONERO_DEFAULT_LOG_CATEGORY +#define MONERO_DEFAULT_LOG_CATEGORY "daemon" + namespace daemonize { @@ -57,12 +60,12 @@ public: : m_server{protocol.get()} { //initialize objects - LOG_PRINT_L0("Initializing p2p server..."); + MGINFO("Initializing p2p server..."); if (!m_server.init(vm)) { throw std::runtime_error("Failed to initialize p2p server."); } - LOG_PRINT_L0("P2p server initialized OK"); + MGINFO("P2p server initialized OK"); } t_node_server & get() @@ -72,9 +75,9 @@ public: void run() { - LOG_PRINT_L0("Starting p2p net loop..."); + MGINFO("Starting p2p net loop..."); m_server.run(); - LOG_PRINT_L0("p2p net loop stopped"); + MGINFO("p2p net loop stopped"); } void stop() @@ -84,11 +87,11 @@ public: ~t_p2p() { - LOG_PRINT_L0("Deinitializing p2p..."); + MGINFO("Deinitializing p2p..."); try { m_server.deinit(); } catch (...) { - LOG_PRINT_L0("Failed to deinitialize p2p..."); + MERROR("Failed to deinitialize p2p..."); } } }; diff --git a/src/daemon/protocol.h b/src/daemon/protocol.h index eb894fb81..bc2333659 100644 --- a/src/daemon/protocol.h +++ b/src/daemon/protocol.h @@ -30,6 +30,9 @@ #pragma once +#undef MONERO_DEFAULT_LOG_CATEGORY +#define MONERO_DEFAULT_LOG_CATEGORY "daemon" + namespace daemonize { @@ -47,12 +50,12 @@ public: ) : m_protocol{core.get(), nullptr} { - LOG_PRINT_L0("Initializing cryptonote protocol..."); + MGINFO("Initializing cryptonote protocol..."); if (!m_protocol.init(vm)) { throw std::runtime_error("Failed to initialize cryptonote protocol."); } - LOG_PRINT_L0("Cryptonote protocol initialized OK"); + MGINFO("Cryptonote protocol initialized OK"); } t_protocol_raw & get() @@ -69,11 +72,11 @@ public: ~t_protocol() { - LOG_PRINT_L0("Stopping cryptonote protocol..."); + MGINFO("Stopping cryptonote protocol..."); try { m_protocol.deinit(); m_protocol.set_p2p_endpoint(nullptr); - LOG_PRINT_L0("Cryptonote protocol stopped successfully"); + MGINFO("Cryptonote protocol stopped successfully"); } catch (...) { LOG_ERROR("Failed to stop cryptonote protocol!"); } diff --git a/src/daemon/rpc.h b/src/daemon/rpc.h index bfd2afd84..8b0d5808d 100644 --- a/src/daemon/rpc.h +++ b/src/daemon/rpc.h @@ -32,6 +32,9 @@ #include "rpc/core_rpc_server.h" +#undef MONERO_DEFAULT_LOG_CATEGORY +#define MONERO_DEFAULT_LOG_CATEGORY "daemon" + namespace daemonize { @@ -52,27 +55,27 @@ public: ) : m_server{core.get(), p2p.get()} { - LOG_PRINT_L0("Initializing core rpc server..."); + MGINFO("Initializing core rpc server..."); if (!m_server.init(vm)) { throw std::runtime_error("Failed to initialize core rpc server."); } - LOG_PRINT_GREEN("Core rpc server initialized OK on port: " << m_server.get_binded_port(), LOG_LEVEL_0); + MGINFO("Core rpc server initialized OK on port: " << m_server.get_binded_port()); } void run() { - LOG_PRINT_L0("Starting core rpc server..."); + MGINFO("Starting core rpc server..."); if (!m_server.run(2, false)) { throw std::runtime_error("Failed to start core rpc server."); } - LOG_PRINT_L0("Core rpc server started ok"); + MGINFO("Core rpc server started ok"); } void stop() { - LOG_PRINT_L0("Stopping core rpc server..."); + MGINFO("Stopping core rpc server..."); m_server.send_stop_signal(); m_server.timed_wait_server_stop(5000); } @@ -84,11 +87,11 @@ public: ~t_rpc() { - LOG_PRINT_L0("Deinitializing rpc server..."); + MGINFO("Deinitializing rpc server..."); try { m_server.deinit(); } catch (...) { - LOG_PRINT_L0("Failed to deinitialize rpc server..."); + MERROR("Failed to deinitialize rpc server..."); } } }; diff --git a/src/daemon/rpc_command_executor.cpp b/src/daemon/rpc_command_executor.cpp index c7a122d00..8558ebc17 100644 --- a/src/daemon/rpc_command_executor.cpp +++ b/src/daemon/rpc_command_executor.cpp @@ -38,6 +38,9 @@ #include #include +#undef MONERO_DEFAULT_LOG_CATEGORY +#define MONERO_DEFAULT_LOG_CATEGORY "daemon" + namespace daemonize { namespace { @@ -517,6 +520,34 @@ bool t_rpc_command_executor::set_log_level(int8_t level) { return true; } +bool t_rpc_command_executor::set_log_categories(const std::string &categories) { + cryptonote::COMMAND_RPC_SET_LOG_CATEGORIES::request req; + cryptonote::COMMAND_RPC_SET_LOG_CATEGORIES::response res; + req.categories = categories; + + std::string fail_message = "Unsuccessful"; + + if (m_is_rpc) + { + if (!m_rpc_client->rpc_request(req, res, "/set_log_categories", fail_message.c_str())) + { + return true; + } + } + else + { + if (!m_rpc_server->on_set_log_categories(req, res) || res.status != CORE_RPC_STATUS_OK) + { + tools::fail_msg_writer() << fail_message.c_str(); + return true; + } + } + + tools::success_msg_writer() << "Log categories are now " << categories; + + return true; +} + bool t_rpc_command_executor::print_height() { cryptonote::COMMAND_RPC_GET_HEIGHT::request req; cryptonote::COMMAND_RPC_GET_HEIGHT::response res; diff --git a/src/daemon/rpc_command_executor.h b/src/daemon/rpc_command_executor.h index af283c1f1..afcd99d32 100644 --- a/src/daemon/rpc_command_executor.h +++ b/src/daemon/rpc_command_executor.h @@ -45,6 +45,9 @@ #include "p2p/net_node.h" #include "rpc/core_rpc_server.h" +#undef MONERO_DEFAULT_LOG_CATEGORY +#define MONERO_DEFAULT_LOG_CATEGORY "daemon" + namespace daemonize { class t_rpc_command_executor final { @@ -82,6 +85,8 @@ public: bool set_log_level(int8_t level); + bool set_log_categories(const std::string &categories); + bool print_height(); bool print_block_by_hash(crypto::hash block_hash); -- cgit v1.2.3