Commit c96db73c by Mike Kruskal Committed by Copybara-Service

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
parent bcc29b8c
...@@ -26,6 +26,27 @@ package(default_visibility = ["//visibility:public"]) ...@@ -26,6 +26,27 @@ package(default_visibility = ["//visibility:public"])
licenses(["notice"]) licenses(["notice"])
# Public targets # 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( cc_library(
name = "check", name = "check",
hdrs = ["check.h"], hdrs = ["check.h"],
...@@ -207,22 +228,34 @@ cc_library( ...@@ -207,22 +228,34 @@ cc_library(
) )
# Test targets # Test targets
cc_test( cc_test(
name = "basic_log_test", name = "absl_check_test",
size = "small", size = "small",
srcs = ["basic_log_test.cc"], srcs = ["absl_check_test.cc"],
copts = ABSL_TEST_COPTS, copts = ABSL_TEST_COPTS,
linkopts = ABSL_DEFAULT_LINKOPTS, linkopts = ABSL_DEFAULT_LINKOPTS,
tags = [
"no_test:os:ios",
"no_test_ios",
"no_test_wasm",
],
deps = [ deps = [
":globals", ":absl_check",
":log", ":check_test_impl",
":log_entry", "@com_google_googletest//:gtest_main",
":scoped_mock_log", ],
"//absl/base", )
"//absl/base:log_severity",
"//absl/log/internal:test_actions", cc_test(
"//absl/log/internal:test_helpers", name = "absl_log_basic_test",
"//absl/log/internal:test_matchers", 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", "@com_google_googletest//:gtest_main",
], ],
) )
...@@ -240,10 +273,28 @@ cc_test( ...@@ -240,10 +273,28 @@ cc_test(
], ],
deps = [ deps = [
":check", ":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:config",
"//absl/base:core_headers", "//absl/base:core_headers",
"//absl/log/internal:test_helpers", "//absl/log/internal:test_helpers",
"@com_google_googletest//:gtest_main", "@com_google_googletest//:gtest",
], ],
) )
...@@ -303,6 +354,39 @@ cc_test( ...@@ -303,6 +354,39 @@ cc_test(
) )
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", name = "log_entry_test",
size = "small", size = "small",
srcs = ["log_entry_test.cc"], srcs = ["log_entry_test.cc"],
......
...@@ -391,6 +391,36 @@ absl_cc_library( ...@@ -391,6 +391,36 @@ absl_cc_library(
# Public targets # Public targets
absl_cc_library( absl_cc_library(
NAME 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 check
SRCS SRCS
HDRS HDRS
...@@ -640,18 +670,39 @@ absl_cc_library( ...@@ -640,18 +670,39 @@ absl_cc_library(
) )
# Test targets # 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( absl_cc_test(
NAME NAME
basic_log_test absl_log_basic_test
SRCS SRCS
"basic_log_test.cc" "log_basic_test.cc"
"log_basic_test_impl.h"
COPTS COPTS
${ABSL_TEST_COPTS} ${ABSL_TEST_COPTS}
LINKOPTS LINKOPTS
${ABSL_DEFAULT_LINKOPTS} ${ABSL_DEFAULT_LINKOPTS}
DEPS DEPS
absl::base absl::base
absl::log absl::absl_log
absl::log_entry absl::log_entry
absl::log_globals absl::log_globals
absl::log_severity absl::log_severity
...@@ -668,6 +719,7 @@ absl_cc_test( ...@@ -668,6 +719,7 @@ absl_cc_test(
check_test check_test
SRCS SRCS
"check_test.cc" "check_test.cc"
"check_test_impl.h"
COPTS COPTS
${ABSL_TEST_COPTS} ${ABSL_TEST_COPTS}
LINKOPTS LINKOPTS
...@@ -699,26 +751,24 @@ absl_cc_test( ...@@ -699,26 +751,24 @@ absl_cc_test(
absl_cc_test( absl_cc_test(
NAME NAME
log_flags_test log_basic_test
SRCS SRCS
"flags_test.cc" "log_basic_test.cc"
"log_basic_test_impl.h"
COPTS COPTS
${ABSL_TEST_COPTS} ${ABSL_TEST_COPTS}
LINKOPTS LINKOPTS
${ABSL_DEFAULT_LINKOPTS} ${ABSL_DEFAULT_LINKOPTS}
DEPS DEPS
absl::core_headers absl::base
absl::log absl::log
absl::log_flags absl::log_entry
absl::log_globals absl::log_globals
absl::log_internal_flags absl::log_severity
absl::log_internal_test_actions
absl::log_internal_test_helpers absl::log_internal_test_helpers
absl::log_internal_test_matchers absl::log_internal_test_matchers
absl::log_severity
absl::flags
absl::flags_reflection
absl::scoped_mock_log absl::scoped_mock_log
absl::strings
GTest::gmock GTest::gmock
GTest::gtest_main GTest::gtest_main
) )
...@@ -750,6 +800,32 @@ absl_cc_test( ...@@ -750,6 +800,32 @@ absl_cc_test(
absl_cc_test( absl_cc_test(
NAME 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 log_globals_test
SRCS SRCS
"globals_test.cc" "globals_test.cc"
......
// 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_
//
// 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"
// 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_
//
// 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"
//
// 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"
...@@ -16,6 +16,15 @@ ...@@ -16,6 +16,15 @@
// The testcases in this file are expected to pass or be skipped with any value // The testcases in this file are expected to pass or be skipped with any value
// of ABSL_MIN_LOG_LEVEL // 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 <cerrno>
#include <sstream> #include <sstream>
#include <string> #include <string>
...@@ -28,11 +37,10 @@ ...@@ -28,11 +37,10 @@
#include "absl/log/internal/test_actions.h" #include "absl/log/internal/test_actions.h"
#include "absl/log/internal/test_helpers.h" #include "absl/log/internal/test_helpers.h"
#include "absl/log/internal/test_matchers.h" #include "absl/log/internal/test_matchers.h"
#include "absl/log/log.h"
#include "absl/log/log_entry.h" #include "absl/log/log_entry.h"
#include "absl/log/scoped_mock_log.h" #include "absl/log/scoped_mock_log.h"
namespace { namespace absl_log_internal {
#if GTEST_HAS_DEATH_TEST #if GTEST_HAS_DEATH_TEST
using ::absl::log_internal::DeathTestExpectedLogging; using ::absl::log_internal::DeathTestExpectedLogging;
using ::absl::log_internal::DeathTestUnexpectedLogging; using ::absl::log_internal::DeathTestUnexpectedLogging;
...@@ -80,13 +88,13 @@ TEST_P(BasicLogTest, Info) { ...@@ -80,13 +88,13 @@ TEST_P(BasicLogTest, Info) {
absl::ScopedMockLog test_sink(absl::MockLogDefault::kDisallowUnexpected); absl::ScopedMockLog test_sink(absl::MockLogDefault::kDisallowUnexpected);
const int log_line = __LINE__ + 1; 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)) { if (LoggingEnabledAt(absl::LogSeverity::kInfo)) {
EXPECT_CALL( EXPECT_CALL(
test_sink, test_sink,
Send(AllOf(SourceFilename(Eq(__FILE__)), Send(AllOf(SourceFilename(Eq(__FILE__)),
SourceBasename(Eq("basic_log_test.cc")), SourceBasename(Eq("log_basic_test_impl.h")),
SourceLine(Eq(log_line)), Prefix(IsTrue()), SourceLine(Eq(log_line)), Prefix(IsTrue()),
LogSeverity(Eq(absl::LogSeverity::kInfo)), LogSeverity(Eq(absl::LogSeverity::kInfo)),
TimestampInMatchWindow(), TimestampInMatchWindow(),
...@@ -109,13 +117,13 @@ TEST_P(BasicLogTest, Warning) { ...@@ -109,13 +117,13 @@ TEST_P(BasicLogTest, Warning) {
absl::ScopedMockLog test_sink(absl::MockLogDefault::kDisallowUnexpected); absl::ScopedMockLog test_sink(absl::MockLogDefault::kDisallowUnexpected);
const int log_line = __LINE__ + 1; 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)) { if (LoggingEnabledAt(absl::LogSeverity::kWarning)) {
EXPECT_CALL( EXPECT_CALL(
test_sink, test_sink,
Send(AllOf(SourceFilename(Eq(__FILE__)), Send(AllOf(SourceFilename(Eq(__FILE__)),
SourceBasename(Eq("basic_log_test.cc")), SourceBasename(Eq("log_basic_test_impl.h")),
SourceLine(Eq(log_line)), Prefix(IsTrue()), SourceLine(Eq(log_line)), Prefix(IsTrue()),
LogSeverity(Eq(absl::LogSeverity::kWarning)), LogSeverity(Eq(absl::LogSeverity::kWarning)),
TimestampInMatchWindow(), TimestampInMatchWindow(),
...@@ -138,13 +146,13 @@ TEST_P(BasicLogTest, Error) { ...@@ -138,13 +146,13 @@ TEST_P(BasicLogTest, Error) {
absl::ScopedMockLog test_sink(absl::MockLogDefault::kDisallowUnexpected); absl::ScopedMockLog test_sink(absl::MockLogDefault::kDisallowUnexpected);
const int log_line = __LINE__ + 1; 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)) { if (LoggingEnabledAt(absl::LogSeverity::kError)) {
EXPECT_CALL( EXPECT_CALL(
test_sink, test_sink,
Send(AllOf(SourceFilename(Eq(__FILE__)), Send(AllOf(SourceFilename(Eq(__FILE__)),
SourceBasename(Eq("basic_log_test.cc")), SourceBasename(Eq("log_basic_test_impl.h")),
SourceLine(Eq(log_line)), Prefix(IsTrue()), SourceLine(Eq(log_line)), Prefix(IsTrue()),
LogSeverity(Eq(absl::LogSeverity::kError)), LogSeverity(Eq(absl::LogSeverity::kError)),
TimestampInMatchWindow(), TimestampInMatchWindow(),
...@@ -174,7 +182,7 @@ TEST_P(BasicLogDeathTest, Fatal) { ...@@ -174,7 +182,7 @@ TEST_P(BasicLogDeathTest, Fatal) {
absl::log_internal::ScopedMinLogLevel scoped_min_log_level(GetParam()); absl::log_internal::ScopedMinLogLevel scoped_min_log_level(GetParam());
const int log_line = __LINE__ + 1; const int log_line = __LINE__ + 1;
auto do_log = [] { LOG(FATAL) << "hello world"; }; auto do_log = [] { ABSL_TEST_LOG(FATAL) << "hello world"; };
EXPECT_EXIT( EXPECT_EXIT(
{ {
...@@ -195,7 +203,7 @@ TEST_P(BasicLogDeathTest, Fatal) { ...@@ -195,7 +203,7 @@ TEST_P(BasicLogDeathTest, Fatal) {
EXPECT_CALL( EXPECT_CALL(
test_sink, test_sink,
Send(AllOf(SourceFilename(Eq(__FILE__)), Send(AllOf(SourceFilename(Eq(__FILE__)),
SourceBasename(Eq("basic_log_test.cc")), SourceBasename(Eq("log_basic_test_impl.h")),
SourceLine(Eq(log_line)), Prefix(IsTrue()), SourceLine(Eq(log_line)), Prefix(IsTrue()),
LogSeverity(Eq(absl::LogSeverity::kFatal)), LogSeverity(Eq(absl::LogSeverity::kFatal)),
TimestampInMatchWindow(), TimestampInMatchWindow(),
...@@ -211,7 +219,7 @@ TEST_P(BasicLogDeathTest, Fatal) { ...@@ -211,7 +219,7 @@ TEST_P(BasicLogDeathTest, Fatal) {
EXPECT_CALL( EXPECT_CALL(
test_sink, test_sink,
Send(AllOf(SourceFilename(Eq(__FILE__)), Send(AllOf(SourceFilename(Eq(__FILE__)),
SourceBasename(Eq("basic_log_test.cc")), SourceBasename(Eq("log_basic_test_impl.h")),
SourceLine(Eq(log_line)), Prefix(IsTrue()), SourceLine(Eq(log_line)), Prefix(IsTrue()),
LogSeverity(Eq(absl::LogSeverity::kFatal)), LogSeverity(Eq(absl::LogSeverity::kFatal)),
TimestampInMatchWindow(), TimestampInMatchWindow(),
...@@ -234,7 +242,7 @@ TEST_P(BasicLogDeathTest, QFatal) { ...@@ -234,7 +242,7 @@ TEST_P(BasicLogDeathTest, QFatal) {
absl::log_internal::ScopedMinLogLevel scoped_min_log_level(GetParam()); absl::log_internal::ScopedMinLogLevel scoped_min_log_level(GetParam());
const int log_line = __LINE__ + 1; const int log_line = __LINE__ + 1;
auto do_log = [] { LOG(QFATAL) << "hello world"; }; auto do_log = [] { ABSL_TEST_LOG(QFATAL) << "hello world"; };
EXPECT_EXIT( EXPECT_EXIT(
{ {
...@@ -249,7 +257,7 @@ TEST_P(BasicLogDeathTest, QFatal) { ...@@ -249,7 +257,7 @@ TEST_P(BasicLogDeathTest, QFatal) {
EXPECT_CALL( EXPECT_CALL(
test_sink, test_sink,
Send(AllOf(SourceFilename(Eq(__FILE__)), Send(AllOf(SourceFilename(Eq(__FILE__)),
SourceBasename(Eq("basic_log_test.cc")), SourceBasename(Eq("log_basic_test_impl.h")),
SourceLine(Eq(log_line)), Prefix(IsTrue()), SourceLine(Eq(log_line)), Prefix(IsTrue()),
LogSeverity(Eq(absl::LogSeverity::kFatal)), LogSeverity(Eq(absl::LogSeverity::kFatal)),
TimestampInMatchWindow(), TimestampInMatchWindow(),
...@@ -276,14 +284,16 @@ TEST_P(BasicLogTest, Level) { ...@@ -276,14 +284,16 @@ TEST_P(BasicLogTest, Level) {
absl::LogSeverity::kError}) { absl::LogSeverity::kError}) {
absl::ScopedMockLog test_sink(absl::MockLogDefault::kDisallowUnexpected); absl::ScopedMockLog test_sink(absl::MockLogDefault::kDisallowUnexpected);
const int log_line = __LINE__ + 1; const int log_line = __LINE__ + 2;
auto do_log = [severity] { LOG(LEVEL(severity)) << "hello world"; }; auto do_log = [severity] {
ABSL_TEST_LOG(LEVEL(severity)) << "hello world";
};
if (LoggingEnabledAt(severity)) { if (LoggingEnabledAt(severity)) {
EXPECT_CALL( EXPECT_CALL(
test_sink, test_sink,
Send(AllOf(SourceFilename(Eq(__FILE__)), Send(AllOf(SourceFilename(Eq(__FILE__)),
SourceBasename(Eq("basic_log_test.cc")), SourceBasename(Eq("log_basic_test_impl.h")),
SourceLine(Eq(log_line)), Prefix(IsTrue()), SourceLine(Eq(log_line)), Prefix(IsTrue()),
LogSeverity(Eq(severity)), TimestampInMatchWindow(), LogSeverity(Eq(severity)), TimestampInMatchWindow(),
ThreadID(Eq(absl::base_internal::GetTID())), ThreadID(Eq(absl::base_internal::GetTID())),
...@@ -309,7 +319,7 @@ TEST_P(BasicLogDeathTest, Level) { ...@@ -309,7 +319,7 @@ TEST_P(BasicLogDeathTest, Level) {
auto volatile severity = absl::LogSeverity::kFatal; auto volatile severity = absl::LogSeverity::kFatal;
const int log_line = __LINE__ + 1; 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( EXPECT_EXIT(
{ {
...@@ -326,7 +336,7 @@ TEST_P(BasicLogDeathTest, Level) { ...@@ -326,7 +336,7 @@ TEST_P(BasicLogDeathTest, Level) {
EXPECT_CALL( EXPECT_CALL(
test_sink, test_sink,
Send(AllOf(SourceFilename(Eq(__FILE__)), Send(AllOf(SourceFilename(Eq(__FILE__)),
SourceBasename(Eq("basic_log_test.cc")), SourceBasename(Eq("log_basic_test_impl.h")),
SourceLine(Eq(log_line)), Prefix(IsTrue()), SourceLine(Eq(log_line)), Prefix(IsTrue()),
LogSeverity(Eq(absl::LogSeverity::kFatal)), LogSeverity(Eq(absl::LogSeverity::kFatal)),
TimestampInMatchWindow(), TimestampInMatchWindow(),
...@@ -341,7 +351,7 @@ TEST_P(BasicLogDeathTest, Level) { ...@@ -341,7 +351,7 @@ TEST_P(BasicLogDeathTest, Level) {
EXPECT_CALL( EXPECT_CALL(
test_sink, test_sink,
Send(AllOf(SourceFilename(Eq(__FILE__)), Send(AllOf(SourceFilename(Eq(__FILE__)),
SourceBasename(Eq("basic_log_test.cc")), SourceBasename(Eq("log_basic_test_impl.h")),
SourceLine(Eq(log_line)), Prefix(IsTrue()), SourceLine(Eq(log_line)), Prefix(IsTrue()),
LogSeverity(Eq(absl::LogSeverity::kFatal)), LogSeverity(Eq(absl::LogSeverity::kFatal)),
TimestampInMatchWindow(), TimestampInMatchWindow(),
...@@ -374,7 +384,7 @@ TEST_P(BasicLogTest, LevelClampsNegativeValues) { ...@@ -374,7 +384,7 @@ TEST_P(BasicLogTest, LevelClampsNegativeValues) {
EXPECT_CALL(test_sink, Send(LogSeverity(Eq(absl::LogSeverity::kInfo)))); EXPECT_CALL(test_sink, Send(LogSeverity(Eq(absl::LogSeverity::kInfo))));
test_sink.StartCapturingLogs(); test_sink.StartCapturingLogs();
LOG(LEVEL(-1)) << "hello world"; ABSL_TEST_LOG(LEVEL(-1)) << "hello world";
} }
TEST_P(BasicLogTest, LevelClampsLargeValues) { TEST_P(BasicLogTest, LevelClampsLargeValues) {
...@@ -390,13 +400,14 @@ TEST_P(BasicLogTest, LevelClampsLargeValues) { ...@@ -390,13 +400,14 @@ TEST_P(BasicLogTest, LevelClampsLargeValues) {
EXPECT_CALL(test_sink, Send(LogSeverity(Eq(absl::LogSeverity::kError)))); EXPECT_CALL(test_sink, Send(LogSeverity(Eq(absl::LogSeverity::kError))));
test_sink.StartCapturingLogs(); 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) { TEST(ErrnoPreservationTest, InSeverityExpression) {
errno = 77; errno = 77;
int saved_errno; 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)); EXPECT_THAT(saved_errno, Eq(77));
} }
...@@ -408,13 +419,13 @@ TEST(ErrnoPreservationTest, InStreamedExpression) { ...@@ -408,13 +419,13 @@ TEST(ErrnoPreservationTest, InStreamedExpression) {
errno = 77; errno = 77;
int saved_errno = 0; 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)); EXPECT_THAT(saved_errno, Eq(77));
} }
TEST(ErrnoPreservationTest, AfterStatement) { TEST(ErrnoPreservationTest, AfterStatement) {
errno = 77; errno = 77;
LOG(INFO); ABSL_TEST_LOG(INFO);
const int saved_errno = errno; const int saved_errno = errno;
EXPECT_THAT(saved_errno, Eq(77)); EXPECT_THAT(saved_errno, Eq(77));
} }
...@@ -427,14 +438,18 @@ class UnusedVariableWarningCompileTest { ...@@ -427,14 +438,18 @@ class UnusedVariableWarningCompileTest {
// `kInfo`. // `kInfo`.
static void LoggedVariable() { static void LoggedVariable() {
const int x = 0; 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() { static void SeverityVariable() {
const int x = 0; 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_
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment