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/gpr/env.h"
25 #include "test/core/util/test_config.h"
26
27 static bool log_func_reached = false;
28
test_callback(gpr_log_func_args * args)29 static void test_callback(gpr_log_func_args* args) {
30 GPR_ASSERT(0 == strcmp(__FILE__, args->file));
31 GPR_ASSERT(args->severity == GPR_LOG_SEVERITY_INFO);
32 GPR_ASSERT(0 == strcmp(args->message, "hello 1 2 3"));
33 }
34
test_should_log(gpr_log_func_args * args)35 static void test_should_log(gpr_log_func_args* args) {
36 log_func_reached = true;
37 }
38
test_should_not_log(gpr_log_func_args * args)39 static void test_should_not_log(gpr_log_func_args* args) { GPR_ASSERT(false); }
40
41 #define test_log_function_reached(SEVERITY) \
42 gpr_set_log_function(test_should_log); \
43 log_func_reached = false; \
44 gpr_log_message(SEVERITY, "hello 1 2 3"); \
45 GPR_ASSERT(log_func_reached); \
46 log_func_reached = false; \
47 gpr_log(SEVERITY, "hello %d %d %d", 1, 2, 3); \
48 GPR_ASSERT(log_func_reached);
49
50 #define test_log_function_unreached(SEVERITY) \
51 gpr_set_log_function(test_should_not_log); \
52 gpr_log_message(SEVERITY, "hello 1 2 3"); \
53 gpr_log(SEVERITY, "hello %d %d %d", 1, 2, 3);
54
main(int argc,char ** argv)55 int main(int argc, char** argv) {
56 grpc_test_init(argc, argv);
57 /* test logging at various verbosity levels */
58 gpr_log(GPR_DEBUG, "%s", "hello world");
59 gpr_log(GPR_INFO, "%s", "hello world");
60 gpr_log(GPR_ERROR, "%s", "hello world");
61 /* should succeed */
62 GPR_ASSERT(1);
63 gpr_set_log_function(test_callback);
64 gpr_log_message(GPR_INFO, "hello 1 2 3");
65 gpr_log(GPR_INFO, "hello %d %d %d", 1, 2, 3);
66 gpr_set_log_function(nullptr);
67
68 /* gpr_log_verbosity_init() will be effective only once, and only before
69 * gpr_set_log_verbosity() is called */
70 gpr_setenv("GRPC_VERBOSITY", "ERROR");
71 gpr_log_verbosity_init();
72
73 test_log_function_reached(GPR_ERROR);
74 test_log_function_unreached(GPR_INFO);
75 test_log_function_unreached(GPR_DEBUG);
76
77 /* gpr_log_verbosity_init() should not be effective */
78 gpr_setenv("GRPC_VERBOSITY", "DEBUG");
79 gpr_log_verbosity_init();
80 test_log_function_reached(GPR_ERROR);
81 test_log_function_unreached(GPR_INFO);
82 test_log_function_unreached(GPR_DEBUG);
83
84 gpr_set_log_verbosity(GPR_LOG_SEVERITY_DEBUG);
85 test_log_function_reached(GPR_ERROR);
86 test_log_function_reached(GPR_INFO);
87 test_log_function_reached(GPR_DEBUG);
88
89 gpr_set_log_verbosity(GPR_LOG_SEVERITY_INFO);
90 test_log_function_reached(GPR_ERROR);
91 test_log_function_reached(GPR_INFO);
92 test_log_function_unreached(GPR_DEBUG);
93
94 gpr_set_log_verbosity(GPR_LOG_SEVERITY_ERROR);
95 test_log_function_reached(GPR_ERROR);
96 test_log_function_unreached(GPR_INFO);
97 test_log_function_unreached(GPR_DEBUG);
98
99 /* gpr_log_verbosity_init() should not be effective */
100 gpr_setenv("GRPC_VERBOSITY", "DEBUG");
101 gpr_log_verbosity_init();
102 test_log_function_reached(GPR_ERROR);
103 test_log_function_unreached(GPR_INFO);
104 test_log_function_unreached(GPR_DEBUG);
105
106 /* TODO(ctiller): should we add a GPR_ASSERT failure test here */
107 return 0;
108 }
109