Skip to content

Commit

Permalink
Merge pull request #2083 from hzeller/20240129-break-out-env-var
Browse files Browse the repository at this point in the history
Break out setting log levels by environment variables.
  • Loading branch information
hzeller authored Jan 29, 2024
2 parents 82ac518 + 3ef85bf commit 31182be
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
25 changes: 14 additions & 11 deletions common/util/init_command_line.cc
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,7 @@ static std::string GetBuildVersion() {
return result;
}

// We might want to have argc edited in the future, hence non-const param.
std::vector<absl::string_view> InitCommandLine(
absl::string_view usage,
int *argc, // NOLINT(readability-non-const-parameter)
char ***argv) {
absl::InitializeSymbolizer(*argv[0]);
absl::FlagsUsageConfig usage_config;
usage_config.version_string = GetBuildVersion;
absl::SetFlagsUsageConfig(usage_config);
absl::SetProgramUsageMessage(usage); // copies usage string

void SetLoggingLevelsFromEnvironment() {
// To avoid confusing and rarely used flags, we just enable logging via
// environment variables.
const char *const stderr_log_level = getenv("VERIBLE_LOGTHRESHOLD");
Expand All @@ -92,7 +82,20 @@ std::vector<absl::string_view> InitCommandLine(
} else {
VERIBLE_INTERNAL_SET_VLOGLEVEL(0);
}
}

// We might want to have argc edited in the future, hence non-const param.
std::vector<absl::string_view> InitCommandLine(
absl::string_view usage,
int *argc, // NOLINT(readability-non-const-parameter)
char ***argv) {
absl::InitializeSymbolizer(*argv[0]);
absl::FlagsUsageConfig usage_config;
usage_config.version_string = GetBuildVersion;
absl::SetFlagsUsageConfig(usage_config);
absl::SetProgramUsageMessage(usage); // copies usage string

SetLoggingLevelsFromEnvironment();
absl::InitializeLog();

// Print stacktrace on issue, but not if --config=asan
Expand Down
6 changes: 6 additions & 0 deletions common/util/init_command_line.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ namespace verible {
// Get a one-line build version string that based on the repository version.
std::string GetRepositoryVersion();

// Set logging thresholds from environment variables.
// * VERIBLE_LOGTHRESHOLD corresponds to absl::LogSeverityAtLeast()
// * VERIBLE_VLOG_DETAIL corresponds to absl::SetGlobalVLogLevel()
// Called in InitCommandLine(), so usually not needed to be called separately.
void SetLoggingLevelsFromEnvironment();

// Initializes command-line tool, including parsing flags.
// The recognized flags are initialized and their text removed from the
// input command line, returning the remaining positional parameters.
Expand Down

0 comments on commit 31182be

Please sign in to comment.