From f35afe62a4ae49e27f2e67c14918bdee133a7b83 Mon Sep 17 00:00:00 2001 From: moneromooo-monero Date: Fri, 22 Sep 2017 14:14:58 +0100 Subject: [PATCH 1/3] epee: factor log level/categories setting --- contrib/epee/src/mlog.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/contrib/epee/src/mlog.cpp b/contrib/epee/src/mlog.cpp index a3f38e677..f7efbe39d 100644 --- a/contrib/epee/src/mlog.cpp +++ b/contrib/epee/src/mlog.cpp @@ -152,8 +152,7 @@ void mlog_set_categories(const char *categories) void mlog_set_log_level(int level) { const char *categories = get_default_categories(level); - el::Loggers::setCategories(categories); - MLOG_LOG("New log categories: " << categories); + mlog_set_categories(categories); } void mlog_set_log(const char *log) From 48f92eb665fb8f15284363f47a690981a25a2a25 Mon Sep 17 00:00:00 2001 From: moneromooo-monero Date: Fri, 22 Sep 2017 14:33:09 +0100 Subject: [PATCH 2/3] easylogging++: add categories getter --- external/easylogging++/easylogging++.cc | 16 +++++++++++++++- external/easylogging++/easylogging++.h | 5 +++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/external/easylogging++/easylogging++.cc b/external/easylogging++/easylogging++.cc index 721b2af15..6bc6b2619 100644 --- a/external/easylogging++/easylogging++.cc +++ b/external/easylogging++/easylogging++.cc @@ -1961,8 +1961,13 @@ void VRegistry::setCategories(const char* categories, bool clear) { m_categories.push_back(std::make_pair(ss.str(), level)); }; - if (clear) + if (clear) { m_categories.clear(); + m_categoriesString.clear(); + } + if (!m_categoriesString.empty()) + m_categoriesString += ","; + m_categoriesString += categories; if (!categories) return; @@ -2001,6 +2006,11 @@ void VRegistry::setCategories(const char* categories, bool clear) { } } +std::string VRegistry::getCategories() { + base::threading::ScopedLock scopedLock(lock()); + return m_categoriesString; +} + // Log levels are sorted in a weird way... static int priority(Level level) { if (level == Level::Fatal) return 0; @@ -3073,6 +3083,10 @@ void Loggers::setCategories(const char* categories, bool clear) { ELPP->vRegistry()->setCategories(categories, clear); } +std::string Loggers::getCategories() { + return ELPP->vRegistry()->getCategories(); +} + void Loggers::clearCategories(void) { ELPP->vRegistry()->clearCategories(); } diff --git a/external/easylogging++/easylogging++.h b/external/easylogging++/easylogging++.h index 8f592899e..c55cce755 100644 --- a/external/easylogging++/easylogging++.h +++ b/external/easylogging++/easylogging++.h @@ -2488,6 +2488,8 @@ class VRegistry : base::NoCopy, public base::threading::ThreadSafe { void setCategories(const char* categories, bool clear = true); + std::string getCategories(); + void setModules(const char* modules); bool allowed(Level level, const char* category); @@ -2518,6 +2520,7 @@ class VRegistry : base::NoCopy, public base::threading::ThreadSafe { base::type::EnumType* m_pFlags; std::map m_modules; std::deque> m_categories; + std::string m_categoriesString; std::string m_filenameCommonPrefix; }; } // namespace base @@ -3953,6 +3956,8 @@ class Loggers : base::StaticClass { static void setVModules(const char* modules); /// @brief Sets categories as specified (on the fly) static void setCategories(const char* categories, bool clear = true); + /// @brief Gets current categories + static std::string getCategories(); /// @brief Clears vmodules static void clearVModules(void); /// @brief Clears categories From 792ba4f0fe88cb29e28d3bede2eacafce154bb64 Mon Sep 17 00:00:00 2001 From: moneromooo-monero Date: Fri, 22 Sep 2017 17:54:58 +0100 Subject: [PATCH 3/3] Log categories can now be added to and removed from Also, set_log without parameters now prints the log categories --- contrib/epee/include/misc_log_ex.h | 1 + contrib/epee/src/mlog.cpp | 41 +++++++++++++++++++++++-- src/daemon/command_parser_executor.cpp | 7 ++++- src/daemon/command_server.cpp | 2 +- src/daemon/rpc_command_executor.cpp | 2 +- src/rpc/core_rpc_server.cpp | 1 + src/rpc/core_rpc_server_commands_defs.h | 3 ++ src/simplewallet/simplewallet.cpp | 8 +++-- 8 files changed, 57 insertions(+), 8 deletions(-) diff --git a/contrib/epee/include/misc_log_ex.h b/contrib/epee/include/misc_log_ex.h index ec4bcbe2d..c79232357 100644 --- a/contrib/epee/include/misc_log_ex.h +++ b/contrib/epee/include/misc_log_ex.h @@ -125,6 +125,7 @@ std::string mlog_get_default_log_path(const char *default_filename); void mlog_configure(const std::string &filename_base, bool console); void mlog_set_categories(const char *categories); +std::string mlog_get_categories(); void mlog_set_log_level(int level); void mlog_set_log(const char *log); diff --git a/contrib/epee/src/mlog.cpp b/contrib/epee/src/mlog.cpp index f7efbe39d..4fd47a350 100644 --- a/contrib/epee/src/mlog.cpp +++ b/contrib/epee/src/mlog.cpp @@ -144,8 +144,45 @@ void mlog_configure(const std::string &filename_base, bool console) void mlog_set_categories(const char *categories) { - el::Loggers::setCategories(categories); - MLOG_LOG("New log categories: " << categories); + std::string new_categories; + if (*categories) + { + if (*categories == '+') + { + ++categories; + new_categories = mlog_get_categories(); + if (*categories) + { + if (!new_categories.empty()) + new_categories += ","; + new_categories += categories; + } + } + else if (*categories == '-') + { + ++categories; + new_categories = mlog_get_categories(); + std::vector single_categories; + boost::split(single_categories, categories, boost::is_any_of(","), boost::token_compress_on); + for (const std::string &s: single_categories) + { + size_t pos = new_categories.find(s); + if (pos != std::string::npos) + new_categories = new_categories.erase(pos, s.size()); + } + } + else + { + new_categories = categories; + } + } + el::Loggers::setCategories(new_categories.c_str(), true); + MLOG_LOG("New log categories: " << el::Loggers::getCategories()); +} + +std::string mlog_get_categories() +{ + return el::Loggers::getCategories(); } // maps epee style log level to new logging system diff --git a/src/daemon/command_parser_executor.cpp b/src/daemon/command_parser_executor.cpp index d949a57b1..7d6464033 100644 --- a/src/daemon/command_parser_executor.cpp +++ b/src/daemon/command_parser_executor.cpp @@ -125,12 +125,17 @@ bool t_command_parser_executor::print_blockchain_info(const std::vector& args) { - if(args.size() != 1) + if(args.size() > 1) { std::cout << "use: set_log [ | ]" << std::endl; return true; } + if (args.empty()) + { + return m_executor.set_log_categories("+"); + } + uint16_t l = 0; if(epee::string_tools::get_xtype_from_string(l, args[0])) { diff --git a/src/daemon/command_server.cpp b/src/daemon/command_server.cpp index b9f503c6b..3f1543857 100644 --- a/src/daemon/command_server.cpp +++ b/src/daemon/command_server.cpp @@ -141,7 +141,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 loglevel, is a number 0-4" + , "set_log |<{+,-,}categories> - Change current log level/categories, is a number 0-4" ); m_command_lookup.set_handler( "diff" diff --git a/src/daemon/rpc_command_executor.cpp b/src/daemon/rpc_command_executor.cpp index 167e24ed3..a55e96796 100644 --- a/src/daemon/rpc_command_executor.cpp +++ b/src/daemon/rpc_command_executor.cpp @@ -599,7 +599,7 @@ bool t_rpc_command_executor::set_log_categories(const std::string &categories) { } } - tools::success_msg_writer() << "Log categories are now " << categories; + tools::success_msg_writer() << "Log categories are now " << res.categories; return true; } diff --git a/src/rpc/core_rpc_server.cpp b/src/rpc/core_rpc_server.cpp index 1f7f4a1ff..8f157dea2 100644 --- a/src/rpc/core_rpc_server.cpp +++ b/src/rpc/core_rpc_server.cpp @@ -810,6 +810,7 @@ namespace cryptonote bool core_rpc_server::on_set_log_categories(const COMMAND_RPC_SET_LOG_CATEGORIES::request& req, COMMAND_RPC_SET_LOG_CATEGORIES::response& res) { mlog_set_log(req.categories.c_str()); + res.categories = mlog_get_categories(); res.status = CORE_RPC_STATUS_OK; return true; } diff --git a/src/rpc/core_rpc_server_commands_defs.h b/src/rpc/core_rpc_server_commands_defs.h index 88327dd75..3b3596d0b 100644 --- a/src/rpc/core_rpc_server_commands_defs.h +++ b/src/rpc/core_rpc_server_commands_defs.h @@ -979,8 +979,11 @@ namespace cryptonote struct response { std::string status; + std::string categories; + BEGIN_KV_SERIALIZE_MAP() KV_SERIALIZE(status) + KV_SERIALIZE(categories) END_KV_SERIALIZE_MAP() }; }; diff --git a/src/simplewallet/simplewallet.cpp b/src/simplewallet/simplewallet.cpp index 3fe68046b..b5aa4953c 100644 --- a/src/simplewallet/simplewallet.cpp +++ b/src/simplewallet/simplewallet.cpp @@ -747,7 +747,7 @@ simple_wallet::simple_wallet() m_cmd_binder.set_handler("donate", boost::bind(&simple_wallet::donate, this, _1), tr("donate [] [payment_id] - Donate to the development team (donate.getmonero.org)")); m_cmd_binder.set_handler("sign_transfer", boost::bind(&simple_wallet::sign_transfer, this, _1), tr("Sign a transaction from a file")); m_cmd_binder.set_handler("submit_transfer", boost::bind(&simple_wallet::submit_transfer, this, _1), tr("Submit a signed transaction from a file")); - m_cmd_binder.set_handler("set_log", boost::bind(&simple_wallet::set_log, this, _1), tr("set_log | - Change current log detail (level must be <0-4>)")); + m_cmd_binder.set_handler("set_log", boost::bind(&simple_wallet::set_log, this, _1), tr("set_log |{+,-,} - Change current log detail (level must be <0-4>)")); m_cmd_binder.set_handler("address", boost::bind(&simple_wallet::print_address, this, _1), tr("Show current wallet public address")); m_cmd_binder.set_handler("integrated_address", boost::bind(&simple_wallet::print_integrated_address, this, _1), tr("integrated_address [PID] - Encode a payment ID into an integrated address for the current wallet public address (no argument uses a random payment ID), or decode an integrated address to standard address and payment ID")); m_cmd_binder.set_handler("address_book", boost::bind(&simple_wallet::address_book, this, _1), tr("address_book [(add (
[pid ])| [])|(delete )] - Print all entries in the address book, optionally adding/deleting an entry to/from it")); @@ -855,12 +855,14 @@ bool simple_wallet::set_variable(const std::vector &args) //---------------------------------------------------------------------------------------------------- bool simple_wallet::set_log(const std::vector &args) { - if(args.size() != 1) + if(args.size() > 1) { fail_msg_writer() << tr("usage: set_log | "); return true; } - mlog_set_log(args[0].c_str()); + if (!args.empty()) + mlog_set_log(args[0].c_str()); + success_msg_writer() << "New log categories: " << mlog_get_categories(); return true; } //----------------------------------------------------------------------------------------------------