Skip to content
This repository was archived by the owner on Jan 30, 2025. It is now read-only.

Commit

Permalink
vm_tools: sommelier: set log-level from cmdline
Browse files Browse the repository at this point in the history
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=<int>, with the default set in the
old way with -DLOG_LEVEL=<int>, 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 <[email protected]>
Reviewed-by: Chloe Pelling <[email protected]>
Auto-Submit: Ryan Neph <[email protected]>
Reviewed-by: Arjun Srinivasan <[email protected]>
Commit-Queue: Ryan Neph <[email protected]>
  • Loading branch information
Ryan Neph authored and Chromeos LUCI committed May 9, 2024
1 parent d591683 commit f7c824f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
2 changes: 2 additions & 0 deletions sommelier-logging.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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('/');
Expand Down
9 changes: 8 additions & 1 deletion sommelier-logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#define VM_TOOLS_SOMMELIER_SOMMELIER_LOGGING_H_

#include <cstdarg>
#include <cstdint>
#include <cstring>
#include <iosfwd>
#include <iostream>
Expand Down Expand Up @@ -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;
Expand All @@ -64,7 +71,7 @@ class Log {

template <typename T>
Log& operator<<(T const& value) {
if (log_level >= LOG_LEVEL) {
if (log_level >= min_log_level) {
this->log_content << value;
}
return *this;
Expand Down
7 changes: 5 additions & 2 deletions sommelier.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit f7c824f

Please sign in to comment.