Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 0605995 | 2015-02-18 08:34:56 -0800 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 4 | * 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 Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 34 | #include <grpc/support/port_platform.h> |
| 35 | |
| 36 | #if defined(GPR_POSIX_LOG) |
| 37 | |
Craig Tiller | ad0dcdc | 2015-01-13 07:31:33 -0800 | [diff] [blame] | 38 | #include <grpc/support/alloc.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 39 | #include <grpc/support/log.h> |
| 40 | #include <grpc/support/time.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 41 | #include <pthread.h> |
Craig Tiller | f40df23 | 2016-03-25 13:38:14 -0700 | [diff] [blame^] | 42 | #include <stdarg.h> |
| 43 | #include <stdio.h> |
| 44 | #include <stdio.h> |
| 45 | #include <string.h> |
| 46 | #include <time.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 47 | |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 48 | static intptr_t gettid(void) { return (intptr_t)pthread_self(); } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 49 | |
ctiller | 0cd6956 | 2015-01-09 14:22:10 -0800 | [diff] [blame] | 50 | void 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 Tiller | ad0dcdc | 2015-01-13 07:31:33 -0800 | [diff] [blame] | 58 | ret = vsnprintf(buf, sizeof(buf), format, args); |
ctiller | 0cd6956 | 2015-01-09 14:22:10 -0800 | [diff] [blame] | 59 | va_end(args); |
| 60 | if (ret < 0) { |
| 61 | message = NULL; |
Craig Tiller | e1a03a6 | 2015-02-02 07:46:55 -0800 | [diff] [blame] | 62 | } else if ((size_t)ret <= sizeof(buf) - 1) { |
ctiller | 0cd6956 | 2015-01-09 14:22:10 -0800 | [diff] [blame] | 63 | message = buf; |
| 64 | } else { |
Craig Tiller | ebc7ef2 | 2015-09-10 22:19:25 -0700 | [diff] [blame] | 65 | message = allocated = gpr_malloc((size_t)ret + 1); |
ctiller | 0cd6956 | 2015-01-09 14:22:10 -0800 | [diff] [blame] | 66 | va_start(args, format); |
David Garcia Quintas | f611927 | 2015-09-12 17:02:30 -0700 | [diff] [blame] | 67 | vsnprintf(message, (size_t)(ret + 1), format, args); |
ctiller | 0cd6956 | 2015-01-09 14:22:10 -0800 | [diff] [blame] | 68 | va_end(args); |
| 69 | } |
| 70 | gpr_log_message(file, line, severity, message); |
| 71 | gpr_free(allocated); |
| 72 | } |
| 73 | |
| 74 | void gpr_default_log(gpr_log_func_args *args) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 75 | char *final_slash; |
| 76 | const char *display_file; |
| 77 | char time_buffer[64]; |
Jan Tattermusch | 8808637 | 2015-12-10 10:54:12 -0800 | [diff] [blame] | 78 | time_t timer; |
Craig Tiller | f3756c1 | 2015-07-01 17:21:01 -0700 | [diff] [blame] | 79 | gpr_timespec now = gpr_now(GPR_CLOCK_REALTIME); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 80 | struct tm tm; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 81 | |
Jan Tattermusch | 8808637 | 2015-12-10 10:54:12 -0800 | [diff] [blame] | 82 | timer = (time_t)now.tv_sec; |
ctiller | 0cd6956 | 2015-01-09 14:22:10 -0800 | [diff] [blame] | 83 | final_slash = strrchr(args->file, '/'); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 84 | if (final_slash == NULL) |
ctiller | 0cd6956 | 2015-01-09 14:22:10 -0800 | [diff] [blame] | 85 | display_file = args->file; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 86 | else |
| 87 | display_file = final_slash + 1; |
| 88 | |
Jan Tattermusch | 8808637 | 2015-12-10 10:54:12 -0800 | [diff] [blame] | 89 | if (!localtime_r(&timer, &tm)) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 90 | 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 Tiller | 2d8f790 | 2015-01-13 21:35:30 -0800 | [diff] [blame] | 96 | fprintf(stderr, "%s%s.%09d %7tu %s:%d] %s\n", |
ctiller | 0cd6956 | 2015-01-09 14:22:10 -0800 | [diff] [blame] | 97 | gpr_log_severity_string(args->severity), time_buffer, |
| 98 | (int)(now.tv_nsec), gettid(), display_file, args->line, |
| 99 | args->message); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 100 | } |
| 101 | |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 102 | #endif /* defined(GPR_POSIX_LOG) */ |