From f7c824fedf9679ad3b0a9e1260e83fe2bc147bd4 Mon Sep 17 00:00:00 2001 From: Ryan Neph Date: Fri, 3 May 2024 11:37:34 -0700 Subject: [PATCH] vm_tools: sommelier: set log-level from cmdline New style logging uses the syntax `LOG(INFO) << "message";` to indicate the logging verbosity. That verbosity is tested prior to visible output, but the selection of which levels will be shown is only controllable at build-time via '-DLOG_LEVEL=-1'. There's no practical reason to require such. The test for visibility _may_ get optimized out by compiler for statically-defined LOG_LEVEL, but it is a trivial cost not worth optimizing in the general case. Other tricks can be used to solve that if it becomes an issue. Now it can be set with --log-level=, with the default set in the old way with -DLOG_LEVEL=, or defaulting to LOG_LEVEL_INFO (0) when not defined. BUG=b:339048928 TEST=sommelier --log-level=-1 ... Change-Id: I2de93e6c11ccba89022a4aa460c082644328e800 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform2/+/5517861 Tested-by: Ryan Neph Reviewed-by: Chloe Pelling Auto-Submit: Ryan Neph Reviewed-by: Arjun Srinivasan Commit-Queue: Ryan Neph --- sommelier-logging.cc | 2 ++ sommelier-logging.h | 9 ++++++++- sommelier.cc | 7 +++++-- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/sommelier-logging.cc b/sommelier-logging.cc index b73eaf8..f1f4447 100644 --- a/sommelier-logging.cc +++ b/sommelier-logging.cc @@ -6,6 +6,8 @@ namespace logging { +int64_t min_log_level = LOG_LEVEL; + std::string file_name(std::string file_path) { // Extract file name from file path. auto found = file_path.find_last_of('/'); diff --git a/sommelier-logging.h b/sommelier-logging.h index 5232f0a..33a384f 100644 --- a/sommelier-logging.h +++ b/sommelier-logging.h @@ -10,6 +10,7 @@ #define VM_TOOLS_SOMMELIER_SOMMELIER_LOGGING_H_ #include +#include #include #include #include @@ -42,6 +43,12 @@ namespace logging { std::string file_name(std::string file_path); std::string log_level_to_string(int level); +// not thread-safe +extern int64_t min_log_level; +inline void set_min_log_level(int64_t level) { + min_log_level = level; +} + class Log { private: std::stringstream log_content; @@ -64,7 +71,7 @@ class Log { template Log& operator<<(T const& value) { - if (log_level >= LOG_LEVEL) { + if (log_level >= min_log_level) { this->log_content << value; } return *this; diff --git a/sommelier.cc b/sommelier.cc index 6a75777..db794ba 100644 --- a/sommelier.cc +++ b/sommelier.cc @@ -3578,6 +3578,8 @@ static void sl_print_usage() { "options:\n" " -h, --help\t\t\tPrint this help\n" " -X\t\t\t\tEnable X11 forwarding\n" + " --log-level=LEVEL\t\tSet minimum log level to be processed\n" + "\t(allowed range: -1 to 3; lower is more verbose))\n" " --parent\t\t\tRun as parent and spawn child processes\n" " --socket=SOCKET\t\tName of socket to listen on\n" " --display=DISPLAY\t\tWayland display to connect to\n" @@ -3652,7 +3654,6 @@ static const char* sl_arg_value(const char* arg) { return s + 1; } -#ifdef QUIRKS_SUPPORT // attempts to both split "--argname=argval" and parse "argval" signed integer, // or exits the program with a fatal error if detecting any non-integer // characters or is out-of-bounds. @@ -3679,7 +3680,6 @@ static int64_t sl_arg_parse_int_checked(const char* arg) { } return res; } -#endif // QUIRKS_SUPPORT // Parse the list of accelerators that should be reserved by the // compositor. Format is "|MODIFIERS|KEYSYM", where MODIFIERS is a @@ -4084,6 +4084,9 @@ int real_main(int argc, char** argv) { if (strstr(arg, "--parent") == arg) { parent = 1; + } else if (strstr(arg, "--log-level") == arg) { + const int64_t log_level = sl_arg_parse_int_checked(arg); + logging::set_min_log_level(log_level); } else if (strstr(arg, "--socket") == arg) { socket_name = sl_arg_value(arg); } else if (strstr(arg, "--display") == arg) {