• 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 #ifdef GPR_POSIX_LOG
22 
23 #include <inttypes.h>
24 #include <pthread.h>
25 #include <stdarg.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <time.h>
29 
30 #include <string>
31 
32 #include "absl/strings/str_format.h"
33 
34 #include <grpc/support/alloc.h>
35 #include <grpc/support/log.h>
36 #include <grpc/support/time.h>
37 
38 #include "src/core/lib/gprpp/crash.h"
39 #include "src/core/lib/gprpp/examine_stack.h"
40 
41 int gpr_should_log_stacktrace(gpr_log_severity severity);
42 
sys_gettid(void)43 static intptr_t sys_gettid(void) { return (intptr_t)pthread_self(); }
44 
gpr_log(const char * file,int line,gpr_log_severity severity,const char * format,...)45 void gpr_log(const char* file, int line, gpr_log_severity severity,
46              const char* format, ...) {
47   // Avoid message construction if gpr_log_message won't log
48   if (gpr_should_log(severity) == 0) {
49     return;
50   }
51   char buf[64];
52   char* allocated = nullptr;
53   char* message = nullptr;
54   int ret;
55   va_list args;
56   va_start(args, format);
57   ret = vsnprintf(buf, sizeof(buf), format, args);
58   va_end(args);
59   if (ret < 0) {
60     message = nullptr;
61   } else if ((size_t)ret <= sizeof(buf) - 1) {
62     message = buf;
63   } else {
64     message = allocated = (char*)gpr_malloc((size_t)ret + 1);
65     va_start(args, format);
66     vsnprintf(message, (size_t)(ret + 1), format, args);
67     va_end(args);
68   }
69   gpr_log_message(file, line, severity, message);
70   gpr_free(allocated);
71 }
72 
gpr_platform_log(gpr_log_func_args * args)73 void gpr_platform_log(gpr_log_func_args* args) {
74   const char* final_slash;
75   const char* display_file;
76   char time_buffer[64];
77   time_t timer;
78   gpr_timespec now = gpr_now(GPR_CLOCK_REALTIME);
79   struct tm tm;
80 
81   timer = (time_t)now.tv_sec;
82   final_slash = strrchr(args->file, '/');
83   if (final_slash == nullptr)
84     display_file = args->file;
85   else
86     display_file = final_slash + 1;
87 
88   if (!localtime_r(&timer, &tm)) {
89     strcpy(time_buffer, "error:localtime");
90   } else if (0 ==
91              strftime(time_buffer, sizeof(time_buffer), "%m%d %H:%M:%S", &tm)) {
92     strcpy(time_buffer, "error:strftime");
93   }
94 
95   std::string prefix = absl::StrFormat(
96       "%s%s.%09d %7" PRIdPTR " %s:%d]", gpr_log_severity_string(args->severity),
97       time_buffer, (int)(now.tv_nsec), sys_gettid(), display_file, args->line);
98 
99   absl::optional<std::string> stack_trace =
100       gpr_should_log_stacktrace(args->severity)
101           ? grpc_core::GetCurrentStackTrace()
102           : absl::nullopt;
103   if (stack_trace) {
104     fprintf(stderr, "%-70s %s\n%s\n", prefix.c_str(), args->message,
105             stack_trace->c_str());
106   } else {
107     fprintf(stderr, "%-70s %s\n", prefix.c_str(), args->message);
108   }
109 }
110 
111 #endif  // defined(GPR_POSIX_LOG)
112