From a99a183cabbc445aa36d2270795e0d6ff1f25dbf Mon Sep 17 00:00:00 2001 From: Mike Kruskal Date: Tue, 6 Dec 2022 12:38:52 -0800 Subject: Move implementations of absl logging to an internal file. This will allow us to create ABSL_-prefixed variants with shared implementation. PiperOrigin-RevId: 493383908 Change-Id: I3529021df7afa642fadaf43eb9fd8249e9202758 --- absl/log/BUILD.bazel | 6 +- absl/log/CMakeLists.txt | 39 ++++++- absl/log/check.h | 124 +++++++-------------- absl/log/internal/BUILD.bazel | 26 +++++ absl/log/internal/check_impl.h | 123 ++++++++++++++++++++ absl/log/internal/log_impl.h | 212 +++++++++++++++++++++++++++++++++++ absl/log/log.h | 223 ++++++++----------------------------- absl/log/log_macro_hygiene_test.cc | 16 +++ 8 files changed, 501 insertions(+), 268 deletions(-) create mode 100644 absl/log/internal/check_impl.h create mode 100644 absl/log/internal/log_impl.h (limited to 'absl/log') diff --git a/absl/log/BUILD.bazel b/absl/log/BUILD.bazel index b5ce616c..957709dd 100644 --- a/absl/log/BUILD.bazel +++ b/absl/log/BUILD.bazel @@ -32,7 +32,7 @@ cc_library( copts = ABSL_DEFAULT_COPTS, linkopts = ABSL_DEFAULT_LINKOPTS, deps = [ - "//absl/base:core_headers", + "//absl/log/internal:check_impl", "//absl/log/internal:check_op", "//absl/log/internal:conditions", "//absl/log/internal:log_message", @@ -114,9 +114,7 @@ cc_library( copts = ABSL_DEFAULT_COPTS, linkopts = ABSL_DEFAULT_LINKOPTS, deps = [ - "//absl/log/internal:conditions", - "//absl/log/internal:log_message", - "//absl/log/internal:strip", + "//absl/log/internal:log_impl", ], ) diff --git a/absl/log/CMakeLists.txt b/absl/log/CMakeLists.txt index 2ab08d81..875540dd 100644 --- a/absl/log/CMakeLists.txt +++ b/absl/log/CMakeLists.txt @@ -15,6 +15,24 @@ # # Internal targets +absl_cc_library( + NAME + log_internal_check_impl + SRCS + HDRS + "internal/check_impl.h" + COPTS + ${ABSL_DEFAULT_COPTS} + LINKOPTS + ${ABSL_DEFAULT_LINKOPTS} + DEPS + absl::core_headers + absl::log_internal_check_op + absl::log_internal_conditions + absl::log_internal_message + absl::log_internal_strip +) + absl_cc_library( NAME log_internal_check_op @@ -126,6 +144,22 @@ absl_cc_library( absl::time ) +absl_cc_library( + NAME + log_internal_log_impl + SRCS + HDRS + "internal/log_impl.h" + COPTS + ${ABSL_DEFAULT_COPTS} + LINKOPTS + ${ABSL_DEFAULT_LINKOPTS} + DEPS + absl::log_internal_conditions + absl::log_internal_message + absl::log_internal_strip +) + absl_cc_library( NAME log_internal_proto @@ -366,6 +400,7 @@ absl_cc_library( LINKOPTS ${ABSL_DEFAULT_LINKOPTS} DEPS + absl::log_internal_check_impl absl::core_headers absl::log_internal_check_op absl::log_internal_conditions @@ -467,9 +502,7 @@ absl_cc_library( LINKOPTS ${ABSL_DEFAULT_LINKOPTS} DEPS - absl::log_internal_conditions - absl::log_internal_message - absl::log_internal_strip + absl::log_internal_log_impl PUBLIC ) diff --git a/absl/log/check.h b/absl/log/check.h index c7303b8d..172436a6 100644 --- a/absl/log/check.h +++ b/absl/log/check.h @@ -34,7 +34,7 @@ #ifndef ABSL_LOG_CHECK_H_ #define ABSL_LOG_CHECK_H_ -#include "absl/base/optimization.h" +#include "absl/log/internal/check_impl.h" #include "absl/log/internal/check_op.h" // IWYU pragma: export #include "absl/log/internal/conditions.h" // IWYU pragma: export #include "absl/log/internal/log_message.h" // IWYU pragma: export @@ -54,10 +54,7 @@ // Might produce a message like: // // Check failed: !cheese.empty() Out of Cheese -#define CHECK(condition) \ - ABSL_LOG_INTERNAL_CONDITION_FATAL(STATELESS, \ - ABSL_PREDICT_FALSE(!(condition))) \ - ABSL_LOG_INTERNAL_CHECK(#condition).InternalStream() +#define CHECK(condition) ABSL_CHECK_IMPL(condition) // QCHECK() // @@ -65,10 +62,7 @@ // not run registered error handlers (as `QFATAL`). It is useful when the // problem is definitely unrelated to program flow, e.g. when validating user // input. -#define QCHECK(condition) \ - ABSL_LOG_INTERNAL_CONDITION_QFATAL(STATELESS, \ - ABSL_PREDICT_FALSE(!(condition))) \ - ABSL_LOG_INTERNAL_QCHECK(#condition).InternalStream() +#define QCHECK(condition) ABSL_QCHECK_IMPL(condition) // PCHECK() // @@ -83,7 +77,7 @@ // Might produce a message like: // // Check failed: fd != -1 posix is difficult: No such file or directory [2] -#define PCHECK(condition) CHECK(condition).WithPerror() +#define PCHECK(condition) ABSL_PCHECK_IMPL(condition) // DCHECK() // @@ -91,11 +85,7 @@ // `DLOG`). Unlike with `CHECK` (but as with `assert`), it is not safe to rely // on evaluation of `condition`: when `NDEBUG` is enabled, DCHECK does not // evaluate the condition. -#ifndef NDEBUG -#define DCHECK(condition) CHECK(condition) -#else -#define DCHECK(condition) CHECK(true || (condition)) -#endif +#define DCHECK(condition) ABSL_DCHECK_IMPL(condition) // `CHECK_EQ` and friends are syntactic sugar for `CHECK(x == y)` that // automatically output the expression being tested and the evaluated values on @@ -123,43 +113,24 @@ // // WARNING: Passing `NULL` as an argument to `CHECK_EQ` and similar macros does // not compile. Use `nullptr` instead. -#define CHECK_EQ(val1, val2) \ - ABSL_LOG_INTERNAL_CHECK_OP(Check_EQ, ==, val1, val2) -#define CHECK_NE(val1, val2) \ - ABSL_LOG_INTERNAL_CHECK_OP(Check_NE, !=, val1, val2) -#define CHECK_LE(val1, val2) \ - ABSL_LOG_INTERNAL_CHECK_OP(Check_LE, <=, val1, val2) -#define CHECK_LT(val1, val2) ABSL_LOG_INTERNAL_CHECK_OP(Check_LT, <, val1, val2) -#define CHECK_GE(val1, val2) \ - ABSL_LOG_INTERNAL_CHECK_OP(Check_GE, >=, val1, val2) -#define CHECK_GT(val1, val2) ABSL_LOG_INTERNAL_CHECK_OP(Check_GT, >, val1, val2) -#define QCHECK_EQ(val1, val2) \ - ABSL_LOG_INTERNAL_QCHECK_OP(Check_EQ, ==, val1, val2) -#define QCHECK_NE(val1, val2) \ - ABSL_LOG_INTERNAL_QCHECK_OP(Check_NE, !=, val1, val2) -#define QCHECK_LE(val1, val2) \ - ABSL_LOG_INTERNAL_QCHECK_OP(Check_LE, <=, val1, val2) -#define QCHECK_LT(val1, val2) \ - ABSL_LOG_INTERNAL_QCHECK_OP(Check_LT, <, val1, val2) -#define QCHECK_GE(val1, val2) \ - ABSL_LOG_INTERNAL_QCHECK_OP(Check_GE, >=, val1, val2) -#define QCHECK_GT(val1, val2) \ - ABSL_LOG_INTERNAL_QCHECK_OP(Check_GT, >, val1, val2) -#ifndef NDEBUG -#define DCHECK_EQ(val1, val2) CHECK_EQ(val1, val2) -#define DCHECK_NE(val1, val2) CHECK_NE(val1, val2) -#define DCHECK_LE(val1, val2) CHECK_LE(val1, val2) -#define DCHECK_LT(val1, val2) CHECK_LT(val1, val2) -#define DCHECK_GE(val1, val2) CHECK_GE(val1, val2) -#define DCHECK_GT(val1, val2) CHECK_GT(val1, val2) -#else // ndef NDEBUG -#define DCHECK_EQ(val1, val2) ABSL_LOG_INTERNAL_DCHECK_NOP(val1, val2) -#define DCHECK_NE(val1, val2) ABSL_LOG_INTERNAL_DCHECK_NOP(val1, val2) -#define DCHECK_LE(val1, val2) ABSL_LOG_INTERNAL_DCHECK_NOP(val1, val2) -#define DCHECK_LT(val1, val2) ABSL_LOG_INTERNAL_DCHECK_NOP(val1, val2) -#define DCHECK_GE(val1, val2) ABSL_LOG_INTERNAL_DCHECK_NOP(val1, val2) -#define DCHECK_GT(val1, val2) ABSL_LOG_INTERNAL_DCHECK_NOP(val1, val2) -#endif // def NDEBUG +#define CHECK_EQ(val1, val2) ABSL_CHECK_EQ_IMPL(val1, val2) +#define CHECK_NE(val1, val2) ABSL_CHECK_NE_IMPL(val1, val2) +#define CHECK_LE(val1, val2) ABSL_CHECK_LE_IMPL(val1, val2) +#define CHECK_LT(val1, val2) ABSL_CHECK_LT_IMPL(val1, val2) +#define CHECK_GE(val1, val2) ABSL_CHECK_GE_IMPL(val1, val2) +#define CHECK_GT(val1, val2) ABSL_CHECK_GT_IMPL(val1, val2) +#define QCHECK_EQ(val1, val2) ABSL_QCHECK_EQ_IMPL(val1, val2) +#define QCHECK_NE(val1, val2) ABSL_QCHECK_NE_IMPL(val1, val2) +#define QCHECK_LE(val1, val2) ABSL_QCHECK_LE_IMPL(val1, val2) +#define QCHECK_LT(val1, val2) ABSL_QCHECK_LT_IMPL(val1, val2) +#define QCHECK_GE(val1, val2) ABSL_QCHECK_GE_IMPL(val1, val2) +#define QCHECK_GT(val1, val2) ABSL_QCHECK_GT_IMPL(val1, val2) +#define DCHECK_EQ(val1, val2) ABSL_DCHECK_EQ_IMPL(val1, val2) +#define DCHECK_NE(val1, val2) ABSL_DCHECK_NE_IMPL(val1, val2) +#define DCHECK_LE(val1, val2) ABSL_DCHECK_LE_IMPL(val1, val2) +#define DCHECK_LT(val1, val2) ABSL_DCHECK_LT_IMPL(val1, val2) +#define DCHECK_GE(val1, val2) ABSL_DCHECK_GE_IMPL(val1, val2) +#define DCHECK_GT(val1, val2) ABSL_DCHECK_GT_IMPL(val1, val2) // `CHECK_OK` and friends validate that the provided `absl::Status` or // `absl::StatusOr` is OK. If it isn't, they print a failure message that @@ -175,13 +146,9 @@ // Might produce a message like: // // Check failed: FunctionReturnsStatus(x, y, z) is OK (ABORTED: timeout) oops! -#define CHECK_OK(status) ABSL_LOG_INTERNAL_CHECK_OK(status) -#define QCHECK_OK(status) ABSL_LOG_INTERNAL_QCHECK_OK(status) -#ifndef NDEBUG -#define DCHECK_OK(status) ABSL_LOG_INTERNAL_CHECK_OK(status) -#else -#define DCHECK_OK(status) ABSL_LOG_INTERNAL_DCHECK_NOP(status, nullptr) -#endif +#define CHECK_OK(status) ABSL_CHECK_OK_IMPL(status) +#define QCHECK_OK(status) ABSL_QCHECK_OK_IMPL(status) +#define DCHECK_OK(status) ABSL_DCHECK_OK_IMPL(status) // `CHECK_STREQ` and friends provide `CHECK_EQ` functionality for C strings, // i.e., nul-terminated char arrays. The `CASE` versions are case-insensitive. @@ -196,32 +163,17 @@ // Example: // // CHECK_STREQ(Foo().c_str(), Bar().c_str()); -#define CHECK_STREQ(s1, s2) \ - ABSL_LOG_INTERNAL_CHECK_STROP(strcmp, ==, true, s1, s2) -#define CHECK_STRNE(s1, s2) \ - ABSL_LOG_INTERNAL_CHECK_STROP(strcmp, !=, false, s1, s2) -#define CHECK_STRCASEEQ(s1, s2) \ - ABSL_LOG_INTERNAL_CHECK_STROP(strcasecmp, ==, true, s1, s2) -#define CHECK_STRCASENE(s1, s2) \ - ABSL_LOG_INTERNAL_CHECK_STROP(strcasecmp, !=, false, s1, s2) -#define QCHECK_STREQ(s1, s2) \ - ABSL_LOG_INTERNAL_QCHECK_STROP(strcmp, ==, true, s1, s2) -#define QCHECK_STRNE(s1, s2) \ - ABSL_LOG_INTERNAL_QCHECK_STROP(strcmp, !=, false, s1, s2) -#define QCHECK_STRCASEEQ(s1, s2) \ - ABSL_LOG_INTERNAL_QCHECK_STROP(strcasecmp, ==, true, s1, s2) -#define QCHECK_STRCASENE(s1, s2) \ - ABSL_LOG_INTERNAL_QCHECK_STROP(strcasecmp, !=, false, s1, s2) -#ifndef NDEBUG -#define DCHECK_STREQ(s1, s2) CHECK_STREQ(s1, s2) -#define DCHECK_STRCASEEQ(s1, s2) CHECK_STRCASEEQ(s1, s2) -#define DCHECK_STRNE(s1, s2) CHECK_STRNE(s1, s2) -#define DCHECK_STRCASENE(s1, s2) CHECK_STRCASENE(s1, s2) -#else // ndef NDEBUG -#define DCHECK_STREQ(s1, s2) ABSL_LOG_INTERNAL_DCHECK_NOP(s1, s2) -#define DCHECK_STRCASEEQ(s1, s2) ABSL_LOG_INTERNAL_DCHECK_NOP(s1, s2) -#define DCHECK_STRNE(s1, s2) ABSL_LOG_INTERNAL_DCHECK_NOP(s1, s2) -#define DCHECK_STRCASENE(s1, s2) ABSL_LOG_INTERNAL_DCHECK_NOP(s1, s2) -#endif // def NDEBUG +#define CHECK_STREQ(s1, s2) ABSL_CHECK_STREQ_IMPL(s1, s2) +#define CHECK_STRNE(s1, s2) ABSL_CHECK_STRNE_IMPL(s1, s2) +#define CHECK_STRCASEEQ(s1, s2) ABSL_CHECK_STRCASEEQ_IMPL(s1, s2) +#define CHECK_STRCASENE(s1, s2) ABSL_CHECK_STRCASENE_IMPL(s1, s2) +#define QCHECK_STREQ(s1, s2) ABSL_QCHECK_STREQ_IMPL(s1, s2) +#define QCHECK_STRNE(s1, s2) ABSL_QCHECK_STRNE_IMPL(s1, s2) +#define QCHECK_STRCASEEQ(s1, s2) ABSL_QCHECK_STRCASEEQ_IMPL(s1, s2) +#define QCHECK_STRCASENE(s1, s2) ABSL_QCHECK_STRCASENE_IMPL(s1, s2) +#define DCHECK_STREQ(s1, s2) ABSL_DCHECK_STREQ_IMPL(s1, s2) +#define DCHECK_STRNE(s1, s2) ABSL_DCHECK_STRNE_IMPL(s1, s2) +#define DCHECK_STRCASEEQ(s1, s2) ABSL_DCHECK_STRCASEEQ_IMPL(s1, s2) +#define DCHECK_STRCASENE(s1, s2) ABSL_DCHECK_STRCASENE_IMPL(s1, s2) #endif // ABSL_LOG_CHECK_H_ diff --git a/absl/log/internal/BUILD.bazel b/absl/log/internal/BUILD.bazel index b2e7085f..1ad9a9d4 100644 --- a/absl/log/internal/BUILD.bazel +++ b/absl/log/internal/BUILD.bazel @@ -27,6 +27,20 @@ package(default_visibility = [ licenses(["notice"]) +cc_library( + name = "check_impl", + hdrs = ["check_impl.h"], + copts = ABSL_DEFAULT_COPTS, + linkopts = ABSL_DEFAULT_LINKOPTS, + deps = [ + ":check_op", + ":conditions", + ":log_message", + ":strip", + "//absl/base:core_headers", + ], +) + cc_library( name = "check_op", srcs = ["check_op.cc"], @@ -123,6 +137,18 @@ cc_library( ], ) +cc_library( + name = "log_impl", + hdrs = ["log_impl.h"], + copts = ABSL_DEFAULT_COPTS, + linkopts = ABSL_DEFAULT_LINKOPTS, + deps = [ + ":conditions", + ":log_message", + ":strip", + ], +) + cc_library( name = "log_message", srcs = ["log_message.cc"], diff --git a/absl/log/internal/check_impl.h b/absl/log/internal/check_impl.h new file mode 100644 index 00000000..301b2915 --- /dev/null +++ b/absl/log/internal/check_impl.h @@ -0,0 +1,123 @@ +// 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. + +#ifndef ABSL_LOG_INTERNAL_CHECK_IMPL_H_ +#define ABSL_LOG_INTERNAL_CHECK_IMPL_H_ + +#include "absl/base/optimization.h" +#include "absl/log/internal/check_op.h" +#include "absl/log/internal/conditions.h" +#include "absl/log/internal/log_message.h" +#include "absl/log/internal/strip.h" + +// CHECK +#define ABSL_CHECK_IMPL(condition) \ + ABSL_LOG_INTERNAL_CONDITION_FATAL(STATELESS, \ + ABSL_PREDICT_FALSE(!(condition))) \ + ABSL_LOG_INTERNAL_CHECK(#condition).InternalStream() + +#define ABSL_QCHECK_IMPL(condition) \ + ABSL_LOG_INTERNAL_CONDITION_QFATAL(STATELESS, \ + ABSL_PREDICT_FALSE(!(condition))) \ + ABSL_LOG_INTERNAL_QCHECK(#condition).InternalStream() + +#define ABSL_PCHECK_IMPL(condition) ABSL_CHECK_IMPL(condition).WithPerror() + +#ifndef NDEBUG +#define ABSL_DCHECK_IMPL(condition) ABSL_CHECK_IMPL(condition) +#else +#define ABSL_DCHECK_IMPL(condition) ABSL_CHECK_IMPL(true || (condition)) +#endif + +// CHECK_EQ +#define ABSL_CHECK_EQ_IMPL(val1, val2) \ + ABSL_LOG_INTERNAL_CHECK_OP(Check_EQ, ==, val1, val2) +#define ABSL_CHECK_NE_IMPL(val1, val2) \ + ABSL_LOG_INTERNAL_CHECK_OP(Check_NE, !=, val1, val2) +#define ABSL_CHECK_LE_IMPL(val1, val2) \ + ABSL_LOG_INTERNAL_CHECK_OP(Check_LE, <=, val1, val2) +#define ABSL_CHECK_LT_IMPL(val1, val2) \ + ABSL_LOG_INTERNAL_CHECK_OP(Check_LT, <, val1, val2) +#define ABSL_CHECK_GE_IMPL(val1, val2) \ + ABSL_LOG_INTERNAL_CHECK_OP(Check_GE, >=, val1, val2) +#define ABSL_CHECK_GT_IMPL(val1, val2) \ + ABSL_LOG_INTERNAL_CHECK_OP(Check_GT, >, val1, val2) +#define ABSL_QCHECK_EQ_IMPL(val1, val2) \ + ABSL_LOG_INTERNAL_QCHECK_OP(Check_EQ, ==, val1, val2) +#define ABSL_QCHECK_NE_IMPL(val1, val2) \ + ABSL_LOG_INTERNAL_QCHECK_OP(Check_NE, !=, val1, val2) +#define ABSL_QCHECK_LE_IMPL(val1, val2) \ + ABSL_LOG_INTERNAL_QCHECK_OP(Check_LE, <=, val1, val2) +#define ABSL_QCHECK_LT_IMPL(val1, val2) \ + ABSL_LOG_INTERNAL_QCHECK_OP(Check_LT, <, val1, val2) +#define ABSL_QCHECK_GE_IMPL(val1, val2) \ + ABSL_LOG_INTERNAL_QCHECK_OP(Check_GE, >=, val1, val2) +#define ABSL_QCHECK_GT_IMPL(val1, val2) \ + ABSL_LOG_INTERNAL_QCHECK_OP(Check_GT, >, val1, val2) +#ifndef NDEBUG +#define ABSL_DCHECK_EQ_IMPL(val1, val2) ABSL_CHECK_EQ_IMPL(val1, val2) +#define ABSL_DCHECK_NE_IMPL(val1, val2) ABSL_CHECK_NE_IMPL(val1, val2) +#define ABSL_DCHECK_LE_IMPL(val1, val2) ABSL_CHECK_LE_IMPL(val1, val2) +#define ABSL_DCHECK_LT_IMPL(val1, val2) ABSL_CHECK_LT_IMPL(val1, val2) +#define ABSL_DCHECK_GE_IMPL(val1, val2) ABSL_CHECK_GE_IMPL(val1, val2) +#define ABSL_DCHECK_GT_IMPL(val1, val2) ABSL_CHECK_GT_IMPL(val1, val2) +#else // ndef NDEBUG +#define ABSL_DCHECK_EQ_IMPL(val1, val2) ABSL_LOG_INTERNAL_DCHECK_NOP(val1, val2) +#define ABSL_DCHECK_NE_IMPL(val1, val2) ABSL_LOG_INTERNAL_DCHECK_NOP(val1, val2) +#define ABSL_DCHECK_LE_IMPL(val1, val2) ABSL_LOG_INTERNAL_DCHECK_NOP(val1, val2) +#define ABSL_DCHECK_LT_IMPL(val1, val2) ABSL_LOG_INTERNAL_DCHECK_NOP(val1, val2) +#define ABSL_DCHECK_GE_IMPL(val1, val2) ABSL_LOG_INTERNAL_DCHECK_NOP(val1, val2) +#define ABSL_DCHECK_GT_IMPL(val1, val2) ABSL_LOG_INTERNAL_DCHECK_NOP(val1, val2) +#endif // def NDEBUG + +// CHECK_OK +#define ABSL_CHECK_OK_IMPL(status) ABSL_LOG_INTERNAL_CHECK_OK(status) +#define ABSL_QCHECK_OK_IMPL(status) ABSL_LOG_INTERNAL_QCHECK_OK(status) +#ifndef NDEBUG +#define ABSL_DCHECK_OK_IMPL(status) ABSL_LOG_INTERNAL_CHECK_OK(status) +#else +#define ABSL_DCHECK_OK_IMPL(status) \ + ABSL_LOG_INTERNAL_DCHECK_NOP(status, nullptr) +#endif + +// CHECK_STREQ +#define ABSL_CHECK_STREQ_IMPL(s1, s2) \ + ABSL_LOG_INTERNAL_CHECK_STROP(strcmp, ==, true, s1, s2) +#define ABSL_CHECK_STRNE_IMPL(s1, s2) \ + ABSL_LOG_INTERNAL_CHECK_STROP(strcmp, !=, false, s1, s2) +#define ABSL_CHECK_STRCASEEQ_IMPL(s1, s2) \ + ABSL_LOG_INTERNAL_CHECK_STROP(strcasecmp, ==, true, s1, s2) +#define ABSL_CHECK_STRCASENE_IMPL(s1, s2) \ + ABSL_LOG_INTERNAL_CHECK_STROP(strcasecmp, !=, false, s1, s2) +#define ABSL_QCHECK_STREQ_IMPL(s1, s2) \ + ABSL_LOG_INTERNAL_QCHECK_STROP(strcmp, ==, true, s1, s2) +#define ABSL_QCHECK_STRNE_IMPL(s1, s2) \ + ABSL_LOG_INTERNAL_QCHECK_STROP(strcmp, !=, false, s1, s2) +#define ABSL_QCHECK_STRCASEEQ_IMPL(s1, s2) \ + ABSL_LOG_INTERNAL_QCHECK_STROP(strcasecmp, ==, true, s1, s2) +#define ABSL_QCHECK_STRCASENE_IMPL(s1, s2) \ + ABSL_LOG_INTERNAL_QCHECK_STROP(strcasecmp, !=, false, s1, s2) +#ifndef NDEBUG +#define ABSL_DCHECK_STREQ_IMPL(s1, s2) ABSL_CHECK_STREQ_IMPL(s1, s2) +#define ABSL_DCHECK_STRCASEEQ_IMPL(s1, s2) ABSL_CHECK_STRCASEEQ_IMPL(s1, s2) +#define ABSL_DCHECK_STRNE_IMPL(s1, s2) ABSL_CHECK_STRNE_IMPL(s1, s2) +#define ABSL_DCHECK_STRCASENE_IMPL(s1, s2) ABSL_CHECK_STRCASENE_IMPL(s1, s2) +#else // ndef NDEBUG +#define ABSL_DCHECK_STREQ_IMPL(s1, s2) ABSL_LOG_INTERNAL_DCHECK_NOP(s1, s2) +#define ABSL_DCHECK_STRCASEEQ_IMPL(s1, s2) ABSL_LOG_INTERNAL_DCHECK_NOP(s1, s2) +#define ABSL_DCHECK_STRNE_IMPL(s1, s2) ABSL_LOG_INTERNAL_DCHECK_NOP(s1, s2) +#define ABSL_DCHECK_STRCASENE_IMPL(s1, s2) ABSL_LOG_INTERNAL_DCHECK_NOP(s1, s2) +#endif // def NDEBUG + +#endif // ABSL_LOG_INTERNAL_CHECK_IMPL_H_ diff --git a/absl/log/internal/log_impl.h b/absl/log/internal/log_impl.h new file mode 100644 index 00000000..82b5ed84 --- /dev/null +++ b/absl/log/internal/log_impl.h @@ -0,0 +1,212 @@ +// 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. + +#ifndef ABSL_LOG_INTERNAL_LOG_IMPL_H_ +#define ABSL_LOG_INTERNAL_LOG_IMPL_H_ + +#include "absl/log/internal/conditions.h" +#include "absl/log/internal/log_message.h" +#include "absl/log/internal/strip.h" + +// ABSL_LOG() +#define ABSL_LOG_IMPL(severity) \ + ABSL_LOG_INTERNAL_CONDITION##severity(STATELESS, true) \ + ABSL_LOGGING_INTERNAL_LOG##severity.InternalStream() + +// ABSL_PLOG() +#define ABSL_PLOG_IMPL(severity) \ + ABSL_LOG_INTERNAL_CONDITION##severity(STATELESS, true) \ + ABSL_LOGGING_INTERNAL_LOG##severity.InternalStream() \ + .WithPerror() + +// ABSL_DLOG() +#ifndef NDEBUG +#define ABSL_DLOG_IMPL(severity) \ + ABSL_LOG_INTERNAL_CONDITION##severity(STATELESS, true) \ + ABSL_LOGGING_INTERNAL_LOG##severity.InternalStream() +#else +#define ABSL_DLOG_IMPL(severity) \ + ABSL_LOG_INTERNAL_CONDITION##severity(STATELESS, false) \ + ABSL_LOGGING_INTERNAL_LOG##severity.InternalStream() +#endif + +#define ABSL_LOG_IF_IMPL(severity, condition) \ + ABSL_LOG_INTERNAL_CONDITION##severity(STATELESS, condition) \ + ABSL_LOGGING_INTERNAL_LOG##severity.InternalStream() +#define ABSL_PLOG_IF_IMPL(severity, condition) \ + ABSL_LOG_INTERNAL_CONDITION##severity(STATELESS, condition) \ + ABSL_LOGGING_INTERNAL_LOG##severity.InternalStream() \ + .WithPerror() + +#ifndef NDEBUG +#define ABSL_DLOG_IF_IMPL(severity, condition) \ + ABSL_LOG_INTERNAL_CONDITION##severity(STATELESS, condition) \ + ABSL_LOGGING_INTERNAL_LOG##severity.InternalStream() +#else +#define ABSL_DLOG_IF_IMPL(severity, condition) \ + ABSL_LOG_INTERNAL_CONDITION##severity(STATELESS, false && (condition)) \ + ABSL_LOGGING_INTERNAL_LOG##severity.InternalStream() +#endif + +// ABSL_LOG_EVERY_N +#define ABSL_LOG_EVERY_N_IMPL(severity, n) \ + ABSL_LOG_INTERNAL_CONDITION##severity(STATEFUL, true)(EveryN, n) \ + ABSL_LOGGING_INTERNAL_LOG##severity.InternalStream() + +// ABSL_LOG_FIRST_N +#define ABSL_LOG_FIRST_N_IMPL(severity, n) \ + ABSL_LOG_INTERNAL_CONDITION##severity(STATEFUL, true)(FirstN, n) \ + ABSL_LOGGING_INTERNAL_LOG##severity.InternalStream() + +// ABSL_LOG_EVERY_POW_2 +#define ABSL_LOG_EVERY_POW_2_IMPL(severity) \ + ABSL_LOG_INTERNAL_CONDITION##severity(STATEFUL, true)(EveryPow2) \ + ABSL_LOGGING_INTERNAL_LOG##severity.InternalStream() + +// ABSL_LOG_EVERY_N_SEC +#define ABSL_LOG_EVERY_N_SEC_IMPL(severity, n_seconds) \ + ABSL_LOG_INTERNAL_CONDITION##severity(STATEFUL, true)(EveryNSec, n_seconds) \ + ABSL_LOGGING_INTERNAL_LOG##severity.InternalStream() + +#define ABSL_PLOG_EVERY_N_IMPL(severity, n) \ + ABSL_LOG_INTERNAL_CONDITION##severity(STATEFUL, true)(EveryN, n) \ + ABSL_LOGGING_INTERNAL_LOG##severity.InternalStream() \ + .WithPerror() + +#define ABSL_PLOG_FIRST_N_IMPL(severity, n) \ + ABSL_LOG_INTERNAL_CONDITION##severity(STATEFUL, true)(FirstN, n) \ + ABSL_LOGGING_INTERNAL_LOG##severity.InternalStream() \ + .WithPerror() + +#define ABSL_PLOG_EVERY_POW_2_IMPL(severity) \ + ABSL_LOG_INTERNAL_CONDITION##severity(STATEFUL, true)(EveryPow2) \ + ABSL_LOGGING_INTERNAL_LOG##severity.InternalStream() \ + .WithPerror() + +#define ABSL_PLOG_EVERY_N_SEC_IMPL(severity, n_seconds) \ + ABSL_LOG_INTERNAL_CONDITION##severity(STATEFUL, true)(EveryNSec, n_seconds) \ + ABSL_LOGGING_INTERNAL_LOG##severity.InternalStream() \ + .WithPerror() + +#ifndef NDEBUG +#define ABSL_DLOG_EVERY_N_IMPL(severity, n) \ + ABSL_LOG_INTERNAL_CONDITION_INFO(STATEFUL, true) \ + (EveryN, n) ABSL_LOGGING_INTERNAL_LOG##severity.InternalStream() + +#define ABSL_DLOG_FIRST_N_IMPL(severity, n) \ + ABSL_LOG_INTERNAL_CONDITION_INFO(STATEFUL, true) \ + (FirstN, n) ABSL_LOGGING_INTERNAL_LOG##severity.InternalStream() + +#define ABSL_DLOG_EVERY_POW_2_IMPL(severity) \ + ABSL_LOG_INTERNAL_CONDITION_INFO(STATEFUL, true) \ + (EveryPow2) ABSL_LOGGING_INTERNAL_LOG##severity.InternalStream() + +#define ABSL_DLOG_EVERY_N_SEC_IMPL(severity, n_seconds) \ + ABSL_LOG_INTERNAL_CONDITION_INFO(STATEFUL, true) \ + (EveryNSec, n_seconds) ABSL_LOGGING_INTERNAL_LOG##severity.InternalStream() + +#else // def NDEBUG +#define ABSL_DLOG_EVERY_N_IMPL(severity, n) \ + ABSL_LOG_INTERNAL_CONDITION_INFO(STATEFUL, false) \ + (EveryN, n) ABSL_LOGGING_INTERNAL_LOG##severity.InternalStream() + +#define ABSL_DLOG_FIRST_N_IMPL(severity, n) \ + ABSL_LOG_INTERNAL_CONDITION_INFO(STATEFUL, false) \ + (FirstN, n) ABSL_LOGGING_INTERNAL_LOG##severity.InternalStream() + +#define ABSL_DLOG_EVERY_POW_2_IMPL(severity) \ + ABSL_LOG_INTERNAL_CONDITION_INFO(STATEFUL, false) \ + (EveryPow2) ABSL_LOGGING_INTERNAL_LOG##severity.InternalStream() + +#define ABSL_DLOG_EVERY_N_SEC_IMPL(severity, n_seconds) \ + ABSL_LOG_INTERNAL_CONDITION_INFO(STATEFUL, false) \ + (EveryNSec, n_seconds) ABSL_LOGGING_INTERNAL_LOG##severity.InternalStream() +#endif // def NDEBUG + +#define ABSL_LOG_IF_EVERY_N_IMPL(severity, condition, n) \ + ABSL_LOG_INTERNAL_CONDITION##severity(STATEFUL, condition)(EveryN, n) \ + ABSL_LOGGING_INTERNAL_LOG##severity.InternalStream() + +#define ABSL_LOG_IF_FIRST_N_IMPL(severity, condition, n) \ + ABSL_LOG_INTERNAL_CONDITION##severity(STATEFUL, condition)(FirstN, n) \ + ABSL_LOGGING_INTERNAL_LOG##severity.InternalStream() + +#define ABSL_LOG_IF_EVERY_POW_2_IMPL(severity, condition) \ + ABSL_LOG_INTERNAL_CONDITION##severity(STATEFUL, condition)(EveryPow2) \ + ABSL_LOGGING_INTERNAL_LOG##severity.InternalStream() + +#define ABSL_LOG_IF_EVERY_N_SEC_IMPL(severity, condition, n_seconds) \ + ABSL_LOG_INTERNAL_CONDITION##severity(STATEFUL, condition)(EveryNSec, \ + n_seconds) \ + ABSL_LOGGING_INTERNAL_LOG##severity.InternalStream() + +#define ABSL_PLOG_IF_EVERY_N_IMPL(severity, condition, n) \ + ABSL_LOG_INTERNAL_CONDITION##severity(STATEFUL, condition)(EveryN, n) \ + ABSL_LOGGING_INTERNAL_LOG##severity.InternalStream() \ + .WithPerror() + +#define ABSL_PLOG_IF_FIRST_N_IMPL(severity, condition, n) \ + ABSL_LOG_INTERNAL_CONDITION##severity(STATEFUL, condition)(FirstN, n) \ + ABSL_LOGGING_INTERNAL_LOG##severity.InternalStream() \ + .WithPerror() + +#define ABSL_PLOG_IF_EVERY_POW_2_IMPL(severity, condition) \ + ABSL_LOG_INTERNAL_CONDITION##severity(STATEFUL, condition)(EveryPow2) \ + ABSL_LOGGING_INTERNAL_LOG##severity.InternalStream() \ + .WithPerror() + +#define ABSL_PLOG_IF_EVERY_N_SEC_IMPL(severity, condition, n_seconds) \ + ABSL_LOG_INTERNAL_CONDITION##severity(STATEFUL, condition)(EveryNSec, \ + n_seconds) \ + ABSL_LOGGING_INTERNAL_LOG##severity.InternalStream() \ + .WithPerror() + +#ifndef NDEBUG +#define ABSL_DLOG_IF_EVERY_N_IMPL(severity, condition, n) \ + ABSL_LOG_INTERNAL_CONDITION##severity(STATEFUL, condition)(EveryN, n) \ + ABSL_LOGGING_INTERNAL_LOG##severity.InternalStream() + +#define ABSL_DLOG_IF_FIRST_N_IMPL(severity, condition, n) \ + ABSL_LOG_INTERNAL_CONDITION##severity(STATEFUL, condition)(FirstN, n) \ + ABSL_LOGGING_INTERNAL_LOG##severity.InternalStream() + +#define ABSL_DLOG_IF_EVERY_POW_2_IMPL(severity, condition) \ + ABSL_LOG_INTERNAL_CONDITION##severity(STATEFUL, condition)(EveryPow2) \ + ABSL_LOGGING_INTERNAL_LOG##severity.InternalStream() + +#define ABSL_DLOG_IF_EVERY_N_SEC_IMPL(severity, condition, n_seconds) \ + ABSL_LOG_INTERNAL_CONDITION##severity(STATEFUL, condition)(EveryNSec, \ + n_seconds) \ + ABSL_LOGGING_INTERNAL_LOG##severity.InternalStream() + +#else // def NDEBUG +#define ABSL_DLOG_IF_EVERY_N_IMPL(severity, condition, n) \ + ABSL_LOG_INTERNAL_CONDITION##severity(STATEFUL, false && (condition))( \ + EveryN, n) ABSL_LOGGING_INTERNAL_LOG##severity.InternalStream() + +#define ABSL_DLOG_IF_FIRST_N_IMPL(severity, condition, n) \ + ABSL_LOG_INTERNAL_CONDITION##severity(STATEFUL, false && (condition))( \ + FirstN, n) ABSL_LOGGING_INTERNAL_LOG##severity.InternalStream() + +#define ABSL_DLOG_IF_EVERY_POW_2_IMPL(severity, condition) \ + ABSL_LOG_INTERNAL_CONDITION##severity(STATEFUL, false && (condition))( \ + EveryPow2) ABSL_LOGGING_INTERNAL_LOG##severity.InternalStream() + +#define ABSL_DLOG_IF_EVERY_N_SEC_IMPL(severity, condition, n_seconds) \ + ABSL_LOG_INTERNAL_CONDITION##severity(STATEFUL, false && (condition))( \ + EveryNSec, n_seconds) \ + ABSL_LOGGING_INTERNAL_LOG##severity.InternalStream() +#endif // def NDEBUG + +#endif // ABSL_LOG_INTERNAL_LOG_IMPL_H_ diff --git a/absl/log/log.h b/absl/log/log.h index 4cd52041..e060a0b6 100644 --- a/absl/log/log.h +++ b/absl/log/log.h @@ -187,9 +187,7 @@ #ifndef ABSL_LOG_LOG_H_ #define ABSL_LOG_LOG_H_ -#include "absl/log/internal/conditions.h" -#include "absl/log/internal/log_message.h" -#include "absl/log/internal/strip.h" +#include "absl/log/internal/log_impl.h" // LOG() // @@ -198,56 +196,29 @@ // Example: // // LOG(INFO) << "Found " << num_cookies << " cookies"; -#define LOG(severity) \ - ABSL_LOG_INTERNAL_CONDITION_##severity(STATELESS, true) \ - ABSL_LOGGING_INTERNAL_LOG_##severity.InternalStream() +#define LOG(severity) ABSL_LOG_IMPL(_##severity) // PLOG() // // `PLOG` behaves like `LOG` except that a description of the current state of // `errno` is appended to the streamed message. -#define PLOG(severity) \ - ABSL_LOG_INTERNAL_CONDITION_##severity(STATELESS, true) \ - ABSL_LOGGING_INTERNAL_LOG_##severity.InternalStream() \ - .WithPerror() +#define PLOG(severity) ABSL_PLOG_IMPL(_##severity) // DLOG() // // `DLOG` behaves like `LOG` in debug mode (i.e. `#ifndef NDEBUG`). Otherwise // it compiles away and does nothing. Note that `DLOG(FATAL)` does not // terminate the program if `NDEBUG` is defined. -#ifndef NDEBUG -#define DLOG(severity) \ - ABSL_LOG_INTERNAL_CONDITION_##severity(STATELESS, true) \ - ABSL_LOGGING_INTERNAL_LOG_##severity.InternalStream() -#else -#define DLOG(severity) \ - ABSL_LOG_INTERNAL_CONDITION_##severity(STATELESS, false) \ - ABSL_LOGGING_INTERNAL_LOG_##severity.InternalStream() -#endif +#define DLOG(severity) ABSL_DLOG_IMPL(_##severity) // `LOG_IF` and friends add a second argument which specifies a condition. If // the condition is false, nothing is logged. // Example: // // LOG_IF(INFO, num_cookies > 10) << "Got lots of cookies"; -#define LOG_IF(severity, condition) \ - ABSL_LOG_INTERNAL_CONDITION_##severity(STATELESS, condition) \ - ABSL_LOGGING_INTERNAL_LOG_##severity.InternalStream() -#define PLOG_IF(severity, condition) \ - ABSL_LOG_INTERNAL_CONDITION_##severity(STATELESS, condition) \ - ABSL_LOGGING_INTERNAL_LOG_##severity.InternalStream() \ - .WithPerror() - -#ifndef NDEBUG -#define DLOG_IF(severity, condition) \ - ABSL_LOG_INTERNAL_CONDITION_##severity(STATELESS, condition) \ - ABSL_LOGGING_INTERNAL_LOG_##severity.InternalStream() -#else -#define DLOG_IF(severity, condition) \ - ABSL_LOG_INTERNAL_CONDITION_##severity(STATELESS, false && (condition)) \ - ABSL_LOGGING_INTERNAL_LOG_##severity.InternalStream() -#endif +#define LOG_IF(severity, condition) ABSL_LOG_IF_IMPL(_##severity, condition) +#define PLOG_IF(severity, condition) ABSL_PLOG_IF_IMPL(_##severity, condition) +#define DLOG_IF(severity, condition) ABSL_DLOG_IF_IMPL(_##severity, condition) // LOG_EVERY_N // @@ -260,27 +231,21 @@ // // LOG_EVERY_N(WARNING, 1000) << "Got a packet with a bad CRC (" << COUNTER // << " total)"; -#define LOG_EVERY_N(severity, n) \ - ABSL_LOG_INTERNAL_CONDITION_##severity(STATEFUL, true)(EveryN, n) \ - ABSL_LOGGING_INTERNAL_LOG_##severity.InternalStream() +#define LOG_EVERY_N(severity, n) ABSL_LOG_EVERY_N_IMPL(_##severity, n) // LOG_FIRST_N // // `LOG_FIRST_N` behaves like `LOG_EVERY_N` except that the specified message is // logged when the counter's value is less than `n`. `LOG_FIRST_N` is // thread-safe. -#define LOG_FIRST_N(severity, n) \ - ABSL_LOG_INTERNAL_CONDITION_##severity(STATEFUL, true)(FirstN, n) \ - ABSL_LOGGING_INTERNAL_LOG_##severity.InternalStream() +#define LOG_FIRST_N(severity, n) ABSL_LOG_FIRST_N_IMPL(_##severity, n) // LOG_EVERY_POW_2 // // `LOG_EVERY_POW_2` behaves like `LOG_EVERY_N` except that the specified // message is logged when the counter's value is a power of 2. // `LOG_EVERY_POW_2` is thread-safe. -#define LOG_EVERY_POW_2(severity) \ - ABSL_LOG_INTERNAL_CONDITION_##severity(STATEFUL, true)(EveryPow2) \ - ABSL_LOGGING_INTERNAL_LOG_##severity.InternalStream() +#define LOG_EVERY_POW_2(severity) ABSL_LOG_EVERY_POW_2_IMPL(_##severity) // LOG_EVERY_N_SEC // @@ -291,64 +256,20 @@ // Example: // // LOG_EVERY_N_SEC(INFO, 2.5) << "Got " << COUNTER << " cookies so far"; -#define LOG_EVERY_N_SEC(severity, n_seconds) \ - ABSL_LOG_INTERNAL_CONDITION_##severity(STATEFUL, true)(EveryNSec, n_seconds) \ - ABSL_LOGGING_INTERNAL_LOG_##severity.InternalStream() - -#define PLOG_EVERY_N(severity, n) \ - ABSL_LOG_INTERNAL_CONDITION_##severity(STATEFUL, true)(EveryN, n) \ - ABSL_LOGGING_INTERNAL_LOG_##severity.InternalStream() \ - .WithPerror() - -#define PLOG_FIRST_N(severity, n) \ - ABSL_LOG_INTERNAL_CONDITION_##severity(STATEFUL, true)(FirstN, n) \ - ABSL_LOGGING_INTERNAL_LOG_##severity.InternalStream() \ - .WithPerror() - -#define PLOG_EVERY_POW_2(severity) \ - ABSL_LOG_INTERNAL_CONDITION_##severity(STATEFUL, true)(EveryPow2) \ - ABSL_LOGGING_INTERNAL_LOG_##severity.InternalStream() \ - .WithPerror() - -#define PLOG_EVERY_N_SEC(severity, n_seconds) \ - ABSL_LOG_INTERNAL_CONDITION_##severity(STATEFUL, true)(EveryNSec, n_seconds) \ - ABSL_LOGGING_INTERNAL_LOG_##severity.InternalStream() \ - .WithPerror() - -#ifndef NDEBUG -#define DLOG_EVERY_N(severity, n) \ - ABSL_LOG_INTERNAL_CONDITION_INFO(STATEFUL, true) \ - (EveryN, n) ABSL_LOGGING_INTERNAL_LOG_##severity.InternalStream() - -#define DLOG_FIRST_N(severity, n) \ - ABSL_LOG_INTERNAL_CONDITION_INFO(STATEFUL, true) \ - (FirstN, n) ABSL_LOGGING_INTERNAL_LOG_##severity.InternalStream() +#define LOG_EVERY_N_SEC(severity, n_seconds) \ + ABSL_LOG_EVERY_N_SEC_IMPL(_##severity, n_seconds) -#define DLOG_EVERY_POW_2(severity) \ - ABSL_LOG_INTERNAL_CONDITION_INFO(STATEFUL, true) \ - (EveryPow2) ABSL_LOGGING_INTERNAL_LOG_##severity.InternalStream() +#define PLOG_EVERY_N(severity, n) ABSL_PLOG_EVERY_N_IMPL(_##severity, n) +#define PLOG_FIRST_N(severity, n) ABSL_PLOG_FIRST_N_IMPL(_##severity, n) +#define PLOG_EVERY_POW_2(severity) ABSL_PLOG_EVERY_POW_2_IMPL(_##severity) +#define PLOG_EVERY_N_SEC(severity, n_seconds) \ + ABSL_PLOG_EVERY_N_SEC_IMPL(_##severity, n_seconds) -#define DLOG_EVERY_N_SEC(severity, n_seconds) \ - ABSL_LOG_INTERNAL_CONDITION_INFO(STATEFUL, true) \ - (EveryNSec, n_seconds) ABSL_LOGGING_INTERNAL_LOG_##severity.InternalStream() - -#else // def NDEBUG -#define DLOG_EVERY_N(severity, n) \ - ABSL_LOG_INTERNAL_CONDITION_INFO(STATEFUL, false) \ - (EveryN, n) ABSL_LOGGING_INTERNAL_LOG_##severity.InternalStream() - -#define DLOG_FIRST_N(severity, n) \ - ABSL_LOG_INTERNAL_CONDITION_INFO(STATEFUL, false) \ - (FirstN, n) ABSL_LOGGING_INTERNAL_LOG_##severity.InternalStream() - -#define DLOG_EVERY_POW_2(severity) \ - ABSL_LOG_INTERNAL_CONDITION_INFO(STATEFUL, false) \ - (EveryPow2) ABSL_LOGGING_INTERNAL_LOG_##severity.InternalStream() - -#define DLOG_EVERY_N_SEC(severity, n_seconds) \ - ABSL_LOG_INTERNAL_CONDITION_INFO(STATEFUL, false) \ - (EveryNSec, n_seconds) ABSL_LOGGING_INTERNAL_LOG_##severity.InternalStream() -#endif // def NDEBUG +#define DLOG_EVERY_N(severity, n) ABSL_DLOG_EVERY_N_IMPL(_##severity, n) +#define DLOG_FIRST_N(severity, n) ABSL_DLOG_FIRST_N_IMPL(_##severity, n) +#define DLOG_EVERY_POW_2(severity) ABSL_DLOG_EVERY_POW_2_IMPL(_##severity) +#define DLOG_EVERY_N_SEC(severity, n_seconds) \ + ABSL_DLOG_EVERY_N_SEC_IMPL(_##severity, n_seconds) // `LOG_IF_EVERY_N` and friends behave as the corresponding `LOG_EVERY_N` // but neither increment a counter nor log a message if condition is false (as @@ -357,79 +278,31 @@ // // LOG_IF_EVERY_N(INFO, (size > 1024), 10) << "Got the " << COUNTER // << "th big cookie"; -#define LOG_IF_EVERY_N(severity, condition, n) \ - ABSL_LOG_INTERNAL_CONDITION_##severity(STATEFUL, condition)(EveryN, n) \ - ABSL_LOGGING_INTERNAL_LOG_##severity.InternalStream() - -#define LOG_IF_FIRST_N(severity, condition, n) \ - ABSL_LOG_INTERNAL_CONDITION_##severity(STATEFUL, condition)(FirstN, n) \ - ABSL_LOGGING_INTERNAL_LOG_##severity.InternalStream() - -#define LOG_IF_EVERY_POW_2(severity, condition) \ - ABSL_LOG_INTERNAL_CONDITION_##severity(STATEFUL, condition)(EveryPow2) \ - ABSL_LOGGING_INTERNAL_LOG_##severity.InternalStream() - -#define LOG_IF_EVERY_N_SEC(severity, condition, n_seconds) \ - ABSL_LOG_INTERNAL_CONDITION_##severity(STATEFUL, condition)(EveryNSec, \ - n_seconds) \ - ABSL_LOGGING_INTERNAL_LOG_##severity.InternalStream() - -#define PLOG_IF_EVERY_N(severity, condition, n) \ - ABSL_LOG_INTERNAL_CONDITION_##severity(STATEFUL, condition)(EveryN, n) \ - ABSL_LOGGING_INTERNAL_LOG_##severity.InternalStream() \ - .WithPerror() - -#define PLOG_IF_FIRST_N(severity, condition, n) \ - ABSL_LOG_INTERNAL_CONDITION_##severity(STATEFUL, condition)(FirstN, n) \ - ABSL_LOGGING_INTERNAL_LOG_##severity.InternalStream() \ - .WithPerror() - -#define PLOG_IF_EVERY_POW_2(severity, condition) \ - ABSL_LOG_INTERNAL_CONDITION_##severity(STATEFUL, condition)(EveryPow2) \ - ABSL_LOGGING_INTERNAL_LOG_##severity.InternalStream() \ - .WithPerror() - -#define PLOG_IF_EVERY_N_SEC(severity, condition, n_seconds) \ - ABSL_LOG_INTERNAL_CONDITION_##severity(STATEFUL, condition)(EveryNSec, \ - n_seconds) \ - ABSL_LOGGING_INTERNAL_LOG_##severity.InternalStream() \ - .WithPerror() - -#ifndef NDEBUG -#define DLOG_IF_EVERY_N(severity, condition, n) \ - ABSL_LOG_INTERNAL_CONDITION_##severity(STATEFUL, condition)(EveryN, n) \ - ABSL_LOGGING_INTERNAL_LOG_##severity.InternalStream() - -#define DLOG_IF_FIRST_N(severity, condition, n) \ - ABSL_LOG_INTERNAL_CONDITION_##severity(STATEFUL, condition)(FirstN, n) \ - ABSL_LOGGING_INTERNAL_LOG_##severity.InternalStream() - -#define DLOG_IF_EVERY_POW_2(severity, condition) \ - ABSL_LOG_INTERNAL_CONDITION_##severity(STATEFUL, condition)(EveryPow2) \ - ABSL_LOGGING_INTERNAL_LOG_##severity.InternalStream() - -#define DLOG_IF_EVERY_N_SEC(severity, condition, n_seconds) \ - ABSL_LOG_INTERNAL_CONDITION_##severity(STATEFUL, condition)(EveryNSec, \ - n_seconds) \ - ABSL_LOGGING_INTERNAL_LOG_##severity.InternalStream() - -#else // def NDEBUG -#define DLOG_IF_EVERY_N(severity, condition, n) \ - ABSL_LOG_INTERNAL_CONDITION_##severity(STATEFUL, false && (condition))( \ - EveryN, n) ABSL_LOGGING_INTERNAL_LOG_##severity.InternalStream() - -#define DLOG_IF_FIRST_N(severity, condition, n) \ - ABSL_LOG_INTERNAL_CONDITION_##severity(STATEFUL, false && (condition))( \ - FirstN, n) ABSL_LOGGING_INTERNAL_LOG_##severity.InternalStream() - -#define DLOG_IF_EVERY_POW_2(severity, condition) \ - ABSL_LOG_INTERNAL_CONDITION_##severity(STATEFUL, false && (condition))( \ - EveryPow2) ABSL_LOGGING_INTERNAL_LOG_##severity.InternalStream() - -#define DLOG_IF_EVERY_N_SEC(severity, condition, n_seconds) \ - ABSL_LOG_INTERNAL_CONDITION_##severity(STATEFUL, false && (condition))( \ - EveryNSec, n_seconds) \ - ABSL_LOGGING_INTERNAL_LOG_##severity.InternalStream() -#endif // def NDEBUG +#define LOG_IF_EVERY_N(severity, condition, n) \ + ABSL_LOG_IF_EVERY_N_IMPL(_##severity, condition, n) +#define LOG_IF_FIRST_N(severity, condition, n) \ + ABSL_LOG_IF_FIRST_N_IMPL(_##severity, condition, n) +#define LOG_IF_EVERY_POW_2(severity, condition) \ + ABSL_LOG_IF_EVERY_POW_2_IMPL(_##severity, condition) +#define LOG_IF_EVERY_N_SEC(severity, condition, n_seconds) \ + ABSL_LOG_IF_EVERY_N_SEC_IMPL(_##severity, condition, n_seconds) + +#define PLOG_IF_EVERY_N(severity, condition, n) \ + ABSL_PLOG_IF_EVERY_N_IMPL(_##severity, condition, n) +#define PLOG_IF_FIRST_N(severity, condition, n) \ + ABSL_PLOG_IF_FIRST_N_IMPL(_##severity, condition, n) +#define PLOG_IF_EVERY_POW_2(severity, condition) \ + ABSL_PLOG_IF_EVERY_POW_2_IMPL(_##severity, condition) +#define PLOG_IF_EVERY_N_SEC(severity, condition, n_seconds) \ + ABSL_PLOG_IF_EVERY_N_SEC_IMPL(_##severity, condition, n_seconds) + +#define DLOG_IF_EVERY_N(severity, condition, n) \ + ABSL_DLOG_IF_EVERY_N_IMPL(_##severity, condition, n) +#define DLOG_IF_FIRST_N(severity, condition, n) \ + ABSL_DLOG_IF_FIRST_N_IMPL(_##severity, condition, n) +#define DLOG_IF_EVERY_POW_2(severity, condition) \ + ABSL_DLOG_IF_EVERY_POW_2_IMPL(_##severity, condition) +#define DLOG_IF_EVERY_N_SEC(severity, condition, n_seconds) \ + ABSL_DLOG_IF_EVERY_N_SEC_IMPL(_##severity, condition, n_seconds) #endif // ABSL_LOG_LOG_H_ diff --git a/absl/log/log_macro_hygiene_test.cc b/absl/log/log_macro_hygiene_test.cc index ab6461f5..dad9389e 100644 --- a/absl/log/log_macro_hygiene_test.cc +++ b/absl/log/log_macro_hygiene_test.cc @@ -139,6 +139,22 @@ TEST(LogHygieneTest, WorksWithINFODefined) { #undef INFO +#define _INFO Bogus +TEST(LogHygieneTest, WorksWithUnderscoreINFODefined) { + absl::ScopedMockLog test_sink(absl::MockLogDefault::kDisallowUnexpected); + + EXPECT_CALL(test_sink, Log(absl::LogSeverity::kInfo, _, "Hello world")) + .Times(2 + (IsOptimized ? 2 : 0)); + + test_sink.StartCapturingLogs(); + LOG(INFO) << "Hello world"; + LOG_IF(INFO, true) << "Hello world"; + + DLOG(INFO) << "Hello world"; + DLOG_IF(INFO, true) << "Hello world"; +} +#undef _INFO + TEST(LogHygieneTest, ExpressionEvaluationInLEVELSeverity) { auto i = static_cast(absl::LogSeverity::kInfo); LOG(LEVEL(++i)) << "hello world"; // NOLINT -- cgit v1.2.3