From fe701ef35a4d5407845f500d37d291048ff5ccc8 Mon Sep 17 00:00:00 2001 From: Eran Date: Wed, 29 May 2024 09:38:54 +0300 Subject: [PATCH] disable unused EL++ log levels for performance --- src/log.h | 64 ++++++++++++++++++++++++++++++------------------------- 1 file changed, 35 insertions(+), 29 deletions(-) diff --git a/src/log.h b/src/log.h index 979d6f55060..6fb9f5295bf 100644 --- a/src/log.h +++ b/src/log.h @@ -55,9 +55,9 @@ namespace librealsense template class logger_type { - rs2_log_severity minimum_log_severity = RS2_LOG_SEVERITY_NONE; rs2_log_severity minimum_console_severity = RS2_LOG_SEVERITY_NONE; rs2_log_severity minimum_file_severity = RS2_LOG_SEVERITY_NONE; + rs2_log_severity minimum_callback_severity = RS2_LOG_SEVERITY_NONE; std::mutex log_mutex; std::ofstream log_file; @@ -104,16 +104,12 @@ namespace librealsense defaultConf.setToDefault(); // To set GLOBAL configurations you may use - defaultConf.setGlobally(el::ConfigurationType::ToFile, "false"); - defaultConf.setGlobally(el::ConfigurationType::ToStandardOutput, "false"); - defaultConf.setGlobally(el::ConfigurationType::LogFlushThreshold, "10"); - defaultConf.setGlobally(el::ConfigurationType::Format, " %datetime{%d/%M %H:%m:%s,%g} %level [%thread] (%fbase:%line) %msg"); - - for (int i = minimum_console_severity; i < RS2_LOG_SEVERITY_NONE; i++) - { - defaultConf.set(severity_to_level(static_cast(i)), - el::ConfigurationType::ToStandardOutput, "true"); - } + defaultConf.setGlobally( el::ConfigurationType::Enabled, "false" ); + defaultConf.setGlobally( el::ConfigurationType::ToFile, "false" ); + defaultConf.setGlobally( el::ConfigurationType::ToStandardOutput, "false" ); + defaultConf.setGlobally( el::ConfigurationType::LogFlushThreshold, "10" ); + defaultConf.setGlobally( el::ConfigurationType::Format, + " %datetime{%d/%M %H:%m:%s,%g} %level [%thread] (%fbase:%line) %msg" ); // NOTE: you can only log to one file, so successive calls to log_to_file // will override one another! @@ -123,10 +119,21 @@ namespace librealsense // prevents creation of empty log files that are not required. defaultConf.setGlobally(el::ConfigurationType::Filename, filename); } - for (int i = minimum_file_severity; i < RS2_LOG_SEVERITY_NONE; i++) + + auto min_severity + = std::min( minimum_file_severity, std::min( minimum_callback_severity, minimum_console_severity ) ); + + for( int i = RS2_LOG_SEVERITY_DEBUG; i < RS2_LOG_SEVERITY_NONE; ++i ) { - defaultConf.set(severity_to_level(static_cast(i)), - el::ConfigurationType::ToFile, "true"); + auto severity = static_cast< rs2_log_severity >( i ); + auto level = severity_to_level( severity ); + defaultConf.set( level, el::ConfigurationType::Enabled, severity >= min_severity ? "true" : "false" ); + defaultConf.set( level, + el::ConfigurationType::ToStandardOutput, + severity >= minimum_console_severity ? "true" : "false" ); + defaultConf.set( level, + el::ConfigurationType::ToFile, + severity >= minimum_file_severity ? "true" : "false" ); } el::Loggers::reconfigureLogger(log_id, defaultConf); @@ -138,8 +145,9 @@ namespace librealsense defaultConf.setToDefault(); // To set GLOBAL configurations you may use - defaultConf.setGlobally(el::ConfigurationType::ToFile, "false"); - defaultConf.setGlobally(el::ConfigurationType::ToStandardOutput, "false"); + defaultConf.setGlobally( el::ConfigurationType::Enabled, "false" ); + defaultConf.setGlobally( el::ConfigurationType::ToFile, "false" ); + defaultConf.setGlobally( el::ConfigurationType::ToStandardOutput, "false" ); el::Loggers::reconfigureLogger(log_id, defaultConf); } @@ -148,15 +156,10 @@ namespace librealsense logger_type() : filename( rsutils::string::from::datetime() + ".log" ) { - rs2_log_severity severity; - if (try_get_log_severity(severity)) - { - log_to_file(severity, filename.c_str()); - } + if( try_get_log_severity( minimum_file_severity ) ) + open(); else - { open_def(); - } } static bool try_get_log_severity(rs2_log_severity& severity) @@ -233,12 +236,11 @@ namespace librealsense for( auto const& dispatch : callback_dispatchers ) el::Helpers::uninstallLogDispatchCallback< elpp_dispatcher >( dispatch ); callback_dispatchers.clear(); + minimum_callback_severity = RS2_LOG_SEVERITY_NONE; } void log_to_callback( rs2_log_severity min_severity, rs2_log_callback_sptr callback ) { - open(); - try_get_log_severity( min_severity ); if( callback && min_severity != RS2_LOG_SEVERITY_NONE ) { @@ -255,21 +257,25 @@ namespace librealsense auto dispatcher = el::Helpers::logDispatchCallback< elpp_dispatcher >( dispatch_name ); dispatcher->callback = callback; dispatcher->min_severity = min_severity; + if( min_severity < minimum_callback_severity ) + minimum_callback_severity = min_severity; // Remove the default logger (which will log to standard out/err) or it'll still be active //el::Helpers::uninstallLogDispatchCallback< el::base::DefaultLogDispatchCallback >( "DefaultLogDispatchCallback" ); + + open(); } } // Stop logging and reset logger to initial configurations void reset_logger() { - el::Loggers::reconfigureLogger(log_id, el::ConfigurationType::ToFile, "false"); - el::Loggers::reconfigureLogger(log_id, el::ConfigurationType::ToStandardOutput, "false"); - el::Loggers::reconfigureLogger(log_id, el::ConfigurationType::MaxLogFileSize, "0"); + el::Loggers::reconfigureLogger( log_id, el::ConfigurationType::Enabled, "false" ); + el::Loggers::reconfigureLogger( log_id, el::ConfigurationType::ToFile, "false" ); + el::Loggers::reconfigureLogger( log_id, el::ConfigurationType::ToStandardOutput, "false" ); + el::Loggers::reconfigureLogger( log_id, el::ConfigurationType::MaxLogFileSize, "0" ); remove_callbacks(); - minimum_log_severity = RS2_LOG_SEVERITY_NONE; minimum_console_severity = RS2_LOG_SEVERITY_NONE; minimum_file_severity = RS2_LOG_SEVERITY_NONE; }