From 8592f387d2f623affea35ad62cf171185ea9ff6b Mon Sep 17 00:00:00 2001 From: Henner Zeller Date: Tue, 19 Dec 2023 02:55:59 -0800 Subject: [PATCH] Get ready for ABSL providing a VLOG() implementation. In the unreleased head of absl, a [VLOG()](https://github.com/abseil/abseil-cpp/blob/bae260199fffe782d5a5414bb80cfe49abb1436f/absl/log/absl_vlog_is_on.h) implementation is available now. Make sure we can compile with the current and future absl. --- common/util/init_command_line.cc | 13 ++++++++----- common/util/logging.h | 16 +++++++++++----- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/common/util/init_command_line.cc b/common/util/init_command_line.cc index 69852114d..a73a5abb1 100644 --- a/common/util/init_command_line.cc +++ b/common/util/init_command_line.cc @@ -82,12 +82,15 @@ std::vector InitCommandLine( static_cast(std::clamp(log_level, 0, 3))); } - // Until vlog is provided in absl, we use our own global variable, defined - // in logging.cc + // Set vlog-level with environment variable. The definition of + // VERIBLE_INTERNAL_SET_VLOGLEVEL() might be different depending on if we + // have an absl implementation or not. const char *const vlog_level_env = getenv("VERIBLE_VLOG_DETAIL"); - if (!vlog_level_env || - !absl::SimpleAtoi(vlog_level_env, &verible::global_vlog_level_)) { - global_vlog_level_ = 0; + int vlog_level = 0; + if (vlog_level_env && absl::SimpleAtoi(vlog_level_env, &vlog_level)) { + VERIBLE_INTERNAL_SET_VLOGLEVEL(vlog_level); + } else { + VERIBLE_INTERNAL_SET_VLOGLEVEL(0); } absl::InitializeLog(); diff --git a/common/util/logging.h b/common/util/logging.h index 238fa48dc..69ba19d53 100644 --- a/common/util/logging.h +++ b/common/util/logging.h @@ -28,14 +28,21 @@ #include "absl/log/die_if_null.h" #include "absl/log/log.h" -// There is no vlog yet, so very simple work-around here until it shows up -#ifndef VLOG +// The VLOG level is set from the VERILOG_VLOG_DETAIL environment variable in +// InitCommandLine(), which calls the VERIBLE_INTERNAL_SET_VLOGLEVEL() macro. +#if defined(VLOG) && defined(ABSL_VLOG_IS_ON) +// If there is an ABSL implementation of VLOG, use that. +#define VERIBLE_INTERNAL_SET_VLOGLEVEL(level) absl::SetGlobalVLogLevel(level) +#else +// ... otherwise provide a minimal local implementation. namespace verible { -// Used in the VLOG macro to check logging condition. This value is set in -// InitCommandLine() from VERIBLE_VLOG_DETAIL environment variable. +// Used in the VLOG macro to check logging condition. extern int global_vlog_level_; } // namespace verible +#define VERIBLE_INTERNAL_SET_VLOGLEVEL(level) \ + verible::global_vlog_level_ = level + #define VLOG_IS_ON(x) (::verible::global_vlog_level_ >= (x)) #define VLOG(x) LOG_IF(INFO, VLOG_IS_ON(x)) @@ -44,7 +51,6 @@ extern int global_vlog_level_; #else #define DVLOG(x) VLOG(x) #endif // NDEBUG - #endif // VLOG #define CHECK_NOTNULL(p) (void)ABSL_DIE_IF_NULL(p)