|
| 1 | +/* |
| 2 | + * Copyright (c) 2020 rxi |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | + * of this software and associated documentation files (the "Software"), to |
| 6 | + * deal in the Software without restriction, including without limitation the |
| 7 | + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 8 | + * sell copies of the Software, and to permit persons to whom the Software is |
| 9 | + * furnished to do so, subject to the following conditions: |
| 10 | + * |
| 11 | + * The above copyright notice and this permission notice shall be included in |
| 12 | + * all copies or substantial portions of the Software. |
| 13 | + * |
| 14 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 19 | + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 20 | + * IN THE SOFTWARE. |
| 21 | + */ |
| 22 | + |
| 23 | +#include "log.h" |
| 24 | + |
| 25 | +#if RV32_HAS(LOG_CALLBACK) |
| 26 | +#define MAX_CALLBACKS 32 |
| 27 | + |
| 28 | +typedef struct { |
| 29 | + log_func_t fn; |
| 30 | + void *udata; |
| 31 | + int level; |
| 32 | +} callback_t; |
| 33 | +#endif /* RV32_HAS(LOG_CALLBACK) */ |
| 34 | + |
| 35 | +static struct { |
| 36 | + void *udata; |
| 37 | + log_lock_func_t lock; |
| 38 | + int level; |
| 39 | + bool quiet; |
| 40 | +#if RV32_HAS(LOG_CALLBACK) |
| 41 | + callback_t callbacks[MAX_CALLBACKS]; |
| 42 | +#endif /* RV32_HAS(LOG_CALLBACK) */ |
| 43 | +} L; |
| 44 | + |
| 45 | +static const char *level_strings[] = {"TRACE", "DEBUG", "INFO", |
| 46 | + "WARN", "ERROR", "FATAL"}; |
| 47 | + |
| 48 | +#if RV32_HAS(LOG_COLOR) |
| 49 | +static const char *level_colors[] = {"\x1b[94m", "\x1b[36m", "\x1b[32m", |
| 50 | + "\x1b[33m", "\x1b[31m", "\x1b[35m"}; |
| 51 | +#endif /* RV32_HAS(LOG_COLOR) */ |
| 52 | + |
| 53 | +static void stdout_callback(log_event_t *ev) |
| 54 | +{ |
| 55 | + char buf[16]; |
| 56 | + buf[strftime(buf, sizeof(buf), "%H:%M:%S", ev->time)] = '\0'; |
| 57 | +#if RV32_HAS(LOG_COLOR) |
| 58 | + fprintf(ev->udata, "%s %s%-5s\x1b[0m \x1b[90m%s:%d:\x1b[0m ", buf, |
| 59 | + level_colors[ev->level], level_strings[ev->level], ev->file, |
| 60 | + ev->line); |
| 61 | +#else |
| 62 | + fprintf(ev->udata, "%s %-5s %s:%d: ", buf, level_strings[ev->level], |
| 63 | + ev->file, ev->line); |
| 64 | +#endif /* RV32_HAS(LOG_COLOR) */ |
| 65 | + vfprintf(ev->udata, ev->fmt, ev->ap); |
| 66 | + fprintf(ev->udata, "\n"); |
| 67 | + fflush(ev->udata); |
| 68 | +} |
| 69 | + |
| 70 | +#if RV32_HAS(LOG_CALLBACK) |
| 71 | +static void file_callback(log_event_t *ev) |
| 72 | +{ |
| 73 | + char buf[64]; |
| 74 | + buf[strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", ev->time)] = '\0'; |
| 75 | + fprintf(ev->udata, "%s %-5s %s:%d: ", buf, level_strings[ev->level], |
| 76 | + ev->file, ev->line); |
| 77 | + vfprintf(ev->udata, ev->fmt, ev->ap); |
| 78 | + fprintf(ev->udata, "\n"); |
| 79 | + fflush(ev->udata); |
| 80 | +} |
| 81 | +#endif /* RV32_HAS(LOG_CALLBACK) */ |
| 82 | + |
| 83 | +static void lock(void) |
| 84 | +{ |
| 85 | + if (L.lock) |
| 86 | + L.lock(true, L.udata); |
| 87 | +} |
| 88 | + |
| 89 | +static void unlock(void) |
| 90 | +{ |
| 91 | + if (L.lock) |
| 92 | + L.lock(false, L.udata); |
| 93 | +} |
| 94 | + |
| 95 | +const char *log_level_string(int level) |
| 96 | +{ |
| 97 | + return level_strings[level]; |
| 98 | +} |
| 99 | + |
| 100 | +void log_set_lock(log_lock_func_t fn, void *udata) |
| 101 | +{ |
| 102 | + L.lock = fn; |
| 103 | + L.udata = udata; |
| 104 | +} |
| 105 | + |
| 106 | +void log_set_level(int level) |
| 107 | +{ |
| 108 | + L.level = level; |
| 109 | +} |
| 110 | + |
| 111 | +void log_set_quiet(bool enable) |
| 112 | +{ |
| 113 | + L.quiet = enable; |
| 114 | +} |
| 115 | + |
| 116 | +#if RV32_HAS(LOG_CALLBACK) |
| 117 | +int log_add_callback(log_func_t fn, void *udata, int level) |
| 118 | +{ |
| 119 | + for (int i = 0; i < MAX_CALLBACKS; i++) { |
| 120 | + if (!L.callbacks[i].fn) { |
| 121 | + L.callbacks[i] = (callback_t){fn, udata, level}; |
| 122 | + return 0; |
| 123 | + } |
| 124 | + } |
| 125 | + return -1; |
| 126 | +} |
| 127 | + |
| 128 | +int log_add_fp(FILE *fp, int level) |
| 129 | +{ |
| 130 | + return log_add_callback(file_callback, fp, level); |
| 131 | +} |
| 132 | +#endif /* RV32_HAS(LOG_CALLBACK) */ |
| 133 | + |
| 134 | +static void init_event(log_event_t *ev, void *udata) |
| 135 | +{ |
| 136 | + if (!ev->time) { |
| 137 | + time_t t = time(NULL); |
| 138 | + ev->time = localtime(&t); |
| 139 | + } |
| 140 | + ev->udata = udata; |
| 141 | +} |
| 142 | + |
| 143 | +void log_set_stdout_stream(FILE *stream) |
| 144 | +{ |
| 145 | + L.udata = stream; |
| 146 | +} |
| 147 | + |
| 148 | +void log_impl(int level, const char *file, int line, const char *fmt, ...) |
| 149 | +{ |
| 150 | + log_event_t ev = { |
| 151 | + .fmt = fmt, |
| 152 | + .file = file, |
| 153 | + .line = line, |
| 154 | + .level = level, |
| 155 | + }; |
| 156 | + |
| 157 | + lock(); |
| 158 | + |
| 159 | + if (!L.quiet && level >= L.level) { |
| 160 | + init_event(&ev, L.udata ? : stdout /* L.stdout_stream might be NULL when CLI parsing failed */); |
| 161 | + va_start(ev.ap, fmt); |
| 162 | + stdout_callback(&ev); |
| 163 | + va_end(ev.ap); |
| 164 | + } |
| 165 | + |
| 166 | +#if RV32_HAS(LOG_CALLBACK) |
| 167 | + for (int i = 0; i < MAX_CALLBACKS && L.callbacks[i].fn; i++) { |
| 168 | + callback_t *cb = &L.callbacks[i]; |
| 169 | + if (level >= cb->level) { |
| 170 | + init_event(&ev, cb->udata); |
| 171 | + va_start(ev.ap, fmt); |
| 172 | + cb->fn(&ev); |
| 173 | + va_end(ev.ap); |
| 174 | + } |
| 175 | + } |
| 176 | +#endif /* RV32_HAS(LOG_CALLBACK) */ |
| 177 | + |
| 178 | + unlock(); |
| 179 | +} |
0 commit comments