• 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 <grpc/support/alloc.h>
24 #include <grpc/support/log.h>
25 #include <grpc/support/string_util.h>
26 #include <grpc/support/time.h>
27 #include <pthread.h>
28 #include <stdarg.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <time.h>
32 
sys_gettid(void)33 static intptr_t sys_gettid(void) { return (intptr_t)pthread_self(); }
34 
gpr_log(const char * file,int line,gpr_log_severity severity,const char * format,...)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) {
39     return;
40   }
41   char buf[64];
42   char* allocated = nullptr;
43   char* message = nullptr;
44   int ret;
45   va_list args;
46   va_start(args, format);
47   ret = vsnprintf(buf, sizeof(buf), format, args);
48   va_end(args);
49   if (ret < 0) {
50     message = nullptr;
51   } else if ((size_t)ret <= sizeof(buf) - 1) {
52     message = buf;
53   } else {
54     message = allocated = (char*)gpr_malloc((size_t)ret + 1);
55     va_start(args, format);
56     vsnprintf(message, (size_t)(ret + 1), format, args);
57     va_end(args);
58   }
59   gpr_log_message(file, line, severity, message);
60   gpr_free(allocated);
61 }
62 
gpr_default_log(gpr_log_func_args * args)63 void gpr_default_log(gpr_log_func_args* args) {
64   const char* final_slash;
65   const char* display_file;
66   char time_buffer[64];
67   time_t timer;
68   gpr_timespec now = gpr_now(GPR_CLOCK_REALTIME);
69   struct tm tm;
70 
71   timer = (time_t)now.tv_sec;
72   final_slash = strrchr(args->file, '/');
73   if (final_slash == nullptr)
74     display_file = args->file;
75   else
76     display_file = final_slash + 1;
77 
78   if (!localtime_r(&timer, &tm)) {
79     strcpy(time_buffer, "error:localtime");
80   } else if (0 ==
81              strftime(time_buffer, sizeof(time_buffer), "%m%d %H:%M:%S", &tm)) {
82     strcpy(time_buffer, "error:strftime");
83   }
84 
85   char* prefix;
86   gpr_asprintf(&prefix, "%s%s.%09d %7tu %s:%d]",
87                gpr_log_severity_string(args->severity), time_buffer,
88                (int)(now.tv_nsec), sys_gettid(), display_file, args->line);
89 
90   fprintf(stderr, "%-70s %s\n", prefix, args->message);
91   gpr_free(prefix);
92 }
93 
94 #endif /* defined(GPR_POSIX_LOG) */
95