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/port_platform.h>
20
21 #include <stdio.h>
22 #include <string.h>
23
24 #include <grpc/support/alloc.h>
25 #include <grpc/support/atm.h>
26 #include <grpc/support/log.h>
27
28 #include "src/core/lib/gpr/string.h"
29 #include "src/core/lib/gprpp/global_config.h"
30
31 #ifndef GPR_DEFAULT_LOG_VERBOSITY_STRING
32 #define GPR_DEFAULT_LOG_VERBOSITY_STRING "ERROR"
33 #endif // !GPR_DEFAULT_LOG_VERBOSITY_STRING
34
35 GPR_GLOBAL_CONFIG_DEFINE_STRING(grpc_verbosity,
36 GPR_DEFAULT_LOG_VERBOSITY_STRING,
37 "Default gRPC logging verbosity")
38 GPR_GLOBAL_CONFIG_DEFINE_STRING(grpc_stacktrace_minloglevel, "",
39 "Messages logged at the same or higher level "
40 "than this will print stacktrace")
41
42 static constexpr gpr_atm GPR_LOG_SEVERITY_UNSET = GPR_LOG_SEVERITY_ERROR + 10;
43 static constexpr gpr_atm GPR_LOG_SEVERITY_NONE = GPR_LOG_SEVERITY_ERROR + 11;
44
45 void gpr_default_log(gpr_log_func_args* args);
46 static gpr_atm g_log_func = reinterpret_cast<gpr_atm>(gpr_default_log);
47 static gpr_atm g_min_severity_to_print = GPR_LOG_SEVERITY_UNSET;
48 static gpr_atm g_min_severity_to_print_stacktrace = GPR_LOG_SEVERITY_UNSET;
49
gpr_log_severity_string(gpr_log_severity severity)50 const char* gpr_log_severity_string(gpr_log_severity severity) {
51 switch (severity) {
52 case GPR_LOG_SEVERITY_DEBUG:
53 return "D";
54 case GPR_LOG_SEVERITY_INFO:
55 return "I";
56 case GPR_LOG_SEVERITY_ERROR:
57 return "E";
58 }
59 GPR_UNREACHABLE_CODE(return "UNKNOWN");
60 }
61
gpr_should_log(gpr_log_severity severity)62 int gpr_should_log(gpr_log_severity severity) {
63 return static_cast<gpr_atm>(severity) >=
64 gpr_atm_no_barrier_load(&g_min_severity_to_print)
65 ? 1
66 : 0;
67 }
68
gpr_should_log_stacktrace(gpr_log_severity severity)69 int gpr_should_log_stacktrace(gpr_log_severity severity) {
70 return static_cast<gpr_atm>(severity) >=
71 gpr_atm_no_barrier_load(&g_min_severity_to_print_stacktrace)
72 ? 1
73 : 0;
74 }
75
gpr_log_message(const char * file,int line,gpr_log_severity severity,const char * message)76 void gpr_log_message(const char* file, int line, gpr_log_severity severity,
77 const char* message) {
78 if (gpr_should_log(severity) == 0) {
79 return;
80 }
81
82 gpr_log_func_args lfargs;
83 memset(&lfargs, 0, sizeof(lfargs));
84 lfargs.file = file;
85 lfargs.line = line;
86 lfargs.severity = severity;
87 lfargs.message = message;
88 reinterpret_cast<gpr_log_func>(gpr_atm_no_barrier_load(&g_log_func))(&lfargs);
89 }
90
gpr_set_log_verbosity(gpr_log_severity min_severity_to_print)91 void gpr_set_log_verbosity(gpr_log_severity min_severity_to_print) {
92 gpr_atm_no_barrier_store(&g_min_severity_to_print,
93 (gpr_atm)min_severity_to_print);
94 }
95
parse_log_severity(const char * str,gpr_atm error_value)96 static gpr_atm parse_log_severity(const char* str, gpr_atm error_value) {
97 if (gpr_stricmp(str, "DEBUG") == 0) {
98 return GPR_LOG_SEVERITY_DEBUG;
99 } else if (gpr_stricmp(str, "INFO") == 0) {
100 return GPR_LOG_SEVERITY_INFO;
101 } else if (gpr_stricmp(str, "ERROR") == 0) {
102 return GPR_LOG_SEVERITY_ERROR;
103 } else if (gpr_stricmp(str, "NONE") == 0) {
104 return GPR_LOG_SEVERITY_NONE;
105 } else {
106 return error_value;
107 }
108 }
109
gpr_log_verbosity_init()110 void gpr_log_verbosity_init() {
111 // init verbosity when it hasn't been set
112 if ((gpr_atm_no_barrier_load(&g_min_severity_to_print)) ==
113 GPR_LOG_SEVERITY_UNSET) {
114 grpc_core::UniquePtr<char> verbosity =
115 GPR_GLOBAL_CONFIG_GET(grpc_verbosity);
116 gpr_atm min_severity_to_print = GPR_LOG_SEVERITY_ERROR;
117 if (strlen(verbosity.get()) > 0) {
118 min_severity_to_print =
119 parse_log_severity(verbosity.get(), min_severity_to_print);
120 }
121 gpr_atm_no_barrier_store(&g_min_severity_to_print, min_severity_to_print);
122 }
123 // init stacktrace_minloglevel when it hasn't been set
124 if ((gpr_atm_no_barrier_load(&g_min_severity_to_print_stacktrace)) ==
125 GPR_LOG_SEVERITY_UNSET) {
126 grpc_core::UniquePtr<char> stacktrace_minloglevel =
127 GPR_GLOBAL_CONFIG_GET(grpc_stacktrace_minloglevel);
128 gpr_atm min_severity_to_print_stacktrace = GPR_LOG_SEVERITY_NONE;
129 if (strlen(stacktrace_minloglevel.get()) > 0) {
130 min_severity_to_print_stacktrace = parse_log_severity(
131 stacktrace_minloglevel.get(), min_severity_to_print_stacktrace);
132 }
133 gpr_atm_no_barrier_store(&g_min_severity_to_print_stacktrace,
134 min_severity_to_print_stacktrace);
135 }
136 }
137
gpr_set_log_function(gpr_log_func f)138 void gpr_set_log_function(gpr_log_func f) {
139 gpr_atm_no_barrier_store(&g_log_func, (gpr_atm)(f ? f : gpr_default_log));
140 }
141