Skip to content

Commit

Permalink
Merge pull request #2055 from hzeller/20231219-ready-for-absl-vlog
Browse files Browse the repository at this point in the history
Get ready for ABSL providing a VLOG() implementation.
  • Loading branch information
hzeller authored Dec 19, 2023
2 parents 13a8e65 + 8592f38 commit ca0b583
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
13 changes: 8 additions & 5 deletions common/util/init_command_line.cc
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,15 @@ std::vector<absl::string_view> InitCommandLine(
static_cast<absl::LogSeverityAtLeast>(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();
Expand Down
16 changes: 11 additions & 5 deletions common/util/logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand All @@ -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)
Expand Down

0 comments on commit ca0b583

Please sign in to comment.