3 * Copyright 2015 gRPC authors.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <grpc/support/port_platform.h>
21 #ifdef GPR_WINDOWS_LOG
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>
32 #include "src/core/lib/gpr/string.h"
33 #include "src/core/lib/gpr/string_windows.h"
35 void gpr_log(const char* file, int line, gpr_log_severity severity,
36 const char* format, ...) {
37 /* Avoid message construction if gpr_log_message won't log */
38 if (gpr_should_log(severity) == 0) {
46 /* Determine the length. */
47 va_start(args, format);
48 ret = _vscprintf(format, args);
53 /* Allocate a new buffer, with space for the NUL terminator. */
54 size_t strp_buflen = (size_t)ret + 1;
55 message = (char*)gpr_malloc(strp_buflen);
57 /* Print to the buffer. */
58 va_start(args, format);
59 ret = vsnprintf_s(message, strp_buflen, _TRUNCATE, format, args);
61 if ((size_t)ret != strp_buflen - 1) {
62 /* This should never happen. */
68 gpr_log_message(file, line, severity, message);
72 /* Simple starter implementation */
73 void gpr_default_log(gpr_log_func_args* args) {
74 const char* final_slash;
75 const char* display_file;
78 gpr_timespec now = gpr_now(GPR_CLOCK_REALTIME);
81 timer = (time_t)now.tv_sec;
82 final_slash = strrchr(args->file, '\\');
83 if (final_slash == NULL)
84 display_file = args->file;
86 display_file = final_slash + 1;
88 if (localtime_s(&tm, &timer)) {
89 strcpy(time_buffer, "error:localtime");
91 strftime(time_buffer, sizeof(time_buffer), "%m%d %H:%M:%S", &tm)) {
92 strcpy(time_buffer, "error:strftime");
95 fprintf(stderr, "%s%s.%09u %5lu %s:%d] %s\n",
96 gpr_log_severity_string(args->severity), time_buffer,
97 (int)(now.tv_nsec), GetCurrentThreadId(), display_file, args->line,
102 #endif /* GPR_WINDOWS_LOG */