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 <grpc/support/alloc.h>
22 #include <grpc/support/atm.h>
23 #include <grpc/support/log.h>
24
25 #include "src/core/lib/gpr/string.h"
26 #include "src/core/lib/gprpp/global_config.h"
27
28 #include <stdio.h>
29 #include <string.h>
30
31 GPR_GLOBAL_CONFIG_DEFINE_STRING(grpc_verbosity, "ERROR",
32 "Default gRPC logging verbosity")
33
34 void gpr_default_log(gpr_log_func_args* args);
35 static gpr_atm g_log_func = (gpr_atm)gpr_default_log;
36 static gpr_atm g_min_severity_to_print = GPR_LOG_VERBOSITY_UNSET;
37
gpr_log_severity_string(gpr_log_severity severity)38 const char* gpr_log_severity_string(gpr_log_severity severity) {
39 switch (severity) {
40 case GPR_LOG_SEVERITY_DEBUG:
41 return "D";
42 case GPR_LOG_SEVERITY_INFO:
43 return "I";
44 case GPR_LOG_SEVERITY_ERROR:
45 return "E";
46 }
47 GPR_UNREACHABLE_CODE(return "UNKNOWN");
48 }
49
gpr_should_log(gpr_log_severity severity)50 int gpr_should_log(gpr_log_severity severity) {
51 return static_cast<gpr_atm>(severity) >=
52 gpr_atm_no_barrier_load(&g_min_severity_to_print)
53 ? 1
54 : 0;
55 }
56
gpr_log_message(const char * file,int line,gpr_log_severity severity,const char * message)57 void gpr_log_message(const char* file, int line, gpr_log_severity severity,
58 const char* message) {
59 if (gpr_should_log(severity) == 0) {
60 return;
61 }
62
63 gpr_log_func_args lfargs;
64 memset(&lfargs, 0, sizeof(lfargs));
65 lfargs.file = file;
66 lfargs.line = line;
67 lfargs.severity = severity;
68 lfargs.message = message;
69 ((gpr_log_func)gpr_atm_no_barrier_load(&g_log_func))(&lfargs);
70 }
71
gpr_set_log_verbosity(gpr_log_severity min_severity_to_print)72 void gpr_set_log_verbosity(gpr_log_severity min_severity_to_print) {
73 gpr_atm_no_barrier_store(&g_min_severity_to_print,
74 (gpr_atm)min_severity_to_print);
75 }
76
gpr_log_verbosity_init()77 void gpr_log_verbosity_init() {
78 grpc_core::UniquePtr<char> verbosity = GPR_GLOBAL_CONFIG_GET(grpc_verbosity);
79
80 gpr_atm min_severity_to_print = GPR_LOG_SEVERITY_ERROR;
81 if (strlen(verbosity.get()) > 0) {
82 if (gpr_stricmp(verbosity.get(), "DEBUG") == 0) {
83 min_severity_to_print = static_cast<gpr_atm>(GPR_LOG_SEVERITY_DEBUG);
84 } else if (gpr_stricmp(verbosity.get(), "INFO") == 0) {
85 min_severity_to_print = static_cast<gpr_atm>(GPR_LOG_SEVERITY_INFO);
86 } else if (gpr_stricmp(verbosity.get(), "ERROR") == 0) {
87 min_severity_to_print = static_cast<gpr_atm>(GPR_LOG_SEVERITY_ERROR);
88 }
89 }
90 if ((gpr_atm_no_barrier_load(&g_min_severity_to_print)) ==
91 GPR_LOG_VERBOSITY_UNSET) {
92 gpr_atm_no_barrier_store(&g_min_severity_to_print, min_severity_to_print);
93 }
94 }
95
gpr_set_log_function(gpr_log_func f)96 void gpr_set_log_function(gpr_log_func f) {
97 gpr_atm_no_barrier_store(&g_log_func, (gpr_atm)(f ? f : gpr_default_log));
98 }
99