Skip to content

Commit

Permalink
Starboard: support --v for verbosity (#4583)
Browse files Browse the repository at this point in the history
Starboard builds and in particular the nplb integration test support a command line flag
--min_log_level=(info|warning|error|fatal). Chromium binaries prefer in general --v
(or -v) for this purpose.

This CL adds logic to handle --v; to preserve previous behaviour, if and when both
--min_log_level and --v are present, the former takes precedence.

This CL also add unit tests for the translation between flag arguments and log level.
First instance of starboard_unittests !

Bug: 378879686
Bug: 384819454
  • Loading branch information
yell0wd0g authored Dec 18, 2024
1 parent 61d7952 commit 0cd7c8f
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 0 deletions.
2 changes: 2 additions & 0 deletions starboard/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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.
2 changes: 2 additions & 0 deletions starboard/common/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -111,6 +112,7 @@ target(gtest_target_type, "common_test") {
]
deps = [
":common",
"//starboard:starboard_group",
"//testing/gmock",
"//testing/gtest",
]
Expand Down
16 changes: 16 additions & 0 deletions starboard/common/log.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <algorithm>
#include <cstring>
#include <iomanip>
#include <map>
#include <sstream>

#include "starboard/common/string.h"
Expand Down Expand Up @@ -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<int, SbLogPriority> 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();
}
Expand Down
1 change: 1 addition & 0 deletions starboard/common/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
73 changes: 73 additions & 0 deletions starboard/common/log_unittest.cc
Original file line number Diff line number Diff line change
@@ -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<SbLogPriority, std::string> 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<TextAndSbPriority>;

TEST_P(MinLogLevelTranslationTest, ExpectedBehaviour) {
EXPECT_EQ(StringToLogLevel(GetParam().text), GetParam().sb_log_priority);
}

INSTANTIATE_TEST_SUITE_P(
,
MinLogLevelTranslationTest,
testing::ValuesIn(std::vector<struct TextAndSbPriority>{
{"", 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<MinLogLevelTranslationTest::ParamType>&
info) {
return "from_" + info.param.text + "_to_" +
kSbLogPriorityToString.at(info.param.sb_log_priority);
});

using ChromiumLogLevelTranslationTest =
testing::TestWithParam<TextAndSbPriority>;

TEST_P(ChromiumLogLevelTranslationTest, ExpectedBehaviour) {
EXPECT_EQ(ChromiumIntToStarboardLogLevel(GetParam().text),
GetParam().sb_log_priority);
}

INSTANTIATE_TEST_SUITE_P(
,
ChromiumLogLevelTranslationTest,
testing::ValuesIn(std::vector<struct TextAndSbPriority>{
{"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<ChromiumLogLevelTranslationTest::ParamType>&
info) {
return "from_" + info.param.text + "_to_" +
kSbLogPriorityToString.at(info.param.sb_log_priority);
});
} // namespace logging
} // namespace starboard
9 changes: 9 additions & 0 deletions starboard/shared/starboard/application.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 0cd7c8f

Please sign in to comment.