Skip to content

Commit

Permalink
Check for unit test log severity override earlier (#21177)
Browse files Browse the repository at this point in the history
### Description
<!-- Describe your changes. -->
Setting the log level after environment creation is too late in some
cases.

If the DML EP is enabled, it will create a composite sink with the
original logger using the creation time log severity, as well as
additional ETW sink. As it saves the current severity levels for each
sink inside the composite sink that prevents being able to get verbose
log output to stdout even if you set that at the session level.

I don't know enough about the setup that combines ETW with the original
sink to say whether we should also be updating the severity of
individual sinks in the combined sink, so this change is limited to
making the unit tests behave in the expected manner when the default log
severity is set in the background and not directly controlled.


### Motivation and Context
<!-- - Why is this change required? What problem does it solve?
- If it fixes an open issue, please link to the issue here. -->
Make it possible to get verbose output to stdout when the DML EP is
enabled.
  • Loading branch information
skottmckay authored Jun 27, 2024
1 parent 3c0b407 commit 887a818
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions onnxruntime/test/unittest_main/test_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,24 @@
#include "test/test_environment.h"

std::unique_ptr<Ort::Env> ort_env;

// ortenv_setup is used by /onnxruntime/test/xctest/xcgtest.mm so can't be file local
void ortenv_setup() {
OrtThreadingOptions tpo;
ort_env.reset(new Ort::Env(&tpo, ORT_LOGGING_LEVEL_WARNING, "Default"));

// allow verbose logging to be enabled by setting this environment variable to a numeric log level
constexpr auto kLogLevelEnvironmentVariableName = "ORT_UNIT_TEST_MAIN_LOG_LEVEL";
OrtLoggingLevel log_level = ORT_LOGGING_LEVEL_WARNING;
if (auto log_level_override = onnxruntime::ParseEnvironmentVariable<int>(kLogLevelEnvironmentVariableName);
log_level_override.has_value()) {
*log_level_override = std::clamp(*log_level_override,
static_cast<int>(ORT_LOGGING_LEVEL_VERBOSE),
static_cast<int>(ORT_LOGGING_LEVEL_FATAL));
std::cout << "Setting log level to " << *log_level_override << "\n";
log_level = static_cast<OrtLoggingLevel>(*log_level_override);
}

ort_env.reset(new Ort::Env(&tpo, log_level, "Default"));
}

#ifdef USE_TENSORRT
Expand Down Expand Up @@ -76,17 +91,6 @@ int TEST_MAIN(int argc, char** argv) {
ortenv_setup();
::testing::InitGoogleTest(&argc, argv);

// allow verbose logging to be enabled by setting this environment variable to a numeric log level
constexpr auto kLogLevelEnvironmentVariableName = "ORT_UNIT_TEST_MAIN_LOG_LEVEL";
if (auto log_level = onnxruntime::ParseEnvironmentVariable<int>(kLogLevelEnvironmentVariableName);
log_level.has_value()) {
*log_level = std::clamp(*log_level,
static_cast<int>(ORT_LOGGING_LEVEL_VERBOSE),
static_cast<int>(ORT_LOGGING_LEVEL_FATAL));
std::cout << "Setting log level to " << *log_level << "\n";
ort_env->UpdateEnvWithCustomLogLevel(static_cast<OrtLoggingLevel>(*log_level));
}

status = RUN_ALL_TESTS();
}
ORT_CATCH(const std::exception& ex) {
Expand Down

0 comments on commit 887a818

Please sign in to comment.