robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 1 | /* |
robert.swiecki@gmail.com | 3b630b4 | 2015-02-16 10:53:53 +0000 | [diff] [blame] | 2 | * |
robert.swiecki@gmail.com | 8118985 | 2015-02-14 23:44:55 +0000 | [diff] [blame] | 3 | * honggfuzz - log messages |
| 4 | * ----------------------------------------- |
robert.swiecki@gmail.com | 3b630b4 | 2015-02-16 10:53:53 +0000 | [diff] [blame] | 5 | * |
robert.swiecki@gmail.com | 772b33d | 2015-02-14 20:35:00 +0000 | [diff] [blame] | 6 | * Author: Robert Swiecki <swiecki@google.com> |
robert.swiecki@gmail.com | 3b630b4 | 2015-02-16 10:53:53 +0000 | [diff] [blame] | 7 | * |
robert.swiecki@gmail.com | 772b33d | 2015-02-14 20:35:00 +0000 | [diff] [blame] | 8 | * Copyright 2010-2015 by Google Inc. All Rights Reserved. |
robert.swiecki@gmail.com | 3b630b4 | 2015-02-16 10:53:53 +0000 | [diff] [blame] | 9 | * |
| 10 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 11 | * not use this file except in compliance with the License. You may obtain |
robert.swiecki@gmail.com | 772b33d | 2015-02-14 20:35:00 +0000 | [diff] [blame] | 12 | * a copy of the License at |
robert.swiecki@gmail.com | 3b630b4 | 2015-02-16 10:53:53 +0000 | [diff] [blame] | 13 | * |
robert.swiecki@gmail.com | 772b33d | 2015-02-14 20:35:00 +0000 | [diff] [blame] | 14 | * http://www.apache.org/licenses/LICENSE-2.0 |
robert.swiecki@gmail.com | 3b630b4 | 2015-02-16 10:53:53 +0000 | [diff] [blame] | 15 | * |
robert.swiecki@gmail.com | 772b33d | 2015-02-14 20:35:00 +0000 | [diff] [blame] | 16 | * Unless required by applicable law or agreed to in writing, software |
| 17 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 18 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 19 | * implied. See the License for the specific language governing |
| 20 | * permissions and limitations under the License. |
robert.swiecki@gmail.com | 3b630b4 | 2015-02-16 10:53:53 +0000 | [diff] [blame] | 21 | * |
robert.swiecki@gmail.com | 772b33d | 2015-02-14 20:35:00 +0000 | [diff] [blame] | 22 | */ |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 23 | |
robert.swiecki@gmail.com | ba85c3e | 2015-02-02 14:55:16 +0000 | [diff] [blame] | 24 | #include "common.h" |
| 25 | #include "log.h" |
| 26 | |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 27 | #include <ctype.h> |
robert.swiecki@gmail.com | ba85c3e | 2015-02-02 14:55:16 +0000 | [diff] [blame] | 28 | #include <errno.h> |
robert.swiecki@gmail.com | 292d60a | 2015-02-13 12:51:57 +0000 | [diff] [blame] | 29 | #include <pthread.h> |
robert.swiecki@gmail.com | ba85c3e | 2015-02-02 14:55:16 +0000 | [diff] [blame] | 30 | #include <stdarg.h> |
| 31 | #include <stdio.h> |
| 32 | #include <stdlib.h> |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 33 | #include <string.h> |
| 34 | #include <sys/time.h> |
robert.swiecki@gmail.com | dfde1c7 | 2015-02-18 13:22:55 +0000 | [diff] [blame] | 35 | #include <sys/types.h> |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 36 | #include <time.h> |
robert.swiecki@gmail.com | ebc1cac | 2011-07-02 03:15:51 +0000 | [diff] [blame] | 37 | #include <unistd.h> |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 38 | |
robert.swiecki@gmail.com | 6f31991 | 2015-02-28 05:01:37 +0000 | [diff] [blame] | 39 | #include "util.h" |
| 40 | |
robert.swiecki@gmail.com | 1dd36d4 | 2015-02-18 13:08:51 +0000 | [diff] [blame] | 41 | static unsigned int log_minLevel = l_INFO; |
| 42 | static bool log_isStdioTTY = true; |
robert.swiecki@gmail.com | 292d60a | 2015-02-13 12:51:57 +0000 | [diff] [blame] | 43 | static pthread_mutex_t log_mutex = PTHREAD_MUTEX_INITIALIZER; |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 44 | |
robert.swiecki@gmail.com | 3a1845c | 2015-03-01 15:25:58 +0000 | [diff] [blame] | 45 | /* *INDENT-OFF* */ |
robert.swiecki@gmail.com | e9571a0 | 2015-02-12 00:22:50 +0000 | [diff] [blame] | 46 | static const struct { |
| 47 | const char *descr; |
| 48 | const char *prefix; |
| 49 | } logLevels[] = { |
robert.swiecki@gmail.com | 3a1845c | 2015-03-01 15:25:58 +0000 | [diff] [blame] | 50 | { "[FATAL]", "\033[1;41m" }, |
| 51 | { "[ERROR]", "\033[1;31m" }, |
| 52 | { "[WARNING]", "\033[1;35m" }, |
| 53 | { "[INFO]", "\033[1m" }, |
| 54 | { "[DEBUG]", "\033[0;37m" }, |
robert.swiecki@gmail.com | e9571a0 | 2015-02-12 00:22:50 +0000 | [diff] [blame] | 55 | }; |
robert.swiecki@gmail.com | 3a1845c | 2015-03-01 15:25:58 +0000 | [diff] [blame] | 56 | /* *INDENT-ON* */ |
robert.swiecki@gmail.com | e9571a0 | 2015-02-12 00:22:50 +0000 | [diff] [blame] | 57 | |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 58 | __attribute__ ((constructor)) |
robert.swiecki@gmail.com | 3a1845c | 2015-03-01 15:25:58 +0000 | [diff] [blame] | 59 | void log_init(void) |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 60 | { |
robert.swiecki@gmail.com | 3a1845c | 2015-03-01 15:25:58 +0000 | [diff] [blame] | 61 | log_isStdioTTY = (isatty(STDOUT_FILENO) == 1); |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | void log_setMinLevel(log_level_t dl) |
| 65 | { |
| 66 | log_minLevel = dl; |
| 67 | } |
| 68 | |
robert.swiecki@gmail.com | bb5d264 | 2015-02-25 20:00:00 +0000 | [diff] [blame] | 69 | void log_msg(log_level_t dl, bool perr, const char *file, const char *func, int line, |
robert.swiecki@gmail.com | 6f31991 | 2015-02-28 05:01:37 +0000 | [diff] [blame] | 70 | const char *fmt, ...) |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 71 | { |
robert.swiecki@gmail.com | 6f31991 | 2015-02-28 05:01:37 +0000 | [diff] [blame] | 72 | char msg[4096] = { "\0" }; |
| 73 | |
robert.swiecki@gmail.com | 341dd12 | 2015-02-18 13:05:20 +0000 | [diff] [blame] | 74 | if (dl > log_minLevel) { |
robert.swiecki@gmail.com | 1dd36d4 | 2015-02-18 13:08:51 +0000 | [diff] [blame] | 75 | if (dl == l_FATAL) { |
| 76 | exit(EXIT_FAILURE); |
robert.swiecki@gmail.com | 341dd12 | 2015-02-18 13:05:20 +0000 | [diff] [blame] | 77 | } |
robert.swiecki@gmail.com | cdf18f9 | 2015-02-11 22:22:18 +0000 | [diff] [blame] | 78 | return; |
robert.swiecki@gmail.com | 341dd12 | 2015-02-18 13:05:20 +0000 | [diff] [blame] | 79 | } |
robert.swiecki@gmail.com | cdf18f9 | 2015-02-11 22:22:18 +0000 | [diff] [blame] | 80 | |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 81 | char strerr[512]; |
| 82 | if (perr) { |
robert.swiecki@gmail.com | 528fa20 | 2011-06-22 17:25:07 +0000 | [diff] [blame] | 83 | snprintf(strerr, sizeof(strerr), "%s", strerror(errno)); |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 84 | } |
| 85 | |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 86 | struct tm tm; |
| 87 | struct timeval tv; |
| 88 | |
| 89 | gettimeofday(&tv, NULL); |
| 90 | localtime_r((const time_t *)&tv.tv_sec, &tm); |
| 91 | |
robert.swiecki@gmail.com | ebc1cac | 2011-07-02 03:15:51 +0000 | [diff] [blame] | 92 | if (log_isStdioTTY) { |
robert.swiecki@gmail.com | 6f31991 | 2015-02-28 05:01:37 +0000 | [diff] [blame] | 93 | util_ssnprintf(msg, sizeof(msg), "%s", logLevels[dl].prefix); |
robert.swiecki@gmail.com | ebc1cac | 2011-07-02 03:15:51 +0000 | [diff] [blame] | 94 | } |
robert.swiecki | 5fa9d90 | 2015-02-25 15:31:56 +0000 | [diff] [blame] | 95 | #if defined(_HF_ARCH_LINUX) |
robert.swiecki@gmail.com | dfde1c7 | 2015-02-18 13:22:55 +0000 | [diff] [blame] | 96 | #include <unistd.h> |
| 97 | #include <sys/syscall.h> |
robert.swiecki@gmail.com | 4fc1969 | 2015-02-25 15:45:11 +0000 | [diff] [blame] | 98 | pid_t pid = (pid_t) syscall(__NR_gettid); |
robert.swiecki@gmail.com | db8145a | 2015-03-01 16:19:10 +0000 | [diff] [blame^] | 99 | #else /* defined(_HF_ARCH_LINUX) */ |
robert.swiecki | 5fa9d90 | 2015-02-25 15:31:56 +0000 | [diff] [blame] | 100 | pid_t pid = getpid(); |
robert.swiecki@gmail.com | db8145a | 2015-03-01 16:19:10 +0000 | [diff] [blame^] | 101 | #endif /* defined(_HF_ARCH_LINUX) */ |
robert.swiecki@gmail.com | dfde1c7 | 2015-02-18 13:22:55 +0000 | [diff] [blame] | 102 | |
robert.swiecki@gmail.com | 1d8a3c1 | 2015-02-22 14:54:05 +0000 | [diff] [blame] | 103 | if (log_minLevel >= l_DEBUG || log_minLevel == l_FATAL || !log_isStdioTTY) { |
robert.swiecki@gmail.com | 6f31991 | 2015-02-28 05:01:37 +0000 | [diff] [blame] | 104 | util_ssnprintf(msg, sizeof(msg), "%s [%d] %d/%02d/%02d %02d:%02d:%02d (%s:%s %d) ", |
| 105 | logLevels[dl].descr, pid, tm.tm_year + 1900, |
| 106 | tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec, file, func, |
| 107 | line); |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 108 | } else { |
robert.swiecki@gmail.com | 6f31991 | 2015-02-28 05:01:37 +0000 | [diff] [blame] | 109 | util_ssnprintf(msg, sizeof(msg), "%s ", logLevels[dl].descr); |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | va_list args; |
| 113 | va_start(args, fmt); |
robert.swiecki@gmail.com | 3d09616 | 2015-02-28 05:08:51 +0000 | [diff] [blame] | 114 | util_vssnprintf(msg, sizeof(msg), fmt, args); |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 115 | va_end(args); |
| 116 | |
| 117 | if (perr) { |
robert.swiecki@gmail.com | 6f31991 | 2015-02-28 05:01:37 +0000 | [diff] [blame] | 118 | util_ssnprintf(msg, sizeof(msg), ": %s", strerr); |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 119 | } |
| 120 | |
robert.swiecki@gmail.com | ebc1cac | 2011-07-02 03:15:51 +0000 | [diff] [blame] | 121 | if (log_isStdioTTY) { |
robert.swiecki@gmail.com | 6f31991 | 2015-02-28 05:01:37 +0000 | [diff] [blame] | 122 | util_ssnprintf(msg, sizeof(msg), "\033[0m"); |
robert.swiecki@gmail.com | ebc1cac | 2011-07-02 03:15:51 +0000 | [diff] [blame] | 123 | } |
| 124 | |
robert.swiecki@gmail.com | 6f31991 | 2015-02-28 05:01:37 +0000 | [diff] [blame] | 125 | util_ssnprintf(msg, sizeof(msg), "\n"); |
robert.swiecki@gmail.com | 292d60a | 2015-02-13 12:51:57 +0000 | [diff] [blame] | 126 | |
robert.swiecki@gmail.com | 6f31991 | 2015-02-28 05:01:37 +0000 | [diff] [blame] | 127 | while (pthread_mutex_lock(&log_mutex)) ; |
| 128 | if (write(STDOUT_FILENO, msg, strlen(msg)) == -1) { |
| 129 | } |
robert.swiecki@gmail.com | 292d60a | 2015-02-13 12:51:57 +0000 | [diff] [blame] | 130 | while (pthread_mutex_unlock(&log_mutex)) ; |
robert.swiecki@gmail.com | e7190b9 | 2015-02-14 23:05:42 +0000 | [diff] [blame] | 131 | |
| 132 | if (dl == l_FATAL) { |
| 133 | exit(EXIT_FAILURE); |
| 134 | } |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 135 | } |