From 887a818aa7d79ac0e08f3531e493dac1f980674d Mon Sep 17 00:00:00 2001 From: Scott McKay Date: Thu, 27 Jun 2024 12:51:13 +1000 Subject: [PATCH] Check for unit test log severity override earlier (#21177) ### Description 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 Make it possible to get verbose output to stdout when the DML EP is enabled. --- onnxruntime/test/unittest_main/test_main.cc | 28 ++++++++++++--------- 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/onnxruntime/test/unittest_main/test_main.cc b/onnxruntime/test/unittest_main/test_main.cc index b7c3b38538421..1d89272680e47 100644 --- a/onnxruntime/test/unittest_main/test_main.cc +++ b/onnxruntime/test/unittest_main/test_main.cc @@ -26,9 +26,24 @@ #include "test/test_environment.h" std::unique_ptr 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(kLogLevelEnvironmentVariableName); + log_level_override.has_value()) { + *log_level_override = std::clamp(*log_level_override, + static_cast(ORT_LOGGING_LEVEL_VERBOSE), + static_cast(ORT_LOGGING_LEVEL_FATAL)); + std::cout << "Setting log level to " << *log_level_override << "\n"; + log_level = static_cast(*log_level_override); + } + + ort_env.reset(new Ort::Env(&tpo, log_level, "Default")); } #ifdef USE_TENSORRT @@ -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(kLogLevelEnvironmentVariableName); - log_level.has_value()) { - *log_level = std::clamp(*log_level, - static_cast(ORT_LOGGING_LEVEL_VERBOSE), - static_cast(ORT_LOGGING_LEVEL_FATAL)); - std::cout << "Setting log level to " << *log_level << "\n"; - ort_env->UpdateEnvWithCustomLogLevel(static_cast(*log_level)); - } - status = RUN_ALL_TESTS(); } ORT_CATCH(const std::exception& ex) {