3 * Copyright 2015 gRPC authors.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <grpc/support/port_platform.h>
21 #include <grpc/support/alloc.h>
22 #include <grpc/support/atm.h>
23 #include <grpc/support/log.h>
25 #include "src/core/lib/gpr/string.h"
26 #include "src/core/lib/gprpp/global_config.h"
31 GPR_GLOBAL_CONFIG_DEFINE_STRING(grpc_verbosity, "ERROR",
32 "Default gRPC logging verbosity")
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;
38 const char* gpr_log_severity_string(gpr_log_severity severity) {
40 case GPR_LOG_SEVERITY_DEBUG:
42 case GPR_LOG_SEVERITY_INFO:
44 case GPR_LOG_SEVERITY_ERROR:
47 GPR_UNREACHABLE_CODE(return "UNKNOWN");
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)
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) {
63 gpr_log_func_args lfargs;
64 memset(&lfargs, 0, sizeof(lfargs));
67 lfargs.severity = severity;
68 lfargs.message = message;
69 ((gpr_log_func)gpr_atm_no_barrier_load(&g_log_func))(&lfargs);
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);
77 void gpr_log_verbosity_init() {
78 grpc_core::UniquePtr<char> verbosity = GPR_GLOBAL_CONFIG_GET(grpc_verbosity);
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);
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);
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));