blob: 6ae63207673a2ea03eb3b4a53fa25a618e5c3eb9 [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Craig Tiller6169d5f2016-03-31 07:46:18 -07003 * Copyright 2015, Google Inc.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08004 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080034#include <grpc/support/port_platform.h>
35
36#if defined(GPR_POSIX_LOG)
37
Craig Tillerad0dcdc2015-01-13 07:31:33 -080038#include <grpc/support/alloc.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080039#include <grpc/support/log.h>
40#include <grpc/support/time.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080041#include <pthread.h>
Craig Tillerf40df232016-03-25 13:38:14 -070042#include <stdarg.h>
43#include <stdio.h>
44#include <stdio.h>
45#include <string.h>
46#include <time.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080047
Craig Tiller7536af02015-12-22 13:49:30 -080048static intptr_t gettid(void) { return (intptr_t)pthread_self(); }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080049
ctiller0cd69562015-01-09 14:22:10 -080050void gpr_log(const char *file, int line, gpr_log_severity severity,
51 const char *format, ...) {
52 char buf[64];
53 char *allocated = NULL;
54 char *message = NULL;
55 int ret;
56 va_list args;
57 va_start(args, format);
Craig Tillerad0dcdc2015-01-13 07:31:33 -080058 ret = vsnprintf(buf, sizeof(buf), format, args);
ctiller0cd69562015-01-09 14:22:10 -080059 va_end(args);
60 if (ret < 0) {
61 message = NULL;
Craig Tillere1a03a62015-02-02 07:46:55 -080062 } else if ((size_t)ret <= sizeof(buf) - 1) {
ctiller0cd69562015-01-09 14:22:10 -080063 message = buf;
64 } else {
Craig Tillerebc7ef22015-09-10 22:19:25 -070065 message = allocated = gpr_malloc((size_t)ret + 1);
ctiller0cd69562015-01-09 14:22:10 -080066 va_start(args, format);
David Garcia Quintasf6119272015-09-12 17:02:30 -070067 vsnprintf(message, (size_t)(ret + 1), format, args);
ctiller0cd69562015-01-09 14:22:10 -080068 va_end(args);
69 }
70 gpr_log_message(file, line, severity, message);
71 gpr_free(allocated);
72}
73
74void gpr_default_log(gpr_log_func_args *args) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080075 char *final_slash;
76 const char *display_file;
77 char time_buffer[64];
Jan Tattermusch88086372015-12-10 10:54:12 -080078 time_t timer;
Craig Tillerf3756c12015-07-01 17:21:01 -070079 gpr_timespec now = gpr_now(GPR_CLOCK_REALTIME);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080080 struct tm tm;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080081
Jan Tattermusch88086372015-12-10 10:54:12 -080082 timer = (time_t)now.tv_sec;
ctiller0cd69562015-01-09 14:22:10 -080083 final_slash = strrchr(args->file, '/');
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080084 if (final_slash == NULL)
ctiller0cd69562015-01-09 14:22:10 -080085 display_file = args->file;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080086 else
87 display_file = final_slash + 1;
88
Jan Tattermusch88086372015-12-10 10:54:12 -080089 if (!localtime_r(&timer, &tm)) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080090 strcpy(time_buffer, "error:localtime");
91 } else if (0 ==
92 strftime(time_buffer, sizeof(time_buffer), "%m%d %H:%M:%S", &tm)) {
93 strcpy(time_buffer, "error:strftime");
94 }
95
Craig Tiller2d8f7902015-01-13 21:35:30 -080096 fprintf(stderr, "%s%s.%09d %7tu %s:%d] %s\n",
ctiller0cd69562015-01-09 14:22:10 -080097 gpr_log_severity_string(args->severity), time_buffer,
98 (int)(now.tv_nsec), gettid(), display_file, args->line,
99 args->message);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800100}
101
Craig Tiller190d3602015-02-18 09:23:38 -0800102#endif /* defined(GPR_POSIX_LOG) */