Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Allow setting ACTS_LOG_FAILURE_THRESHOLD at runtime #1195

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 37 additions & 2 deletions Core/include/Acts/Utilities/Logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,41 @@ constexpr Level FAILURE_THRESHOLD =
Level::MAX;
#endif

/// Function which returns the failure threshold for log messages.
/// This can either be from a compilation option or from an environment
/// variable.
/// @return The log level failure threshold
inline Level getFailureThreshold() {
static const Level threshold{[]() -> Level {
Level level = FAILURE_THRESHOLD;
const char* envvar = std::getenv("ACTS_LOG_FAILURE_THRESHOLD");
if (envvar == nullptr) {
return level;
}
std::string slevel = envvar;
if (slevel == "VERBOSE") {
level = std::min(level, Level::VERBOSE);
} else if (slevel == "DEBUG") {
level = std::min(level, Level::DEBUG);
} else if (slevel == "INFO") {
level = std::min(level, Level::INFO);
} else if (slevel == "WARNING") {
level = std::min(level, Level::WARNING);
} else if (slevel == "ERROR") {
level = std::min(level, Level::ERROR);
} else if (slevel == "FATAL") {
level = std::min(level, Level::FATAL);
} else {
std::cerr << "ACTS_LOG_FAILURE_THRESHOLD environment variable is set to "
"unknown value: "
<< slevel << std::endl;
}
return level;
}()};

return threshold;
}

/// @brief abstract base class for printing debug output
///
/// Implementations of this interface need to define how and where to @a print
Expand Down Expand Up @@ -215,7 +250,7 @@ class DefaultFilterPolicy final : public OutputFilterPolicy {
///
/// @param [in] lvl threshold debug level
explicit DefaultFilterPolicy(const Level& lvl) : m_level(lvl) {
if (lvl > FAILURE_THRESHOLD) {
if (lvl > getFailureThreshold()) {
throw std::runtime_error(
"Requested debug level is incompatible with "
"the ACTS_LOG_FAILURE_THRESHOLD configuration");
Expand Down Expand Up @@ -428,7 +463,7 @@ class DefaultPrintPolicy final : public OutputPrintPolicy {
/// @param [in] input text of debug message
void flush(const Level& lvl, const std::string& input) final {
(*m_out) << input << std::endl;
if (lvl >= FAILURE_THRESHOLD) {
if (lvl >= getFailureThreshold()) {
throw std::runtime_error(
"Previous debug message exceeds the "
"ACTS_LOG_FAILURE_THRESHOLD configuration, bailing out");
Expand Down
4 changes: 2 additions & 2 deletions Tests/UnitTests/Core/Utilities/LoggerTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ void debug_level_test(const char* output_file, Logging::Level lvl) {

// If fail-on-error is enabled, then the logger will not, and should not,
// tolerate being set up with a coarser debug level.
if (lvl > Logging::FAILURE_THRESHOLD) {
if (lvl > Logging::getFailureThreshold()) {
BOOST_CHECK_THROW(detail::create_logger("TestLogger", &logfile, lvl),
std::runtime_error);
return;
Expand All @@ -62,7 +62,7 @@ void debug_level_test(const char* output_file, Logging::Level lvl) {

// Test logging at a certain debug level
auto test_logging = [](auto&& test_operation, Logging::Level test_lvl) {
if (test_lvl >= Logging::FAILURE_THRESHOLD) {
if (test_lvl >= Logging::getFailureThreshold()) {
BOOST_CHECK_THROW(test_operation(), std::runtime_error);
} else {
test_operation();
Expand Down
Loading