• 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 #ifndef _POSIX_SOURCE
20 #define _POSIX_SOURCE
21 #endif
22 
23 #ifndef _GNU_SOURCE
24 #define _GNU_SOURCE
25 #endif
26 
27 #include <grpc/support/port_platform.h>
28 
29 #ifdef GPR_LINUX_LOG
30 
31 #include <grpc/support/alloc.h>
32 #include <grpc/support/log.h>
33 #include <grpc/support/time.h>
34 #include <inttypes.h>
35 #include <stdarg.h>
36 #include <stdio.h>
37 #include <string.h>
38 #include <sys/syscall.h>
39 #include <time.h>
40 #include <unistd.h>
41 
42 #include <string>
43 
44 #include "absl/strings/str_format.h"
45 #include "src/core/lib/gprpp/examine_stack.h"
46 
47 int gpr_should_log_stacktrace(gpr_log_severity severity);
48 
sys_gettid(void)49 static long sys_gettid(void) { return syscall(__NR_gettid); }
50 
gpr_log(const char * file,int line,gpr_log_severity severity,const char * format,...)51 void gpr_log(const char* file, int line, gpr_log_severity severity,
52              const char* format, ...) {
53   /* Avoid message construction if gpr_log_message won't log */
54   if (gpr_should_log(severity) == 0) {
55     return;
56   }
57   char* message = nullptr;
58   va_list args;
59   va_start(args, format);
60   if (vasprintf(&message, format, args) == -1) {
61     va_end(args);
62     return;
63   }
64   va_end(args);
65   gpr_log_message(file, line, severity, message);
66   /* message has been allocated by vasprintf above, and needs free */
67   free(message);
68 }
69 
gpr_default_log(gpr_log_func_args * args)70 void gpr_default_log(gpr_log_func_args* args) {
71   const char* final_slash;
72   const char* display_file;
73   char time_buffer[64];
74   time_t timer;
75   gpr_timespec now = gpr_now(GPR_CLOCK_REALTIME);
76   struct tm tm;
77   static __thread long tid = 0;
78   if (tid == 0) tid = sys_gettid();
79 
80   timer = static_cast<time_t>(now.tv_sec);
81   final_slash = strrchr(args->file, '/');
82   if (final_slash == nullptr) {
83     display_file = args->file;
84   } else {
85     display_file = final_slash + 1;
86   }
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.%09" PRId32 " %7ld %s:%d]", gpr_log_severity_string(args->severity),
97       time_buffer, now.tv_nsec, tid, 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, "%-60s %s\n%s\n", prefix.c_str(), args->message,
105             stack_trace->c_str());
106   } else {
107     fprintf(stderr, "%-60s %s\n", prefix.c_str(), args->message);
108   }
109 }
110 
111 #endif /* GPR_LINUX_LOG */
112