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.
This commit is contained in:
moneromooo-monero 2017-01-01 16:34:23 +00:00
parent dc98019b59
commit 5833d66f65
No known key found for this signature in database
GPG key ID: 686F07454D6CEFC3
125 changed files with 1461 additions and 4409 deletions

View file

@ -89,7 +89,6 @@ target_link_libraries(daemon
cryptonote_core
crypto
common
otshell_utils
p2p
cryptonote_protocol
daemonizer

View file

@ -46,10 +46,10 @@ namespace daemon_args
, "Specify log file"
, ""
};
const command_line::arg_descriptor<int> arg_log_level = {
const command_line::arg_descriptor<std::string> arg_log_level = {
"log-level"
, ""
, LOG_LEVEL_0
, ""
};
const command_line::arg_descriptor<std::vector<std::string>> arg_command = {
"daemon_command"

View file

@ -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<std::string>& ar
{
if(args.size() != 1)
{
std::cout << "use: set_log <log_level_number_0-4>" << std::endl;
std::cout << "use: set_log [<log_level_number_0-4> | <categories>]" << 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 <log_level_number_0-4>" << std::endl;
return true;
if(4 < l)
{
std::cout << "wrong number range, use: set_log <log_level_number_0-4>" << 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 <log_level_number_0-4>" << 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<std::string>& args)

View file

@ -72,6 +72,8 @@ public:
bool set_log_level(const std::vector<std::string>& args);
bool set_log_categories(const std::vector<std::string>& args);
bool print_height(const std::vector<std::string>& args);
bool print_block(const std::vector<std::string>& args);

View file

@ -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 <level> - Change current log detalization level, <level> is a number 0-4"
, "set_log <level>|<categories> - Change current loglevel, <level> is a number 0-4"
);
m_command_lookup.set_handler(
"diff"

View file

@ -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...");
}
}
};

View file

@ -46,6 +46,9 @@ using namespace epee;
#include <functional>
#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;
}
}

View file

@ -29,6 +29,9 @@
#pragma once
#include <boost/program_options.hpp>
#undef MONERO_DEFAULT_LOG_CATEGORY
#define MONERO_DEFAULT_LOG_CATEGORY "daemon"
namespace daemonize {
struct t_internals;

View file

@ -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 <string>
#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);
}
}

View file

@ -34,6 +34,9 @@
#include <string>
#include <vector>
#undef MONERO_DEFAULT_LOG_CATEGORY
#define MONERO_DEFAULT_LOG_CATEGORY "daemon"
namespace daemonize
{
class t_executor final

View file

@ -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: <data_dir>/<CRYPTONOTE_NAME>.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: <data_dir>/<CRYPTONOTE_NAME>.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);
}

View file

@ -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...");
}
}
};

View file

@ -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!");
}

View file

@ -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...");
}
}
};

View file

@ -38,6 +38,9 @@
#include <ctime>
#include <string>
#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;

View file

@ -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);