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 <grpc/support/log.h>
20
21 #include <stdbool.h>
22 #include <string.h>
23
24 #include "src/core/lib/gprpp/global_config.h"
25 #include "test/core/util/test_config.h"
26
27 // Config declaration is supposed to be located at log.h but
28 // log.h doesn't include global_config headers because it has to
29 // be a strict C so declaration statement gets to be here.
30 GPR_GLOBAL_CONFIG_DECLARE_STRING(grpc_verbosity);
31
32 static bool log_func_reached = false;
33
test_callback(gpr_log_func_args * args)34 static void test_callback(gpr_log_func_args* args) {
35 GPR_ASSERT(0 == strcmp(__FILE__, args->file));
36 GPR_ASSERT(args->severity == GPR_LOG_SEVERITY_INFO);
37 GPR_ASSERT(0 == strcmp(args->message, "hello 1 2 3"));
38 }
39
test_should_log(gpr_log_func_args *)40 static void test_should_log(gpr_log_func_args* /*args*/) {
41 log_func_reached = true;
42 }
43
test_should_not_log(gpr_log_func_args *)44 static void test_should_not_log(gpr_log_func_args* /*args*/) {
45 GPR_ASSERT(false);
46 }
47
48 #define test_log_function_reached(SEVERITY) \
49 gpr_set_log_function(test_should_log); \
50 log_func_reached = false; \
51 gpr_log_message(SEVERITY, "hello 1 2 3"); \
52 GPR_ASSERT(log_func_reached); \
53 log_func_reached = false; \
54 gpr_log(SEVERITY, "hello %d %d %d", 1, 2, 3); \
55 GPR_ASSERT(log_func_reached);
56
57 #define test_log_function_unreached(SEVERITY) \
58 gpr_set_log_function(test_should_not_log); \
59 gpr_log_message(SEVERITY, "hello 1 2 3"); \
60 gpr_log(SEVERITY, "hello %d %d %d", 1, 2, 3);
61
main(int argc,char ** argv)62 int main(int argc, char** argv) {
63 grpc::testing::TestEnvironment env(argc, argv);
64 /* test logging at various verbosity levels */
65 gpr_log(GPR_DEBUG, "%s", "hello world");
66 gpr_log(GPR_INFO, "%s", "hello world");
67 gpr_log(GPR_ERROR, "%s", "hello world");
68 /* should succeed */
69 GPR_ASSERT(1);
70 gpr_set_log_function(test_callback);
71 gpr_log_message(GPR_INFO, "hello 1 2 3");
72 gpr_log(GPR_INFO, "hello %d %d %d", 1, 2, 3);
73 gpr_set_log_function(nullptr);
74
75 /* gpr_log_verbosity_init() will be effective only once, and only before
76 * gpr_set_log_verbosity() is called */
77 GPR_GLOBAL_CONFIG_SET(grpc_verbosity, "ERROR");
78 gpr_log_verbosity_init();
79
80 test_log_function_reached(GPR_ERROR);
81 test_log_function_unreached(GPR_INFO);
82 test_log_function_unreached(GPR_DEBUG);
83
84 /* gpr_log_verbosity_init() should not be effective */
85 GPR_GLOBAL_CONFIG_SET(grpc_verbosity, "DEBUG");
86 gpr_log_verbosity_init();
87 test_log_function_reached(GPR_ERROR);
88 test_log_function_unreached(GPR_INFO);
89 test_log_function_unreached(GPR_DEBUG);
90
91 gpr_set_log_verbosity(GPR_LOG_SEVERITY_DEBUG);
92 test_log_function_reached(GPR_ERROR);
93 test_log_function_reached(GPR_INFO);
94 test_log_function_reached(GPR_DEBUG);
95
96 gpr_set_log_verbosity(GPR_LOG_SEVERITY_INFO);
97 test_log_function_reached(GPR_ERROR);
98 test_log_function_reached(GPR_INFO);
99 test_log_function_unreached(GPR_DEBUG);
100
101 gpr_set_log_verbosity(GPR_LOG_SEVERITY_ERROR);
102 test_log_function_reached(GPR_ERROR);
103 test_log_function_unreached(GPR_INFO);
104 test_log_function_unreached(GPR_DEBUG);
105
106 /* gpr_log_verbosity_init() should not be effective */
107 GPR_GLOBAL_CONFIG_SET(grpc_verbosity, "DEBUG");
108 gpr_log_verbosity_init();
109 test_log_function_reached(GPR_ERROR);
110 test_log_function_unreached(GPR_INFO);
111 test_log_function_unreached(GPR_DEBUG);
112
113 /* TODO(ctiller): should we add a GPR_ASSERT failure test here */
114 return 0;
115 }
116