Debian Codegraph @

src/core/lib/gpr/log_windows.cc

raw
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_WINDOWS_LOG
22
23 #include <stdarg.h>
24 #include <stdio.h>
25
26 #include <grpc/support/alloc.h>
27 #include <grpc/support/log.h>
28 #include <grpc/support/log_windows.h>
29 #include <grpc/support/string_util.h>
30 #include <grpc/support/time.h>
31
32 #include "src/core/lib/gpr/string.h"
33 #include "src/core/lib/gprpp/examine_stack.h"
34
35 int gpr_should_log_stacktrace(gpr_log_severity severity);
36
37 void gpr_log(const char* file, int line, gpr_log_severity severity,
38 const char* format, ...) {
39 /* Avoid message construction if gpr_log_message won't log */
40 if (gpr_should_log(severity) == 0) {
41 return;
42 }
43
44 char* message = NULL;
45 va_list args;
46 int ret;
47
48 /* Determine the length. */
49 va_start(args, format);
50 ret = _vscprintf(format, args);
51 va_end(args);
52 if (ret < 0) {
53 message = NULL;
54 } else {
55 /* Allocate a new buffer, with space for the NUL terminator. */
56 size_t strp_buflen = (size_t)ret + 1;
57 message = (char*)gpr_malloc(strp_buflen);
58
59 /* Print to the buffer. */
60 va_start(args, format);
61 ret = vsnprintf_s(message, strp_buflen, _TRUNCATE, format, args);
62 va_end(args);
63 if ((size_t)ret != strp_buflen - 1) {
64 /* This should never happen. */
65 gpr_free(message);
66 message = NULL;
67 }
68 }
69
70 gpr_log_message(file, line, severity, message);
71 gpr_free(message);
72 }
73
74 /* Simple starter implementation */
75 void gpr_default_log(gpr_log_func_args* args) {
76 const char* final_slash;
77 const char* display_file;
78 char time_buffer[64];
79 time_t timer;
80 gpr_timespec now = gpr_now(GPR_CLOCK_REALTIME);
81 struct tm tm;
82
83 timer = (time_t)now.tv_sec;
84 final_slash = strrchr(args->file, '\\');
85 if (final_slash == NULL)
86 display_file = args->file;
87 else
88 display_file = final_slash + 1;
89
90 if (localtime_s(&tm, &timer)) {
91 strcpy(time_buffer, "error:localtime");
92 } else if (0 ==
93 strftime(time_buffer, sizeof(time_buffer), "%m%d %H:%M:%S", &tm)) {
94 strcpy(time_buffer, "error:strftime");
95 }
96
97 absl::optional<std::string> stack_trace =
98 gpr_should_log_stacktrace(args->severity)
99 ? grpc_core::GetCurrentStackTrace()
100 : absl::nullopt;
101 if (stack_trace) {
102 fprintf(stderr, "%s%s.%09u %5lu %s:%d] %s\n%s\n",
103 gpr_log_severity_string(args->severity), time_buffer,
104 (int)(now.tv_nsec), GetCurrentThreadId(), display_file, args->line,
105 args->message, stack_trace->c_str());
106 } else {
107 fprintf(stderr, "%s%s.%09u %5lu %s:%d] %s\n",
108 gpr_log_severity_string(args->severity), time_buffer,
109 (int)(now.tv_nsec), GetCurrentThreadId(), display_file, args->line,
110 args->message);
111 }
112 fflush(stderr);
113 }
114
115 #endif /* GPR_WINDOWS_LOG */