diff options
author | Mike Kruskal <mkruskal@google.com> | 2022-12-06 13:55:58 -0800 |
---|---|---|
committer | Copybara-Service <copybara-worker@google.com> | 2022-12-06 13:56:42 -0800 |
commit | c96db73c09dbb528eca6d19a50bd258b37e9fd5e (patch) | |
tree | 0cbf2f7b1ea6eb98f0fc39d5214e7a9a30de0505 | |
parent | bcc29b8c261b2fa843889c63b79118398b9df4a3 (diff) | |
download | abseil-c96db73c09dbb528eca6d19a50bd258b37e9fd5e.tar.gz abseil-c96db73c09dbb528eca6d19a50bd258b37e9fd5e.tar.bz2 abseil-c96db73c09dbb528eca6d19a50bd258b37e9fd5e.zip |
Create alternate absl-prefixed versions of absl logging macros.
This will allow OSS code to use absl logging without necessarily polluting the preprocessor symbols with definitions for LOG and CHECK
PiperOrigin-RevId: 493404211
Change-Id: I7bc5807252218dd7fc26da3af13d5734ef8b2601
-rw-r--r-- | absl/log/BUILD.bazel | 108 | ||||
-rw-r--r-- | absl/log/CMakeLists.txt | 100 | ||||
-rw-r--r-- | absl/log/absl_check.h | 81 | ||||
-rw-r--r-- | absl/log/absl_check_test.cc | 55 | ||||
-rw-r--r-- | absl/log/absl_log.h | 94 | ||||
-rw-r--r-- | absl/log/absl_log_basic_test.cc | 21 | ||||
-rw-r--r-- | absl/log/check_test.cc | 450 | ||||
-rw-r--r-- | absl/log/check_test_impl.h | 449 | ||||
-rw-r--r-- | absl/log/log_basic_test.cc | 21 | ||||
-rw-r--r-- | absl/log/log_basic_test_impl.h (renamed from absl/log/basic_log_test.cc) | 73 |
10 files changed, 985 insertions, 467 deletions
diff --git a/absl/log/BUILD.bazel b/absl/log/BUILD.bazel index 957709dd..a420f21a 100644 --- a/absl/log/BUILD.bazel +++ b/absl/log/BUILD.bazel @@ -26,6 +26,27 @@ package(default_visibility = ["//visibility:public"]) licenses(["notice"]) # Public targets + +cc_library( + name = "absl_check", + hdrs = ["absl_check.h"], + copts = ABSL_DEFAULT_COPTS, + linkopts = ABSL_DEFAULT_LINKOPTS, + deps = [ + "//absl/log/internal:check_impl", + ], +) + +cc_library( + name = "absl_log", + hdrs = ["absl_log.h"], + copts = ABSL_DEFAULT_COPTS, + linkopts = ABSL_DEFAULT_LINKOPTS, + deps = [ + "//absl/log/internal:log_impl", + ], +) + cc_library( name = "check", hdrs = ["check.h"], @@ -207,22 +228,34 @@ cc_library( ) # Test targets + cc_test( - name = "basic_log_test", + name = "absl_check_test", size = "small", - srcs = ["basic_log_test.cc"], + srcs = ["absl_check_test.cc"], copts = ABSL_TEST_COPTS, linkopts = ABSL_DEFAULT_LINKOPTS, + tags = [ + "no_test:os:ios", + "no_test_ios", + "no_test_wasm", + ], deps = [ - ":globals", - ":log", - ":log_entry", - ":scoped_mock_log", - "//absl/base", - "//absl/base:log_severity", - "//absl/log/internal:test_actions", - "//absl/log/internal:test_helpers", - "//absl/log/internal:test_matchers", + ":absl_check", + ":check_test_impl", + "@com_google_googletest//:gtest_main", + ], +) + +cc_test( + name = "absl_log_basic_test", + size = "small", + srcs = ["absl_log_basic_test.cc"], + copts = ABSL_TEST_COPTS, + linkopts = ABSL_DEFAULT_LINKOPTS, + deps = [ + ":absl_log", + ":log_basic_test_impl", "@com_google_googletest//:gtest_main", ], ) @@ -240,10 +273,28 @@ cc_test( ], deps = [ ":check", + ":check_test_impl", + "@com_google_googletest//:gtest_main", + ], +) + +cc_library( + name = "check_test_impl", + testonly = True, + copts = ABSL_TEST_COPTS, + linkopts = ABSL_DEFAULT_LINKOPTS, + tags = [ + "no_test:os:ios", + "no_test_ios", + "no_test_wasm", + ], + textual_hdrs = ["check_test_impl.h"], + visibility = ["//visibility:private"], + deps = [ "//absl/base:config", "//absl/base:core_headers", "//absl/log/internal:test_helpers", - "@com_google_googletest//:gtest_main", + "@com_google_googletest//:gtest", ], ) @@ -303,6 +354,39 @@ cc_test( ) cc_test( + name = "log_basic_test", + size = "small", + srcs = ["log_basic_test.cc"], + copts = ABSL_TEST_COPTS, + linkopts = ABSL_DEFAULT_LINKOPTS, + deps = [ + ":log", + ":log_basic_test_impl", + "@com_google_googletest//:gtest_main", + ], +) + +cc_library( + name = "log_basic_test_impl", + testonly = True, + copts = ABSL_TEST_COPTS, + linkopts = ABSL_DEFAULT_LINKOPTS, + textual_hdrs = ["log_basic_test_impl.h"], + visibility = ["//visibility:private"], + deps = [ + "//absl/base", + "//absl/base:log_severity", + "//absl/log:globals", + "//absl/log:log_entry", + "//absl/log:scoped_mock_log", + "//absl/log/internal:test_actions", + "//absl/log/internal:test_helpers", + "//absl/log/internal:test_matchers", + "@com_google_googletest//:gtest", + ], +) + +cc_test( name = "log_entry_test", size = "small", srcs = ["log_entry_test.cc"], diff --git a/absl/log/CMakeLists.txt b/absl/log/CMakeLists.txt index 875540dd..36a60da3 100644 --- a/absl/log/CMakeLists.txt +++ b/absl/log/CMakeLists.txt @@ -391,6 +391,36 @@ absl_cc_library( # Public targets absl_cc_library( NAME + absl_check + SRCS + HDRS + "absl_check.h" + COPTS + ${ABSL_DEFAULT_COPTS} + LINKOPTS + ${ABSL_DEFAULT_LINKOPTS} + DEPS + absl::log_internal_check_impl + PUBLIC +) + +absl_cc_library( + NAME + absl_log + SRCS + HDRS + "absl_log.h" + COPTS + ${ABSL_DEFAULT_COPTS} + LINKOPTS + ${ABSL_DEFAULT_LINKOPTS} + DEPS + absl::log_internal_log_impl + PUBLIC +) + +absl_cc_library( + NAME check SRCS HDRS @@ -640,18 +670,39 @@ absl_cc_library( ) # Test targets + +absl_cc_test( + NAME + absl_check_test + SRCS + "absl_check_test.cc" + "check_test_impl.h" + COPTS + ${ABSL_TEST_COPTS} + LINKOPTS + ${ABSL_DEFAULT_LINKOPTS} + DEPS + absl::absl_check + absl::config + absl::core_headers + absl::log_internal_test_helpers + GTest::gmock + GTest::gtest_main +) + absl_cc_test( NAME - basic_log_test + absl_log_basic_test SRCS - "basic_log_test.cc" + "log_basic_test.cc" + "log_basic_test_impl.h" COPTS ${ABSL_TEST_COPTS} LINKOPTS ${ABSL_DEFAULT_LINKOPTS} DEPS absl::base - absl::log + absl::absl_log absl::log_entry absl::log_globals absl::log_severity @@ -668,6 +719,7 @@ absl_cc_test( check_test SRCS "check_test.cc" + "check_test_impl.h" COPTS ${ABSL_TEST_COPTS} LINKOPTS @@ -699,26 +751,24 @@ absl_cc_test( absl_cc_test( NAME - log_flags_test + log_basic_test SRCS - "flags_test.cc" + "log_basic_test.cc" + "log_basic_test_impl.h" COPTS ${ABSL_TEST_COPTS} LINKOPTS ${ABSL_DEFAULT_LINKOPTS} DEPS - absl::core_headers + absl::base absl::log - absl::log_flags + absl::log_entry absl::log_globals - absl::log_internal_flags + absl::log_severity + absl::log_internal_test_actions absl::log_internal_test_helpers absl::log_internal_test_matchers - absl::log_severity - absl::flags - absl::flags_reflection absl::scoped_mock_log - absl::strings GTest::gmock GTest::gtest_main ) @@ -750,6 +800,32 @@ absl_cc_test( absl_cc_test( NAME + log_flags_test + SRCS + "flags_test.cc" + COPTS + ${ABSL_TEST_COPTS} + LINKOPTS + ${ABSL_DEFAULT_LINKOPTS} + DEPS + absl::core_headers + absl::log + absl::log_flags + absl::log_globals + absl::log_internal_flags + absl::log_internal_test_helpers + absl::log_internal_test_matchers + absl::log_severity + absl::flags + absl::flags_reflection + absl::scoped_mock_log + absl::strings + GTest::gmock + GTest::gtest_main +) + +absl_cc_test( + NAME log_globals_test SRCS "globals_test.cc" diff --git a/absl/log/absl_check.h b/absl/log/absl_check.h new file mode 100644 index 00000000..b8428bf7 --- /dev/null +++ b/absl/log/absl_check.h @@ -0,0 +1,81 @@ +// Copyright 2022 The Abseil Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ----------------------------------------------------------------------------- +// File: log/absl_check.h +// ----------------------------------------------------------------------------- +// +// This header declares a family of `ABSL_CHECK` macros as alternative spellings +// for `CHECK` macros in `check.h`. +// +// Except for those whose names begin with `ABSL_DCHECK`, these macros are not +// controlled by `NDEBUG` (cf. `assert`), so the check will be executed +// regardless of compilation mode. `ABSL_CHECK` and friends are thus useful for +// confirming invariants in situations where continuing to run would be worse +// than terminating, e.g., due to risk of data corruption or security +// compromise. It is also more robust and portable to deliberately terminate +// at a particular place with a useful message and backtrace than to assume some +// ultimately unspecified and unreliable crashing behavior (such as a +// "segmentation fault"). +// +// For full documentation of each macro, see comments in `check.h`, which has an +// identical set of macros without the ABSL_* prefix. + +#ifndef ABSL_LOG_ABSL_CHECK_H_ +#define ABSL_LOG_ABSL_CHECK_H_ + +#include "absl/log/internal/check_impl.h" + +#define ABSL_CHECK(condition) ABSL_CHECK_IMPL(condition) +#define ABSL_QCHECK(condition) ABSL_QCHECK_IMPL(condition) +#define ABSL_PCHECK(condition) ABSL_PCHECK_IMPL(condition) +#define ABSL_DCHECK(condition) ABSL_DCHECK_IMPL(condition) + +#define ABSL_CHECK_EQ(val1, val2) ABSL_CHECK_EQ_IMPL(val1, val2) +#define ABSL_CHECK_NE(val1, val2) ABSL_CHECK_NE_IMPL(val1, val2) +#define ABSL_CHECK_LE(val1, val2) ABSL_CHECK_LE_IMPL(val1, val2) +#define ABSL_CHECK_LT(val1, val2) ABSL_CHECK_LT_IMPL(val1, val2) +#define ABSL_CHECK_GE(val1, val2) ABSL_CHECK_GE_IMPL(val1, val2) +#define ABSL_CHECK_GT(val1, val2) ABSL_CHECK_GT_IMPL(val1, val2) +#define ABSL_QCHECK_EQ(val1, val2) ABSL_QCHECK_EQ_IMPL(val1, val2) +#define ABSL_QCHECK_NE(val1, val2) ABSL_QCHECK_NE_IMPL(val1, val2) +#define ABSL_QCHECK_LE(val1, val2) ABSL_QCHECK_LE_IMPL(val1, val2) +#define ABSL_QCHECK_LT(val1, val2) ABSL_QCHECK_LT_IMPL(val1, val2) +#define ABSL_QCHECK_GE(val1, val2) ABSL_QCHECK_GE_IMPL(val1, val2) +#define ABSL_QCHECK_GT(val1, val2) ABSL_QCHECK_GT_IMPL(val1, val2) +#define ABSL_DCHECK_EQ(val1, val2) ABSL_DCHECK_EQ_IMPL(val1, val2) +#define ABSL_DCHECK_NE(val1, val2) ABSL_DCHECK_NE_IMPL(val1, val2) +#define ABSL_DCHECK_LE(val1, val2) ABSL_DCHECK_LE_IMPL(val1, val2) +#define ABSL_DCHECK_LT(val1, val2) ABSL_DCHECK_LT_IMPL(val1, val2) +#define ABSL_DCHECK_GE(val1, val2) ABSL_DCHECK_GE_IMPL(val1, val2) +#define ABSL_DCHECK_GT(val1, val2) ABSL_DCHECK_GT_IMPL(val1, val2) + +#define ABSL_CHECK_OK(status) ABSL_CHECK_OK_IMPL(status) +#define ABSL_QCHECK_OK(status) ABSL_QCHECK_OK_IMPL(status) +#define ABSL_DCHECK_OK(status) ABSL_DCHECK_OK_IMPL(status) + +#define ABSL_CHECK_STREQ(s1, s2) ABSL_CHECK_STREQ_IMPL(s1, s2) +#define ABSL_CHECK_STRNE(s1, s2) ABSL_CHECK_STRNE_IMPL(s1, s2) +#define ABSL_CHECK_STRCASEEQ(s1, s2) ABSL_CHECK_STRCASEEQ_IMPL(s1, s2) +#define ABSL_CHECK_STRCASENE(s1, s2) ABSL_CHECK_STRCASENE_IMPL(s1, s2) +#define ABSL_QCHECK_STREQ(s1, s2) ABSL_QCHECK_STREQ_IMPL(s1, s2) +#define ABSL_QCHECK_STRNE(s1, s2) ABSL_QCHECK_STRNE_IMPL(s1, s2) +#define ABSL_QCHECK_STRCASEEQ(s1, s2) ABSL_QCHECK_STRCASEEQ_IMPL(s1, s2) +#define ABSL_QCHECK_STRCASENE(s1, s2) ABSL_QCHECK_STRCASENE_IMPL(s1, s2) +#define ABSL_DCHECK_STREQ(s1, s2) ABSL_DCHECK_STREQ_IMPL(s1, s2) +#define ABSL_DCHECK_STRNE(s1, s2) ABSL_DCHECK_STRNE_IMPL(s1, s2) +#define ABSL_DCHECK_STRCASEEQ(s1, s2) ABSL_DCHECK_STRCASEEQ_IMPL(s1, s2) +#define ABSL_DCHECK_STRCASENE(s1, s2) ABSL_DCHECK_STRCASENE_IMPL(s1, s2) + +#endif // ABSL_LOG_ABSL_CHECK_H_ diff --git a/absl/log/absl_check_test.cc b/absl/log/absl_check_test.cc new file mode 100644 index 00000000..2c4853dd --- /dev/null +++ b/absl/log/absl_check_test.cc @@ -0,0 +1,55 @@ +// +// 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. + +#include "absl/log/absl_check.h" + +#define ABSL_TEST_CHECK ABSL_CHECK +#define ABSL_TEST_CHECK_EQ ABSL_CHECK_EQ +#define ABSL_TEST_CHECK_NE ABSL_CHECK_NE +#define ABSL_TEST_CHECK_GE ABSL_CHECK_GE +#define ABSL_TEST_CHECK_LE ABSL_CHECK_LE +#define ABSL_TEST_CHECK_GT ABSL_CHECK_GT +#define ABSL_TEST_CHECK_LT ABSL_CHECK_LT +#define ABSL_TEST_CHECK_STREQ ABSL_CHECK_STREQ +#define ABSL_TEST_CHECK_STRNE ABSL_CHECK_STRNE +#define ABSL_TEST_CHECK_STRCASEEQ ABSL_CHECK_STRCASEEQ +#define ABSL_TEST_CHECK_STRCASENE ABSL_CHECK_STRCASENE + +#define ABSL_TEST_DCHECK ABSL_DCHECK +#define ABSL_TEST_DCHECK_EQ ABSL_DCHECK_EQ +#define ABSL_TEST_DCHECK_NE ABSL_DCHECK_NE +#define ABSL_TEST_DCHECK_GE ABSL_DCHECK_GE +#define ABSL_TEST_DCHECK_LE ABSL_DCHECK_LE +#define ABSL_TEST_DCHECK_GT ABSL_DCHECK_GT +#define ABSL_TEST_DCHECK_LT ABSL_DCHECK_LT +#define ABSL_TEST_DCHECK_STREQ ABSL_DCHECK_STREQ +#define ABSL_TEST_DCHECK_STRNE ABSL_DCHECK_STRNE +#define ABSL_TEST_DCHECK_STRCASEEQ ABSL_DCHECK_STRCASEEQ +#define ABSL_TEST_DCHECK_STRCASENE ABSL_DCHECK_STRCASENE + +#define ABSL_TEST_QCHECK ABSL_QCHECK +#define ABSL_TEST_QCHECK_EQ ABSL_QCHECK_EQ +#define ABSL_TEST_QCHECK_NE ABSL_QCHECK_NE +#define ABSL_TEST_QCHECK_GE ABSL_QCHECK_GE +#define ABSL_TEST_QCHECK_LE ABSL_QCHECK_LE +#define ABSL_TEST_QCHECK_GT ABSL_QCHECK_GT +#define ABSL_TEST_QCHECK_LT ABSL_QCHECK_LT +#define ABSL_TEST_QCHECK_STREQ ABSL_QCHECK_STREQ +#define ABSL_TEST_QCHECK_STRNE ABSL_QCHECK_STRNE +#define ABSL_TEST_QCHECK_STRCASEEQ ABSL_QCHECK_STRCASEEQ +#define ABSL_TEST_QCHECK_STRCASENE ABSL_QCHECK_STRCASENE + +#include "gtest/gtest.h" +#include "absl/log/check_test_impl.h" diff --git a/absl/log/absl_log.h b/absl/log/absl_log.h new file mode 100644 index 00000000..1c6cf263 --- /dev/null +++ b/absl/log/absl_log.h @@ -0,0 +1,94 @@ +// Copyright 2022 The Abseil Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ----------------------------------------------------------------------------- +// File: log/absl_log.h +// ----------------------------------------------------------------------------- +// +// This header declares a family of `ABSL_LOG` macros as alternative spellings +// for macros in `log.h`. +// +// Basic invocation looks like this: +// +// ABSL_LOG(INFO) << "Found " << num_cookies << " cookies"; +// +// Most `ABSL_LOG` macros take a severity level argument. The severity levels +// are `INFO`, `WARNING`, `ERROR`, and `FATAL`. +// +// For full documentation, see comments in `log.h`, which includes full +// reference documentation on use of the equivalent `LOG` macro and has an +// identical set of macros without the ABSL_* prefix. + +#ifndef ABSL_LOG_ABSL_LOG_H_ +#define ABSL_LOG_ABSL_LOG_H_ + +#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_IF(severity, condition) \ + ABSL_LOG_IF_IMPL(_##severity, condition) +#define ABSL_PLOG_IF(severity, condition) \ + ABSL_PLOG_IF_IMPL(_##severity, condition) +#define ABSL_DLOG_IF(severity, condition) \ + ABSL_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_SEC(severity, n_seconds) \ + ABSL_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_SEC(severity, n_seconds) \ + ABSL_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_SEC(severity, n_seconds) \ + ABSL_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) +#define ABSL_LOG_IF_FIRST_N(severity, condition, n) \ + ABSL_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) +#define ABSL_LOG_IF_EVERY_N_SEC(severity, condition, n_seconds) \ + ABSL_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) +#define ABSL_PLOG_IF_FIRST_N(severity, condition, n) \ + ABSL_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) +#define ABSL_PLOG_IF_EVERY_N_SEC(severity, condition, n_seconds) \ + ABSL_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) +#define ABSL_DLOG_IF_FIRST_N(severity, condition, n) \ + ABSL_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) +#define ABSL_DLOG_IF_EVERY_N_SEC(severity, condition, n_seconds) \ + ABSL_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 new file mode 100644 index 00000000..bc8a787d --- /dev/null +++ b/absl/log/absl_log_basic_test.cc @@ -0,0 +1,21 @@ +// +// 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. + +#include "absl/log/absl_log.h" + +#define ABSL_TEST_LOG ABSL_LOG + +#include "gtest/gtest.h" +#include "absl/log/log_basic_test_impl.h" diff --git a/absl/log/check_test.cc b/absl/log/check_test.cc index 755adb3a..a03183a5 100644 --- a/absl/log/check_test.cc +++ b/absl/log/check_test.cc @@ -15,419 +15,41 @@ #include "absl/log/check.h" -#include <ostream> -#include <string> +#define ABSL_TEST_CHECK CHECK +#define ABSL_TEST_CHECK_EQ CHECK_EQ +#define ABSL_TEST_CHECK_NE CHECK_NE +#define ABSL_TEST_CHECK_GE CHECK_GE +#define ABSL_TEST_CHECK_LE CHECK_LE +#define ABSL_TEST_CHECK_GT CHECK_GT +#define ABSL_TEST_CHECK_LT CHECK_LT +#define ABSL_TEST_CHECK_STREQ CHECK_STREQ +#define ABSL_TEST_CHECK_STRNE CHECK_STRNE +#define ABSL_TEST_CHECK_STRCASEEQ CHECK_STRCASEEQ +#define ABSL_TEST_CHECK_STRCASENE CHECK_STRCASENE + +#define ABSL_TEST_DCHECK DCHECK +#define ABSL_TEST_DCHECK_EQ DCHECK_EQ +#define ABSL_TEST_DCHECK_NE DCHECK_NE +#define ABSL_TEST_DCHECK_GE DCHECK_GE +#define ABSL_TEST_DCHECK_LE DCHECK_LE +#define ABSL_TEST_DCHECK_GT DCHECK_GT +#define ABSL_TEST_DCHECK_LT DCHECK_LT +#define ABSL_TEST_DCHECK_STREQ DCHECK_STREQ +#define ABSL_TEST_DCHECK_STRNE DCHECK_STRNE +#define ABSL_TEST_DCHECK_STRCASEEQ DCHECK_STRCASEEQ +#define ABSL_TEST_DCHECK_STRCASENE DCHECK_STRCASENE + +#define ABSL_TEST_QCHECK QCHECK +#define ABSL_TEST_QCHECK_EQ QCHECK_EQ +#define ABSL_TEST_QCHECK_NE QCHECK_NE +#define ABSL_TEST_QCHECK_GE QCHECK_GE +#define ABSL_TEST_QCHECK_LE QCHECK_LE +#define ABSL_TEST_QCHECK_GT QCHECK_GT +#define ABSL_TEST_QCHECK_LT QCHECK_LT +#define ABSL_TEST_QCHECK_STREQ QCHECK_STREQ +#define ABSL_TEST_QCHECK_STRNE QCHECK_STRNE +#define ABSL_TEST_QCHECK_STRCASEEQ QCHECK_STRCASEEQ +#define ABSL_TEST_QCHECK_STRCASENE QCHECK_STRCASENE -#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" - -namespace { -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) { - CHECK(true); - - EXPECT_DEATH(CHECK(false), "Check failed: false"); - - int i = 2; - CHECK(i != 3); // NOLINT -} - -#endif // GTEST_HAS_DEATH_TEST - -TEST(CHECKTest, TestLogicExpressions) { - int i = 5; - CHECK(i > 0 && i < 10); - CHECK(i < 0 || i > 3); -} - -#if ABSL_INTERNAL_CPLUSPLUS_LANG >= 201703L -ABSL_CONST_INIT const auto global_var_check = [](int i) { - CHECK(i > 0); // NOLINT - return i + 1; -}(3); - -ABSL_CONST_INIT const auto global_var = [](int i) { - 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) CHECK(true); - - if (false) - ; // NOLINT - else - CHECK(true); - - switch (0) - case 0: - CHECK(true); // NOLINT - -#if ABSL_INTERNAL_CPLUSPLUS_LANG >= 201703L - constexpr auto var = [](int i) { - CHECK(i > 0); // NOLINT - return i + 1; - }(global_var); - (void)var; -#endif // ABSL_INTERNAL_CPLUSPLUS_LANG -} - -TEST(CHECKTest, TestBoolConvertible) { - struct Tester { - } tester; - CHECK([&]() { return &tester; }()); -} - -#if GTEST_HAS_DEATH_TEST - -TEST(CHECKDeathTest, TestChecksWithSideEffects) { - int var = 0; - CHECK([&var]() { - ++var; - return true; - }()); - EXPECT_EQ(var, 1); - - EXPECT_DEATH(CHECK([&var]() { - ++var; - return false; - }()) << var, - "Check failed: .* 2"); -} - -#endif // GTEST_HAS_DEATH_TEST - -#if GTEST_HAS_DEATH_TEST - -TEST(CHECKDeachTest, TestOrderOfInvocationsBetweenCheckAndMessage) { - int counter = 0; - - auto GetStr = [&counter]() -> std::string { - return counter++ == 0 ? "" : "non-empty"; - }; - - EXPECT_DEATH(CHECK(!GetStr().empty()) << GetStr(), HasSubstr("non-empty")); -} - -TEST(CHECKTest, TestSecondaryFailure) { - auto FailingRoutine = []() { - CHECK(false) << "Secondary"; - return false; - }; - EXPECT_DEATH(CHECK(FailingRoutine()) << "Primary", - AllOf(HasSubstr("Secondary"), Not(HasSubstr("Primary")))); -} - -TEST(CHECKTest, TestSecondaryFailureInMessage) { - auto MessageGen = []() { - CHECK(false) << "Secondary"; - return "Primary"; - }; - EXPECT_DEATH(CHECK(false) << MessageGen(), - AllOf(HasSubstr("Secondary"), Not(HasSubstr("Primary")))); -} - -#endif // GTEST_HAS_DEATH_TEST - -TEST(CHECKTest, TestBinaryChecksWithPrimitives) { - CHECK_EQ(1, 1); - CHECK_NE(1, 2); - CHECK_GE(1, 1); - CHECK_GE(2, 1); - CHECK_LE(1, 1); - CHECK_LE(1, 2); - CHECK_GT(2, 1); - 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. - CHECK_EQ(CASE_A, CASE_A); - CHECK_NE(CASE_A, CASE_B); - CHECK_GE(CASE_A, CASE_A); - CHECK_GE(CASE_B, CASE_A); - CHECK_LE(CASE_A, CASE_A); - CHECK_LE(CASE_A, CASE_B); - CHECK_GT(CASE_B, CASE_A); - CHECK_LT(CASE_A, CASE_B); -} - -TEST(CHECKTest, TestBinaryChecksWithNullptr) { - const void* p_null = nullptr; - const void* p_not_null = &p_null; - CHECK_EQ(p_null, nullptr); - CHECK_EQ(nullptr, p_null); - CHECK_NE(p_not_null, nullptr); - 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(CHECK_EQ(a, b), "Check failed: a == b \\(';' vs. 'b'\\)"); - b = 1; - EXPECT_DEATH(CHECK_EQ(a, b), - "Check failed: a == b \\(';' vs. char value 1\\)"); - } - { - signed char a = ';'; - signed char b = 'b'; - EXPECT_DEATH(CHECK_EQ(a, b), "Check failed: a == b \\(';' vs. 'b'\\)"); - b = -128; - EXPECT_DEATH(CHECK_EQ(a, b), - "Check failed: a == b \\(';' vs. signed char value -128\\)"); - } - { - unsigned char a = ';'; - unsigned char b = 'b'; - EXPECT_DEATH(CHECK_EQ(a, b), "Check failed: a == b \\(';' vs. 'b'\\)"); - b = 128; - EXPECT_DEATH(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(CHECK_NE(a, b), - "Check failed: a != b \\(\\(null\\) vs. \\(null\\)\\)"); - - a = "xx"; - EXPECT_DEATH(CHECK_EQ(a, b), "Check failed: a == b \\(xx vs. \\(null\\)\\)"); - EXPECT_DEATH(CHECK_EQ(b, a), "Check failed: b == a \\(\\(null\\) vs. xx\\)"); - - std::nullptr_t n{}; - EXPECT_DEATH(CHECK_NE(n, nullptr), - "Check failed: n != nullptr \\(\\(null\\) vs. \\(null\\)\\)"); -} - -#endif // GTEST_HAS_DEATH_TEST - -TEST(CHECKTest, TestSTREQ) { - CHECK_STREQ("this", "this"); - CHECK_STREQ(nullptr, nullptr); - CHECK_STRCASEEQ("this", "tHiS"); - CHECK_STRCASEEQ(nullptr, nullptr); - CHECK_STRNE("this", "tHiS"); - CHECK_STRNE("this", nullptr); - CHECK_STRCASENE("this", "that"); - CHECK_STRCASENE(nullptr, "that"); - CHECK_STREQ((std::string("a") + "b").c_str(), "ab"); - 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) CHECK_EQ(1, 1); - if (true) CHECK_STREQ("c", "c"); - - if (false) - ; // NOLINT - else - CHECK_LE(0, 1); - - if (false) - ; // NOLINT - else - CHECK_STRNE("a", "b"); - - switch (0) - case 0: - CHECK_NE(1, 0); - - switch (0) - case 0: - CHECK_STRCASEEQ("A", "a"); - -#if ABSL_INTERNAL_CPLUSPLUS_LANG >= 201703L - constexpr auto var = [](int i) { - 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) { - // CHECK_STRNE("c", "d"); - // return i + 1; - // }(global_var); - -#if defined(__GNUC__) - int var3 = (({ CHECK_LE(1, 2); }), global_var < 10) ? 1 : 0; - (void)var3; - - int var4 = (({ CHECK_STREQ("a", "a"); }), global_var < 10) ? 1 : 0; - (void)var4; -#endif // __GNUC__ -#endif // ABSL_INTERNAL_CPLUSPLUS_LANG -} - -TEST(CHECKTest, TestDCHECK) { -#ifdef NDEBUG - DCHECK(1 == 2) << " DCHECK's shouldn't be compiled in normal mode"; -#endif - DCHECK(1 == 1); // NOLINT(readability/check) - DCHECK_EQ(1, 1); - DCHECK_NE(1, 2); - DCHECK_GE(1, 1); - DCHECK_GE(2, 1); - DCHECK_LE(1, 1); - DCHECK_LE(1, 2); - DCHECK_GT(2, 1); - DCHECK_LT(1, 2); - - // Test DCHECK on std::nullptr_t - const void* p_null = nullptr; - const void* p_not_null = &p_null; - DCHECK_EQ(p_null, nullptr); - DCHECK_EQ(nullptr, p_null); - DCHECK_NE(p_not_null, nullptr); - DCHECK_NE(nullptr, p_not_null); -} - -TEST(CHECKTest, TestQCHECK) { - // The tests that QCHECK does the same as CHECK - QCHECK(1 == 1); // NOLINT(readability/check) - QCHECK_EQ(1, 1); - QCHECK_NE(1, 2); - QCHECK_GE(1, 1); - QCHECK_GE(2, 1); - QCHECK_LE(1, 1); - QCHECK_LE(1, 2); - QCHECK_GT(2, 1); - QCHECK_LT(1, 2); - - // Tests using QCHECK*() on anonymous enums. - QCHECK_EQ(CASE_A, CASE_A); - QCHECK_NE(CASE_A, CASE_B); - QCHECK_GE(CASE_A, CASE_A); - QCHECK_GE(CASE_B, CASE_A); - QCHECK_LE(CASE_A, CASE_A); - QCHECK_LE(CASE_A, CASE_B); - QCHECK_GT(CASE_B, CASE_A); - QCHECK_LT(CASE_A, CASE_B); -} - -TEST(CHECKTest, TestQCHECKPlacementsInCompoundStatements) { - // check placement inside if/else clauses - if (true) QCHECK(true); - - if (false) - ; // NOLINT - else - QCHECK(true); - - if (false) - ; // NOLINT - else - QCHECK(true); - - switch (0) - case 0: - QCHECK(true); - -#if ABSL_INTERNAL_CPLUSPLUS_LANG >= 201703L - constexpr auto var = [](int i) { - QCHECK(i > 0); // NOLINT - return i + 1; - }(global_var); - (void)var; - -#if defined(__GNUC__) - int var2 = (({ 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) { - CHECK_EQ(*this, i); - 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) { - CHECK_EQ(ComparableType{0}, ComparableType{0}); - CHECK_NE(ComparableType{1}, ComparableType{2}); - CHECK_LT(ComparableType{1}, ComparableType{2}); - CHECK_LE(ComparableType{1}, ComparableType{2}); - CHECK_GT(ComparableType{2}, ComparableType{1}); - 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( - CHECK_EQ(v1, v2), - HasSubstr( - "Check failed: v1 == v2 (ComparableType{1} vs. ComparableType{2})")); -} - -} // namespace +#include "absl/log/check_test_impl.h" diff --git a/absl/log/check_test_impl.h b/absl/log/check_test_impl.h new file mode 100644 index 00000000..bcf5711f --- /dev/null +++ b/absl/log/check_test_impl.h @@ -0,0 +1,449 @@ +// +// 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 <ostream> +#include <string> + +#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" + +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 + +#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 + +#endif // ABSL_LOG_CHECK_TEST_IMPL_H_ diff --git a/absl/log/log_basic_test.cc b/absl/log/log_basic_test.cc new file mode 100644 index 00000000..b8d87c94 --- /dev/null +++ b/absl/log/log_basic_test.cc @@ -0,0 +1,21 @@ +// +// 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. + +#include "absl/log/log.h" + +#define ABSL_TEST_LOG LOG + +#include "gtest/gtest.h" +#include "absl/log/log_basic_test_impl.h" diff --git a/absl/log/basic_log_test.cc b/absl/log/log_basic_test_impl.h index bc40f0d0..35c0b690 100644 --- a/absl/log/basic_log_test.cc +++ b/absl/log/log_basic_test_impl.h @@ -16,6 +16,15 @@ // 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 <cerrno> #include <sstream> #include <string> @@ -28,11 +37,10 @@ #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.h" #include "absl/log/log_entry.h" #include "absl/log/scoped_mock_log.h" -namespace { +namespace absl_log_internal { #if GTEST_HAS_DEATH_TEST using ::absl::log_internal::DeathTestExpectedLogging; using ::absl::log_internal::DeathTestUnexpectedLogging; @@ -80,13 +88,13 @@ TEST_P(BasicLogTest, Info) { absl::ScopedMockLog test_sink(absl::MockLogDefault::kDisallowUnexpected); const int log_line = __LINE__ + 1; - auto do_log = [] { LOG(INFO) << "hello world"; }; + 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("basic_log_test.cc")), + SourceBasename(Eq("log_basic_test_impl.h")), SourceLine(Eq(log_line)), Prefix(IsTrue()), LogSeverity(Eq(absl::LogSeverity::kInfo)), TimestampInMatchWindow(), @@ -109,13 +117,13 @@ TEST_P(BasicLogTest, Warning) { absl::ScopedMockLog test_sink(absl::MockLogDefault::kDisallowUnexpected); const int log_line = __LINE__ + 1; - auto do_log = [] { LOG(WARNING) << "hello world"; }; + 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("basic_log_test.cc")), + SourceBasename(Eq("log_basic_test_impl.h")), SourceLine(Eq(log_line)), Prefix(IsTrue()), LogSeverity(Eq(absl::LogSeverity::kWarning)), TimestampInMatchWindow(), @@ -138,13 +146,13 @@ TEST_P(BasicLogTest, Error) { absl::ScopedMockLog test_sink(absl::MockLogDefault::kDisallowUnexpected); const int log_line = __LINE__ + 1; - auto do_log = [] { LOG(ERROR) << "hello world"; }; + 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("basic_log_test.cc")), + SourceBasename(Eq("log_basic_test_impl.h")), SourceLine(Eq(log_line)), Prefix(IsTrue()), LogSeverity(Eq(absl::LogSeverity::kError)), TimestampInMatchWindow(), @@ -174,7 +182,7 @@ TEST_P(BasicLogDeathTest, Fatal) { absl::log_internal::ScopedMinLogLevel scoped_min_log_level(GetParam()); const int log_line = __LINE__ + 1; - auto do_log = [] { LOG(FATAL) << "hello world"; }; + auto do_log = [] { ABSL_TEST_LOG(FATAL) << "hello world"; }; EXPECT_EXIT( { @@ -195,7 +203,7 @@ TEST_P(BasicLogDeathTest, Fatal) { EXPECT_CALL( test_sink, Send(AllOf(SourceFilename(Eq(__FILE__)), - SourceBasename(Eq("basic_log_test.cc")), + SourceBasename(Eq("log_basic_test_impl.h")), SourceLine(Eq(log_line)), Prefix(IsTrue()), LogSeverity(Eq(absl::LogSeverity::kFatal)), TimestampInMatchWindow(), @@ -211,7 +219,7 @@ TEST_P(BasicLogDeathTest, Fatal) { EXPECT_CALL( test_sink, Send(AllOf(SourceFilename(Eq(__FILE__)), - SourceBasename(Eq("basic_log_test.cc")), + SourceBasename(Eq("log_basic_test_impl.h")), SourceLine(Eq(log_line)), Prefix(IsTrue()), LogSeverity(Eq(absl::LogSeverity::kFatal)), TimestampInMatchWindow(), @@ -234,7 +242,7 @@ TEST_P(BasicLogDeathTest, QFatal) { absl::log_internal::ScopedMinLogLevel scoped_min_log_level(GetParam()); const int log_line = __LINE__ + 1; - auto do_log = [] { LOG(QFATAL) << "hello world"; }; + auto do_log = [] { ABSL_TEST_LOG(QFATAL) << "hello world"; }; EXPECT_EXIT( { @@ -249,7 +257,7 @@ TEST_P(BasicLogDeathTest, QFatal) { EXPECT_CALL( test_sink, Send(AllOf(SourceFilename(Eq(__FILE__)), - SourceBasename(Eq("basic_log_test.cc")), + SourceBasename(Eq("log_basic_test_impl.h")), SourceLine(Eq(log_line)), Prefix(IsTrue()), LogSeverity(Eq(absl::LogSeverity::kFatal)), TimestampInMatchWindow(), @@ -276,14 +284,16 @@ TEST_P(BasicLogTest, Level) { absl::LogSeverity::kError}) { absl::ScopedMockLog test_sink(absl::MockLogDefault::kDisallowUnexpected); - const int log_line = __LINE__ + 1; - auto do_log = [severity] { LOG(LEVEL(severity)) << "hello world"; }; + 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("basic_log_test.cc")), + SourceBasename(Eq("log_basic_test_impl.h")), SourceLine(Eq(log_line)), Prefix(IsTrue()), LogSeverity(Eq(severity)), TimestampInMatchWindow(), ThreadID(Eq(absl::base_internal::GetTID())), @@ -309,7 +319,7 @@ TEST_P(BasicLogDeathTest, Level) { auto volatile severity = absl::LogSeverity::kFatal; const int log_line = __LINE__ + 1; - auto do_log = [severity] { LOG(LEVEL(severity)) << "hello world"; }; + auto do_log = [severity] { ABSL_TEST_LOG(LEVEL(severity)) << "hello world"; }; EXPECT_EXIT( { @@ -326,7 +336,7 @@ TEST_P(BasicLogDeathTest, Level) { EXPECT_CALL( test_sink, Send(AllOf(SourceFilename(Eq(__FILE__)), - SourceBasename(Eq("basic_log_test.cc")), + SourceBasename(Eq("log_basic_test_impl.h")), SourceLine(Eq(log_line)), Prefix(IsTrue()), LogSeverity(Eq(absl::LogSeverity::kFatal)), TimestampInMatchWindow(), @@ -341,7 +351,7 @@ TEST_P(BasicLogDeathTest, Level) { EXPECT_CALL( test_sink, Send(AllOf(SourceFilename(Eq(__FILE__)), - SourceBasename(Eq("basic_log_test.cc")), + SourceBasename(Eq("log_basic_test_impl.h")), SourceLine(Eq(log_line)), Prefix(IsTrue()), LogSeverity(Eq(absl::LogSeverity::kFatal)), TimestampInMatchWindow(), @@ -374,7 +384,7 @@ TEST_P(BasicLogTest, LevelClampsNegativeValues) { EXPECT_CALL(test_sink, Send(LogSeverity(Eq(absl::LogSeverity::kInfo)))); test_sink.StartCapturingLogs(); - LOG(LEVEL(-1)) << "hello world"; + ABSL_TEST_LOG(LEVEL(-1)) << "hello world"; } TEST_P(BasicLogTest, LevelClampsLargeValues) { @@ -390,13 +400,14 @@ TEST_P(BasicLogTest, LevelClampsLargeValues) { EXPECT_CALL(test_sink, Send(LogSeverity(Eq(absl::LogSeverity::kError)))); test_sink.StartCapturingLogs(); - LOG(LEVEL(static_cast<int>(absl::LogSeverity::kFatal) + 1)) << "hello world"; + ABSL_TEST_LOG(LEVEL(static_cast<int>(absl::LogSeverity::kFatal) + 1)) + << "hello world"; } TEST(ErrnoPreservationTest, InSeverityExpression) { errno = 77; int saved_errno; - LOG(LEVEL((saved_errno = errno, absl::LogSeverity::kInfo))); + ABSL_TEST_LOG(LEVEL((saved_errno = errno, absl::LogSeverity::kInfo))); EXPECT_THAT(saved_errno, Eq(77)); } @@ -408,13 +419,13 @@ TEST(ErrnoPreservationTest, InStreamedExpression) { errno = 77; int saved_errno = 0; - LOG(INFO) << (saved_errno = errno, "hello world"); + ABSL_TEST_LOG(INFO) << (saved_errno = errno, "hello world"); EXPECT_THAT(saved_errno, Eq(77)); } TEST(ErrnoPreservationTest, AfterStatement) { errno = 77; - LOG(INFO); + ABSL_TEST_LOG(INFO); const int saved_errno = errno; EXPECT_THAT(saved_errno, Eq(77)); } @@ -427,14 +438,18 @@ class UnusedVariableWarningCompileTest { // `kInfo`. static void LoggedVariable() { const int x = 0; - LOG(INFO) << x; + ABSL_TEST_LOG(INFO) << x; } - static void LoggedParameter(const int x) { LOG(INFO) << x; } + static void LoggedParameter(const int x) { ABSL_TEST_LOG(INFO) << x; } static void SeverityVariable() { const int x = 0; - LOG(LEVEL(x)) << "hello world"; + ABSL_TEST_LOG(LEVEL(x)) << "hello world"; + } + static void SeverityParameter(const int x) { + ABSL_TEST_LOG(LEVEL(x)) << "hello world"; } - static void SeverityParameter(const int x) { LOG(LEVEL(x)) << "hello world"; } }; -} // namespace +} // namespace absl_log_internal + +#endif // ABSL_LOG_LOG_BASIC_TEST_IMPL_H_ |