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