• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/env.h"
26 #include "src/core/lib/gpr/string.h"
27 
28 #include <stdio.h>
29 #include <string.h>
30 
31 void gpr_default_log(gpr_log_func_args* args);
32 static gpr_atm g_log_func = (gpr_atm)gpr_default_log;
33 static gpr_atm g_min_severity_to_print = GPR_LOG_VERBOSITY_UNSET;
34 
gpr_log_severity_string(gpr_log_severity severity)35 const char* gpr_log_severity_string(gpr_log_severity severity) {
36   switch (severity) {
37     case GPR_LOG_SEVERITY_DEBUG:
38       return "D";
39     case GPR_LOG_SEVERITY_INFO:
40       return "I";
41     case GPR_LOG_SEVERITY_ERROR:
42       return "E";
43   }
44   GPR_UNREACHABLE_CODE(return "UNKNOWN");
45 }
46 
gpr_should_log(gpr_log_severity severity)47 int gpr_should_log(gpr_log_severity severity) {
48   return static_cast<gpr_atm>(severity) >=
49                  gpr_atm_no_barrier_load(&g_min_severity_to_print)
50              ? 1
51              : 0;
52 }
53 
gpr_log_message(const char * file,int line,gpr_log_severity severity,const char * message)54 void gpr_log_message(const char* file, int line, gpr_log_severity severity,
55                      const char* message) {
56   if (gpr_should_log(severity) == 0) {
57     return;
58   }
59 
60   gpr_log_func_args lfargs;
61   memset(&lfargs, 0, sizeof(lfargs));
62   lfargs.file = file;
63   lfargs.line = line;
64   lfargs.severity = severity;
65   lfargs.message = message;
66   ((gpr_log_func)gpr_atm_no_barrier_load(&g_log_func))(&lfargs);
67 }
68 
gpr_set_log_verbosity(gpr_log_severity min_severity_to_print)69 void gpr_set_log_verbosity(gpr_log_severity min_severity_to_print) {
70   gpr_atm_no_barrier_store(&g_min_severity_to_print,
71                            (gpr_atm)min_severity_to_print);
72 }
73 
gpr_log_verbosity_init()74 void gpr_log_verbosity_init() {
75   char* verbosity = nullptr;
76   const char* insecure_getenv = gpr_getenv_silent("GRPC_VERBOSITY", &verbosity);
77 
78   gpr_atm min_severity_to_print = GPR_LOG_SEVERITY_ERROR;
79   if (verbosity != nullptr) {
80     if (gpr_stricmp(verbosity, "DEBUG") == 0) {
81       min_severity_to_print = static_cast<gpr_atm>(GPR_LOG_SEVERITY_DEBUG);
82     } else if (gpr_stricmp(verbosity, "INFO") == 0) {
83       min_severity_to_print = static_cast<gpr_atm>(GPR_LOG_SEVERITY_INFO);
84     } else if (gpr_stricmp(verbosity, "ERROR") == 0) {
85       min_severity_to_print = static_cast<gpr_atm>(GPR_LOG_SEVERITY_ERROR);
86     }
87     gpr_free(verbosity);
88   }
89   if ((gpr_atm_no_barrier_load(&g_min_severity_to_print)) ==
90       GPR_LOG_VERBOSITY_UNSET) {
91     gpr_atm_no_barrier_store(&g_min_severity_to_print, min_severity_to_print);
92   }
93 
94   if (insecure_getenv != nullptr) {
95     gpr_log(GPR_DEBUG, "Warning: insecure environment read function '%s' used",
96             insecure_getenv);
97   }
98 }
99 
gpr_set_log_function(gpr_log_func f)100 void gpr_set_log_function(gpr_log_func f) {
101   gpr_atm_no_barrier_store(&g_log_func, (gpr_atm)(f ? f : gpr_default_log));
102 }
103