robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 1 | /* |
| 2 | |
| 3 | honggfuzz - log messages |
| 4 | ----------------------------------------- |
| 5 | |
| 6 | Author: Robert Swiecki <swiecki@google.com> |
| 7 | |
robert.swiecki@gmail.com | ba85c3e | 2015-02-02 14:55:16 +0000 | [diff] [blame] | 8 | Copyright 2010-2015 by Google Inc. All Rights Reserved. |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 9 | |
| 10 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 11 | you may not use this file except in compliance with the License. |
| 12 | You may obtain a copy of the License at |
| 13 | |
| 14 | http://www.apache.org/licenses/LICENSE-2.0 |
| 15 | |
| 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 implied. |
| 19 | See the License for the specific language governing permissions and |
| 20 | limitations under the License. |
| 21 | |
| 22 | */ |
| 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> |
| 29 | #include <stdarg.h> |
| 30 | #include <stdio.h> |
| 31 | #include <stdlib.h> |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 32 | #include <string.h> |
| 33 | #include <sys/time.h> |
| 34 | #include <time.h> |
robert.swiecki@gmail.com | ebc1cac | 2011-07-02 03:15:51 +0000 | [diff] [blame] | 35 | #include <unistd.h> |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 36 | |
robert.swiecki@gmail.com | cdf18f9 | 2015-02-11 22:22:18 +0000 | [diff] [blame^] | 37 | static unsigned int log_minLevel; |
| 38 | static bool log_isStdioTTY; |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 39 | |
| 40 | __attribute__ ((constructor)) |
| 41 | void log_init(void |
| 42 | ) |
| 43 | { |
| 44 | log_minLevel = l_INFO; |
robert.swiecki@gmail.com | cdf18f9 | 2015-02-11 22:22:18 +0000 | [diff] [blame^] | 45 | if (isatty(STDOUT_FILENO) == 1) { |
robert.swiecki@gmail.com | ebc1cac | 2011-07-02 03:15:51 +0000 | [diff] [blame] | 46 | log_isStdioTTY = true; |
| 47 | } else { |
| 48 | log_isStdioTTY = false; |
| 49 | } |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | void log_setMinLevel(log_level_t dl) |
| 53 | { |
| 54 | log_minLevel = dl; |
| 55 | } |
| 56 | |
| 57 | void log_msg(log_level_t dl, |
| 58 | bool perr, const char *file, const char *func, int line, const char *fmt, ... |
| 59 | ) |
| 60 | { |
| 61 | struct { |
| 62 | char *descr; |
| 63 | char *prefix; |
| 64 | } logLevels[] = { |
| 65 | { |
| 66 | "[FATAL]", "\033[1;31m"}, { |
| 67 | "[ERROR]", "\033[1;35m"}, { |
| 68 | "[WARNING]", "\033[1;34m"}, { |
| 69 | "[INFO]", "\033[1m"}, { |
| 70 | "[DEBUG]", "\033[0;37m"} |
| 71 | }; |
| 72 | |
robert.swiecki@gmail.com | cdf18f9 | 2015-02-11 22:22:18 +0000 | [diff] [blame^] | 73 | if (dl > log_minLevel) |
| 74 | return; |
| 75 | |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 76 | char strerr[512]; |
| 77 | if (perr) { |
robert.swiecki@gmail.com | 528fa20 | 2011-06-22 17:25:07 +0000 | [diff] [blame] | 78 | snprintf(strerr, sizeof(strerr), "%s", strerror(errno)); |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 79 | } |
| 80 | |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 81 | struct tm tm; |
| 82 | struct timeval tv; |
| 83 | |
| 84 | gettimeofday(&tv, NULL); |
| 85 | localtime_r((const time_t *)&tv.tv_sec, &tm); |
| 86 | |
robert.swiecki@gmail.com | ebc1cac | 2011-07-02 03:15:51 +0000 | [diff] [blame] | 87 | if (log_isStdioTTY) { |
robert.swiecki@gmail.com | cdf18f9 | 2015-02-11 22:22:18 +0000 | [diff] [blame^] | 88 | dprintf(STDOUT_FILENO, "%s", logLevels[dl].prefix); |
robert.swiecki@gmail.com | ebc1cac | 2011-07-02 03:15:51 +0000 | [diff] [blame] | 89 | } |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 90 | |
robert.swiecki@gmail.com | ebc1cac | 2011-07-02 03:15:51 +0000 | [diff] [blame] | 91 | if (log_minLevel >= l_DEBUG || !log_isStdioTTY) { |
robert.swiecki@gmail.com | cdf18f9 | 2015-02-11 22:22:18 +0000 | [diff] [blame^] | 92 | dprintf |
| 93 | (STDOUT_FILENO, "%s [%d] %d/%02d/%02d %02d:%02d:%02d (%s:%s %d) ", |
groebert@google.com | 1bd4c21 | 2013-06-19 11:13:56 +0000 | [diff] [blame] | 94 | logLevels[dl].descr, getpid(), tm.tm_year + 1900, tm.tm_mon + 1, |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 95 | tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec, file, func, line); |
| 96 | } else { |
robert.swiecki@gmail.com | cdf18f9 | 2015-02-11 22:22:18 +0000 | [diff] [blame^] | 97 | dprintf(STDOUT_FILENO, "%s ", logLevels[dl].descr); |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | va_list args; |
| 101 | va_start(args, fmt); |
robert.swiecki@gmail.com | cdf18f9 | 2015-02-11 22:22:18 +0000 | [diff] [blame^] | 102 | vdprintf(STDOUT_FILENO, fmt, args); |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 103 | va_end(args); |
| 104 | |
| 105 | if (perr) { |
robert.swiecki@gmail.com | cdf18f9 | 2015-02-11 22:22:18 +0000 | [diff] [blame^] | 106 | dprintf(STDOUT_FILENO, ": %s", strerr); |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 107 | } |
| 108 | |
robert.swiecki@gmail.com | ebc1cac | 2011-07-02 03:15:51 +0000 | [diff] [blame] | 109 | if (log_isStdioTTY) { |
robert.swiecki@gmail.com | cdf18f9 | 2015-02-11 22:22:18 +0000 | [diff] [blame^] | 110 | dprintf(STDOUT_FILENO, "\033[0m"); |
robert.swiecki@gmail.com | ebc1cac | 2011-07-02 03:15:51 +0000 | [diff] [blame] | 111 | } |
| 112 | |
robert.swiecki@gmail.com | cdf18f9 | 2015-02-11 22:22:18 +0000 | [diff] [blame^] | 113 | dprintf(STDOUT_FILENO, "\n"); |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 114 | fflush(stdout); |
| 115 | } |