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