Skip to content

Commit

Permalink
Move absl::Set[Global]VLogLevel() to //absl/log/globals.h
Browse files Browse the repository at this point in the history
For consistency, all global logging configurations lives in
//absl/log/globals.h

PiperOrigin-RevId: 598194040
Change-Id: I815b7d07f8fe06c70cef83bdf825c2f7ca504a2b
  • Loading branch information
derekmauro authored and copybara-github committed Jan 13, 2024
1 parent 27134f2 commit b2dd3a5
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 23 deletions.
2 changes: 2 additions & 0 deletions absl/log/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ cc_library(
"//absl/base:log_severity",
"//absl/base:raw_logging_internal",
"//absl/hash",
"//absl/log/internal:vlog_config",
"//absl/strings",
],
)
Expand Down Expand Up @@ -277,6 +278,7 @@ cc_test(
linkopts = ABSL_DEFAULT_LINKOPTS,
deps = [
":flags",
":globals",
":log",
":scoped_mock_log",
":vlog_is_on",
Expand Down
4 changes: 3 additions & 1 deletion absl/log/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,7 @@ absl_cc_library(
absl::log_severity
absl::raw_logging_internal
absl::strings
absl::vlog_config_internal
)

absl_cc_library(
Expand Down Expand Up @@ -737,8 +738,9 @@ absl_cc_test(
LINKOPTS
${ABSL_DEFAULT_LINKOPTS}
DEPS
absl::log_flags
absl::log
absl::log_flags
absl::log_globals
absl::scoped_mock_log
absl::vlog_is_on
absl::log_severity
Expand Down
22 changes: 0 additions & 22 deletions absl/log/absl_vlog_is_on.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,26 +90,4 @@
}() \
->IsEnabled(verbose_level))

namespace absl {
ABSL_NAMESPACE_BEGIN

// Sets the global `(ABSL_)VLOG(_IS_ON)` level to `log_level`. This level is
// applied to any sites whose filename doesn't match any `module_pattern`.
// Returns the prior value.
inline int SetGlobalVLogLevel(int log_level) {
return absl::log_internal::UpdateGlobalVLogLevel(log_level);
}

// Sets `(ABSL_)VLOG(_IS_ON)` level for `module_pattern` to `log_level`.
// This lets us dynamically control what is normally set by the --vmodule flag.
// Returns the level that previously applied to module_pattern.
// Calling this with `log_level` of kUseFlag will have all sites for that
// pattern use the value of --v.
inline int SetVLogLevel(absl::string_view module_pattern, int log_level) {
return absl::log_internal::PrependVModule(module_pattern, log_level);
}

ABSL_NAMESPACE_END
} // namespace absl

#endif // ABSL_LOG_ABSL_VLOG_IS_ON_H_
23 changes: 23 additions & 0 deletions absl/log/globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "absl/base/attributes.h"
#include "absl/base/config.h"
#include "absl/base/log_severity.h"
#include "absl/log/internal/vlog_config.h"
#include "absl/strings/string_view.h"

namespace absl {
Expand Down Expand Up @@ -152,6 +153,28 @@ ABSL_MUST_USE_RESULT bool ShouldPrependLogPrefix();
// This function is async-signal-safe.
void EnableLogPrefix(bool on_off);

//------------------------------------------------------------------------------
// Set Global VLOG Level
//------------------------------------------------------------------------------
//
// Sets the global `(ABSL_)VLOG(_IS_ON)` level to `log_level`. This level is
// applied to any sites whose filename doesn't match any `module_pattern`.
// Returns the prior value.
inline int SetGlobalVLogLevel(int log_level) {
return absl::log_internal::UpdateGlobalVLogLevel(log_level);
}

//------------------------------------------------------------------------------
// Set VLOG Level
//------------------------------------------------------------------------------
//
// Sets `(ABSL_)VLOG(_IS_ON)` level for `module_pattern` to `log_level`. This
// allows programmatic control of what is normally set by the --vmodule flag.
// Returns the level that previously applied to `module_pattern`.
inline int SetVLogLevel(absl::string_view module_pattern, int log_level) {
return absl::log_internal::PrependVModule(module_pattern, log_level);
}

//------------------------------------------------------------------------------
// Configure Android Native Log Tag
//------------------------------------------------------------------------------
Expand Down
14 changes: 14 additions & 0 deletions absl/log/globals_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,20 @@ TEST(TestGlobals, LogPrefix) {
EXPECT_TRUE(absl::ShouldPrependLogPrefix());
}

TEST(TestGlobals, SetGlobalVLogLevel) {
EXPECT_EQ(absl::SetGlobalVLogLevel(42), 0);
EXPECT_EQ(absl::SetGlobalVLogLevel(1337), 42);
// Restore the value since it affects the default unset module value for
// `SetVLogLevel()`.
EXPECT_EQ(absl::SetGlobalVLogLevel(0), 1337);
}

TEST(TestGlobals, SetVLogLevel) {
EXPECT_EQ(absl::SetVLogLevel("setvloglevel", 42), 0);
EXPECT_EQ(absl::SetVLogLevel("setvloglevel", 1337), 42);
EXPECT_EQ(absl::SetVLogLevel("othersetvloglevel", 50), 0);
}

TEST(TestGlobals, AndroidLogTag) {
// Verify invalid tags result in a check failure.
EXPECT_DEATH_IF_SUPPORTED(absl::SetAndroidNativeTag(nullptr), ".*");
Expand Down
1 change: 1 addition & 0 deletions absl/log/vlog_is_on_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "absl/base/log_severity.h"
#include "absl/flags/flag.h"
#include "absl/log/flags.h"
#include "absl/log/globals.h"
#include "absl/log/log.h"
#include "absl/log/scoped_mock_log.h"
#include "absl/types/optional.h"
Expand Down

0 comments on commit b2dd3a5

Please sign in to comment.