diff --git a/starboard/BUILD.gn b/starboard/BUILD.gn index 397aa9a92b3..f4bd47c0263 100644 --- a/starboard/BUILD.gn +++ b/starboard/BUILD.gn @@ -311,3 +311,5 @@ if (current_toolchain == starboard_toolchain) { } } } +# TODO: Create a starboard_unittest target here and fold any gtest_target_type +# in it, e.g. common:common_test. diff --git a/starboard/common/BUILD.gn b/starboard/common/BUILD.gn index 7d4c530de2c..7abb462a0ea 100644 --- a/starboard/common/BUILD.gn +++ b/starboard/common/BUILD.gn @@ -103,6 +103,7 @@ target(gtest_target_type, "common_test") { testonly = true sources = [ "fixed_no_free_allocator_test.cc", + "log_unittest.cc", "media_test.cc", "memory_test.cc", "player_test.cc", @@ -111,6 +112,7 @@ target(gtest_target_type, "common_test") { ] deps = [ ":common", + "//starboard:starboard_group", "//testing/gmock", "//testing/gtest", ] diff --git a/starboard/common/log.cc b/starboard/common/log.cc index e4cca971a9f..1c01faaef38 100644 --- a/starboard/common/log.cc +++ b/starboard/common/log.cc @@ -21,6 +21,7 @@ #include #include #include +#include #include #include "starboard/common/string.h" @@ -73,6 +74,21 @@ SbLogPriority StringToLogLevel(const std::string& log_level) { return SB_LOG_INFO; } +SbLogPriority ChromiumIntToStarboardLogLevel(const std::string& log_level) { + static const std::map kLogLevelToSbLogPriority = { + {0, SB_LOG_INFO}, + {1, SB_LOG_INFO}, + {2, SB_LOG_WARNING}, + {3, SB_LOG_ERROR}, + {4, SB_LOG_FATAL}}; + + const auto log_level_as_int = std::stoi(log_level); + if (kLogLevelToSbLogPriority.count(log_level_as_int) == 0) { + return SB_LOG_INFO; // Replicate StringToLogLevel() behaviour. + } + return kLogLevelToSbLogPriority.at(log_level_as_int); +} + void Break() { SbSystemBreakIntoDebugger(); } diff --git a/starboard/common/log.h b/starboard/common/log.h index 44bed6e42c4..dcd72dc277c 100644 --- a/starboard/common/log.h +++ b/starboard/common/log.h @@ -46,6 +46,7 @@ namespace logging { void SetMinLogLevel(SbLogPriority level); SbLogPriority GetMinLogLevel(); SbLogPriority StringToLogLevel(const std::string& log_level); +SbLogPriority ChromiumIntToStarboardLogLevel(const std::string& log_level); void Break(); // An object which will dumps the stack to the given ostream, without adding any diff --git a/starboard/common/log_unittest.cc b/starboard/common/log_unittest.cc new file mode 100644 index 00000000000..0ae639aebd1 --- /dev/null +++ b/starboard/common/log_unittest.cc @@ -0,0 +1,73 @@ +// Copyright 2024 The Chromium Authors +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "starboard/common/log.h" + +#include "testing/gtest/include/gtest/gtest.h" + +namespace starboard { +namespace logging { + +static const std::map kSbLogPriorityToString = { + {kSbLogPriorityUnknown, "unknown"}, + {kSbLogPriorityInfo, "info"}, + {kSbLogPriorityWarning, "warning"}, + {kSbLogPriorityError, "error"}, + {kSbLogPriorityFatal, "fatal"}}; + +struct TextAndSbPriority { + const std::string text; + const SbLogPriority sb_log_priority; +}; + +using MinLogLevelTranslationTest = testing::TestWithParam; + +TEST_P(MinLogLevelTranslationTest, ExpectedBehaviour) { + EXPECT_EQ(StringToLogLevel(GetParam().text), GetParam().sb_log_priority); +} + +INSTANTIATE_TEST_SUITE_P( + , + MinLogLevelTranslationTest, + testing::ValuesIn(std::vector{ + {"", SB_LOG_INFO}, + {"blabla", SB_LOG_INFO}, + {"info", SB_LOG_INFO}, + {"warning", SB_LOG_WARNING}, + {"error", SB_LOG_ERROR}, + {"fatal", SB_LOG_FATAL}, + }), + [](const testing::TestParamInfo& + info) { + return "from_" + info.param.text + "_to_" + + kSbLogPriorityToString.at(info.param.sb_log_priority); + }); + +using ChromiumLogLevelTranslationTest = + testing::TestWithParam; + +TEST_P(ChromiumLogLevelTranslationTest, ExpectedBehaviour) { + EXPECT_EQ(ChromiumIntToStarboardLogLevel(GetParam().text), + GetParam().sb_log_priority); +} + +INSTANTIATE_TEST_SUITE_P( + , + ChromiumLogLevelTranslationTest, + testing::ValuesIn(std::vector{ + {"0", SB_LOG_INFO}, + {"1", SB_LOG_INFO}, + {"2", SB_LOG_WARNING}, + {"3", SB_LOG_ERROR}, + {"4", SB_LOG_FATAL}, + {"5", SB_LOG_INFO}, + {"100", SB_LOG_INFO}, + }), + [](const testing::TestParamInfo& + info) { + return "from_" + info.param.text + "_to_" + + kSbLogPriorityToString.at(info.param.sb_log_priority); + }); +} // namespace logging +} // namespace starboard diff --git a/starboard/shared/starboard/application.cc b/starboard/shared/starboard/application.cc index 414d497ccec..15d71dca5e4 100644 --- a/starboard/shared/starboard/application.cc +++ b/starboard/shared/starboard/application.cc @@ -34,6 +34,10 @@ namespace { const char kPreloadSwitch[] = "preload"; const char kLinkSwitch[] = "link"; const char kMinLogLevel[] = "min_log_level"; +// Chromium's base/base_switches.h "--v". Positive numbers are equivalent to +// debug (0), info, warning, error, fatal. Note that Starboard has no debug; +// levels start at kSbLogPriorityInfo which is a 1. +const char kV[] = "v"; // Dispatches an event of |type| with |data| to the system event handler, // calling |destructor| on |data| when finished dispatching. Does all @@ -107,9 +111,14 @@ int Application::Run(CommandLine command_line) { } } + // kMinLogLevel should take priority over kV if both are defined. if (command_line_->HasSwitch(kMinLogLevel)) { ::starboard::logging::SetMinLogLevel(::starboard::logging::StringToLogLevel( command_line_->GetSwitchValue(kMinLogLevel))); + } else if (command_line_->HasSwitch(kV)) { + ::starboard::logging::SetMinLogLevel( + ::starboard::logging::ChromiumIntToStarboardLogLevel( + command_line_->GetSwitchValue(kV))); } else { #if SB_LOGGING_IS_OFFICIAL_BUILD ::starboard::logging::SetMinLogLevel(::starboard::logging::SB_LOG_FATAL);