Commit 8588e7d1 by Derek Mauro Committed by Copybara-Service

Avoid leaking an unprefixed VLOG_IS_ON transitively though absl_log.h

absl_log.h is supposed to be a mirror of log.h that only uses ABSL_
prefixed macros. This change adds ABSL_VLOG_IS_ON to absl_vlog_is_on.h
and uses it in the implementation of VLOG. VLOG_IS_ON is then only
exported though vlog_is_on.h.

PiperOrigin-RevId: 588435577
Change-Id: Ifa9193afa0b782194b64447f4fedfaf24b72c95a
parent 9b83dc9f
......@@ -150,6 +150,7 @@ set(ABSL_INTERNAL_DLL_FILES
"hash/internal/low_level_hash.cc"
"log/absl_check.h"
"log/absl_log.h"
"log/absl_vlog_is_on.h"
"log/check.h"
"log/die_if_null.cc"
"log/die_if_null.h"
......
......@@ -50,7 +50,6 @@ cc_library(
copts = ABSL_DEFAULT_COPTS,
linkopts = ABSL_DEFAULT_LINKOPTS,
deps = [
":vlog_is_on",
"//absl/log/internal:log_impl",
],
)
......@@ -239,8 +238,8 @@ cc_library(
)
cc_library(
name = "vlog_is_on",
hdrs = ["vlog_is_on.h"],
name = "absl_vlog_is_on",
hdrs = ["absl_vlog_is_on.h"],
copts = ABSL_DEFAULT_COPTS,
linkopts = ABSL_DEFAULT_LINKOPTS,
visibility = [
......@@ -254,6 +253,19 @@ cc_library(
],
)
cc_library(
name = "vlog_is_on",
hdrs = ["vlog_is_on.h"],
copts = ABSL_DEFAULT_COPTS,
linkopts = ABSL_DEFAULT_LINKOPTS,
visibility = [
"//absl/log:__subpackages__",
],
deps = [
":absl_vlog_is_on",
],
)
# TODO(b/200695798): run this in TAP projects with -DABSL_MAX_VLOG_VERBOSITY={-100,100}
cc_test(
name = "vlog_is_on_test",
......
......@@ -157,7 +157,7 @@ absl_cc_library(
absl::log_internal_conditions
absl::log_internal_message
absl::log_internal_strip
absl::vlog_is_on
absl::absl_vlog_is_on
)
absl_cc_library(
......@@ -419,7 +419,6 @@ absl_cc_library(
${ABSL_DEFAULT_LINKOPTS}
DEPS
absl::log_internal_log_impl
absl::vlog_is_on
PUBLIC
)
......@@ -698,16 +697,15 @@ absl_cc_library(
absl::optional
)
absl_cc_library(
NAME
vlog_is_on
absl_vlog_is_on
COPTS
${ABSL_DEFAULT_COPTS}
LINKOPTS
${ABSL_DEFAULT_LINKOPTS}
HDRS
"vlog_is_on.h"
"absl_vlog_is_on.h"
DEPS
absl::vlog_config_internal
absl::config
......@@ -715,6 +713,19 @@ absl_cc_library(
absl::strings
)
absl_cc_library(
NAME
vlog_is_on
COPTS
${ABSL_DEFAULT_COPTS}
LINKOPTS
${ABSL_DEFAULT_LINKOPTS}
HDRS
"vlog_is_on.h"
DEPS
absl::absl_vlog_is_on
)
absl_cc_test(
NAME
vlog_is_on_test
......
// Copyright 2022 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// -----------------------------------------------------------------------------
// File: log/absl_vlog_is_on.h
// -----------------------------------------------------------------------------
//
// This header defines the `ABSL_VLOG_IS_ON()` macro that controls the
// variable-verbosity conditional logging.
//
// It's used by `VLOG` in log.h, or it can also be used directly like this:
//
// if (ABSL_VLOG_IS_ON(2)) {
// foo_server.RecomputeStatisticsExpensive();
// LOG(INFO) << foo_server.LastStatisticsAsString();
// }
//
// Each source file has an effective verbosity level that's a non-negative
// integer computed from the `--vmodule` and `--v` flags.
// `ABSL_VLOG_IS_ON(n)` is true, and `VLOG(n)` logs, if that effective verbosity
// level is greater than or equal to `n`.
//
// `--vmodule` takes a comma-delimited list of key=value pairs. Each key is a
// pattern matched against filenames, and the values give the effective severity
// level applied to matching files. '?' and '*' characters in patterns are
// interpreted as single-character and zero-or-more-character wildcards.
// Patterns including a slash character are matched against full pathnames,
// while those without are matched against basenames only. One suffix (i.e. the
// last . and everything after it) is stripped from each filename prior to
// matching, as is the special suffix "-inl".
//
// Files are matched against globs in `--vmodule` in order, and the first match
// determines the verbosity level.
//
// Files which do not match any pattern in `--vmodule` use the value of `--v` as
// their effective verbosity level. The default is 0.
//
// SetVLOGLevel helper function is provided to do limited dynamic control over
// V-logging by appending to `--vmodule`. Because these go at the beginning of
// the list, they take priority over any globs previously added.
//
// Resetting --vmodule will override all previous modifications to `--vmodule`,
// including via SetVLOGLevel.
#ifndef ABSL_LOG_ABSL_VLOG_IS_ON_H_
#define ABSL_LOG_ABSL_VLOG_IS_ON_H_
#include "absl/base/attributes.h"
#include "absl/base/config.h"
#include "absl/log/internal/vlog_config.h" // IWYU pragma: export
#include "absl/strings/string_view.h"
// IWYU pragma: private, include "absl/log/log.h"
// This is expanded at the callsite to allow the compiler to optimize
// always-false cases out of the build.
// An ABSL_MAX_VLOG_VERBOSITY of 2 means that VLOG(3) and above should never
// log.
#ifdef ABSL_MAX_VLOG_VERBOSITY
#define ABSL_LOG_INTERNAL_MAX_LOG_VERBOSITY_CHECK(x) \
((x) <= ABSL_MAX_VLOG_VERBOSITY)&&
#else
#define ABSL_LOG_INTERNAL_MAX_LOG_VERBOSITY_CHECK(x)
#endif
// Each ABSL_VLOG_IS_ON call site gets its own VLogSite that registers with the
// global linked list of sites to asynchronously update its verbosity level on
// changes to --v or --vmodule. The verbosity can also be set by manually
// calling SetVLOGLevel.
//
// ABSL_VLOG_IS_ON is not async signal safe, but it is guaranteed not to
// allocate new memory.
#define ABSL_VLOG_IS_ON(verbose_level) \
(ABSL_LOG_INTERNAL_MAX_LOG_VERBOSITY_CHECK(verbose_level)[]() \
->::absl::log_internal::VLogSite * \
{ \
ABSL_CONST_INIT static ::absl::log_internal::VLogSite site(__FILE__); \
return &site; \
}() \
->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_
......@@ -153,7 +153,7 @@ cc_library(
":conditions",
":log_message",
":strip",
"//absl/log:vlog_is_on",
"//absl/log:absl_vlog_is_on",
],
)
......
......@@ -15,10 +15,10 @@
#ifndef ABSL_LOG_INTERNAL_LOG_IMPL_H_
#define ABSL_LOG_INTERNAL_LOG_IMPL_H_
#include "absl/log/absl_vlog_is_on.h"
#include "absl/log/internal/conditions.h"
#include "absl/log/internal/log_message.h"
#include "absl/log/internal/strip.h"
#include "absl/log/vlog_is_on.h"
// ABSL_LOG()
#define ABSL_LOG_INTERNAL_LOG_IMPL(severity) \
......@@ -50,7 +50,7 @@
case 0: \
default: \
ABSL_LOG_INTERNAL_LOG_IF_IMPL( \
_INFO, VLOG_IS_ON(absl_logging_internal_verbose_level)) \
_INFO, ABSL_VLOG_IS_ON(absl_logging_internal_verbose_level)) \
.WithVerbosity(absl_logging_internal_verbose_level)
#ifndef NDEBUG
......@@ -59,7 +59,7 @@
case 0: \
default: \
ABSL_LOG_INTERNAL_LOG_IF_IMPL( \
_INFO, VLOG_IS_ON(absl_logging_internal_verbose_level)) \
_INFO, ABSL_VLOG_IS_ON(absl_logging_internal_verbose_level)) \
.WithVerbosity(absl_logging_internal_verbose_level)
#else
#define ABSL_LOG_INTERNAL_DVLOG_IMPL(verbose_level) \
......@@ -67,7 +67,7 @@
case 0: \
default: \
ABSL_LOG_INTERNAL_LOG_IF_IMPL( \
_INFO, false && VLOG_IS_ON(absl_logging_internal_verbose_level)) \
_INFO, false && ABSL_VLOG_IS_ON(absl_logging_internal_verbose_level)) \
.WithVerbosity(absl_logging_internal_verbose_level)
#endif
......@@ -169,7 +169,7 @@
case 0: \
default: \
ABSL_LOG_INTERNAL_CONDITION_INFO( \
STATEFUL, VLOG_IS_ON(absl_logging_internal_verbose_level)) \
STATEFUL, ABSL_VLOG_IS_ON(absl_logging_internal_verbose_level)) \
(EveryN, n) ABSL_LOGGING_INTERNAL_LOG_INFO.InternalStream().WithVerbosity( \
absl_logging_internal_verbose_level)
......@@ -178,7 +178,7 @@
case 0: \
default: \
ABSL_LOG_INTERNAL_CONDITION_INFO( \
STATEFUL, VLOG_IS_ON(absl_logging_internal_verbose_level)) \
STATEFUL, ABSL_VLOG_IS_ON(absl_logging_internal_verbose_level)) \
(FirstN, n) ABSL_LOGGING_INTERNAL_LOG_INFO.InternalStream().WithVerbosity( \
absl_logging_internal_verbose_level)
......@@ -187,7 +187,7 @@
case 0: \
default: \
ABSL_LOG_INTERNAL_CONDITION_INFO( \
STATEFUL, VLOG_IS_ON(absl_logging_internal_verbose_level)) \
STATEFUL, ABSL_VLOG_IS_ON(absl_logging_internal_verbose_level)) \
(EveryPow2) ABSL_LOGGING_INTERNAL_LOG_INFO.InternalStream().WithVerbosity( \
absl_logging_internal_verbose_level)
......@@ -196,7 +196,7 @@
case 0: \
default: \
ABSL_LOG_INTERNAL_CONDITION_INFO( \
STATEFUL, VLOG_IS_ON(absl_logging_internal_verbose_level)) \
STATEFUL, ABSL_VLOG_IS_ON(absl_logging_internal_verbose_level)) \
(EveryNSec, n_seconds) ABSL_LOGGING_INTERNAL_LOG_INFO.InternalStream() \
.WithVerbosity(absl_logging_internal_verbose_level)
......
......@@ -56,23 +56,7 @@
#ifndef ABSL_LOG_VLOG_IS_ON_H_
#define ABSL_LOG_VLOG_IS_ON_H_
#include "absl/base/attributes.h"
#include "absl/base/config.h"
#include "absl/log/internal/vlog_config.h" // IWYU pragma: export
#include "absl/strings/string_view.h"
// IWYU pragma: private, include "absl/log/log.h"
// This is expanded at the callsite to allow the compiler to optimize
// always-false cases out of the build.
// An ABSL_MAX_VLOG_VERBOSITY of 2 means that VLOG(3) and above should never
// log.
#ifdef ABSL_MAX_VLOG_VERBOSITY
#define ABSL_LOG_INTERNAL_MAX_LOG_VERBOSITY_CHECK(x) \
((x) <= ABSL_MAX_VLOG_VERBOSITY)&&
#else
#define ABSL_LOG_INTERNAL_MAX_LOG_VERBOSITY_CHECK(x)
#endif
#include "absl/log/absl_vlog_is_on.h" // IWYU pragma: export
// Each VLOG_IS_ON call site gets its own VLogSite that registers with the
// global linked list of sites to asynchronously update its verbosity level on
......@@ -81,35 +65,6 @@
//
// VLOG_IS_ON is not async signal safe, but it is guaranteed not to allocate
// new memory.
#define VLOG_IS_ON(verbose_level) \
(ABSL_LOG_INTERNAL_MAX_LOG_VERBOSITY_CHECK(verbose_level)[]() \
->::absl::log_internal::VLogSite * \
{ \
ABSL_CONST_INIT static ::absl::log_internal::VLogSite site(__FILE__); \
return &site; \
}() \
->IsEnabled(verbose_level))
namespace absl {
ABSL_NAMESPACE_BEGIN
// Sets the global `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 `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
#define VLOG_IS_ON(verbose_level) ABSL_VLOG_IS_ON(verbose_level)
#endif // ABSL_LOG_VLOG_IS_ON_H_
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment