| 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 |
|
| 50 |
void gpr_unreachable_code(const char* reason, const char* file, int line) { |
| 51 |
gpr_log(file, line, GPR_LOG_SEVERITY_ERROR, "UNREACHABLE CODE: %s", reason); |
| 52 |
abort(); |
| 53 |
} |
| 54 |
|
| 55 |
const char* gpr_log_severity_string(gpr_log_severity severity) { |
| 56 |
switch (severity) { |
| 57 |
case GPR_LOG_SEVERITY_DEBUG: |
| 58 |
return "D"; |
| 59 |
case GPR_LOG_SEVERITY_INFO: |
| 60 |
return "I"; |
| 61 |
case GPR_LOG_SEVERITY_ERROR: |
| 62 |
return "E"; |
| 63 |
} |
| 64 |
GPR_UNREACHABLE_CODE(return "UNKNOWN"); |
| 65 |
} |
| 66 |
|
| 67 |
int gpr_should_log(gpr_log_severity severity) { |
| 68 |
return static_cast<gpr_atm>(severity) >= |
| 69 |
gpr_atm_no_barrier_load(&g_min_severity_to_print) |
| 70 |
? 1 |
| 71 |
: 0; |
| 72 |
} |
| 73 |
|
| 74 |
int gpr_should_log_stacktrace(gpr_log_severity severity) { |
| 75 |
return static_cast<gpr_atm>(severity) >= |
| 76 |
gpr_atm_no_barrier_load(&g_min_severity_to_print_stacktrace) |
| 77 |
? 1 |
| 78 |
: 0; |
| 79 |
} |
| 80 |
|
| 81 |
void gpr_log_message(const char* file, int line, gpr_log_severity severity, |
| 82 |
const char* message) { |
| 83 |
if (gpr_should_log(severity) == 0) { |
| 84 |
return; |
| 85 |
} |
| 86 |
|
| 87 |
gpr_log_func_args lfargs; |
| 88 |
memset(&lfargs, 0, sizeof(lfargs)); |
| 89 |
lfargs.file = file; |
| 90 |
lfargs.line = line; |
| 91 |
lfargs.severity = severity; |
| 92 |
lfargs.message = message; |
| 93 |
reinterpret_cast<gpr_log_func>(gpr_atm_no_barrier_load(&g_log_func))(&lfargs); |
| 94 |
} |
| 95 |
|
| 96 |
void gpr_set_log_verbosity(gpr_log_severity min_severity_to_print) { |
| 97 |
gpr_atm_no_barrier_store(&g_min_severity_to_print, |
| 98 |
(gpr_atm)min_severity_to_print); |
| 99 |
} |
| 100 |
|
| 101 |
static gpr_atm parse_log_severity(const char* str, gpr_atm error_value) { |
| 102 |
if (gpr_stricmp(str, "DEBUG") == 0) { |
| 103 |
return GPR_LOG_SEVERITY_DEBUG; |
| 104 |
} else if (gpr_stricmp(str, "INFO") == 0) { |
| 105 |
return GPR_LOG_SEVERITY_INFO; |
| 106 |
} else if (gpr_stricmp(str, "ERROR") == 0) { |
| 107 |
return GPR_LOG_SEVERITY_ERROR; |
| 108 |
} else if (gpr_stricmp(str, "NONE") == 0) { |
| 109 |
return GPR_LOG_SEVERITY_NONE; |
| 110 |
} else { |
| 111 |
return error_value; |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
void gpr_log_verbosity_init() { |
| 116 |
// init verbosity when it hasn't been set |
| 117 |
if ((gpr_atm_no_barrier_load(&g_min_severity_to_print)) == |
| 118 |
GPR_LOG_SEVERITY_UNSET) { |
| 119 |
grpc_core::UniquePtr<char> verbosity = |
| 120 |
GPR_GLOBAL_CONFIG_GET(grpc_verbosity); |
| 121 |
gpr_atm min_severity_to_print = GPR_LOG_SEVERITY_ERROR; |
| 122 |
if (strlen(verbosity.get()) > 0) { |
| 123 |
min_severity_to_print = |
| 124 |
parse_log_severity(verbosity.get(), min_severity_to_print); |
| 125 |
} |
| 126 |
gpr_atm_no_barrier_store(&g_min_severity_to_print, min_severity_to_print); |
| 127 |
} |
| 128 |
// init stacktrace_minloglevel when it hasn't been set |
| 129 |
if ((gpr_atm_no_barrier_load(&g_min_severity_to_print_stacktrace)) == |
| 130 |
GPR_LOG_SEVERITY_UNSET) { |
| 131 |
grpc_core::UniquePtr<char> stacktrace_minloglevel = |
| 132 |
GPR_GLOBAL_CONFIG_GET(grpc_stacktrace_minloglevel); |
| 133 |
gpr_atm min_severity_to_print_stacktrace = GPR_LOG_SEVERITY_NONE; |
| 134 |
if (strlen(stacktrace_minloglevel.get()) > 0) { |
| 135 |
min_severity_to_print_stacktrace = parse_log_severity( |
| 136 |
stacktrace_minloglevel.get(), min_severity_to_print_stacktrace); |
| 137 |
} |
| 138 |
gpr_atm_no_barrier_store(&g_min_severity_to_print_stacktrace, |
| 139 |
min_severity_to_print_stacktrace); |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
void gpr_set_log_function(gpr_log_func f) { |
| 144 |
gpr_atm_no_barrier_store(&g_log_func, (gpr_atm)(f ? f : gpr_default_log)); |
| 145 |
} |