xref: /aosp_15_r20/external/grpc-grpc/test/core/gpr/log_test.cc (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
1 //
2 //
3 // Copyright 2015 gRPC authors.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 //
18 
19 #include <string.h>
20 
21 #include <memory>
22 
23 #include <gtest/gtest.h>
24 
25 #include <grpc/support/log.h>
26 
27 #include "src/core/lib/gprpp/crash.h"
28 #include "test/core/util/test_config.h"
29 
30 static bool log_func_reached = false;
31 
test_callback(gpr_log_func_args * args)32 static void test_callback(gpr_log_func_args* args) {
33   GPR_ASSERT(0 == strcmp(__FILE__, args->file));
34   GPR_ASSERT(args->severity == GPR_LOG_SEVERITY_INFO);
35   GPR_ASSERT(0 == strcmp(args->message, "hello 1 2 3"));
36 }
37 
test_should_log(gpr_log_func_args *)38 static void test_should_log(gpr_log_func_args* /*args*/) {
39   log_func_reached = true;
40 }
41 
test_should_not_log(gpr_log_func_args *)42 static void test_should_not_log(gpr_log_func_args* /*args*/) {
43   grpc_core::Crash("unreachable");
44 }
45 
46 #define test_log_function_reached(SEVERITY)     \
47   gpr_set_log_function(test_should_log);        \
48   log_func_reached = false;                     \
49   gpr_log_message(SEVERITY, "hello 1 2 3");     \
50   GPR_ASSERT(log_func_reached);                 \
51   log_func_reached = false;                     \
52   gpr_log(SEVERITY, "hello %d %d %d", 1, 2, 3); \
53   GPR_ASSERT(log_func_reached);                 \
54   gpr_set_log_function(nullptr);
55 
56 #define test_log_function_unreached(SEVERITY)   \
57   gpr_set_log_function(test_should_not_log);    \
58   gpr_log_message(SEVERITY, "hello 1 2 3");     \
59   gpr_log(SEVERITY, "hello %d %d %d", 1, 2, 3); \
60   gpr_set_log_function(nullptr);
61 
TEST(LogTest,Basic)62 TEST(LogTest, Basic) {
63   // test logging at various verbosity levels
64   gpr_log(GPR_DEBUG, "%s", "hello world");
65   gpr_log(GPR_INFO, "%s", "hello world");
66   gpr_log(GPR_ERROR, "%s", "hello world");
67   // should succeed
68   GPR_ASSERT(1);
69   gpr_set_log_function(test_callback);
70   gpr_log_message(GPR_INFO, "hello 1 2 3");
71   gpr_log(GPR_INFO, "hello %d %d %d", 1, 2, 3);
72   gpr_set_log_function(nullptr);
73 }
74 
TEST(LogTest,LogVerbosity)75 TEST(LogTest, LogVerbosity) {
76   gpr_set_log_verbosity(GPR_LOG_SEVERITY_DEBUG);
77   test_log_function_reached(GPR_ERROR);
78   test_log_function_reached(GPR_INFO);
79   test_log_function_reached(GPR_DEBUG);
80 
81   gpr_set_log_verbosity(GPR_LOG_SEVERITY_INFO);
82   test_log_function_reached(GPR_ERROR);
83   test_log_function_reached(GPR_INFO);
84   test_log_function_unreached(GPR_DEBUG);
85 
86   gpr_set_log_verbosity(GPR_LOG_SEVERITY_ERROR);
87   test_log_function_reached(GPR_ERROR);
88   test_log_function_unreached(GPR_INFO);
89   test_log_function_unreached(GPR_DEBUG);
90 }
91 
main(int argc,char ** argv)92 int main(int argc, char** argv) {
93   grpc::testing::TestEnvironment env(&argc, argv);
94   ::testing::InitGoogleTest(&argc, argv);
95   int ret = RUN_ALL_TESTS();
96   return ret;
97 }
98