• 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 "absl/log/log.h"
20 
21 #include <grpc/support/alloc.h>
22 #include <grpc/support/atm.h>
23 #include <grpc/support/log.h>
24 #include <grpc/support/port_platform.h>
25 #include <stdio.h>
26 #include <string.h>
27 
28 #include "absl/log/check.h"
29 #include "absl/log/globals.h"
30 #include "absl/strings/match.h"
31 #include "absl/strings/str_cat.h"
32 #include "src/core/config/config_vars.h"
33 #include "src/core/util/crash.h"
34 #include "src/core/util/string.h"
35 
gpr_unreachable_code(const char * reason,const char * file,int line)36 void gpr_unreachable_code(const char* reason, const char* file, int line) {
37   grpc_core::Crash(absl::StrCat("UNREACHABLE CODE: ", reason),
38                    grpc_core::SourceLocation(file, line));
39 }
40 
grpc_absl_log(const char * file,int line,gpr_log_severity severity,const char * message_str)41 GPRAPI void grpc_absl_log(const char* file, int line, gpr_log_severity severity,
42                           const char* message_str) {
43   switch (severity) {
44     case GPR_LOG_SEVERITY_DEBUG:
45       VLOG(2).AtLocation(file, line) << message_str;
46       return;
47     case GPR_LOG_SEVERITY_INFO:
48       LOG(INFO).AtLocation(file, line) << message_str;
49       return;
50     case GPR_LOG_SEVERITY_ERROR:
51       LOG(ERROR).AtLocation(file, line) << message_str;
52       return;
53     default:
54       DCHECK(false) << "Invalid severity";
55   }
56 }
57 
grpc_absl_log_int(const char * file,int line,gpr_log_severity severity,const char * message_str,intptr_t num)58 GPRAPI void grpc_absl_log_int(const char* file, int line,
59                               gpr_log_severity severity,
60                               const char* message_str, intptr_t num) {
61   switch (severity) {
62     case GPR_LOG_SEVERITY_DEBUG:
63       VLOG(2).AtLocation(file, line) << message_str << num;
64       return;
65     case GPR_LOG_SEVERITY_INFO:
66       LOG(INFO).AtLocation(file, line) << message_str << num;
67       return;
68     case GPR_LOG_SEVERITY_ERROR:
69       LOG(ERROR).AtLocation(file, line) << message_str << num;
70       return;
71     default:
72       DCHECK(false) << "Invalid severity";
73   }
74 }
75 
grpc_absl_log_str(const char * file,int line,gpr_log_severity severity,const char * message_str1,const char * message_str2)76 GPRAPI void grpc_absl_log_str(const char* file, int line,
77                               gpr_log_severity severity,
78                               const char* message_str1,
79                               const char* message_str2) {
80   switch (severity) {
81     case GPR_LOG_SEVERITY_DEBUG:
82       VLOG(2).AtLocation(file, line) << message_str1 << message_str2;
83       return;
84     case GPR_LOG_SEVERITY_INFO:
85       LOG(INFO).AtLocation(file, line) << message_str1 << message_str2;
86       return;
87     case GPR_LOG_SEVERITY_ERROR:
88       LOG(ERROR).AtLocation(file, line) << message_str1 << message_str2;
89       return;
90     default:
91       DCHECK(false) << "Invalid severity";
92   }
93 }
94 
gpr_log_verbosity_init(void)95 void gpr_log_verbosity_init(void) {
96 #ifndef GRPC_VERBOSITY_MACRO
97   // SetMinLogLevel sets the value for the entire binary, not just gRPC.
98   // This setting will change things for other libraries/code that is unrelated
99   // to grpc.
100   absl::string_view verbosity = grpc_core::ConfigVars::Get().Verbosity();
101   if (absl::EqualsIgnoreCase(verbosity, "INFO")) {
102     LOG_FIRST_N(WARNING, 1)
103         << "Log level INFO is not suitable for production. Prefer WARNING or "
104            "ERROR. However if you see this message in a debug environment or "
105            "test environment it is safe to ignore this message.";
106     absl::SetVLogLevel("*grpc*/*", -1);
107     absl::SetMinLogLevel(absl::LogSeverityAtLeast::kInfo);
108   } else if (absl::EqualsIgnoreCase(verbosity, "DEBUG")) {
109     LOG_FIRST_N(WARNING, 1)
110         << "Log level DEBUG is not suitable for production. Prefer WARNING or "
111            "ERROR. However if you see this message in a debug environment or "
112            "test environment it is safe to ignore this message.";
113     absl::SetVLogLevel("*grpc*/*", 2);
114     absl::SetMinLogLevel(absl::LogSeverityAtLeast::kInfo);
115   } else if (absl::EqualsIgnoreCase(verbosity, "ERROR")) {
116     absl::SetVLogLevel("*grpc*/*", -1);
117     absl::SetMinLogLevel(absl::LogSeverityAtLeast::kError);
118   } else if (absl::EqualsIgnoreCase(verbosity, "NONE")) {
119     absl::SetVLogLevel("*grpc*/*", -1);
120     absl::SetMinLogLevel(absl::LogSeverityAtLeast::kInfinity);
121   } else if (verbosity.empty()) {
122     // Do not alter absl settings if GRPC_VERBOSITY flag is not set.
123   } else {
124     LOG(ERROR) << "Unknown log verbosity: " << verbosity;
125   }
126 #endif  // GRPC_VERBOSITY_MACRO
127 }
128