Guard `LogSeverity` streaming by `ABSL_INTERNAL_HAVE_STD_OSTREAM`. Omit `absl::LogSeverity` `operator<<` overloads when libc++ is configured without localization support (`std::ostream`), and add `log_severity_no_ostream_test`. PiperOrigin-RevId: 1013074983
diff --git a/absl/base/BUILD.bazel b/absl/base/BUILD.bazel index 66f173e..665c920 100644 --- a/absl/base/BUILD.bazel +++ b/absl/base/BUILD.bazel
@@ -907,6 +907,34 @@ ) cc_test( + name = "log_severity_no_ostream_test", + size = "small", + srcs = [ + "log_severity.cc", + "log_severity.h", + "log_severity_no_ostream_test.cc", + ], + copts = ABSL_TEST_COPTS, + linkopts = ABSL_DEFAULT_LINKOPTS, + # Predefine standard library <ostream> include guards so hosted #include + # <ostream> is empty (matching _LIBCPP_HAS_LOCALIZATION == 0) and unguarded + # std::ostream references fail at compile time. + local_defines = [ + "_GLIBCXX_OSTREAM", + "_LIBCPP_HAS_NO_LOCALIZATION", + "_LIBCPP_OSTREAM", + "_OSTREAM_", + ], + tags = [ + "no_test_wasm", + ], + deps = [ + ":config", + ":core_headers", + ], +) + +cc_test( name = "log_severity_test", size = "small", srcs = ["log_severity_test.cc"],
diff --git a/absl/base/CMakeLists.txt b/absl/base/CMakeLists.txt index dc72d45..37a9651 100644 --- a/absl/base/CMakeLists.txt +++ b/absl/base/CMakeLists.txt
@@ -733,6 +733,30 @@ absl_cc_test( NAME + log_severity_no_ostream_test + SRCS + "log_severity.cc" + "log_severity.h" + "log_severity_no_ostream_test.cc" + COPTS + ${ABSL_TEST_COPTS} + # Predefine standard library <ostream> include guards so hosted #include + # <ostream> is empty (matching _LIBCPP_HAS_LOCALIZATION == 0) and unguarded + # std::ostream references fail at compile time. + DEFINES + "_GLIBCXX_OSTREAM" + "_LIBCPP_HAS_NO_LOCALIZATION" + "_LIBCPP_OSTREAM" + "_OSTREAM_" + LINKOPTS + ${ABSL_DEFAULT_LINKOPTS} + DEPS + absl::config + absl::core_headers +) + +absl_cc_test( + NAME log_severity_test SRCS "log_severity_test.cc"
diff --git a/absl/base/config.h b/absl/base/config.h index 4761f25..0a961aa 100644 --- a/absl/base/config.h +++ b/absl/base/config.h
@@ -312,6 +312,19 @@ #define ABSL_HAVE_EXCEPTIONS 1 #endif +// ABSL_INTERNAL_HAVE_STD_OSTREAM +// +// Checks whether the C++ standard library provides `std::ostream`. libc++ omits +// `std::ostream` from `<ostream>` when built without localization support: +// LLVM < 20 defines `_LIBCPP_HAS_NO_LOCALIZATION` only when disabled, whereas +// LLVM >= 20 always defines `_LIBCPP_HAS_LOCALIZATION` to `0` or `1`. +#ifdef ABSL_INTERNAL_HAVE_STD_OSTREAM +#error ABSL_INTERNAL_HAVE_STD_OSTREAM cannot be directly set +#elif defined(__cplusplus) && !defined(_LIBCPP_HAS_NO_LOCALIZATION) && \ + (!defined(_LIBCPP_HAS_LOCALIZATION) || _LIBCPP_HAS_LOCALIZATION != 0) +#define ABSL_INTERNAL_HAVE_STD_OSTREAM 1 +#endif + // ----------------------------------------------------------------------------- // Platform Feature Checks // -----------------------------------------------------------------------------
diff --git a/absl/base/log_severity.cc b/absl/base/log_severity.cc index 8e7bbbc..59dc228 100644 --- a/absl/base/log_severity.cc +++ b/absl/base/log_severity.cc
@@ -22,6 +22,8 @@ namespace absl { ABSL_NAMESPACE_BEGIN +#ifdef ABSL_INTERNAL_HAVE_STD_OSTREAM + std::ostream& operator<<(std::ostream& os, absl::LogSeverity s) { if (s == absl::NormalizeLogSeverity(s)) return os << absl::LogSeverityName(s); return os << "absl::LogSeverity(" << static_cast<int>(s) << ")"; @@ -52,5 +54,7 @@ } return os; } + +#endif // ABSL_INTERNAL_HAVE_STD_OSTREAM ABSL_NAMESPACE_END } // namespace absl
diff --git a/absl/base/log_severity.h b/absl/base/log_severity.h index de9702a..a26a311 100644 --- a/absl/base/log_severity.h +++ b/absl/base/log_severity.h
@@ -126,7 +126,9 @@ // // The exact representation of a streamed `absl::LogSeverity` is deliberately // unspecified; do not rely on it. +#ifdef ABSL_INTERNAL_HAVE_STD_OSTREAM std::ostream& operator<<(std::ostream& os, absl::LogSeverity s); +#endif // ABSL_INTERNAL_HAVE_STD_OSTREAM // Enums representing a lower bound for LogSeverity. APIs that only operate on // messages of at least a certain level (for example, `SetMinLogLevel()`) use @@ -141,7 +143,9 @@ kInfinity = 1000, }; +#ifdef ABSL_INTERNAL_HAVE_STD_OSTREAM std::ostream& operator<<(std::ostream& os, absl::LogSeverityAtLeast s); +#endif // ABSL_INTERNAL_HAVE_STD_OSTREAM // Enums representing an upper bound for LogSeverity. APIs that only operate on // messages of at most a certain level (for example, buffer all messages at or @@ -156,7 +160,9 @@ kFatal = static_cast<int>(absl::LogSeverity::kFatal), }; +#ifdef ABSL_INTERNAL_HAVE_STD_OSTREAM std::ostream& operator<<(std::ostream& os, absl::LogSeverityAtMost s); +#endif // ABSL_INTERNAL_HAVE_STD_OSTREAM #define COMPOP(op1, op2, T) \ constexpr bool operator op1(absl::T lhs, absl::LogSeverity rhs) { \
diff --git a/absl/base/log_severity_no_ostream_test.cc b/absl/base/log_severity_no_ostream_test.cc new file mode 100644 index 0000000..a008aec --- /dev/null +++ b/absl/base/log_severity_no_ostream_test.cc
@@ -0,0 +1,78 @@ +// Copyright 2026 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. + +#include "absl/base/log_severity.h" + +#ifdef ABSL_INTERNAL_HAVE_STD_OSTREAM +#error "ABSL_INTERNAL_HAVE_STD_OSTREAM must not be defined in this test." +#endif + +#include <cstring> +#include <iosfwd> +#include <type_traits> +#include <utility> + +namespace { + +template <typename T, typename = void> +struct HasOstreamInsertion : std::false_type {}; + +template <typename T> +struct HasOstreamInsertion<T, + std::void_t<decltype(std::declval<std::ostream&>() + << std::declval<const T&>())>> + : std::true_type {}; + +static_assert(!HasOstreamInsertion<absl::LogSeverity>::value, + "LogSeverity operator<< must be omitted"); +static_assert(!HasOstreamInsertion<absl::LogSeverityAtLeast>::value, + "LogSeverityAtLeast operator<< must be omitted"); +static_assert(!HasOstreamInsertion<absl::LogSeverityAtMost>::value, + "LogSeverityAtMost operator<< must be omitted"); + +static_assert(absl::LogSeverities().size() == 4, ""); +static_assert(absl::NormalizeLogSeverity(-1) == absl::LogSeverity::kInfo, ""); +static_assert(absl::NormalizeLogSeverity(99) == absl::LogSeverity::kError, ""); +static_assert(absl::LogSeverity::kError < absl::LogSeverityAtLeast::kFatal, ""); +static_assert(absl::LogSeverity::kError > absl::LogSeverityAtMost::kWarning, + ""); +static_assert(absl::kLogDebugFatal == absl::LogSeverity::kError || + absl::kLogDebugFatal == absl::LogSeverity::kFatal, + ""); + +} // namespace + +int main() { + if (std::strcmp(absl::LogSeverityName(absl::LogSeverity::kInfo), "INFO") != + 0) { + return 1; + } + if (std::strcmp(absl::LogSeverityName(absl::LogSeverity::kWarning), + "WARNING") != 0) { + return 2; + } + if (std::strcmp(absl::LogSeverityName(absl::LogSeverity::kError), "ERROR") != + 0) { + return 3; + } + if (std::strcmp(absl::LogSeverityName(absl::LogSeverity::kFatal), "FATAL") != + 0) { + return 4; + } + if (std::strcmp(absl::LogSeverityName(static_cast<absl::LogSeverity>(42)), + "UNKNOWN") != 0) { + return 5; + } + return 0; +}