From 68be7315913c8ef8a791a0fb60d98e98787d88b6 Mon Sep 17 00:00:00 2001 From: Andy Getzendanner Date: Fri, 19 May 2023 18:53:34 -0700 Subject: Rename ABSL_*_IMPL macros to match the other ABSL_LOG_INTERNAL_* macros and to make sure it's clear that they're internal. Also rename the log and check test files from .h to .inc per https://google.github.io/styleguide/cppguide.html#Self_contained_Headers. PiperOrigin-RevId: 533603350 Change-Id: Iad5d8b683e33b63784cc8e64b84da09f5fc3bf1e --- absl/log/BUILD.bazel | 4 +- absl/log/CMakeLists.txt | 9 +- absl/log/absl_check.h | 86 ++++--- absl/log/absl_check_test.cc | 2 +- absl/log/absl_log.h | 69 ++--- absl/log/absl_log_basic_test.cc | 2 +- absl/log/check.h | 100 +++++--- absl/log/check_test.cc | 2 +- absl/log/check_test_impl.h | 528 --------------------------------------- absl/log/check_test_impl.inc | 528 +++++++++++++++++++++++++++++++++++++++ absl/log/internal/check_impl.h | 126 +++++----- absl/log/internal/log_impl.h | 100 ++++---- absl/log/log.h | 72 +++--- absl/log/log_basic_test.cc | 2 +- absl/log/log_basic_test_impl.h | 455 --------------------------------- absl/log/log_basic_test_impl.inc | 455 +++++++++++++++++++++++++++++++++ 16 files changed, 1301 insertions(+), 1239 deletions(-) delete mode 100644 absl/log/check_test_impl.h create mode 100644 absl/log/check_test_impl.inc delete mode 100644 absl/log/log_basic_test_impl.h create mode 100644 absl/log/log_basic_test_impl.inc diff --git a/absl/log/BUILD.bazel b/absl/log/BUILD.bazel index 4813111f..560f6c44 100644 --- a/absl/log/BUILD.bazel +++ b/absl/log/BUILD.bazel @@ -289,7 +289,7 @@ cc_library( "no_test_ios", "no_test_wasm", ], - textual_hdrs = ["check_test_impl.h"], + textual_hdrs = ["check_test_impl.inc"], visibility = ["//visibility:private"], deps = [ "//absl/base:config", @@ -373,7 +373,7 @@ cc_library( testonly = True, copts = ABSL_TEST_COPTS, linkopts = ABSL_DEFAULT_LINKOPTS, - textual_hdrs = ["log_basic_test_impl.h"], + textual_hdrs = ["log_basic_test_impl.inc"], visibility = ["//visibility:private"], deps = [ "//absl/base", diff --git a/absl/log/CMakeLists.txt b/absl/log/CMakeLists.txt index 4cba0082..2990984f 100644 --- a/absl/log/CMakeLists.txt +++ b/absl/log/CMakeLists.txt @@ -1,4 +1,3 @@ -# # Copyright 2022 The Abseil Authors. # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -679,7 +678,7 @@ absl_cc_test( absl_check_test SRCS "absl_check_test.cc" - "check_test_impl.h" + "check_test_impl.inc" COPTS ${ABSL_TEST_COPTS} LINKOPTS @@ -699,7 +698,7 @@ absl_cc_test( absl_log_basic_test SRCS "log_basic_test.cc" - "log_basic_test_impl.h" + "log_basic_test_impl.inc" COPTS ${ABSL_TEST_COPTS} LINKOPTS @@ -723,7 +722,7 @@ absl_cc_test( check_test SRCS "check_test.cc" - "check_test_impl.h" + "check_test_impl.inc" COPTS ${ABSL_TEST_COPTS} LINKOPTS @@ -759,7 +758,7 @@ absl_cc_test( log_basic_test SRCS "log_basic_test.cc" - "log_basic_test_impl.h" + "log_basic_test_impl.inc" COPTS ${ABSL_TEST_COPTS} LINKOPTS diff --git a/absl/log/absl_check.h b/absl/log/absl_check.h index 14a2307f..1bb43bd3 100644 --- a/absl/log/absl_check.h +++ b/absl/log/absl_check.h @@ -37,69 +37,81 @@ #include "absl/log/internal/check_impl.h" -#define ABSL_CHECK(condition) ABSL_CHECK_IMPL((condition), #condition) -#define ABSL_QCHECK(condition) ABSL_QCHECK_IMPL((condition), #condition) -#define ABSL_PCHECK(condition) ABSL_PCHECK_IMPL((condition), #condition) -#define ABSL_DCHECK(condition) ABSL_DCHECK_IMPL((condition), #condition) +#define ABSL_CHECK(condition) \ + ABSL_LOG_INTERNAL_CHECK_IMPL((condition), #condition) +#define ABSL_QCHECK(condition) \ + ABSL_LOG_INTERNAL_QCHECK_IMPL((condition), #condition) +#define ABSL_PCHECK(condition) \ + ABSL_LOG_INTERNAL_PCHECK_IMPL((condition), #condition) +#define ABSL_DCHECK(condition) \ + ABSL_LOG_INTERNAL_DCHECK_IMPL((condition), #condition) #define ABSL_CHECK_EQ(val1, val2) \ - ABSL_CHECK_EQ_IMPL((val1), #val1, (val2), #val2) + ABSL_LOG_INTERNAL_CHECK_EQ_IMPL((val1), #val1, (val2), #val2) #define ABSL_CHECK_NE(val1, val2) \ - ABSL_CHECK_NE_IMPL((val1), #val1, (val2), #val2) + ABSL_LOG_INTERNAL_CHECK_NE_IMPL((val1), #val1, (val2), #val2) #define ABSL_CHECK_LE(val1, val2) \ - ABSL_CHECK_LE_IMPL((val1), #val1, (val2), #val2) + ABSL_LOG_INTERNAL_CHECK_LE_IMPL((val1), #val1, (val2), #val2) #define ABSL_CHECK_LT(val1, val2) \ - ABSL_CHECK_LT_IMPL((val1), #val1, (val2), #val2) + ABSL_LOG_INTERNAL_CHECK_LT_IMPL((val1), #val1, (val2), #val2) #define ABSL_CHECK_GE(val1, val2) \ - ABSL_CHECK_GE_IMPL((val1), #val1, (val2), #val2) + ABSL_LOG_INTERNAL_CHECK_GE_IMPL((val1), #val1, (val2), #val2) #define ABSL_CHECK_GT(val1, val2) \ - ABSL_CHECK_GT_IMPL((val1), #val1, (val2), #val2) + ABSL_LOG_INTERNAL_CHECK_GT_IMPL((val1), #val1, (val2), #val2) #define ABSL_QCHECK_EQ(val1, val2) \ - ABSL_QCHECK_EQ_IMPL((val1), #val1, (val2), #val2) + ABSL_LOG_INTERNAL_QCHECK_EQ_IMPL((val1), #val1, (val2), #val2) #define ABSL_QCHECK_NE(val1, val2) \ - ABSL_QCHECK_NE_IMPL((val1), #val1, (val2), #val2) + ABSL_LOG_INTERNAL_QCHECK_NE_IMPL((val1), #val1, (val2), #val2) #define ABSL_QCHECK_LE(val1, val2) \ - ABSL_QCHECK_LE_IMPL((val1), #val1, (val2), #val2) + ABSL_LOG_INTERNAL_QCHECK_LE_IMPL((val1), #val1, (val2), #val2) #define ABSL_QCHECK_LT(val1, val2) \ - ABSL_QCHECK_LT_IMPL((val1), #val1, (val2), #val2) + ABSL_LOG_INTERNAL_QCHECK_LT_IMPL((val1), #val1, (val2), #val2) #define ABSL_QCHECK_GE(val1, val2) \ - ABSL_QCHECK_GE_IMPL((val1), #val1, (val2), #val2) + ABSL_LOG_INTERNAL_QCHECK_GE_IMPL((val1), #val1, (val2), #val2) #define ABSL_QCHECK_GT(val1, val2) \ - ABSL_QCHECK_GT_IMPL((val1), #val1, (val2), #val2) + ABSL_LOG_INTERNAL_QCHECK_GT_IMPL((val1), #val1, (val2), #val2) #define ABSL_DCHECK_EQ(val1, val2) \ - ABSL_DCHECK_EQ_IMPL((val1), #val1, (val2), #val2) + ABSL_LOG_INTERNAL_DCHECK_EQ_IMPL((val1), #val1, (val2), #val2) #define ABSL_DCHECK_NE(val1, val2) \ - ABSL_DCHECK_NE_IMPL((val1), #val1, (val2), #val2) + ABSL_LOG_INTERNAL_DCHECK_NE_IMPL((val1), #val1, (val2), #val2) #define ABSL_DCHECK_LE(val1, val2) \ - ABSL_DCHECK_LE_IMPL((val1), #val1, (val2), #val2) + ABSL_LOG_INTERNAL_DCHECK_LE_IMPL((val1), #val1, (val2), #val2) #define ABSL_DCHECK_LT(val1, val2) \ - ABSL_DCHECK_LT_IMPL((val1), #val1, (val2), #val2) + ABSL_LOG_INTERNAL_DCHECK_LT_IMPL((val1), #val1, (val2), #val2) #define ABSL_DCHECK_GE(val1, val2) \ - ABSL_DCHECK_GE_IMPL((val1), #val1, (val2), #val2) + ABSL_LOG_INTERNAL_DCHECK_GE_IMPL((val1), #val1, (val2), #val2) #define ABSL_DCHECK_GT(val1, val2) \ - ABSL_DCHECK_GT_IMPL((val1), #val1, (val2), #val2) + ABSL_LOG_INTERNAL_DCHECK_GT_IMPL((val1), #val1, (val2), #val2) -#define ABSL_CHECK_OK(status) ABSL_CHECK_OK_IMPL((status), #status) -#define ABSL_QCHECK_OK(status) ABSL_QCHECK_OK_IMPL((status), #status) -#define ABSL_DCHECK_OK(status) ABSL_DCHECK_OK_IMPL((status), #status) +#define ABSL_CHECK_OK(status) ABSL_LOG_INTERNAL_CHECK_OK_IMPL((status), #status) +#define ABSL_QCHECK_OK(status) \ + ABSL_LOG_INTERNAL_QCHECK_OK_IMPL((status), #status) +#define ABSL_DCHECK_OK(status) \ + ABSL_LOG_INTERNAL_DCHECK_OK_IMPL((status), #status) -#define ABSL_CHECK_STREQ(s1, s2) ABSL_CHECK_STREQ_IMPL((s1), #s1, (s2), #s2) -#define ABSL_CHECK_STRNE(s1, s2) ABSL_CHECK_STRNE_IMPL((s1), #s1, (s2), #s2) +#define ABSL_CHECK_STREQ(s1, s2) \ + ABSL_LOG_INTERNAL_CHECK_STREQ_IMPL((s1), #s1, (s2), #s2) +#define ABSL_CHECK_STRNE(s1, s2) \ + ABSL_LOG_INTERNAL_CHECK_STRNE_IMPL((s1), #s1, (s2), #s2) #define ABSL_CHECK_STRCASEEQ(s1, s2) \ - ABSL_CHECK_STRCASEEQ_IMPL((s1), #s1, (s2), #s2) + ABSL_LOG_INTERNAL_CHECK_STRCASEEQ_IMPL((s1), #s1, (s2), #s2) #define ABSL_CHECK_STRCASENE(s1, s2) \ - ABSL_CHECK_STRCASENE_IMPL((s1), #s1, (s2), #s2) -#define ABSL_QCHECK_STREQ(s1, s2) ABSL_QCHECK_STREQ_IMPL((s1), #s1, (s2), #s2) -#define ABSL_QCHECK_STRNE(s1, s2) ABSL_QCHECK_STRNE_IMPL((s1), #s1, (s2), #s2) + ABSL_LOG_INTERNAL_CHECK_STRCASENE_IMPL((s1), #s1, (s2), #s2) +#define ABSL_QCHECK_STREQ(s1, s2) \ + ABSL_LOG_INTERNAL_QCHECK_STREQ_IMPL((s1), #s1, (s2), #s2) +#define ABSL_QCHECK_STRNE(s1, s2) \ + ABSL_LOG_INTERNAL_QCHECK_STRNE_IMPL((s1), #s1, (s2), #s2) #define ABSL_QCHECK_STRCASEEQ(s1, s2) \ - ABSL_QCHECK_STRCASEEQ_IMPL((s1), #s1, (s2), #s2) + ABSL_LOG_INTERNAL_QCHECK_STRCASEEQ_IMPL((s1), #s1, (s2), #s2) #define ABSL_QCHECK_STRCASENE(s1, s2) \ - ABSL_QCHECK_STRCASENE_IMPL((s1), #s1, (s2), #s2) -#define ABSL_DCHECK_STREQ(s1, s2) ABSL_DCHECK_STREQ_IMPL((s1), #s1, (s2), #s2) -#define ABSL_DCHECK_STRNE(s1, s2) ABSL_DCHECK_STRNE_IMPL((s1), #s1, (s2), #s2) + ABSL_LOG_INTERNAL_QCHECK_STRCASENE_IMPL((s1), #s1, (s2), #s2) +#define ABSL_DCHECK_STREQ(s1, s2) \ + ABSL_LOG_INTERNAL_DCHECK_STREQ_IMPL((s1), #s1, (s2), #s2) +#define ABSL_DCHECK_STRNE(s1, s2) \ + ABSL_LOG_INTERNAL_DCHECK_STRNE_IMPL((s1), #s1, (s2), #s2) #define ABSL_DCHECK_STRCASEEQ(s1, s2) \ - ABSL_DCHECK_STRCASEEQ_IMPL((s1), #s1, (s2), #s2) + ABSL_LOG_INTERNAL_DCHECK_STRCASEEQ_IMPL((s1), #s1, (s2), #s2) #define ABSL_DCHECK_STRCASENE(s1, s2) \ - ABSL_DCHECK_STRCASENE_IMPL((s1), #s1, (s2), #s2) + ABSL_LOG_INTERNAL_DCHECK_STRCASENE_IMPL((s1), #s1, (s2), #s2) #endif // ABSL_LOG_ABSL_CHECK_H_ diff --git a/absl/log/absl_check_test.cc b/absl/log/absl_check_test.cc index 8ddacdb1..d84940fa 100644 --- a/absl/log/absl_check_test.cc +++ b/absl/log/absl_check_test.cc @@ -55,4 +55,4 @@ #define ABSL_TEST_QCHECK_STRCASENE ABSL_QCHECK_STRCASENE #include "gtest/gtest.h" -#include "absl/log/check_test_impl.h" +#include "absl/log/check_test_impl.inc" diff --git a/absl/log/absl_log.h b/absl/log/absl_log.h index 1c6cf263..0517760b 100644 --- a/absl/log/absl_log.h +++ b/absl/log/absl_log.h @@ -35,60 +35,69 @@ #include "absl/log/internal/log_impl.h" -#define ABSL_LOG(severity) ABSL_LOG_IMPL(_##severity) -#define ABSL_PLOG(severity) ABSL_PLOG_IMPL(_##severity) -#define ABSL_DLOG(severity) ABSL_DLOG_IMPL(_##severity) +#define ABSL_LOG(severity) ABSL_LOG_INTERNAL_LOG_IMPL(_##severity) +#define ABSL_PLOG(severity) ABSL_LOG_INTERNAL_PLOG_IMPL(_##severity) +#define ABSL_DLOG(severity) ABSL_LOG_INTERNAL_DLOG_IMPL(_##severity) #define ABSL_LOG_IF(severity, condition) \ - ABSL_LOG_IF_IMPL(_##severity, condition) + ABSL_LOG_INTERNAL_LOG_IF_IMPL(_##severity, condition) #define ABSL_PLOG_IF(severity, condition) \ - ABSL_PLOG_IF_IMPL(_##severity, condition) + ABSL_LOG_INTERNAL_PLOG_IF_IMPL(_##severity, condition) #define ABSL_DLOG_IF(severity, condition) \ - ABSL_DLOG_IF_IMPL(_##severity, condition) + ABSL_LOG_INTERNAL_DLOG_IF_IMPL(_##severity, condition) -#define ABSL_LOG_EVERY_N(severity, n) ABSL_LOG_EVERY_N_IMPL(_##severity, n) -#define ABSL_LOG_FIRST_N(severity, n) ABSL_LOG_FIRST_N_IMPL(_##severity, n) -#define ABSL_LOG_EVERY_POW_2(severity) ABSL_LOG_EVERY_POW_2_IMPL(_##severity) +#define ABSL_LOG_EVERY_N(severity, n) \ + ABSL_LOG_INTERNAL_LOG_EVERY_N_IMPL(_##severity, n) +#define ABSL_LOG_FIRST_N(severity, n) \ + ABSL_LOG_INTERNAL_LOG_FIRST_N_IMPL(_##severity, n) +#define ABSL_LOG_EVERY_POW_2(severity) \ + ABSL_LOG_INTERNAL_LOG_EVERY_POW_2_IMPL(_##severity) #define ABSL_LOG_EVERY_N_SEC(severity, n_seconds) \ - ABSL_LOG_EVERY_N_SEC_IMPL(_##severity, n_seconds) + ABSL_LOG_INTERNAL_LOG_EVERY_N_SEC_IMPL(_##severity, n_seconds) -#define ABSL_PLOG_EVERY_N(severity, n) ABSL_PLOG_EVERY_N_IMPL(_##severity, n) -#define ABSL_PLOG_FIRST_N(severity, n) ABSL_PLOG_FIRST_N_IMPL(_##severity, n) -#define ABSL_PLOG_EVERY_POW_2(severity) ABSL_PLOG_EVERY_POW_2_IMPL(_##severity) +#define ABSL_PLOG_EVERY_N(severity, n) \ + ABSL_LOG_INTERNAL_PLOG_EVERY_N_IMPL(_##severity, n) +#define ABSL_PLOG_FIRST_N(severity, n) \ + ABSL_LOG_INTERNAL_PLOG_FIRST_N_IMPL(_##severity, n) +#define ABSL_PLOG_EVERY_POW_2(severity) \ + ABSL_LOG_INTERNAL_PLOG_EVERY_POW_2_IMPL(_##severity) #define ABSL_PLOG_EVERY_N_SEC(severity, n_seconds) \ - ABSL_PLOG_EVERY_N_SEC_IMPL(_##severity, n_seconds) + ABSL_LOG_INTERNAL_PLOG_EVERY_N_SEC_IMPL(_##severity, n_seconds) -#define ABSL_DLOG_EVERY_N(severity, n) ABSL_DLOG_EVERY_N_IMPL(_##severity, n) -#define ABSL_DLOG_FIRST_N(severity, n) ABSL_DLOG_FIRST_N_IMPL(_##severity, n) -#define ABSL_DLOG_EVERY_POW_2(severity) ABSL_DLOG_EVERY_POW_2_IMPL(_##severity) +#define ABSL_DLOG_EVERY_N(severity, n) \ + ABSL_LOG_INTERNAL_DLOG_EVERY_N_IMPL(_##severity, n) +#define ABSL_DLOG_FIRST_N(severity, n) \ + ABSL_LOG_INTERNAL_DLOG_FIRST_N_IMPL(_##severity, n) +#define ABSL_DLOG_EVERY_POW_2(severity) \ + ABSL_LOG_INTERNAL_DLOG_EVERY_POW_2_IMPL(_##severity) #define ABSL_DLOG_EVERY_N_SEC(severity, n_seconds) \ - ABSL_DLOG_EVERY_N_SEC_IMPL(_##severity, n_seconds) + ABSL_LOG_INTERNAL_DLOG_EVERY_N_SEC_IMPL(_##severity, n_seconds) #define ABSL_LOG_IF_EVERY_N(severity, condition, n) \ - ABSL_LOG_IF_EVERY_N_IMPL(_##severity, condition, n) + ABSL_LOG_INTERNAL_LOG_IF_EVERY_N_IMPL(_##severity, condition, n) #define ABSL_LOG_IF_FIRST_N(severity, condition, n) \ - ABSL_LOG_IF_FIRST_N_IMPL(_##severity, condition, n) + ABSL_LOG_INTERNAL_LOG_IF_FIRST_N_IMPL(_##severity, condition, n) #define ABSL_LOG_IF_EVERY_POW_2(severity, condition) \ - ABSL_LOG_IF_EVERY_POW_2_IMPL(_##severity, condition) + ABSL_LOG_INTERNAL_LOG_IF_EVERY_POW_2_IMPL(_##severity, condition) #define ABSL_LOG_IF_EVERY_N_SEC(severity, condition, n_seconds) \ - ABSL_LOG_IF_EVERY_N_SEC_IMPL(_##severity, condition, n_seconds) + ABSL_LOG_INTERNAL_LOG_IF_EVERY_N_SEC_IMPL(_##severity, condition, n_seconds) #define ABSL_PLOG_IF_EVERY_N(severity, condition, n) \ - ABSL_PLOG_IF_EVERY_N_IMPL(_##severity, condition, n) + ABSL_LOG_INTERNAL_PLOG_IF_EVERY_N_IMPL(_##severity, condition, n) #define ABSL_PLOG_IF_FIRST_N(severity, condition, n) \ - ABSL_PLOG_IF_FIRST_N_IMPL(_##severity, condition, n) + ABSL_LOG_INTERNAL_PLOG_IF_FIRST_N_IMPL(_##severity, condition, n) #define ABSL_PLOG_IF_EVERY_POW_2(severity, condition) \ - ABSL_PLOG_IF_EVERY_POW_2_IMPL(_##severity, condition) + ABSL_LOG_INTERNAL_PLOG_IF_EVERY_POW_2_IMPL(_##severity, condition) #define ABSL_PLOG_IF_EVERY_N_SEC(severity, condition, n_seconds) \ - ABSL_PLOG_IF_EVERY_N_SEC_IMPL(_##severity, condition, n_seconds) + ABSL_LOG_INTERNAL_PLOG_IF_EVERY_N_SEC_IMPL(_##severity, condition, n_seconds) #define ABSL_DLOG_IF_EVERY_N(severity, condition, n) \ - ABSL_DLOG_IF_EVERY_N_IMPL(_##severity, condition, n) + ABSL_LOG_INTERNAL_DLOG_IF_EVERY_N_IMPL(_##severity, condition, n) #define ABSL_DLOG_IF_FIRST_N(severity, condition, n) \ - ABSL_DLOG_IF_FIRST_N_IMPL(_##severity, condition, n) + ABSL_LOG_INTERNAL_DLOG_IF_FIRST_N_IMPL(_##severity, condition, n) #define ABSL_DLOG_IF_EVERY_POW_2(severity, condition) \ - ABSL_DLOG_IF_EVERY_POW_2_IMPL(_##severity, condition) + ABSL_LOG_INTERNAL_DLOG_IF_EVERY_POW_2_IMPL(_##severity, condition) #define ABSL_DLOG_IF_EVERY_N_SEC(severity, condition, n_seconds) \ - ABSL_DLOG_IF_EVERY_N_SEC_IMPL(_##severity, condition, n_seconds) + ABSL_LOG_INTERNAL_DLOG_IF_EVERY_N_SEC_IMPL(_##severity, condition, n_seconds) #endif // ABSL_LOG_ABSL_LOG_H_ diff --git a/absl/log/absl_log_basic_test.cc b/absl/log/absl_log_basic_test.cc index bc8a787d..3a4b83c1 100644 --- a/absl/log/absl_log_basic_test.cc +++ b/absl/log/absl_log_basic_test.cc @@ -18,4 +18,4 @@ #define ABSL_TEST_LOG ABSL_LOG #include "gtest/gtest.h" -#include "absl/log/log_basic_test_impl.h" +#include "absl/log/log_basic_test_impl.inc" diff --git a/absl/log/check.h b/absl/log/check.h index 33145a57..0a2f2e4e 100644 --- a/absl/log/check.h +++ b/absl/log/check.h @@ -54,7 +54,7 @@ // Might produce a message like: // // Check failed: !cheese.empty() Out of Cheese -#define CHECK(condition) ABSL_CHECK_IMPL((condition), #condition) +#define CHECK(condition) ABSL_LOG_INTERNAL_CHECK_IMPL((condition), #condition) // QCHECK() // @@ -62,7 +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_QCHECK_IMPL((condition), #condition) +#define QCHECK(condition) ABSL_LOG_INTERNAL_QCHECK_IMPL((condition), #condition) // PCHECK() // @@ -77,7 +77,7 @@ // Might produce a message like: // // Check failed: fd != -1 posix is difficult: No such file or directory [2] -#define PCHECK(condition) ABSL_PCHECK_IMPL((condition), #condition) +#define PCHECK(condition) ABSL_LOG_INTERNAL_PCHECK_IMPL((condition), #condition) // DCHECK() // @@ -85,7 +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. -#define DCHECK(condition) ABSL_DCHECK_IMPL((condition), #condition) +#define DCHECK(condition) ABSL_LOG_INTERNAL_DCHECK_IMPL((condition), #condition) // `CHECK_EQ` and friends are syntactic sugar for `CHECK(x == y)` that // automatically output the expression being tested and the evaluated values on @@ -113,24 +113,42 @@ // // WARNING: Passing `NULL` as an argument to `CHECK_EQ` and similar macros does // not compile. Use `nullptr` instead. -#define CHECK_EQ(val1, val2) ABSL_CHECK_EQ_IMPL((val1), #val1, (val2), #val2) -#define CHECK_NE(val1, val2) ABSL_CHECK_NE_IMPL((val1), #val1, (val2), #val2) -#define CHECK_LE(val1, val2) ABSL_CHECK_LE_IMPL((val1), #val1, (val2), #val2) -#define CHECK_LT(val1, val2) ABSL_CHECK_LT_IMPL((val1), #val1, (val2), #val2) -#define CHECK_GE(val1, val2) ABSL_CHECK_GE_IMPL((val1), #val1, (val2), #val2) -#define CHECK_GT(val1, val2) ABSL_CHECK_GT_IMPL((val1), #val1, (val2), #val2) -#define QCHECK_EQ(val1, val2) ABSL_QCHECK_EQ_IMPL((val1), #val1, (val2), #val2) -#define QCHECK_NE(val1, val2) ABSL_QCHECK_NE_IMPL((val1), #val1, (val2), #val2) -#define QCHECK_LE(val1, val2) ABSL_QCHECK_LE_IMPL((val1), #val1, (val2), #val2) -#define QCHECK_LT(val1, val2) ABSL_QCHECK_LT_IMPL((val1), #val1, (val2), #val2) -#define QCHECK_GE(val1, val2) ABSL_QCHECK_GE_IMPL((val1), #val1, (val2), #val2) -#define QCHECK_GT(val1, val2) ABSL_QCHECK_GT_IMPL((val1), #val1, (val2), #val2) -#define DCHECK_EQ(val1, val2) ABSL_DCHECK_EQ_IMPL((val1), #val1, (val2), #val2) -#define DCHECK_NE(val1, val2) ABSL_DCHECK_NE_IMPL((val1), #val1, (val2), #val2) -#define DCHECK_LE(val1, val2) ABSL_DCHECK_LE_IMPL((val1), #val1, (val2), #val2) -#define DCHECK_LT(val1, val2) ABSL_DCHECK_LT_IMPL((val1), #val1, (val2), #val2) -#define DCHECK_GE(val1, val2) ABSL_DCHECK_GE_IMPL((val1), #val1, (val2), #val2) -#define DCHECK_GT(val1, val2) ABSL_DCHECK_GT_IMPL((val1), #val1, (val2), #val2) +#define CHECK_EQ(val1, val2) \ + ABSL_LOG_INTERNAL_CHECK_EQ_IMPL((val1), #val1, (val2), #val2) +#define CHECK_NE(val1, val2) \ + ABSL_LOG_INTERNAL_CHECK_NE_IMPL((val1), #val1, (val2), #val2) +#define CHECK_LE(val1, val2) \ + ABSL_LOG_INTERNAL_CHECK_LE_IMPL((val1), #val1, (val2), #val2) +#define CHECK_LT(val1, val2) \ + ABSL_LOG_INTERNAL_CHECK_LT_IMPL((val1), #val1, (val2), #val2) +#define CHECK_GE(val1, val2) \ + ABSL_LOG_INTERNAL_CHECK_GE_IMPL((val1), #val1, (val2), #val2) +#define CHECK_GT(val1, val2) \ + ABSL_LOG_INTERNAL_CHECK_GT_IMPL((val1), #val1, (val2), #val2) +#define QCHECK_EQ(val1, val2) \ + ABSL_LOG_INTERNAL_QCHECK_EQ_IMPL((val1), #val1, (val2), #val2) +#define QCHECK_NE(val1, val2) \ + ABSL_LOG_INTERNAL_QCHECK_NE_IMPL((val1), #val1, (val2), #val2) +#define QCHECK_LE(val1, val2) \ + ABSL_LOG_INTERNAL_QCHECK_LE_IMPL((val1), #val1, (val2), #val2) +#define QCHECK_LT(val1, val2) \ + ABSL_LOG_INTERNAL_QCHECK_LT_IMPL((val1), #val1, (val2), #val2) +#define QCHECK_GE(val1, val2) \ + ABSL_LOG_INTERNAL_QCHECK_GE_IMPL((val1), #val1, (val2), #val2) +#define QCHECK_GT(val1, val2) \ + ABSL_LOG_INTERNAL_QCHECK_GT_IMPL((val1), #val1, (val2), #val2) +#define DCHECK_EQ(val1, val2) \ + ABSL_LOG_INTERNAL_DCHECK_EQ_IMPL((val1), #val1, (val2), #val2) +#define DCHECK_NE(val1, val2) \ + ABSL_LOG_INTERNAL_DCHECK_NE_IMPL((val1), #val1, (val2), #val2) +#define DCHECK_LE(val1, val2) \ + ABSL_LOG_INTERNAL_DCHECK_LE_IMPL((val1), #val1, (val2), #val2) +#define DCHECK_LT(val1, val2) \ + ABSL_LOG_INTERNAL_DCHECK_LT_IMPL((val1), #val1, (val2), #val2) +#define DCHECK_GE(val1, val2) \ + ABSL_LOG_INTERNAL_DCHECK_GE_IMPL((val1), #val1, (val2), #val2) +#define DCHECK_GT(val1, val2) \ + ABSL_LOG_INTERNAL_DCHECK_GT_IMPL((val1), #val1, (val2), #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 @@ -146,9 +164,9 @@ // Might produce a message like: // // Check failed: FunctionReturnsStatus(x, y, z) is OK (ABORTED: timeout) oops! -#define CHECK_OK(status) ABSL_CHECK_OK_IMPL((status), #status) -#define QCHECK_OK(status) ABSL_QCHECK_OK_IMPL((status), #status) -#define DCHECK_OK(status) ABSL_DCHECK_OK_IMPL((status), #status) +#define CHECK_OK(status) ABSL_LOG_INTERNAL_CHECK_OK_IMPL((status), #status) +#define QCHECK_OK(status) ABSL_LOG_INTERNAL_QCHECK_OK_IMPL((status), #status) +#define DCHECK_OK(status) ABSL_LOG_INTERNAL_DCHECK_OK_IMPL((status), #status) // `CHECK_STREQ` and friends provide `CHECK_EQ` functionality for C strings, // i.e., nul-terminated char arrays. The `CASE` versions are case-insensitive. @@ -163,21 +181,29 @@ // Example: // // CHECK_STREQ(Foo().c_str(), Bar().c_str()); -#define CHECK_STREQ(s1, s2) ABSL_CHECK_STREQ_IMPL((s1), #s1, (s2), #s2) -#define CHECK_STRNE(s1, s2) ABSL_CHECK_STRNE_IMPL((s1), #s1, (s2), #s2) -#define CHECK_STRCASEEQ(s1, s2) ABSL_CHECK_STRCASEEQ_IMPL((s1), #s1, (s2), #s2) -#define CHECK_STRCASENE(s1, s2) ABSL_CHECK_STRCASENE_IMPL((s1), #s1, (s2), #s2) -#define QCHECK_STREQ(s1, s2) ABSL_QCHECK_STREQ_IMPL((s1), #s1, (s2), #s2) -#define QCHECK_STRNE(s1, s2) ABSL_QCHECK_STRNE_IMPL((s1), #s1, (s2), #s2) +#define CHECK_STREQ(s1, s2) \ + ABSL_LOG_INTERNAL_CHECK_STREQ_IMPL((s1), #s1, (s2), #s2) +#define CHECK_STRNE(s1, s2) \ + ABSL_LOG_INTERNAL_CHECK_STRNE_IMPL((s1), #s1, (s2), #s2) +#define CHECK_STRCASEEQ(s1, s2) \ + ABSL_LOG_INTERNAL_CHECK_STRCASEEQ_IMPL((s1), #s1, (s2), #s2) +#define CHECK_STRCASENE(s1, s2) \ + ABSL_LOG_INTERNAL_CHECK_STRCASENE_IMPL((s1), #s1, (s2), #s2) +#define QCHECK_STREQ(s1, s2) \ + ABSL_LOG_INTERNAL_QCHECK_STREQ_IMPL((s1), #s1, (s2), #s2) +#define QCHECK_STRNE(s1, s2) \ + ABSL_LOG_INTERNAL_QCHECK_STRNE_IMPL((s1), #s1, (s2), #s2) #define QCHECK_STRCASEEQ(s1, s2) \ - ABSL_QCHECK_STRCASEEQ_IMPL((s1), #s1, (s2), #s2) + ABSL_LOG_INTERNAL_QCHECK_STRCASEEQ_IMPL((s1), #s1, (s2), #s2) #define QCHECK_STRCASENE(s1, s2) \ - ABSL_QCHECK_STRCASENE_IMPL((s1), #s1, (s2), #s2) -#define DCHECK_STREQ(s1, s2) ABSL_DCHECK_STREQ_IMPL((s1), #s1, (s2), #s2) -#define DCHECK_STRNE(s1, s2) ABSL_DCHECK_STRNE_IMPL((s1), #s1, (s2), #s2) + ABSL_LOG_INTERNAL_QCHECK_STRCASENE_IMPL((s1), #s1, (s2), #s2) +#define DCHECK_STREQ(s1, s2) \ + ABSL_LOG_INTERNAL_DCHECK_STREQ_IMPL((s1), #s1, (s2), #s2) +#define DCHECK_STRNE(s1, s2) \ + ABSL_LOG_INTERNAL_DCHECK_STRNE_IMPL((s1), #s1, (s2), #s2) #define DCHECK_STRCASEEQ(s1, s2) \ - ABSL_DCHECK_STRCASEEQ_IMPL((s1), #s1, (s2), #s2) + ABSL_LOG_INTERNAL_DCHECK_STRCASEEQ_IMPL((s1), #s1, (s2), #s2) #define DCHECK_STRCASENE(s1, s2) \ - ABSL_DCHECK_STRCASENE_IMPL((s1), #s1, (s2), #s2) + ABSL_LOG_INTERNAL_DCHECK_STRCASENE_IMPL((s1), #s1, (s2), #s2) #endif // ABSL_LOG_CHECK_H_ diff --git a/absl/log/check_test.cc b/absl/log/check_test.cc index f44a686e..ef415bd4 100644 --- a/absl/log/check_test.cc +++ b/absl/log/check_test.cc @@ -55,4 +55,4 @@ #define ABSL_TEST_QCHECK_STRCASENE QCHECK_STRCASENE #include "gtest/gtest.h" -#include "absl/log/check_test_impl.h" +#include "absl/log/check_test_impl.inc" diff --git a/absl/log/check_test_impl.h b/absl/log/check_test_impl.h deleted file mode 100644 index d5c0aee4..00000000 --- a/absl/log/check_test_impl.h +++ /dev/null @@ -1,528 +0,0 @@ -// -// 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_CHECK_TEST_IMPL_H_ -#define ABSL_LOG_CHECK_TEST_IMPL_H_ - -// Verify that both sets of macros behave identically by parameterizing the -// entire test file. -#ifndef ABSL_TEST_CHECK -#error ABSL_TEST_CHECK must be defined for these tests to work. -#endif - -#include -#include - -#include "gmock/gmock.h" -#include "gtest/gtest.h" -#include "absl/base/attributes.h" -#include "absl/base/config.h" -#include "absl/log/internal/test_helpers.h" -#include "absl/status/status.h" - -// NOLINTBEGIN(misc-definitions-in-headers) - -namespace absl_log_internal { - -using ::testing::AllOf; -using ::testing::HasSubstr; -using ::testing::Not; - -auto* test_env ABSL_ATTRIBUTE_UNUSED = ::testing::AddGlobalTestEnvironment( - new absl::log_internal::LogTestEnvironment); - -#if GTEST_HAS_DEATH_TEST - -TEST(CHECKDeathTest, TestBasicValues) { - ABSL_TEST_CHECK(true); - - EXPECT_DEATH(ABSL_TEST_CHECK(false), "Check failed: false"); - - int i = 2; - ABSL_TEST_CHECK(i != 3); // NOLINT -} - -#endif // GTEST_HAS_DEATH_TEST - -TEST(CHECKTest, TestLogicExpressions) { - int i = 5; - ABSL_TEST_CHECK(i > 0 && i < 10); - ABSL_TEST_CHECK(i < 0 || i > 3); -} - -#if ABSL_INTERNAL_CPLUSPLUS_LANG >= 201703L -ABSL_CONST_INIT const auto global_var_check = [](int i) { - ABSL_TEST_CHECK(i > 0); // NOLINT - return i + 1; -}(3); - -ABSL_CONST_INIT const auto global_var = [](int i) { - ABSL_TEST_CHECK_GE(i, 0); // NOLINT - return i + 1; -}(global_var_check); -#endif // ABSL_INTERNAL_CPLUSPLUS_LANG - -TEST(CHECKTest, TestPlacementsInCompoundStatements) { - // check placement inside if/else clauses - if (true) ABSL_TEST_CHECK(true); - - if (false) - ; // NOLINT - else - ABSL_TEST_CHECK(true); - - switch (0) - case 0: - ABSL_TEST_CHECK(true); // NOLINT - -#if ABSL_INTERNAL_CPLUSPLUS_LANG >= 201703L - constexpr auto var = [](int i) { - ABSL_TEST_CHECK(i > 0); // NOLINT - return i + 1; - }(global_var); - (void)var; -#endif // ABSL_INTERNAL_CPLUSPLUS_LANG -} - -TEST(CHECKTest, TestBoolConvertible) { - struct Tester { - } tester; - ABSL_TEST_CHECK([&]() { return &tester; }()); -} - -#if GTEST_HAS_DEATH_TEST - -TEST(CHECKDeathTest, TestChecksWithSideEffects) { - int var = 0; - ABSL_TEST_CHECK([&var]() { - ++var; - return true; - }()); - EXPECT_EQ(var, 1); - - EXPECT_DEATH(ABSL_TEST_CHECK([&var]() { - ++var; - return false; - }()) << var, - "Check failed: .* 2"); -} - -#endif // GTEST_HAS_DEATH_TEST - -template -constexpr int sum() { - return a + b; -} -#define MACRO_ONE 1 -#define TEMPLATE_SUM(a, b) sum() -#define CONCAT(a, b) a b -#define IDENTITY(x) x - -TEST(CHECKTest, TestPassingMacroExpansion) { - ABSL_TEST_CHECK(IDENTITY(true)); - ABSL_TEST_CHECK_EQ(TEMPLATE_SUM(MACRO_ONE, 2), 3); - ABSL_TEST_CHECK_STREQ(CONCAT("x", "y"), "xy"); -} - -#if GTEST_HAS_DEATH_TEST - -TEST(CHECKTest, TestMacroExpansionInMessage) { - auto MessageGen = []() { ABSL_TEST_CHECK(IDENTITY(false)); }; - EXPECT_DEATH(MessageGen(), HasSubstr("IDENTITY(false)")); -} - -TEST(CHECKTest, TestNestedMacroExpansionInMessage) { - EXPECT_DEATH(ABSL_TEST_CHECK(IDENTITY(false)), HasSubstr("IDENTITY(false)")); -} - -TEST(CHECKTest, TestMacroExpansionCompare) { - EXPECT_DEATH(ABSL_TEST_CHECK_EQ(IDENTITY(false), IDENTITY(true)), - HasSubstr("IDENTITY(false) == IDENTITY(true)")); - EXPECT_DEATH(ABSL_TEST_CHECK_GT(IDENTITY(1), IDENTITY(2)), - HasSubstr("IDENTITY(1) > IDENTITY(2)")); -} - -TEST(CHECKTest, TestMacroExpansionStrCompare) { - EXPECT_DEATH(ABSL_TEST_CHECK_STREQ(IDENTITY("x"), IDENTITY("y")), - HasSubstr("IDENTITY(\"x\") == IDENTITY(\"y\")")); - EXPECT_DEATH(ABSL_TEST_CHECK_STRCASENE(IDENTITY("a"), IDENTITY("A")), - HasSubstr("IDENTITY(\"a\") != IDENTITY(\"A\")")); -} - -TEST(CHECKTest, TestMacroExpansionStatus) { - EXPECT_DEATH( - ABSL_TEST_CHECK_OK(IDENTITY(absl::FailedPreconditionError("message"))), - HasSubstr("IDENTITY(absl::FailedPreconditionError(\"message\"))")); -} - -TEST(CHECKTest, TestMacroExpansionComma) { - EXPECT_DEATH(ABSL_TEST_CHECK(TEMPLATE_SUM(MACRO_ONE, 2) == 4), - HasSubstr("TEMPLATE_SUM(MACRO_ONE, 2) == 4")); -} - -TEST(CHECKTest, TestMacroExpansionCommaCompare) { - EXPECT_DEATH( - ABSL_TEST_CHECK_EQ(TEMPLATE_SUM(2, MACRO_ONE), TEMPLATE_SUM(3, 2)), - HasSubstr("TEMPLATE_SUM(2, MACRO_ONE) == TEMPLATE_SUM(3, 2)")); - EXPECT_DEATH( - ABSL_TEST_CHECK_GT(TEMPLATE_SUM(2, MACRO_ONE), TEMPLATE_SUM(3, 2)), - HasSubstr("TEMPLATE_SUM(2, MACRO_ONE) > TEMPLATE_SUM(3, 2)")); -} - -TEST(CHECKTest, TestMacroExpansionCommaStrCompare) { - EXPECT_DEATH(ABSL_TEST_CHECK_STREQ(CONCAT("x", "y"), "z"), - HasSubstr("CONCAT(\"x\", \"y\") == \"z\"")); - EXPECT_DEATH(ABSL_TEST_CHECK_STRNE(CONCAT("x", "y"), "xy"), - HasSubstr("CONCAT(\"x\", \"y\") != \"xy\"")); -} - -#endif // GTEST_HAS_DEATH_TEST - -#undef TEMPLATE_SUM -#undef CONCAT -#undef MACRO -#undef ONE - -#if GTEST_HAS_DEATH_TEST - -TEST(CHECKDeachTest, TestOrderOfInvocationsBetweenCheckAndMessage) { - int counter = 0; - - auto GetStr = [&counter]() -> std::string { - return counter++ == 0 ? "" : "non-empty"; - }; - - EXPECT_DEATH(ABSL_TEST_CHECK(!GetStr().empty()) << GetStr(), - HasSubstr("non-empty")); -} - -TEST(CHECKTest, TestSecondaryFailure) { - auto FailingRoutine = []() { - ABSL_TEST_CHECK(false) << "Secondary"; - return false; - }; - EXPECT_DEATH(ABSL_TEST_CHECK(FailingRoutine()) << "Primary", - AllOf(HasSubstr("Secondary"), Not(HasSubstr("Primary")))); -} - -TEST(CHECKTest, TestSecondaryFailureInMessage) { - auto MessageGen = []() { - ABSL_TEST_CHECK(false) << "Secondary"; - return "Primary"; - }; - EXPECT_DEATH(ABSL_TEST_CHECK(false) << MessageGen(), - AllOf(HasSubstr("Secondary"), Not(HasSubstr("Primary")))); -} - -#endif // GTEST_HAS_DEATH_TEST - -TEST(CHECKTest, TestBinaryChecksWithPrimitives) { - ABSL_TEST_CHECK_EQ(1, 1); - ABSL_TEST_CHECK_NE(1, 2); - ABSL_TEST_CHECK_GE(1, 1); - ABSL_TEST_CHECK_GE(2, 1); - ABSL_TEST_CHECK_LE(1, 1); - ABSL_TEST_CHECK_LE(1, 2); - ABSL_TEST_CHECK_GT(2, 1); - ABSL_TEST_CHECK_LT(1, 2); -} - -// For testing using CHECK*() on anonymous enums. -enum { CASE_A, CASE_B }; - -TEST(CHECKTest, TestBinaryChecksWithEnumValues) { - // Tests using CHECK*() on anonymous enums. - ABSL_TEST_CHECK_EQ(CASE_A, CASE_A); - ABSL_TEST_CHECK_NE(CASE_A, CASE_B); - ABSL_TEST_CHECK_GE(CASE_A, CASE_A); - ABSL_TEST_CHECK_GE(CASE_B, CASE_A); - ABSL_TEST_CHECK_LE(CASE_A, CASE_A); - ABSL_TEST_CHECK_LE(CASE_A, CASE_B); - ABSL_TEST_CHECK_GT(CASE_B, CASE_A); - ABSL_TEST_CHECK_LT(CASE_A, CASE_B); -} - -TEST(CHECKTest, TestBinaryChecksWithNullptr) { - const void* p_null = nullptr; - const void* p_not_null = &p_null; - ABSL_TEST_CHECK_EQ(p_null, nullptr); - ABSL_TEST_CHECK_EQ(nullptr, p_null); - ABSL_TEST_CHECK_NE(p_not_null, nullptr); - ABSL_TEST_CHECK_NE(nullptr, p_not_null); -} - -#if GTEST_HAS_DEATH_TEST - -// Test logging of various char-typed values by failing CHECK*(). -TEST(CHECKDeathTest, TestComparingCharsValues) { - { - char a = ';'; - char b = 'b'; - EXPECT_DEATH(ABSL_TEST_CHECK_EQ(a, b), - "Check failed: a == b \\(';' vs. 'b'\\)"); - b = 1; - EXPECT_DEATH(ABSL_TEST_CHECK_EQ(a, b), - "Check failed: a == b \\(';' vs. char value 1\\)"); - } - { - signed char a = ';'; - signed char b = 'b'; - EXPECT_DEATH(ABSL_TEST_CHECK_EQ(a, b), - "Check failed: a == b \\(';' vs. 'b'\\)"); - b = -128; - EXPECT_DEATH(ABSL_TEST_CHECK_EQ(a, b), - "Check failed: a == b \\(';' vs. signed char value -128\\)"); - } - { - unsigned char a = ';'; - unsigned char b = 'b'; - EXPECT_DEATH(ABSL_TEST_CHECK_EQ(a, b), - "Check failed: a == b \\(';' vs. 'b'\\)"); - b = 128; - EXPECT_DEATH(ABSL_TEST_CHECK_EQ(a, b), - "Check failed: a == b \\(';' vs. unsigned char value 128\\)"); - } -} - -TEST(CHECKDeathTest, TestNullValuesAreReportedCleanly) { - const char* a = nullptr; - const char* b = nullptr; - EXPECT_DEATH(ABSL_TEST_CHECK_NE(a, b), - "Check failed: a != b \\(\\(null\\) vs. \\(null\\)\\)"); - - a = "xx"; - EXPECT_DEATH(ABSL_TEST_CHECK_EQ(a, b), - "Check failed: a == b \\(xx vs. \\(null\\)\\)"); - EXPECT_DEATH(ABSL_TEST_CHECK_EQ(b, a), - "Check failed: b == a \\(\\(null\\) vs. xx\\)"); - - std::nullptr_t n{}; - EXPECT_DEATH(ABSL_TEST_CHECK_NE(n, nullptr), - "Check failed: n != nullptr \\(\\(null\\) vs. \\(null\\)\\)"); -} - -#endif // GTEST_HAS_DEATH_TEST - -TEST(CHECKTest, TestSTREQ) { - ABSL_TEST_CHECK_STREQ("this", "this"); - ABSL_TEST_CHECK_STREQ(nullptr, nullptr); - ABSL_TEST_CHECK_STRCASEEQ("this", "tHiS"); - ABSL_TEST_CHECK_STRCASEEQ(nullptr, nullptr); - ABSL_TEST_CHECK_STRNE("this", "tHiS"); - ABSL_TEST_CHECK_STRNE("this", nullptr); - ABSL_TEST_CHECK_STRCASENE("this", "that"); - ABSL_TEST_CHECK_STRCASENE(nullptr, "that"); - ABSL_TEST_CHECK_STREQ((std::string("a") + "b").c_str(), "ab"); - ABSL_TEST_CHECK_STREQ(std::string("test").c_str(), - (std::string("te") + std::string("st")).c_str()); -} - -TEST(CHECKTest, TestComparisonPlacementsInCompoundStatements) { - // check placement inside if/else clauses - if (true) ABSL_TEST_CHECK_EQ(1, 1); - if (true) ABSL_TEST_CHECK_STREQ("c", "c"); - - if (false) - ; // NOLINT - else - ABSL_TEST_CHECK_LE(0, 1); - - if (false) - ; // NOLINT - else - ABSL_TEST_CHECK_STRNE("a", "b"); - - switch (0) - case 0: - ABSL_TEST_CHECK_NE(1, 0); - - switch (0) - case 0: - ABSL_TEST_CHECK_STRCASEEQ("A", "a"); - -#if ABSL_INTERNAL_CPLUSPLUS_LANG >= 201703L - constexpr auto var = [](int i) { - ABSL_TEST_CHECK_GT(i, 0); - return i + 1; - }(global_var); - (void)var; - - // CHECK_STR... checks are not supported in constexpr routines. - // constexpr auto var2 = [](int i) { - // ABSL_TEST_CHECK_STRNE("c", "d"); - // return i + 1; - // }(global_var); - -#if defined(__GNUC__) - int var3 = (({ ABSL_TEST_CHECK_LE(1, 2); }), global_var < 10) ? 1 : 0; - (void)var3; - - int var4 = (({ ABSL_TEST_CHECK_STREQ("a", "a"); }), global_var < 10) ? 1 : 0; - (void)var4; -#endif // __GNUC__ -#endif // ABSL_INTERNAL_CPLUSPLUS_LANG -} - -TEST(CHECKTest, TestDCHECK) { -#ifdef NDEBUG - ABSL_TEST_DCHECK(1 == 2) << " DCHECK's shouldn't be compiled in normal mode"; -#endif - ABSL_TEST_DCHECK(1 == 1); // NOLINT(readability/check) - ABSL_TEST_DCHECK_EQ(1, 1); - ABSL_TEST_DCHECK_NE(1, 2); - ABSL_TEST_DCHECK_GE(1, 1); - ABSL_TEST_DCHECK_GE(2, 1); - ABSL_TEST_DCHECK_LE(1, 1); - ABSL_TEST_DCHECK_LE(1, 2); - ABSL_TEST_DCHECK_GT(2, 1); - ABSL_TEST_DCHECK_LT(1, 2); - - // Test DCHECK on std::nullptr_t - const void* p_null = nullptr; - const void* p_not_null = &p_null; - ABSL_TEST_DCHECK_EQ(p_null, nullptr); - ABSL_TEST_DCHECK_EQ(nullptr, p_null); - ABSL_TEST_DCHECK_NE(p_not_null, nullptr); - ABSL_TEST_DCHECK_NE(nullptr, p_not_null); -} - -TEST(CHECKTest, TestQCHECK) { - // The tests that QCHECK does the same as CHECK - ABSL_TEST_QCHECK(1 == 1); // NOLINT(readability/check) - ABSL_TEST_QCHECK_EQ(1, 1); - ABSL_TEST_QCHECK_NE(1, 2); - ABSL_TEST_QCHECK_GE(1, 1); - ABSL_TEST_QCHECK_GE(2, 1); - ABSL_TEST_QCHECK_LE(1, 1); - ABSL_TEST_QCHECK_LE(1, 2); - ABSL_TEST_QCHECK_GT(2, 1); - ABSL_TEST_QCHECK_LT(1, 2); - - // Tests using QCHECK*() on anonymous enums. - ABSL_TEST_QCHECK_EQ(CASE_A, CASE_A); - ABSL_TEST_QCHECK_NE(CASE_A, CASE_B); - ABSL_TEST_QCHECK_GE(CASE_A, CASE_A); - ABSL_TEST_QCHECK_GE(CASE_B, CASE_A); - ABSL_TEST_QCHECK_LE(CASE_A, CASE_A); - ABSL_TEST_QCHECK_LE(CASE_A, CASE_B); - ABSL_TEST_QCHECK_GT(CASE_B, CASE_A); - ABSL_TEST_QCHECK_LT(CASE_A, CASE_B); -} - -TEST(CHECKTest, TestQCHECKPlacementsInCompoundStatements) { - // check placement inside if/else clauses - if (true) ABSL_TEST_QCHECK(true); - - if (false) - ; // NOLINT - else - ABSL_TEST_QCHECK(true); - - if (false) - ; // NOLINT - else - ABSL_TEST_QCHECK(true); - - switch (0) - case 0: - ABSL_TEST_QCHECK(true); - -#if ABSL_INTERNAL_CPLUSPLUS_LANG >= 201703L - constexpr auto var = [](int i) { - ABSL_TEST_QCHECK(i > 0); // NOLINT - return i + 1; - }(global_var); - (void)var; - -#if defined(__GNUC__) - int var2 = (({ ABSL_TEST_CHECK_LE(1, 2); }), global_var < 10) ? 1 : 0; - (void)var2; -#endif // __GNUC__ -#endif // ABSL_INTERNAL_CPLUSPLUS_LANG -} - -class ComparableType { - public: - explicit ComparableType(int v) : v_(v) {} - - void MethodWithCheck(int i) { - ABSL_TEST_CHECK_EQ(*this, i); - ABSL_TEST_CHECK_EQ(i, *this); - } - - int Get() const { return v_; } - - private: - friend bool operator==(const ComparableType& lhs, const ComparableType& rhs) { - return lhs.v_ == rhs.v_; - } - friend bool operator!=(const ComparableType& lhs, const ComparableType& rhs) { - return lhs.v_ != rhs.v_; - } - friend bool operator<(const ComparableType& lhs, const ComparableType& rhs) { - return lhs.v_ < rhs.v_; - } - friend bool operator<=(const ComparableType& lhs, const ComparableType& rhs) { - return lhs.v_ <= rhs.v_; - } - friend bool operator>(const ComparableType& lhs, const ComparableType& rhs) { - return lhs.v_ > rhs.v_; - } - friend bool operator>=(const ComparableType& lhs, const ComparableType& rhs) { - return lhs.v_ >= rhs.v_; - } - friend bool operator==(const ComparableType& lhs, int rhs) { - return lhs.v_ == rhs; - } - friend bool operator==(int lhs, const ComparableType& rhs) { - return lhs == rhs.v_; - } - - friend std::ostream& operator<<(std::ostream& out, const ComparableType& v) { - return out << "ComparableType{" << v.Get() << "}"; - } - - int v_; -}; - -TEST(CHECKTest, TestUserDefinedCompOp) { - ABSL_TEST_CHECK_EQ(ComparableType{0}, ComparableType{0}); - ABSL_TEST_CHECK_NE(ComparableType{1}, ComparableType{2}); - ABSL_TEST_CHECK_LT(ComparableType{1}, ComparableType{2}); - ABSL_TEST_CHECK_LE(ComparableType{1}, ComparableType{2}); - ABSL_TEST_CHECK_GT(ComparableType{2}, ComparableType{1}); - ABSL_TEST_CHECK_GE(ComparableType{2}, ComparableType{2}); -} - -TEST(CHECKTest, TestCheckInMethod) { - ComparableType v{1}; - v.MethodWithCheck(1); -} - -TEST(CHECKDeathTest, TestUserDefinedStreaming) { - ComparableType v1{1}; - ComparableType v2{2}; - - EXPECT_DEATH( - ABSL_TEST_CHECK_EQ(v1, v2), - HasSubstr( - "Check failed: v1 == v2 (ComparableType{1} vs. ComparableType{2})")); -} - -} // namespace absl_log_internal - -// NOLINTEND(misc-definitions-in-headers) - -#endif // ABSL_LOG_CHECK_TEST_IMPL_H_ diff --git a/absl/log/check_test_impl.inc b/absl/log/check_test_impl.inc new file mode 100644 index 00000000..d5c0aee4 --- /dev/null +++ b/absl/log/check_test_impl.inc @@ -0,0 +1,528 @@ +// +// 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_CHECK_TEST_IMPL_H_ +#define ABSL_LOG_CHECK_TEST_IMPL_H_ + +// Verify that both sets of macros behave identically by parameterizing the +// entire test file. +#ifndef ABSL_TEST_CHECK +#error ABSL_TEST_CHECK must be defined for these tests to work. +#endif + +#include +#include + +#include "gmock/gmock.h" +#include "gtest/gtest.h" +#include "absl/base/attributes.h" +#include "absl/base/config.h" +#include "absl/log/internal/test_helpers.h" +#include "absl/status/status.h" + +// NOLINTBEGIN(misc-definitions-in-headers) + +namespace absl_log_internal { + +using ::testing::AllOf; +using ::testing::HasSubstr; +using ::testing::Not; + +auto* test_env ABSL_ATTRIBUTE_UNUSED = ::testing::AddGlobalTestEnvironment( + new absl::log_internal::LogTestEnvironment); + +#if GTEST_HAS_DEATH_TEST + +TEST(CHECKDeathTest, TestBasicValues) { + ABSL_TEST_CHECK(true); + + EXPECT_DEATH(ABSL_TEST_CHECK(false), "Check failed: false"); + + int i = 2; + ABSL_TEST_CHECK(i != 3); // NOLINT +} + +#endif // GTEST_HAS_DEATH_TEST + +TEST(CHECKTest, TestLogicExpressions) { + int i = 5; + ABSL_TEST_CHECK(i > 0 && i < 10); + ABSL_TEST_CHECK(i < 0 || i > 3); +} + +#if ABSL_INTERNAL_CPLUSPLUS_LANG >= 201703L +ABSL_CONST_INIT const auto global_var_check = [](int i) { + ABSL_TEST_CHECK(i > 0); // NOLINT + return i + 1; +}(3); + +ABSL_CONST_INIT const auto global_var = [](int i) { + ABSL_TEST_CHECK_GE(i, 0); // NOLINT + return i + 1; +}(global_var_check); +#endif // ABSL_INTERNAL_CPLUSPLUS_LANG + +TEST(CHECKTest, TestPlacementsInCompoundStatements) { + // check placement inside if/else clauses + if (true) ABSL_TEST_CHECK(true); + + if (false) + ; // NOLINT + else + ABSL_TEST_CHECK(true); + + switch (0) + case 0: + ABSL_TEST_CHECK(true); // NOLINT + +#if ABSL_INTERNAL_CPLUSPLUS_LANG >= 201703L + constexpr auto var = [](int i) { + ABSL_TEST_CHECK(i > 0); // NOLINT + return i + 1; + }(global_var); + (void)var; +#endif // ABSL_INTERNAL_CPLUSPLUS_LANG +} + +TEST(CHECKTest, TestBoolConvertible) { + struct Tester { + } tester; + ABSL_TEST_CHECK([&]() { return &tester; }()); +} + +#if GTEST_HAS_DEATH_TEST + +TEST(CHECKDeathTest, TestChecksWithSideEffects) { + int var = 0; + ABSL_TEST_CHECK([&var]() { + ++var; + return true; + }()); + EXPECT_EQ(var, 1); + + EXPECT_DEATH(ABSL_TEST_CHECK([&var]() { + ++var; + return false; + }()) << var, + "Check failed: .* 2"); +} + +#endif // GTEST_HAS_DEATH_TEST + +template +constexpr int sum() { + return a + b; +} +#define MACRO_ONE 1 +#define TEMPLATE_SUM(a, b) sum() +#define CONCAT(a, b) a b +#define IDENTITY(x) x + +TEST(CHECKTest, TestPassingMacroExpansion) { + ABSL_TEST_CHECK(IDENTITY(true)); + ABSL_TEST_CHECK_EQ(TEMPLATE_SUM(MACRO_ONE, 2), 3); + ABSL_TEST_CHECK_STREQ(CONCAT("x", "y"), "xy"); +} + +#if GTEST_HAS_DEATH_TEST + +TEST(CHECKTest, TestMacroExpansionInMessage) { + auto MessageGen = []() { ABSL_TEST_CHECK(IDENTITY(false)); }; + EXPECT_DEATH(MessageGen(), HasSubstr("IDENTITY(false)")); +} + +TEST(CHECKTest, TestNestedMacroExpansionInMessage) { + EXPECT_DEATH(ABSL_TEST_CHECK(IDENTITY(false)), HasSubstr("IDENTITY(false)")); +} + +TEST(CHECKTest, TestMacroExpansionCompare) { + EXPECT_DEATH(ABSL_TEST_CHECK_EQ(IDENTITY(false), IDENTITY(true)), + HasSubstr("IDENTITY(false) == IDENTITY(true)")); + EXPECT_DEATH(ABSL_TEST_CHECK_GT(IDENTITY(1), IDENTITY(2)), + HasSubstr("IDENTITY(1) > IDENTITY(2)")); +} + +TEST(CHECKTest, TestMacroExpansionStrCompare) { + EXPECT_DEATH(ABSL_TEST_CHECK_STREQ(IDENTITY("x"), IDENTITY("y")), + HasSubstr("IDENTITY(\"x\") == IDENTITY(\"y\")")); + EXPECT_DEATH(ABSL_TEST_CHECK_STRCASENE(IDENTITY("a"), IDENTITY("A")), + HasSubstr("IDENTITY(\"a\") != IDENTITY(\"A\")")); +} + +TEST(CHECKTest, TestMacroExpansionStatus) { + EXPECT_DEATH( + ABSL_TEST_CHECK_OK(IDENTITY(absl::FailedPreconditionError("message"))), + HasSubstr("IDENTITY(absl::FailedPreconditionError(\"message\"))")); +} + +TEST(CHECKTest, TestMacroExpansionComma) { + EXPECT_DEATH(ABSL_TEST_CHECK(TEMPLATE_SUM(MACRO_ONE, 2) == 4), + HasSubstr("TEMPLATE_SUM(MACRO_ONE, 2) == 4")); +} + +TEST(CHECKTest, TestMacroExpansionCommaCompare) { + EXPECT_DEATH( + ABSL_TEST_CHECK_EQ(TEMPLATE_SUM(2, MACRO_ONE), TEMPLATE_SUM(3, 2)), + HasSubstr("TEMPLATE_SUM(2, MACRO_ONE) == TEMPLATE_SUM(3, 2)")); + EXPECT_DEATH( + ABSL_TEST_CHECK_GT(TEMPLATE_SUM(2, MACRO_ONE), TEMPLATE_SUM(3, 2)), + HasSubstr("TEMPLATE_SUM(2, MACRO_ONE) > TEMPLATE_SUM(3, 2)")); +} + +TEST(CHECKTest, TestMacroExpansionCommaStrCompare) { + EXPECT_DEATH(ABSL_TEST_CHECK_STREQ(CONCAT("x", "y"), "z"), + HasSubstr("CONCAT(\"x\", \"y\") == \"z\"")); + EXPECT_DEATH(ABSL_TEST_CHECK_STRNE(CONCAT("x", "y"), "xy"), + HasSubstr("CONCAT(\"x\", \"y\") != \"xy\"")); +} + +#endif // GTEST_HAS_DEATH_TEST + +#undef TEMPLATE_SUM +#undef CONCAT +#undef MACRO +#undef ONE + +#if GTEST_HAS_DEATH_TEST + +TEST(CHECKDeachTest, TestOrderOfInvocationsBetweenCheckAndMessage) { + int counter = 0; + + auto GetStr = [&counter]() -> std::string { + return counter++ == 0 ? "" : "non-empty"; + }; + + EXPECT_DEATH(ABSL_TEST_CHECK(!GetStr().empty()) << GetStr(), + HasSubstr("non-empty")); +} + +TEST(CHECKTest, TestSecondaryFailure) { + auto FailingRoutine = []() { + ABSL_TEST_CHECK(false) << "Secondary"; + return false; + }; + EXPECT_DEATH(ABSL_TEST_CHECK(FailingRoutine()) << "Primary", + AllOf(HasSubstr("Secondary"), Not(HasSubstr("Primary")))); +} + +TEST(CHECKTest, TestSecondaryFailureInMessage) { + auto MessageGen = []() { + ABSL_TEST_CHECK(false) << "Secondary"; + return "Primary"; + }; + EXPECT_DEATH(ABSL_TEST_CHECK(false) << MessageGen(), + AllOf(HasSubstr("Secondary"), Not(HasSubstr("Primary")))); +} + +#endif // GTEST_HAS_DEATH_TEST + +TEST(CHECKTest, TestBinaryChecksWithPrimitives) { + ABSL_TEST_CHECK_EQ(1, 1); + ABSL_TEST_CHECK_NE(1, 2); + ABSL_TEST_CHECK_GE(1, 1); + ABSL_TEST_CHECK_GE(2, 1); + ABSL_TEST_CHECK_LE(1, 1); + ABSL_TEST_CHECK_LE(1, 2); + ABSL_TEST_CHECK_GT(2, 1); + ABSL_TEST_CHECK_LT(1, 2); +} + +// For testing using CHECK*() on anonymous enums. +enum { CASE_A, CASE_B }; + +TEST(CHECKTest, TestBinaryChecksWithEnumValues) { + // Tests using CHECK*() on anonymous enums. + ABSL_TEST_CHECK_EQ(CASE_A, CASE_A); + ABSL_TEST_CHECK_NE(CASE_A, CASE_B); + ABSL_TEST_CHECK_GE(CASE_A, CASE_A); + ABSL_TEST_CHECK_GE(CASE_B, CASE_A); + ABSL_TEST_CHECK_LE(CASE_A, CASE_A); + ABSL_TEST_CHECK_LE(CASE_A, CASE_B); + ABSL_TEST_CHECK_GT(CASE_B, CASE_A); + ABSL_TEST_CHECK_LT(CASE_A, CASE_B); +} + +TEST(CHECKTest, TestBinaryChecksWithNullptr) { + const void* p_null = nullptr; + const void* p_not_null = &p_null; + ABSL_TEST_CHECK_EQ(p_null, nullptr); + ABSL_TEST_CHECK_EQ(nullptr, p_null); + ABSL_TEST_CHECK_NE(p_not_null, nullptr); + ABSL_TEST_CHECK_NE(nullptr, p_not_null); +} + +#if GTEST_HAS_DEATH_TEST + +// Test logging of various char-typed values by failing CHECK*(). +TEST(CHECKDeathTest, TestComparingCharsValues) { + { + char a = ';'; + char b = 'b'; + EXPECT_DEATH(ABSL_TEST_CHECK_EQ(a, b), + "Check failed: a == b \\(';' vs. 'b'\\)"); + b = 1; + EXPECT_DEATH(ABSL_TEST_CHECK_EQ(a, b), + "Check failed: a == b \\(';' vs. char value 1\\)"); + } + { + signed char a = ';'; + signed char b = 'b'; + EXPECT_DEATH(ABSL_TEST_CHECK_EQ(a, b), + "Check failed: a == b \\(';' vs. 'b'\\)"); + b = -128; + EXPECT_DEATH(ABSL_TEST_CHECK_EQ(a, b), + "Check failed: a == b \\(';' vs. signed char value -128\\)"); + } + { + unsigned char a = ';'; + unsigned char b = 'b'; + EXPECT_DEATH(ABSL_TEST_CHECK_EQ(a, b), + "Check failed: a == b \\(';' vs. 'b'\\)"); + b = 128; + EXPECT_DEATH(ABSL_TEST_CHECK_EQ(a, b), + "Check failed: a == b \\(';' vs. unsigned char value 128\\)"); + } +} + +TEST(CHECKDeathTest, TestNullValuesAreReportedCleanly) { + const char* a = nullptr; + const char* b = nullptr; + EXPECT_DEATH(ABSL_TEST_CHECK_NE(a, b), + "Check failed: a != b \\(\\(null\\) vs. \\(null\\)\\)"); + + a = "xx"; + EXPECT_DEATH(ABSL_TEST_CHECK_EQ(a, b), + "Check failed: a == b \\(xx vs. \\(null\\)\\)"); + EXPECT_DEATH(ABSL_TEST_CHECK_EQ(b, a), + "Check failed: b == a \\(\\(null\\) vs. xx\\)"); + + std::nullptr_t n{}; + EXPECT_DEATH(ABSL_TEST_CHECK_NE(n, nullptr), + "Check failed: n != nullptr \\(\\(null\\) vs. \\(null\\)\\)"); +} + +#endif // GTEST_HAS_DEATH_TEST + +TEST(CHECKTest, TestSTREQ) { + ABSL_TEST_CHECK_STREQ("this", "this"); + ABSL_TEST_CHECK_STREQ(nullptr, nullptr); + ABSL_TEST_CHECK_STRCASEEQ("this", "tHiS"); + ABSL_TEST_CHECK_STRCASEEQ(nullptr, nullptr); + ABSL_TEST_CHECK_STRNE("this", "tHiS"); + ABSL_TEST_CHECK_STRNE("this", nullptr); + ABSL_TEST_CHECK_STRCASENE("this", "that"); + ABSL_TEST_CHECK_STRCASENE(nullptr, "that"); + ABSL_TEST_CHECK_STREQ((std::string("a") + "b").c_str(), "ab"); + ABSL_TEST_CHECK_STREQ(std::string("test").c_str(), + (std::string("te") + std::string("st")).c_str()); +} + +TEST(CHECKTest, TestComparisonPlacementsInCompoundStatements) { + // check placement inside if/else clauses + if (true) ABSL_TEST_CHECK_EQ(1, 1); + if (true) ABSL_TEST_CHECK_STREQ("c", "c"); + + if (false) + ; // NOLINT + else + ABSL_TEST_CHECK_LE(0, 1); + + if (false) + ; // NOLINT + else + ABSL_TEST_CHECK_STRNE("a", "b"); + + switch (0) + case 0: + ABSL_TEST_CHECK_NE(1, 0); + + switch (0) + case 0: + ABSL_TEST_CHECK_STRCASEEQ("A", "a"); + +#if ABSL_INTERNAL_CPLUSPLUS_LANG >= 201703L + constexpr auto var = [](int i) { + ABSL_TEST_CHECK_GT(i, 0); + return i + 1; + }(global_var); + (void)var; + + // CHECK_STR... checks are not supported in constexpr routines. + // constexpr auto var2 = [](int i) { + // ABSL_TEST_CHECK_STRNE("c", "d"); + // return i + 1; + // }(global_var); + +#if defined(__GNUC__) + int var3 = (({ ABSL_TEST_CHECK_LE(1, 2); }), global_var < 10) ? 1 : 0; + (void)var3; + + int var4 = (({ ABSL_TEST_CHECK_STREQ("a", "a"); }), global_var < 10) ? 1 : 0; + (void)var4; +#endif // __GNUC__ +#endif // ABSL_INTERNAL_CPLUSPLUS_LANG +} + +TEST(CHECKTest, TestDCHECK) { +#ifdef NDEBUG + ABSL_TEST_DCHECK(1 == 2) << " DCHECK's shouldn't be compiled in normal mode"; +#endif + ABSL_TEST_DCHECK(1 == 1); // NOLINT(readability/check) + ABSL_TEST_DCHECK_EQ(1, 1); + ABSL_TEST_DCHECK_NE(1, 2); + ABSL_TEST_DCHECK_GE(1, 1); + ABSL_TEST_DCHECK_GE(2, 1); + ABSL_TEST_DCHECK_LE(1, 1); + ABSL_TEST_DCHECK_LE(1, 2); + ABSL_TEST_DCHECK_GT(2, 1); + ABSL_TEST_DCHECK_LT(1, 2); + + // Test DCHECK on std::nullptr_t + const void* p_null = nullptr; + const void* p_not_null = &p_null; + ABSL_TEST_DCHECK_EQ(p_null, nullptr); + ABSL_TEST_DCHECK_EQ(nullptr, p_null); + ABSL_TEST_DCHECK_NE(p_not_null, nullptr); + ABSL_TEST_DCHECK_NE(nullptr, p_not_null); +} + +TEST(CHECKTest, TestQCHECK) { + // The tests that QCHECK does the same as CHECK + ABSL_TEST_QCHECK(1 == 1); // NOLINT(readability/check) + ABSL_TEST_QCHECK_EQ(1, 1); + ABSL_TEST_QCHECK_NE(1, 2); + ABSL_TEST_QCHECK_GE(1, 1); + ABSL_TEST_QCHECK_GE(2, 1); + ABSL_TEST_QCHECK_LE(1, 1); + ABSL_TEST_QCHECK_LE(1, 2); + ABSL_TEST_QCHECK_GT(2, 1); + ABSL_TEST_QCHECK_LT(1, 2); + + // Tests using QCHECK*() on anonymous enums. + ABSL_TEST_QCHECK_EQ(CASE_A, CASE_A); + ABSL_TEST_QCHECK_NE(CASE_A, CASE_B); + ABSL_TEST_QCHECK_GE(CASE_A, CASE_A); + ABSL_TEST_QCHECK_GE(CASE_B, CASE_A); + ABSL_TEST_QCHECK_LE(CASE_A, CASE_A); + ABSL_TEST_QCHECK_LE(CASE_A, CASE_B); + ABSL_TEST_QCHECK_GT(CASE_B, CASE_A); + ABSL_TEST_QCHECK_LT(CASE_A, CASE_B); +} + +TEST(CHECKTest, TestQCHECKPlacementsInCompoundStatements) { + // check placement inside if/else clauses + if (true) ABSL_TEST_QCHECK(true); + + if (false) + ; // NOLINT + else + ABSL_TEST_QCHECK(true); + + if (false) + ; // NOLINT + else + ABSL_TEST_QCHECK(true); + + switch (0) + case 0: + ABSL_TEST_QCHECK(true); + +#if ABSL_INTERNAL_CPLUSPLUS_LANG >= 201703L + constexpr auto var = [](int i) { + ABSL_TEST_QCHECK(i > 0); // NOLINT + return i + 1; + }(global_var); + (void)var; + +#if defined(__GNUC__) + int var2 = (({ ABSL_TEST_CHECK_LE(1, 2); }), global_var < 10) ? 1 : 0; + (void)var2; +#endif // __GNUC__ +#endif // ABSL_INTERNAL_CPLUSPLUS_LANG +} + +class ComparableType { + public: + explicit ComparableType(int v) : v_(v) {} + + void MethodWithCheck(int i) { + ABSL_TEST_CHECK_EQ(*this, i); + ABSL_TEST_CHECK_EQ(i, *this); + } + + int Get() const { return v_; } + + private: + friend bool operator==(const ComparableType& lhs, const ComparableType& rhs) { + return lhs.v_ == rhs.v_; + } + friend bool operator!=(const ComparableType& lhs, const ComparableType& rhs) { + return lhs.v_ != rhs.v_; + } + friend bool operator<(const ComparableType& lhs, const ComparableType& rhs) { + return lhs.v_ < rhs.v_; + } + friend bool operator<=(const ComparableType& lhs, const ComparableType& rhs) { + return lhs.v_ <= rhs.v_; + } + friend bool operator>(const ComparableType& lhs, const ComparableType& rhs) { + return lhs.v_ > rhs.v_; + } + friend bool operator>=(const ComparableType& lhs, const ComparableType& rhs) { + return lhs.v_ >= rhs.v_; + } + friend bool operator==(const ComparableType& lhs, int rhs) { + return lhs.v_ == rhs; + } + friend bool operator==(int lhs, const ComparableType& rhs) { + return lhs == rhs.v_; + } + + friend std::ostream& operator<<(std::ostream& out, const ComparableType& v) { + return out << "ComparableType{" << v.Get() << "}"; + } + + int v_; +}; + +TEST(CHECKTest, TestUserDefinedCompOp) { + ABSL_TEST_CHECK_EQ(ComparableType{0}, ComparableType{0}); + ABSL_TEST_CHECK_NE(ComparableType{1}, ComparableType{2}); + ABSL_TEST_CHECK_LT(ComparableType{1}, ComparableType{2}); + ABSL_TEST_CHECK_LE(ComparableType{1}, ComparableType{2}); + ABSL_TEST_CHECK_GT(ComparableType{2}, ComparableType{1}); + ABSL_TEST_CHECK_GE(ComparableType{2}, ComparableType{2}); +} + +TEST(CHECKTest, TestCheckInMethod) { + ComparableType v{1}; + v.MethodWithCheck(1); +} + +TEST(CHECKDeathTest, TestUserDefinedStreaming) { + ComparableType v1{1}; + ComparableType v2{2}; + + EXPECT_DEATH( + ABSL_TEST_CHECK_EQ(v1, v2), + HasSubstr( + "Check failed: v1 == v2 (ComparableType{1} vs. ComparableType{2})")); +} + +} // namespace absl_log_internal + +// NOLINTEND(misc-definitions-in-headers) + +#endif // ABSL_LOG_CHECK_TEST_IMPL_H_ diff --git a/absl/log/internal/check_impl.h b/absl/log/internal/check_impl.h index c9c28e3e..00f25f80 100644 --- a/absl/log/internal/check_impl.h +++ b/absl/log/internal/check_impl.h @@ -22,128 +22,128 @@ #include "absl/log/internal/strip.h" // CHECK -#define ABSL_CHECK_IMPL(condition, condition_text) \ +#define ABSL_LOG_INTERNAL_CHECK_IMPL(condition, condition_text) \ ABSL_LOG_INTERNAL_CONDITION_FATAL(STATELESS, \ ABSL_PREDICT_FALSE(!(condition))) \ ABSL_LOG_INTERNAL_CHECK(condition_text).InternalStream() -#define ABSL_QCHECK_IMPL(condition, condition_text) \ +#define ABSL_LOG_INTERNAL_QCHECK_IMPL(condition, condition_text) \ ABSL_LOG_INTERNAL_CONDITION_QFATAL(STATELESS, \ ABSL_PREDICT_FALSE(!(condition))) \ ABSL_LOG_INTERNAL_QCHECK(condition_text).InternalStream() -#define ABSL_PCHECK_IMPL(condition, condition_text) \ - ABSL_CHECK_IMPL(condition, condition_text).WithPerror() +#define ABSL_LOG_INTERNAL_PCHECK_IMPL(condition, condition_text) \ + ABSL_LOG_INTERNAL_CHECK_IMPL(condition, condition_text).WithPerror() #ifndef NDEBUG -#define ABSL_DCHECK_IMPL(condition, condition_text) \ - ABSL_CHECK_IMPL(condition, condition_text) +#define ABSL_LOG_INTERNAL_DCHECK_IMPL(condition, condition_text) \ + ABSL_LOG_INTERNAL_CHECK_IMPL(condition, condition_text) #else -#define ABSL_DCHECK_IMPL(condition, condition_text) \ - ABSL_CHECK_IMPL(true || (condition), "true") +#define ABSL_LOG_INTERNAL_DCHECK_IMPL(condition, condition_text) \ + ABSL_LOG_INTERNAL_CHECK_IMPL(true || (condition), "true") #endif // CHECK_EQ -#define ABSL_CHECK_EQ_IMPL(val1, val1_text, val2, val2_text) \ +#define ABSL_LOG_INTERNAL_CHECK_EQ_IMPL(val1, val1_text, val2, val2_text) \ ABSL_LOG_INTERNAL_CHECK_OP(Check_EQ, ==, val1, val1_text, val2, val2_text) -#define ABSL_CHECK_NE_IMPL(val1, val1_text, val2, val2_text) \ +#define ABSL_LOG_INTERNAL_CHECK_NE_IMPL(val1, val1_text, val2, val2_text) \ ABSL_LOG_INTERNAL_CHECK_OP(Check_NE, !=, val1, val1_text, val2, val2_text) -#define ABSL_CHECK_LE_IMPL(val1, val1_text, val2, val2_text) \ +#define ABSL_LOG_INTERNAL_CHECK_LE_IMPL(val1, val1_text, val2, val2_text) \ ABSL_LOG_INTERNAL_CHECK_OP(Check_LE, <=, val1, val1_text, val2, val2_text) -#define ABSL_CHECK_LT_IMPL(val1, val1_text, val2, val2_text) \ +#define ABSL_LOG_INTERNAL_CHECK_LT_IMPL(val1, val1_text, val2, val2_text) \ ABSL_LOG_INTERNAL_CHECK_OP(Check_LT, <, val1, val1_text, val2, val2_text) -#define ABSL_CHECK_GE_IMPL(val1, val1_text, val2, val2_text) \ +#define ABSL_LOG_INTERNAL_CHECK_GE_IMPL(val1, val1_text, val2, val2_text) \ ABSL_LOG_INTERNAL_CHECK_OP(Check_GE, >=, val1, val1_text, val2, val2_text) -#define ABSL_CHECK_GT_IMPL(val1, val1_text, val2, val2_text) \ +#define ABSL_LOG_INTERNAL_CHECK_GT_IMPL(val1, val1_text, val2, val2_text) \ ABSL_LOG_INTERNAL_CHECK_OP(Check_GT, >, val1, val1_text, val2, val2_text) -#define ABSL_QCHECK_EQ_IMPL(val1, val1_text, val2, val2_text) \ +#define ABSL_LOG_INTERNAL_QCHECK_EQ_IMPL(val1, val1_text, val2, val2_text) \ ABSL_LOG_INTERNAL_QCHECK_OP(Check_EQ, ==, val1, val1_text, val2, val2_text) -#define ABSL_QCHECK_NE_IMPL(val1, val1_text, val2, val2_text) \ +#define ABSL_LOG_INTERNAL_QCHECK_NE_IMPL(val1, val1_text, val2, val2_text) \ ABSL_LOG_INTERNAL_QCHECK_OP(Check_NE, !=, val1, val1_text, val2, val2_text) -#define ABSL_QCHECK_LE_IMPL(val1, val1_text, val2, val2_text) \ +#define ABSL_LOG_INTERNAL_QCHECK_LE_IMPL(val1, val1_text, val2, val2_text) \ ABSL_LOG_INTERNAL_QCHECK_OP(Check_LE, <=, val1, val1_text, val2, val2_text) -#define ABSL_QCHECK_LT_IMPL(val1, val1_text, val2, val2_text) \ +#define ABSL_LOG_INTERNAL_QCHECK_LT_IMPL(val1, val1_text, val2, val2_text) \ ABSL_LOG_INTERNAL_QCHECK_OP(Check_LT, <, val1, val1_text, val2, val2_text) -#define ABSL_QCHECK_GE_IMPL(val1, val1_text, val2, val2_text) \ +#define ABSL_LOG_INTERNAL_QCHECK_GE_IMPL(val1, val1_text, val2, val2_text) \ ABSL_LOG_INTERNAL_QCHECK_OP(Check_GE, >=, val1, val1_text, val2, val2_text) -#define ABSL_QCHECK_GT_IMPL(val1, val1_text, val2, val2_text) \ +#define ABSL_LOG_INTERNAL_QCHECK_GT_IMPL(val1, val1_text, val2, val2_text) \ ABSL_LOG_INTERNAL_QCHECK_OP(Check_GT, >, val1, val1_text, val2, val2_text) #ifndef NDEBUG -#define ABSL_DCHECK_EQ_IMPL(val1, val1_text, val2, val2_text) \ - ABSL_CHECK_EQ_IMPL(val1, val1_text, val2, val2_text) -#define ABSL_DCHECK_NE_IMPL(val1, val1_text, val2, val2_text) \ - ABSL_CHECK_NE_IMPL(val1, val1_text, val2, val2_text) -#define ABSL_DCHECK_LE_IMPL(val1, val1_text, val2, val2_text) \ - ABSL_CHECK_LE_IMPL(val1, val1_text, val2, val2_text) -#define ABSL_DCHECK_LT_IMPL(val1, val1_text, val2, val2_text) \ - ABSL_CHECK_LT_IMPL(val1, val1_text, val2, val2_text) -#define ABSL_DCHECK_GE_IMPL(val1, val1_text, val2, val2_text) \ - ABSL_CHECK_GE_IMPL(val1, val1_text, val2, val2_text) -#define ABSL_DCHECK_GT_IMPL(val1, val1_text, val2, val2_text) \ - ABSL_CHECK_GT_IMPL(val1, val1_text, val2, val2_text) +#define ABSL_LOG_INTERNAL_DCHECK_EQ_IMPL(val1, val1_text, val2, val2_text) \ + ABSL_LOG_INTERNAL_CHECK_EQ_IMPL(val1, val1_text, val2, val2_text) +#define ABSL_LOG_INTERNAL_DCHECK_NE_IMPL(val1, val1_text, val2, val2_text) \ + ABSL_LOG_INTERNAL_CHECK_NE_IMPL(val1, val1_text, val2, val2_text) +#define ABSL_LOG_INTERNAL_DCHECK_LE_IMPL(val1, val1_text, val2, val2_text) \ + ABSL_LOG_INTERNAL_CHECK_LE_IMPL(val1, val1_text, val2, val2_text) +#define ABSL_LOG_INTERNAL_DCHECK_LT_IMPL(val1, val1_text, val2, val2_text) \ + ABSL_LOG_INTERNAL_CHECK_LT_IMPL(val1, val1_text, val2, val2_text) +#define ABSL_LOG_INTERNAL_DCHECK_GE_IMPL(val1, val1_text, val2, val2_text) \ + ABSL_LOG_INTERNAL_CHECK_GE_IMPL(val1, val1_text, val2, val2_text) +#define ABSL_LOG_INTERNAL_DCHECK_GT_IMPL(val1, val1_text, val2, val2_text) \ + ABSL_LOG_INTERNAL_CHECK_GT_IMPL(val1, val1_text, val2, val2_text) #else // ndef NDEBUG -#define ABSL_DCHECK_EQ_IMPL(val1, val1_text, val2, val2_text) \ +#define ABSL_LOG_INTERNAL_DCHECK_EQ_IMPL(val1, val1_text, val2, val2_text) \ ABSL_LOG_INTERNAL_DCHECK_NOP(val1, val2) -#define ABSL_DCHECK_NE_IMPL(val1, val1_text, val2, val2_text) \ +#define ABSL_LOG_INTERNAL_DCHECK_NE_IMPL(val1, val1_text, val2, val2_text) \ ABSL_LOG_INTERNAL_DCHECK_NOP(val1, val2) -#define ABSL_DCHECK_LE_IMPL(val1, val1_text, val2, val2_text) \ +#define ABSL_LOG_INTERNAL_DCHECK_LE_IMPL(val1, val1_text, val2, val2_text) \ ABSL_LOG_INTERNAL_DCHECK_NOP(val1, val2) -#define ABSL_DCHECK_LT_IMPL(val1, val1_text, val2, val2_text) \ +#define ABSL_LOG_INTERNAL_DCHECK_LT_IMPL(val1, val1_text, val2, val2_text) \ ABSL_LOG_INTERNAL_DCHECK_NOP(val1, val2) -#define ABSL_DCHECK_GE_IMPL(val1, val1_text, val2, val2_text) \ +#define ABSL_LOG_INTERNAL_DCHECK_GE_IMPL(val1, val1_text, val2, val2_text) \ ABSL_LOG_INTERNAL_DCHECK_NOP(val1, val2) -#define ABSL_DCHECK_GT_IMPL(val1, val1_text, val2, val2_text) \ +#define ABSL_LOG_INTERNAL_DCHECK_GT_IMPL(val1, val1_text, val2, val2_text) \ ABSL_LOG_INTERNAL_DCHECK_NOP(val1, val2) #endif // def NDEBUG // CHECK_OK -#define ABSL_CHECK_OK_IMPL(status, status_text) \ +#define ABSL_LOG_INTERNAL_CHECK_OK_IMPL(status, status_text) \ ABSL_LOG_INTERNAL_CHECK_OK(status, status_text) -#define ABSL_QCHECK_OK_IMPL(status, status_text) \ +#define ABSL_LOG_INTERNAL_QCHECK_OK_IMPL(status, status_text) \ ABSL_LOG_INTERNAL_QCHECK_OK(status, status_text) #ifndef NDEBUG -#define ABSL_DCHECK_OK_IMPL(status, status_text) \ +#define ABSL_LOG_INTERNAL_DCHECK_OK_IMPL(status, status_text) \ ABSL_LOG_INTERNAL_CHECK_OK(status, status_text) #else -#define ABSL_DCHECK_OK_IMPL(status, status_text) \ +#define ABSL_LOG_INTERNAL_DCHECK_OK_IMPL(status, status_text) \ ABSL_LOG_INTERNAL_DCHECK_NOP(status, nullptr) #endif // CHECK_STREQ -#define ABSL_CHECK_STREQ_IMPL(s1, s1_text, s2, s2_text) \ +#define ABSL_LOG_INTERNAL_CHECK_STREQ_IMPL(s1, s1_text, s2, s2_text) \ ABSL_LOG_INTERNAL_CHECK_STROP(strcmp, ==, true, s1, s1_text, s2, s2_text) -#define ABSL_CHECK_STRNE_IMPL(s1, s1_text, s2, s2_text) \ +#define ABSL_LOG_INTERNAL_CHECK_STRNE_IMPL(s1, s1_text, s2, s2_text) \ ABSL_LOG_INTERNAL_CHECK_STROP(strcmp, !=, false, s1, s1_text, s2, s2_text) -#define ABSL_CHECK_STRCASEEQ_IMPL(s1, s1_text, s2, s2_text) \ +#define ABSL_LOG_INTERNAL_CHECK_STRCASEEQ_IMPL(s1, s1_text, s2, s2_text) \ ABSL_LOG_INTERNAL_CHECK_STROP(strcasecmp, ==, true, s1, s1_text, s2, s2_text) -#define ABSL_CHECK_STRCASENE_IMPL(s1, s1_text, s2, s2_text) \ +#define ABSL_LOG_INTERNAL_CHECK_STRCASENE_IMPL(s1, s1_text, s2, s2_text) \ ABSL_LOG_INTERNAL_CHECK_STROP(strcasecmp, !=, false, s1, s1_text, s2, s2_text) -#define ABSL_QCHECK_STREQ_IMPL(s1, s1_text, s2, s2_text) \ +#define ABSL_LOG_INTERNAL_QCHECK_STREQ_IMPL(s1, s1_text, s2, s2_text) \ ABSL_LOG_INTERNAL_QCHECK_STROP(strcmp, ==, true, s1, s1_text, s2, s2_text) -#define ABSL_QCHECK_STRNE_IMPL(s1, s1_text, s2, s2_text) \ +#define ABSL_LOG_INTERNAL_QCHECK_STRNE_IMPL(s1, s1_text, s2, s2_text) \ ABSL_LOG_INTERNAL_QCHECK_STROP(strcmp, !=, false, s1, s1_text, s2, s2_text) -#define ABSL_QCHECK_STRCASEEQ_IMPL(s1, s1_text, s2, s2_text) \ +#define ABSL_LOG_INTERNAL_QCHECK_STRCASEEQ_IMPL(s1, s1_text, s2, s2_text) \ ABSL_LOG_INTERNAL_QCHECK_STROP(strcasecmp, ==, true, s1, s1_text, s2, s2_text) -#define ABSL_QCHECK_STRCASENE_IMPL(s1, s1_text, s2, s2_text) \ - ABSL_LOG_INTERNAL_QCHECK_STROP(strcasecmp, !=, false, s1, s1_text, s2, \ +#define ABSL_LOG_INTERNAL_QCHECK_STRCASENE_IMPL(s1, s1_text, s2, s2_text) \ + ABSL_LOG_INTERNAL_QCHECK_STROP(strcasecmp, !=, false, s1, s1_text, s2, \ s2_text) #ifndef NDEBUG -#define ABSL_DCHECK_STREQ_IMPL(s1, s1_text, s2, s2_text) \ - ABSL_CHECK_STREQ_IMPL(s1, s1_text, s2, s2_text) -#define ABSL_DCHECK_STRCASEEQ_IMPL(s1, s1_text, s2, s2_text) \ - ABSL_CHECK_STRCASEEQ_IMPL(s1, s1_text, s2, s2_text) -#define ABSL_DCHECK_STRNE_IMPL(s1, s1_text, s2, s2_text) \ - ABSL_CHECK_STRNE_IMPL(s1, s1_text, s2, s2_text) -#define ABSL_DCHECK_STRCASENE_IMPL(s1, s1_text, s2, s2_text) \ - ABSL_CHECK_STRCASENE_IMPL(s1, s1_text, s2, s2_text) +#define ABSL_LOG_INTERNAL_DCHECK_STREQ_IMPL(s1, s1_text, s2, s2_text) \ + ABSL_LOG_INTERNAL_CHECK_STREQ_IMPL(s1, s1_text, s2, s2_text) +#define ABSL_LOG_INTERNAL_DCHECK_STRCASEEQ_IMPL(s1, s1_text, s2, s2_text) \ + ABSL_LOG_INTERNAL_CHECK_STRCASEEQ_IMPL(s1, s1_text, s2, s2_text) +#define ABSL_LOG_INTERNAL_DCHECK_STRNE_IMPL(s1, s1_text, s2, s2_text) \ + ABSL_LOG_INTERNAL_CHECK_STRNE_IMPL(s1, s1_text, s2, s2_text) +#define ABSL_LOG_INTERNAL_DCHECK_STRCASENE_IMPL(s1, s1_text, s2, s2_text) \ + ABSL_LOG_INTERNAL_CHECK_STRCASENE_IMPL(s1, s1_text, s2, s2_text) #else // ndef NDEBUG -#define ABSL_DCHECK_STREQ_IMPL(s1, s1_text, s2, s2_text) \ +#define ABSL_LOG_INTERNAL_DCHECK_STREQ_IMPL(s1, s1_text, s2, s2_text) \ ABSL_LOG_INTERNAL_DCHECK_NOP(s1, s2) -#define ABSL_DCHECK_STRCASEEQ_IMPL(s1, s1_text, s2, s2_text) \ +#define ABSL_LOG_INTERNAL_DCHECK_STRCASEEQ_IMPL(s1, s1_text, s2, s2_text) \ ABSL_LOG_INTERNAL_DCHECK_NOP(s1, s2) -#define ABSL_DCHECK_STRNE_IMPL(s1, s1_text, s2, s2_text) \ +#define ABSL_LOG_INTERNAL_DCHECK_STRNE_IMPL(s1, s1_text, s2, s2_text) \ ABSL_LOG_INTERNAL_DCHECK_NOP(s1, s2) -#define ABSL_DCHECK_STRCASENE_IMPL(s1, s1_text, s2, s2_text) \ +#define ABSL_LOG_INTERNAL_DCHECK_STRCASENE_IMPL(s1, s1_text, s2, s2_text) \ ABSL_LOG_INTERNAL_DCHECK_NOP(s1, s2) #endif // def NDEBUG diff --git a/absl/log/internal/log_impl.h b/absl/log/internal/log_impl.h index 82b5ed84..9326780d 100644 --- a/absl/log/internal/log_impl.h +++ b/absl/log/internal/log_impl.h @@ -20,190 +20,194 @@ #include "absl/log/internal/strip.h" // ABSL_LOG() -#define ABSL_LOG_IMPL(severity) \ +#define ABSL_LOG_INTERNAL_LOG_IMPL(severity) \ ABSL_LOG_INTERNAL_CONDITION##severity(STATELESS, true) \ ABSL_LOGGING_INTERNAL_LOG##severity.InternalStream() // ABSL_PLOG() -#define ABSL_PLOG_IMPL(severity) \ +#define ABSL_LOG_INTERNAL_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) \ +#define ABSL_LOG_INTERNAL_DLOG_IMPL(severity) \ ABSL_LOG_INTERNAL_CONDITION##severity(STATELESS, true) \ ABSL_LOGGING_INTERNAL_LOG##severity.InternalStream() #else -#define ABSL_DLOG_IMPL(severity) \ +#define ABSL_LOG_INTERNAL_DLOG_IMPL(severity) \ ABSL_LOG_INTERNAL_CONDITION##severity(STATELESS, false) \ ABSL_LOGGING_INTERNAL_LOG##severity.InternalStream() #endif -#define ABSL_LOG_IF_IMPL(severity, condition) \ +#define ABSL_LOG_INTERNAL_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) \ +#define ABSL_LOG_INTERNAL_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) \ +#define ABSL_LOG_INTERNAL_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) \ +#define ABSL_LOG_INTERNAL_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) \ +#define ABSL_LOG_INTERNAL_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) \ +#define ABSL_LOG_INTERNAL_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) \ +#define ABSL_LOG_INTERNAL_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) \ +#define ABSL_LOG_INTERNAL_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) \ +#define ABSL_LOG_INTERNAL_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) \ +#define ABSL_LOG_INTERNAL_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) \ +#define ABSL_LOG_INTERNAL_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) \ +#define ABSL_LOG_INTERNAL_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) \ +#define ABSL_LOG_INTERNAL_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) \ +#define ABSL_LOG_INTERNAL_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) \ +#define ABSL_LOG_INTERNAL_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) \ +#define ABSL_LOG_INTERNAL_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) \ +#define ABSL_LOG_INTERNAL_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) \ +#define ABSL_LOG_INTERNAL_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) \ +#define ABSL_LOG_INTERNAL_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) \ +#define ABSL_LOG_INTERNAL_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) \ +#define ABSL_LOG_INTERNAL_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) \ +#define ABSL_LOG_INTERNAL_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) \ +#define ABSL_LOG_INTERNAL_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) \ +#define ABSL_LOG_INTERNAL_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) \ +#define ABSL_LOG_INTERNAL_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) \ +#define ABSL_LOG_INTERNAL_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) \ +#define ABSL_LOG_INTERNAL_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) \ +#define ABSL_LOG_INTERNAL_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) \ +#define ABSL_LOG_INTERNAL_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) \ +#define ABSL_LOG_INTERNAL_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) \ +#define ABSL_LOG_INTERNAL_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) \ +#define ABSL_LOG_INTERNAL_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) \ +#define ABSL_LOG_INTERNAL_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) \ +#define ABSL_LOG_INTERNAL_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) \ +#define ABSL_LOG_INTERNAL_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) \ +#define ABSL_LOG_INTERNAL_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() diff --git a/absl/log/log.h b/absl/log/log.h index e060a0b6..602b5acf 100644 --- a/absl/log/log.h +++ b/absl/log/log.h @@ -196,29 +196,32 @@ // Example: // // LOG(INFO) << "Found " << num_cookies << " cookies"; -#define LOG(severity) ABSL_LOG_IMPL(_##severity) +#define LOG(severity) ABSL_LOG_INTERNAL_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_PLOG_IMPL(_##severity) +#define PLOG(severity) ABSL_LOG_INTERNAL_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. -#define DLOG(severity) ABSL_DLOG_IMPL(_##severity) +#define DLOG(severity) ABSL_LOG_INTERNAL_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_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) +#define LOG_IF(severity, condition) \ + ABSL_LOG_INTERNAL_LOG_IF_IMPL(_##severity, condition) +#define PLOG_IF(severity, condition) \ + ABSL_LOG_INTERNAL_PLOG_IF_IMPL(_##severity, condition) +#define DLOG_IF(severity, condition) \ + ABSL_LOG_INTERNAL_DLOG_IF_IMPL(_##severity, condition) // LOG_EVERY_N // @@ -231,21 +234,24 @@ // // LOG_EVERY_N(WARNING, 1000) << "Got a packet with a bad CRC (" << COUNTER // << " total)"; -#define LOG_EVERY_N(severity, n) ABSL_LOG_EVERY_N_IMPL(_##severity, n) +#define LOG_EVERY_N(severity, n) \ + ABSL_LOG_INTERNAL_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_FIRST_N_IMPL(_##severity, n) +#define LOG_FIRST_N(severity, n) \ + ABSL_LOG_INTERNAL_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_EVERY_POW_2_IMPL(_##severity) +#define LOG_EVERY_POW_2(severity) \ + ABSL_LOG_INTERNAL_LOG_EVERY_POW_2_IMPL(_##severity) // LOG_EVERY_N_SEC // @@ -257,19 +263,25 @@ // // LOG_EVERY_N_SEC(INFO, 2.5) << "Got " << COUNTER << " cookies so far"; #define LOG_EVERY_N_SEC(severity, n_seconds) \ - ABSL_LOG_EVERY_N_SEC_IMPL(_##severity, n_seconds) + ABSL_LOG_INTERNAL_LOG_EVERY_N_SEC_IMPL(_##severity, n_seconds) -#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(severity, n) \ + ABSL_LOG_INTERNAL_PLOG_EVERY_N_IMPL(_##severity, n) +#define PLOG_FIRST_N(severity, n) \ + ABSL_LOG_INTERNAL_PLOG_FIRST_N_IMPL(_##severity, n) +#define PLOG_EVERY_POW_2(severity) \ + ABSL_LOG_INTERNAL_PLOG_EVERY_POW_2_IMPL(_##severity) #define PLOG_EVERY_N_SEC(severity, n_seconds) \ - ABSL_PLOG_EVERY_N_SEC_IMPL(_##severity, n_seconds) + ABSL_LOG_INTERNAL_PLOG_EVERY_N_SEC_IMPL(_##severity, n_seconds) -#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(severity, n) \ + ABSL_LOG_INTERNAL_DLOG_EVERY_N_IMPL(_##severity, n) +#define DLOG_FIRST_N(severity, n) \ + ABSL_LOG_INTERNAL_DLOG_FIRST_N_IMPL(_##severity, n) +#define DLOG_EVERY_POW_2(severity) \ + ABSL_LOG_INTERNAL_DLOG_EVERY_POW_2_IMPL(_##severity) #define DLOG_EVERY_N_SEC(severity, n_seconds) \ - ABSL_DLOG_EVERY_N_SEC_IMPL(_##severity, n_seconds) + ABSL_LOG_INTERNAL_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 @@ -279,30 +291,30 @@ // LOG_IF_EVERY_N(INFO, (size > 1024), 10) << "Got the " << COUNTER // << "th big cookie"; #define LOG_IF_EVERY_N(severity, condition, n) \ - ABSL_LOG_IF_EVERY_N_IMPL(_##severity, condition, n) + ABSL_LOG_INTERNAL_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) + ABSL_LOG_INTERNAL_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) + ABSL_LOG_INTERNAL_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) + ABSL_LOG_INTERNAL_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) + ABSL_LOG_INTERNAL_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) + ABSL_LOG_INTERNAL_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) + ABSL_LOG_INTERNAL_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) + ABSL_LOG_INTERNAL_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) + ABSL_LOG_INTERNAL_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) + ABSL_LOG_INTERNAL_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) + ABSL_LOG_INTERNAL_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) + ABSL_LOG_INTERNAL_DLOG_IF_EVERY_N_SEC_IMPL(_##severity, condition, n_seconds) #endif // ABSL_LOG_LOG_H_ diff --git a/absl/log/log_basic_test.cc b/absl/log/log_basic_test.cc index b8d87c94..7fc7111d 100644 --- a/absl/log/log_basic_test.cc +++ b/absl/log/log_basic_test.cc @@ -18,4 +18,4 @@ #define ABSL_TEST_LOG LOG #include "gtest/gtest.h" -#include "absl/log/log_basic_test_impl.h" +#include "absl/log/log_basic_test_impl.inc" diff --git a/absl/log/log_basic_test_impl.h b/absl/log/log_basic_test_impl.h deleted file mode 100644 index 35c0b690..00000000 --- a/absl/log/log_basic_test_impl.h +++ /dev/null @@ -1,455 +0,0 @@ -// -// 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. - -// The testcases in this file are expected to pass or be skipped with any value -// of ABSL_MIN_LOG_LEVEL - -#ifndef ABSL_LOG_LOG_BASIC_TEST_IMPL_H_ -#define ABSL_LOG_LOG_BASIC_TEST_IMPL_H_ - -// Verify that both sets of macros behave identically by parameterizing the -// entire test file. -#ifndef ABSL_TEST_LOG -#error ABSL_TEST_LOG must be defined for these tests to work. -#endif - -#include -#include -#include - -#include "gmock/gmock.h" -#include "gtest/gtest.h" -#include "absl/base/internal/sysinfo.h" -#include "absl/base/log_severity.h" -#include "absl/log/globals.h" -#include "absl/log/internal/test_actions.h" -#include "absl/log/internal/test_helpers.h" -#include "absl/log/internal/test_matchers.h" -#include "absl/log/log_entry.h" -#include "absl/log/scoped_mock_log.h" - -namespace absl_log_internal { -#if GTEST_HAS_DEATH_TEST -using ::absl::log_internal::DeathTestExpectedLogging; -using ::absl::log_internal::DeathTestUnexpectedLogging; -using ::absl::log_internal::DeathTestValidateExpectations; -using ::absl::log_internal::DiedOfFatal; -using ::absl::log_internal::DiedOfQFatal; -#endif -using ::absl::log_internal::LoggingEnabledAt; -using ::absl::log_internal::LogSeverity; -using ::absl::log_internal::Prefix; -using ::absl::log_internal::SourceBasename; -using ::absl::log_internal::SourceFilename; -using ::absl::log_internal::SourceLine; -using ::absl::log_internal::Stacktrace; -using ::absl::log_internal::TextMessage; -using ::absl::log_internal::ThreadID; -using ::absl::log_internal::TimestampInMatchWindow; -using ::absl::log_internal::Verbosity; -using ::testing::AnyNumber; -using ::testing::Eq; -using ::testing::IsEmpty; -using ::testing::IsTrue; - -class BasicLogTest : public testing::TestWithParam {}; - -std::string ThresholdName( - testing::TestParamInfo severity) { - std::stringstream ostr; - ostr << severity.param; - return ostr.str().substr( - severity.param == absl::LogSeverityAtLeast::kInfinity ? 0 : 2); -} - -INSTANTIATE_TEST_SUITE_P(WithParam, BasicLogTest, - testing::Values(absl::LogSeverityAtLeast::kInfo, - absl::LogSeverityAtLeast::kWarning, - absl::LogSeverityAtLeast::kError, - absl::LogSeverityAtLeast::kFatal, - absl::LogSeverityAtLeast::kInfinity), - ThresholdName); - -TEST_P(BasicLogTest, Info) { - absl::log_internal::ScopedMinLogLevel scoped_min_log_level(GetParam()); - - absl::ScopedMockLog test_sink(absl::MockLogDefault::kDisallowUnexpected); - - const int log_line = __LINE__ + 1; - auto do_log = [] { ABSL_TEST_LOG(INFO) << "hello world"; }; - - if (LoggingEnabledAt(absl::LogSeverity::kInfo)) { - EXPECT_CALL( - test_sink, - Send(AllOf(SourceFilename(Eq(__FILE__)), - SourceBasename(Eq("log_basic_test_impl.h")), - SourceLine(Eq(log_line)), Prefix(IsTrue()), - LogSeverity(Eq(absl::LogSeverity::kInfo)), - TimestampInMatchWindow(), - ThreadID(Eq(absl::base_internal::GetTID())), - TextMessage(Eq("hello world")), - Verbosity(Eq(absl::LogEntry::kNoVerbosityLevel)), - ENCODED_MESSAGE(EqualsProto(R"pb(value { - literal: "hello world" - })pb")), - Stacktrace(IsEmpty())))); - } - - test_sink.StartCapturingLogs(); - do_log(); -} - -TEST_P(BasicLogTest, Warning) { - absl::log_internal::ScopedMinLogLevel scoped_min_log_level(GetParam()); - - absl::ScopedMockLog test_sink(absl::MockLogDefault::kDisallowUnexpected); - - const int log_line = __LINE__ + 1; - auto do_log = [] { ABSL_TEST_LOG(WARNING) << "hello world"; }; - - if (LoggingEnabledAt(absl::LogSeverity::kWarning)) { - EXPECT_CALL( - test_sink, - Send(AllOf(SourceFilename(Eq(__FILE__)), - SourceBasename(Eq("log_basic_test_impl.h")), - SourceLine(Eq(log_line)), Prefix(IsTrue()), - LogSeverity(Eq(absl::LogSeverity::kWarning)), - TimestampInMatchWindow(), - ThreadID(Eq(absl::base_internal::GetTID())), - TextMessage(Eq("hello world")), - Verbosity(Eq(absl::LogEntry::kNoVerbosityLevel)), - ENCODED_MESSAGE(EqualsProto(R"pb(value { - literal: "hello world" - })pb")), - Stacktrace(IsEmpty())))); - } - - test_sink.StartCapturingLogs(); - do_log(); -} - -TEST_P(BasicLogTest, Error) { - absl::log_internal::ScopedMinLogLevel scoped_min_log_level(GetParam()); - - absl::ScopedMockLog test_sink(absl::MockLogDefault::kDisallowUnexpected); - - const int log_line = __LINE__ + 1; - auto do_log = [] { ABSL_TEST_LOG(ERROR) << "hello world"; }; - - if (LoggingEnabledAt(absl::LogSeverity::kError)) { - EXPECT_CALL( - test_sink, - Send(AllOf(SourceFilename(Eq(__FILE__)), - SourceBasename(Eq("log_basic_test_impl.h")), - SourceLine(Eq(log_line)), Prefix(IsTrue()), - LogSeverity(Eq(absl::LogSeverity::kError)), - TimestampInMatchWindow(), - ThreadID(Eq(absl::base_internal::GetTID())), - TextMessage(Eq("hello world")), - Verbosity(Eq(absl::LogEntry::kNoVerbosityLevel)), - ENCODED_MESSAGE(EqualsProto(R"pb(value { - literal: "hello world" - })pb")), - Stacktrace(IsEmpty())))); - } - - test_sink.StartCapturingLogs(); - do_log(); -} - -#if GTEST_HAS_DEATH_TEST -using BasicLogDeathTest = BasicLogTest; - -INSTANTIATE_TEST_SUITE_P(WithParam, BasicLogDeathTest, - testing::Values(absl::LogSeverityAtLeast::kInfo, - absl::LogSeverityAtLeast::kFatal, - absl::LogSeverityAtLeast::kInfinity), - ThresholdName); - -TEST_P(BasicLogDeathTest, Fatal) { - absl::log_internal::ScopedMinLogLevel scoped_min_log_level(GetParam()); - - const int log_line = __LINE__ + 1; - auto do_log = [] { ABSL_TEST_LOG(FATAL) << "hello world"; }; - - EXPECT_EXIT( - { - absl::ScopedMockLog test_sink( - absl::MockLogDefault::kDisallowUnexpected); - - EXPECT_CALL(test_sink, Send) - .Times(AnyNumber()) - .WillRepeatedly(DeathTestUnexpectedLogging()); - - ::testing::InSequence s; - - // Note the logic in DeathTestValidateExpectations() caters for the case - // of logging being disabled at FATAL level. - - if (LoggingEnabledAt(absl::LogSeverity::kFatal)) { - // The first call without the stack trace. - EXPECT_CALL( - test_sink, - Send(AllOf(SourceFilename(Eq(__FILE__)), - SourceBasename(Eq("log_basic_test_impl.h")), - SourceLine(Eq(log_line)), Prefix(IsTrue()), - LogSeverity(Eq(absl::LogSeverity::kFatal)), - TimestampInMatchWindow(), - ThreadID(Eq(absl::base_internal::GetTID())), - TextMessage(Eq("hello world")), - Verbosity(Eq(absl::LogEntry::kNoVerbosityLevel)), - ENCODED_MESSAGE(EqualsProto( - R"pb(value { literal: "hello world" })pb")), - Stacktrace(IsEmpty())))) - .WillOnce(DeathTestExpectedLogging()); - - // The second call with the stack trace. - EXPECT_CALL( - test_sink, - Send(AllOf(SourceFilename(Eq(__FILE__)), - SourceBasename(Eq("log_basic_test_impl.h")), - SourceLine(Eq(log_line)), Prefix(IsTrue()), - LogSeverity(Eq(absl::LogSeverity::kFatal)), - TimestampInMatchWindow(), - ThreadID(Eq(absl::base_internal::GetTID())), - TextMessage(Eq("hello world")), - Verbosity(Eq(absl::LogEntry::kNoVerbosityLevel)), - ENCODED_MESSAGE(EqualsProto( - R"pb(value { literal: "hello world" })pb")), - Stacktrace(Not(IsEmpty()))))) - .WillOnce(DeathTestExpectedLogging()); - } - - test_sink.StartCapturingLogs(); - do_log(); - }, - DiedOfFatal, DeathTestValidateExpectations()); -} - -TEST_P(BasicLogDeathTest, QFatal) { - absl::log_internal::ScopedMinLogLevel scoped_min_log_level(GetParam()); - - const int log_line = __LINE__ + 1; - auto do_log = [] { ABSL_TEST_LOG(QFATAL) << "hello world"; }; - - EXPECT_EXIT( - { - absl::ScopedMockLog test_sink( - absl::MockLogDefault::kDisallowUnexpected); - - EXPECT_CALL(test_sink, Send) - .Times(AnyNumber()) - .WillRepeatedly(DeathTestUnexpectedLogging()); - - if (LoggingEnabledAt(absl::LogSeverity::kFatal)) { - EXPECT_CALL( - test_sink, - Send(AllOf(SourceFilename(Eq(__FILE__)), - SourceBasename(Eq("log_basic_test_impl.h")), - SourceLine(Eq(log_line)), Prefix(IsTrue()), - LogSeverity(Eq(absl::LogSeverity::kFatal)), - TimestampInMatchWindow(), - ThreadID(Eq(absl::base_internal::GetTID())), - TextMessage(Eq("hello world")), - Verbosity(Eq(absl::LogEntry::kNoVerbosityLevel)), - ENCODED_MESSAGE(EqualsProto( - R"pb(value { literal: "hello world" })pb")), - Stacktrace(IsEmpty())))) - .WillOnce(DeathTestExpectedLogging()); - } - - test_sink.StartCapturingLogs(); - do_log(); - }, - DiedOfQFatal, DeathTestValidateExpectations()); -} -#endif - -TEST_P(BasicLogTest, Level) { - absl::log_internal::ScopedMinLogLevel scoped_min_log_level(GetParam()); - - for (auto severity : {absl::LogSeverity::kInfo, absl::LogSeverity::kWarning, - absl::LogSeverity::kError}) { - absl::ScopedMockLog test_sink(absl::MockLogDefault::kDisallowUnexpected); - - const int log_line = __LINE__ + 2; - auto do_log = [severity] { - ABSL_TEST_LOG(LEVEL(severity)) << "hello world"; - }; - - if (LoggingEnabledAt(severity)) { - EXPECT_CALL( - test_sink, - Send(AllOf(SourceFilename(Eq(__FILE__)), - SourceBasename(Eq("log_basic_test_impl.h")), - SourceLine(Eq(log_line)), Prefix(IsTrue()), - LogSeverity(Eq(severity)), TimestampInMatchWindow(), - ThreadID(Eq(absl::base_internal::GetTID())), - TextMessage(Eq("hello world")), - Verbosity(Eq(absl::LogEntry::kNoVerbosityLevel)), - ENCODED_MESSAGE(EqualsProto(R"pb(value { - literal: "hello world" - })pb")), - Stacktrace(IsEmpty())))); - } - test_sink.StartCapturingLogs(); - do_log(); - } -} - -#if GTEST_HAS_DEATH_TEST -TEST_P(BasicLogDeathTest, Level) { - // TODO(b/242568884): re-enable once bug is fixed. - // absl::log_internal::ScopedMinLogLevel scoped_min_log_level(GetParam()); - - // Ensure that `severity` is not a compile-time constant to prove that - // `LOG(LEVEL(severity))` works regardless: - auto volatile severity = absl::LogSeverity::kFatal; - - const int log_line = __LINE__ + 1; - auto do_log = [severity] { ABSL_TEST_LOG(LEVEL(severity)) << "hello world"; }; - - EXPECT_EXIT( - { - absl::ScopedMockLog test_sink( - absl::MockLogDefault::kDisallowUnexpected); - - EXPECT_CALL(test_sink, Send) - .Times(AnyNumber()) - .WillRepeatedly(DeathTestUnexpectedLogging()); - - ::testing::InSequence s; - - if (LoggingEnabledAt(absl::LogSeverity::kFatal)) { - EXPECT_CALL( - test_sink, - Send(AllOf(SourceFilename(Eq(__FILE__)), - SourceBasename(Eq("log_basic_test_impl.h")), - SourceLine(Eq(log_line)), Prefix(IsTrue()), - LogSeverity(Eq(absl::LogSeverity::kFatal)), - TimestampInMatchWindow(), - ThreadID(Eq(absl::base_internal::GetTID())), - TextMessage(Eq("hello world")), - Verbosity(Eq(absl::LogEntry::kNoVerbosityLevel)), - ENCODED_MESSAGE(EqualsProto( - R"pb(value { literal: "hello world" })pb")), - Stacktrace(IsEmpty())))) - .WillOnce(DeathTestExpectedLogging()); - - EXPECT_CALL( - test_sink, - Send(AllOf(SourceFilename(Eq(__FILE__)), - SourceBasename(Eq("log_basic_test_impl.h")), - SourceLine(Eq(log_line)), Prefix(IsTrue()), - LogSeverity(Eq(absl::LogSeverity::kFatal)), - TimestampInMatchWindow(), - ThreadID(Eq(absl::base_internal::GetTID())), - TextMessage(Eq("hello world")), - Verbosity(Eq(absl::LogEntry::kNoVerbosityLevel)), - ENCODED_MESSAGE(EqualsProto( - R"pb(value { literal: "hello world" })pb")), - Stacktrace(Not(IsEmpty()))))) - .WillOnce(DeathTestExpectedLogging()); - } - - test_sink.StartCapturingLogs(); - do_log(); - }, - DiedOfFatal, DeathTestValidateExpectations()); -} -#endif - -TEST_P(BasicLogTest, LevelClampsNegativeValues) { - absl::log_internal::ScopedMinLogLevel scoped_min_log_level(GetParam()); - - if (!LoggingEnabledAt(absl::LogSeverity::kInfo)) { - GTEST_SKIP() << "This test cases required INFO log to be enabled"; - return; - } - - absl::ScopedMockLog test_sink(absl::MockLogDefault::kDisallowUnexpected); - - EXPECT_CALL(test_sink, Send(LogSeverity(Eq(absl::LogSeverity::kInfo)))); - - test_sink.StartCapturingLogs(); - ABSL_TEST_LOG(LEVEL(-1)) << "hello world"; -} - -TEST_P(BasicLogTest, LevelClampsLargeValues) { - absl::log_internal::ScopedMinLogLevel scoped_min_log_level(GetParam()); - - if (!LoggingEnabledAt(absl::LogSeverity::kError)) { - GTEST_SKIP() << "This test cases required ERROR log to be enabled"; - return; - } - - absl::ScopedMockLog test_sink(absl::MockLogDefault::kDisallowUnexpected); - - EXPECT_CALL(test_sink, Send(LogSeverity(Eq(absl::LogSeverity::kError)))); - - test_sink.StartCapturingLogs(); - ABSL_TEST_LOG(LEVEL(static_cast(absl::LogSeverity::kFatal) + 1)) - << "hello world"; -} - -TEST(ErrnoPreservationTest, InSeverityExpression) { - errno = 77; - int saved_errno; - ABSL_TEST_LOG(LEVEL((saved_errno = errno, absl::LogSeverity::kInfo))); - EXPECT_THAT(saved_errno, Eq(77)); -} - -TEST(ErrnoPreservationTest, InStreamedExpression) { - if (!LoggingEnabledAt(absl::LogSeverity::kInfo)) { - GTEST_SKIP() << "This test cases required INFO log to be enabled"; - return; - } - - errno = 77; - int saved_errno = 0; - ABSL_TEST_LOG(INFO) << (saved_errno = errno, "hello world"); - EXPECT_THAT(saved_errno, Eq(77)); -} - -TEST(ErrnoPreservationTest, AfterStatement) { - errno = 77; - ABSL_TEST_LOG(INFO); - const int saved_errno = errno; - EXPECT_THAT(saved_errno, Eq(77)); -} - -// Tests that using a variable/parameter in a logging statement suppresses -// unused-variable/parameter warnings. -// ----------------------------------------------------------------------- -class UnusedVariableWarningCompileTest { - // These four don't prove anything unless `ABSL_MIN_LOG_LEVEL` is greater than - // `kInfo`. - static void LoggedVariable() { - const int x = 0; - ABSL_TEST_LOG(INFO) << x; - } - static void LoggedParameter(const int x) { ABSL_TEST_LOG(INFO) << x; } - static void SeverityVariable() { - const int x = 0; - ABSL_TEST_LOG(LEVEL(x)) << "hello world"; - } - static void SeverityParameter(const int x) { - ABSL_TEST_LOG(LEVEL(x)) << "hello world"; - } -}; - -} // namespace absl_log_internal - -#endif // ABSL_LOG_LOG_BASIC_TEST_IMPL_H_ diff --git a/absl/log/log_basic_test_impl.inc b/absl/log/log_basic_test_impl.inc new file mode 100644 index 00000000..f3400095 --- /dev/null +++ b/absl/log/log_basic_test_impl.inc @@ -0,0 +1,455 @@ +// +// 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. + +// The testcases in this file are expected to pass or be skipped with any value +// of ABSL_MIN_LOG_LEVEL + +#ifndef ABSL_LOG_LOG_BASIC_TEST_IMPL_H_ +#define ABSL_LOG_LOG_BASIC_TEST_IMPL_H_ + +// Verify that both sets of macros behave identically by parameterizing the +// entire test file. +#ifndef ABSL_TEST_LOG +#error ABSL_TEST_LOG must be defined for these tests to work. +#endif + +#include +#include +#include + +#include "gmock/gmock.h" +#include "gtest/gtest.h" +#include "absl/base/internal/sysinfo.h" +#include "absl/base/log_severity.h" +#include "absl/log/globals.h" +#include "absl/log/internal/test_actions.h" +#include "absl/log/internal/test_helpers.h" +#include "absl/log/internal/test_matchers.h" +#include "absl/log/log_entry.h" +#include "absl/log/scoped_mock_log.h" + +namespace absl_log_internal { +#if GTEST_HAS_DEATH_TEST +using ::absl::log_internal::DeathTestExpectedLogging; +using ::absl::log_internal::DeathTestUnexpectedLogging; +using ::absl::log_internal::DeathTestValidateExpectations; +using ::absl::log_internal::DiedOfFatal; +using ::absl::log_internal::DiedOfQFatal; +#endif +using ::absl::log_internal::LoggingEnabledAt; +using ::absl::log_internal::LogSeverity; +using ::absl::log_internal::Prefix; +using ::absl::log_internal::SourceBasename; +using ::absl::log_internal::SourceFilename; +using ::absl::log_internal::SourceLine; +using ::absl::log_internal::Stacktrace; +using ::absl::log_internal::TextMessage; +using ::absl::log_internal::ThreadID; +using ::absl::log_internal::TimestampInMatchWindow; +using ::absl::log_internal::Verbosity; +using ::testing::AnyNumber; +using ::testing::Eq; +using ::testing::IsEmpty; +using ::testing::IsTrue; + +class BasicLogTest : public testing::TestWithParam {}; + +std::string ThresholdName( + testing::TestParamInfo severity) { + std::stringstream ostr; + ostr << severity.param; + return ostr.str().substr( + severity.param == absl::LogSeverityAtLeast::kInfinity ? 0 : 2); +} + +INSTANTIATE_TEST_SUITE_P(WithParam, BasicLogTest, + testing::Values(absl::LogSeverityAtLeast::kInfo, + absl::LogSeverityAtLeast::kWarning, + absl::LogSeverityAtLeast::kError, + absl::LogSeverityAtLeast::kFatal, + absl::LogSeverityAtLeast::kInfinity), + ThresholdName); + +TEST_P(BasicLogTest, Info) { + absl::log_internal::ScopedMinLogLevel scoped_min_log_level(GetParam()); + + absl::ScopedMockLog test_sink(absl::MockLogDefault::kDisallowUnexpected); + + const int log_line = __LINE__ + 1; + auto do_log = [] { ABSL_TEST_LOG(INFO) << "hello world"; }; + + if (LoggingEnabledAt(absl::LogSeverity::kInfo)) { + EXPECT_CALL( + test_sink, + Send(AllOf(SourceFilename(Eq(__FILE__)), + SourceBasename(Eq("log_basic_test_impl.inc")), + SourceLine(Eq(log_line)), Prefix(IsTrue()), + LogSeverity(Eq(absl::LogSeverity::kInfo)), + TimestampInMatchWindow(), + ThreadID(Eq(absl::base_internal::GetTID())), + TextMessage(Eq("hello world")), + Verbosity(Eq(absl::LogEntry::kNoVerbosityLevel)), + ENCODED_MESSAGE(EqualsProto(R"pb(value { + literal: "hello world" + })pb")), + Stacktrace(IsEmpty())))); + } + + test_sink.StartCapturingLogs(); + do_log(); +} + +TEST_P(BasicLogTest, Warning) { + absl::log_internal::ScopedMinLogLevel scoped_min_log_level(GetParam()); + + absl::ScopedMockLog test_sink(absl::MockLogDefault::kDisallowUnexpected); + + const int log_line = __LINE__ + 1; + auto do_log = [] { ABSL_TEST_LOG(WARNING) << "hello world"; }; + + if (LoggingEnabledAt(absl::LogSeverity::kWarning)) { + EXPECT_CALL( + test_sink, + Send(AllOf(SourceFilename(Eq(__FILE__)), + SourceBasename(Eq("log_basic_test_impl.inc")), + SourceLine(Eq(log_line)), Prefix(IsTrue()), + LogSeverity(Eq(absl::LogSeverity::kWarning)), + TimestampInMatchWindow(), + ThreadID(Eq(absl::base_internal::GetTID())), + TextMessage(Eq("hello world")), + Verbosity(Eq(absl::LogEntry::kNoVerbosityLevel)), + ENCODED_MESSAGE(EqualsProto(R"pb(value { + literal: "hello world" + })pb")), + Stacktrace(IsEmpty())))); + } + + test_sink.StartCapturingLogs(); + do_log(); +} + +TEST_P(BasicLogTest, Error) { + absl::log_internal::ScopedMinLogLevel scoped_min_log_level(GetParam()); + + absl::ScopedMockLog test_sink(absl::MockLogDefault::kDisallowUnexpected); + + const int log_line = __LINE__ + 1; + auto do_log = [] { ABSL_TEST_LOG(ERROR) << "hello world"; }; + + if (LoggingEnabledAt(absl::LogSeverity::kError)) { + EXPECT_CALL( + test_sink, + Send(AllOf(SourceFilename(Eq(__FILE__)), + SourceBasename(Eq("log_basic_test_impl.inc")), + SourceLine(Eq(log_line)), Prefix(IsTrue()), + LogSeverity(Eq(absl::LogSeverity::kError)), + TimestampInMatchWindow(), + ThreadID(Eq(absl::base_internal::GetTID())), + TextMessage(Eq("hello world")), + Verbosity(Eq(absl::LogEntry::kNoVerbosityLevel)), + ENCODED_MESSAGE(EqualsProto(R"pb(value { + literal: "hello world" + })pb")), + Stacktrace(IsEmpty())))); + } + + test_sink.StartCapturingLogs(); + do_log(); +} + +#if GTEST_HAS_DEATH_TEST +using BasicLogDeathTest = BasicLogTest; + +INSTANTIATE_TEST_SUITE_P(WithParam, BasicLogDeathTest, + testing::Values(absl::LogSeverityAtLeast::kInfo, + absl::LogSeverityAtLeast::kFatal, + absl::LogSeverityAtLeast::kInfinity), + ThresholdName); + +TEST_P(BasicLogDeathTest, Fatal) { + absl::log_internal::ScopedMinLogLevel scoped_min_log_level(GetParam()); + + const int log_line = __LINE__ + 1; + auto do_log = [] { ABSL_TEST_LOG(FATAL) << "hello world"; }; + + EXPECT_EXIT( + { + absl::ScopedMockLog test_sink( + absl::MockLogDefault::kDisallowUnexpected); + + EXPECT_CALL(test_sink, Send) + .Times(AnyNumber()) + .WillRepeatedly(DeathTestUnexpectedLogging()); + + ::testing::InSequence s; + + // Note the logic in DeathTestValidateExpectations() caters for the case + // of logging being disabled at FATAL level. + + if (LoggingEnabledAt(absl::LogSeverity::kFatal)) { + // The first call without the stack trace. + EXPECT_CALL( + test_sink, + Send(AllOf(SourceFilename(Eq(__FILE__)), + SourceBasename(Eq("log_basic_test_impl.inc")), + SourceLine(Eq(log_line)), Prefix(IsTrue()), + LogSeverity(Eq(absl::LogSeverity::kFatal)), + TimestampInMatchWindow(), + ThreadID(Eq(absl::base_internal::GetTID())), + TextMessage(Eq("hello world")), + Verbosity(Eq(absl::LogEntry::kNoVerbosityLevel)), + ENCODED_MESSAGE(EqualsProto( + R"pb(value { literal: "hello world" })pb")), + Stacktrace(IsEmpty())))) + .WillOnce(DeathTestExpectedLogging()); + + // The second call with the stack trace. + EXPECT_CALL( + test_sink, + Send(AllOf(SourceFilename(Eq(__FILE__)), + SourceBasename(Eq("log_basic_test_impl.inc")), + SourceLine(Eq(log_line)), Prefix(IsTrue()), + LogSeverity(Eq(absl::LogSeverity::kFatal)), + TimestampInMatchWindow(), + ThreadID(Eq(absl::base_internal::GetTID())), + TextMessage(Eq("hello world")), + Verbosity(Eq(absl::LogEntry::kNoVerbosityLevel)), + ENCODED_MESSAGE(EqualsProto( + R"pb(value { literal: "hello world" })pb")), + Stacktrace(Not(IsEmpty()))))) + .WillOnce(DeathTestExpectedLogging()); + } + + test_sink.StartCapturingLogs(); + do_log(); + }, + DiedOfFatal, DeathTestValidateExpectations()); +} + +TEST_P(BasicLogDeathTest, QFatal) { + absl::log_internal::ScopedMinLogLevel scoped_min_log_level(GetParam()); + + const int log_line = __LINE__ + 1; + auto do_log = [] { ABSL_TEST_LOG(QFATAL) << "hello world"; }; + + EXPECT_EXIT( + { + absl::ScopedMockLog test_sink( + absl::MockLogDefault::kDisallowUnexpected); + + EXPECT_CALL(test_sink, Send) + .Times(AnyNumber()) + .WillRepeatedly(DeathTestUnexpectedLogging()); + + if (LoggingEnabledAt(absl::LogSeverity::kFatal)) { + EXPECT_CALL( + test_sink, + Send(AllOf(SourceFilename(Eq(__FILE__)), + SourceBasename(Eq("log_basic_test_impl.inc")), + SourceLine(Eq(log_line)), Prefix(IsTrue()), + LogSeverity(Eq(absl::LogSeverity::kFatal)), + TimestampInMatchWindow(), + ThreadID(Eq(absl::base_internal::GetTID())), + TextMessage(Eq("hello world")), + Verbosity(Eq(absl::LogEntry::kNoVerbosityLevel)), + ENCODED_MESSAGE(EqualsProto( + R"pb(value { literal: "hello world" })pb")), + Stacktrace(IsEmpty())))) + .WillOnce(DeathTestExpectedLogging()); + } + + test_sink.StartCapturingLogs(); + do_log(); + }, + DiedOfQFatal, DeathTestValidateExpectations()); +} +#endif + +TEST_P(BasicLogTest, Level) { + absl::log_internal::ScopedMinLogLevel scoped_min_log_level(GetParam()); + + for (auto severity : {absl::LogSeverity::kInfo, absl::LogSeverity::kWarning, + absl::LogSeverity::kError}) { + absl::ScopedMockLog test_sink(absl::MockLogDefault::kDisallowUnexpected); + + const int log_line = __LINE__ + 2; + auto do_log = [severity] { + ABSL_TEST_LOG(LEVEL(severity)) << "hello world"; + }; + + if (LoggingEnabledAt(severity)) { + EXPECT_CALL( + test_sink, + Send(AllOf(SourceFilename(Eq(__FILE__)), + SourceBasename(Eq("log_basic_test_impl.inc")), + SourceLine(Eq(log_line)), Prefix(IsTrue()), + LogSeverity(Eq(severity)), TimestampInMatchWindow(), + ThreadID(Eq(absl::base_internal::GetTID())), + TextMessage(Eq("hello world")), + Verbosity(Eq(absl::LogEntry::kNoVerbosityLevel)), + ENCODED_MESSAGE(EqualsProto(R"pb(value { + literal: "hello world" + })pb")), + Stacktrace(IsEmpty())))); + } + test_sink.StartCapturingLogs(); + do_log(); + } +} + +#if GTEST_HAS_DEATH_TEST +TEST_P(BasicLogDeathTest, Level) { + // TODO(b/242568884): re-enable once bug is fixed. + // absl::log_internal::ScopedMinLogLevel scoped_min_log_level(GetParam()); + + // Ensure that `severity` is not a compile-time constant to prove that + // `LOG(LEVEL(severity))` works regardless: + auto volatile severity = absl::LogSeverity::kFatal; + + const int log_line = __LINE__ + 1; + auto do_log = [severity] { ABSL_TEST_LOG(LEVEL(severity)) << "hello world"; }; + + EXPECT_EXIT( + { + absl::ScopedMockLog test_sink( + absl::MockLogDefault::kDisallowUnexpected); + + EXPECT_CALL(test_sink, Send) + .Times(AnyNumber()) + .WillRepeatedly(DeathTestUnexpectedLogging()); + + ::testing::InSequence s; + + if (LoggingEnabledAt(absl::LogSeverity::kFatal)) { + EXPECT_CALL( + test_sink, + Send(AllOf(SourceFilename(Eq(__FILE__)), + SourceBasename(Eq("log_basic_test_impl.inc")), + SourceLine(Eq(log_line)), Prefix(IsTrue()), + LogSeverity(Eq(absl::LogSeverity::kFatal)), + TimestampInMatchWindow(), + ThreadID(Eq(absl::base_internal::GetTID())), + TextMessage(Eq("hello world")), + Verbosity(Eq(absl::LogEntry::kNoVerbosityLevel)), + ENCODED_MESSAGE(EqualsProto( + R"pb(value { literal: "hello world" })pb")), + Stacktrace(IsEmpty())))) + .WillOnce(DeathTestExpectedLogging()); + + EXPECT_CALL( + test_sink, + Send(AllOf(SourceFilename(Eq(__FILE__)), + SourceBasename(Eq("log_basic_test_impl.inc")), + SourceLine(Eq(log_line)), Prefix(IsTrue()), + LogSeverity(Eq(absl::LogSeverity::kFatal)), + TimestampInMatchWindow(), + ThreadID(Eq(absl::base_internal::GetTID())), + TextMessage(Eq("hello world")), + Verbosity(Eq(absl::LogEntry::kNoVerbosityLevel)), + ENCODED_MESSAGE(EqualsProto( + R"pb(value { literal: "hello world" })pb")), + Stacktrace(Not(IsEmpty()))))) + .WillOnce(DeathTestExpectedLogging()); + } + + test_sink.StartCapturingLogs(); + do_log(); + }, + DiedOfFatal, DeathTestValidateExpectations()); +} +#endif + +TEST_P(BasicLogTest, LevelClampsNegativeValues) { + absl::log_internal::ScopedMinLogLevel scoped_min_log_level(GetParam()); + + if (!LoggingEnabledAt(absl::LogSeverity::kInfo)) { + GTEST_SKIP() << "This test cases required INFO log to be enabled"; + return; + } + + absl::ScopedMockLog test_sink(absl::MockLogDefault::kDisallowUnexpected); + + EXPECT_CALL(test_sink, Send(LogSeverity(Eq(absl::LogSeverity::kInfo)))); + + test_sink.StartCapturingLogs(); + ABSL_TEST_LOG(LEVEL(-1)) << "hello world"; +} + +TEST_P(BasicLogTest, LevelClampsLargeValues) { + absl::log_internal::ScopedMinLogLevel scoped_min_log_level(GetParam()); + + if (!LoggingEnabledAt(absl::LogSeverity::kError)) { + GTEST_SKIP() << "This test cases required ERROR log to be enabled"; + return; + } + + absl::ScopedMockLog test_sink(absl::MockLogDefault::kDisallowUnexpected); + + EXPECT_CALL(test_sink, Send(LogSeverity(Eq(absl::LogSeverity::kError)))); + + test_sink.StartCapturingLogs(); + ABSL_TEST_LOG(LEVEL(static_cast(absl::LogSeverity::kFatal) + 1)) + << "hello world"; +} + +TEST(ErrnoPreservationTest, InSeverityExpression) { + errno = 77; + int saved_errno; + ABSL_TEST_LOG(LEVEL((saved_errno = errno, absl::LogSeverity::kInfo))); + EXPECT_THAT(saved_errno, Eq(77)); +} + +TEST(ErrnoPreservationTest, InStreamedExpression) { + if (!LoggingEnabledAt(absl::LogSeverity::kInfo)) { + GTEST_SKIP() << "This test cases required INFO log to be enabled"; + return; + } + + errno = 77; + int saved_errno = 0; + ABSL_TEST_LOG(INFO) << (saved_errno = errno, "hello world"); + EXPECT_THAT(saved_errno, Eq(77)); +} + +TEST(ErrnoPreservationTest, AfterStatement) { + errno = 77; + ABSL_TEST_LOG(INFO); + const int saved_errno = errno; + EXPECT_THAT(saved_errno, Eq(77)); +} + +// Tests that using a variable/parameter in a logging statement suppresses +// unused-variable/parameter warnings. +// ----------------------------------------------------------------------- +class UnusedVariableWarningCompileTest { + // These four don't prove anything unless `ABSL_MIN_LOG_LEVEL` is greater than + // `kInfo`. + static void LoggedVariable() { + const int x = 0; + ABSL_TEST_LOG(INFO) << x; + } + static void LoggedParameter(const int x) { ABSL_TEST_LOG(INFO) << x; } + static void SeverityVariable() { + const int x = 0; + ABSL_TEST_LOG(LEVEL(x)) << "hello world"; + } + static void SeverityParameter(const int x) { + ABSL_TEST_LOG(LEVEL(x)) << "hello world"; + } +}; + +} // namespace absl_log_internal + +#endif // ABSL_LOG_LOG_BASIC_TEST_IMPL_H_ -- cgit v1.2.3