|
| 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 | +static struct { |
| 26 | + void *udata; |
| 27 | + log_lock_func_t lock; |
| 28 | + int level; |
| 29 | + bool quiet; |
| 30 | +} L; |
| 31 | + |
| 32 | +static const char *level_strings[] = { |
| 33 | + "TRACE", "DEBUG", "INFO", "WARN", "ERROR", "FATAL", |
| 34 | +}; |
| 35 | + |
| 36 | +#if RV32_HAS(LOG_COLOR) |
| 37 | +static const char *level_colors[] = { |
| 38 | + "\x1b[94m", "\x1b[36m", "\x1b[32m", "\x1b[33m", "\x1b[31m", "\x1b[35m", |
| 39 | +}; |
| 40 | +#endif /* RV32_HAS(LOG_COLOR) */ |
| 41 | + |
| 42 | +static void stdout_callback(log_event_t *ev) |
| 43 | +{ |
| 44 | + char buf[16]; |
| 45 | + buf[strftime(buf, sizeof(buf), "%H:%M:%S", ev->time)] = '\0'; |
| 46 | +#if RV32_HAS(LOG_COLOR) |
| 47 | + fprintf(ev->udata, "%s %s%-5s\x1b[0m \x1b[90m%s:%d:\x1b[0m ", buf, |
| 48 | + level_colors[ev->level], level_strings[ev->level], ev->file, |
| 49 | + ev->line); |
| 50 | +#else |
| 51 | + fprintf(ev->udata, "%s %-5s %s:%d: ", buf, level_strings[ev->level], |
| 52 | + ev->file, ev->line); |
| 53 | +#endif /* RV32_HAS(LOG_COLOR) */ |
| 54 | + vfprintf(ev->udata, ev->fmt, ev->ap); |
| 55 | + fprintf(ev->udata, "\n"); |
| 56 | + fflush(ev->udata); |
| 57 | +} |
| 58 | + |
| 59 | +static void lock(void) |
| 60 | +{ |
| 61 | + if (L.lock) |
| 62 | + L.lock(true, L.udata); |
| 63 | +} |
| 64 | + |
| 65 | +static void unlock(void) |
| 66 | +{ |
| 67 | + if (L.lock) |
| 68 | + L.lock(false, L.udata); |
| 69 | +} |
| 70 | + |
| 71 | +const char *log_level_string(int level) |
| 72 | +{ |
| 73 | + return level_strings[level]; |
| 74 | +} |
| 75 | + |
| 76 | +void log_set_lock(log_lock_func_t fn, void *udata) |
| 77 | +{ |
| 78 | + L.lock = fn; |
| 79 | + L.udata = udata; |
| 80 | +} |
| 81 | + |
| 82 | +void log_set_level(int level) |
| 83 | +{ |
| 84 | + L.level = level; |
| 85 | +} |
| 86 | + |
| 87 | +void log_set_quiet(bool enable) |
| 88 | +{ |
| 89 | + L.quiet = enable; |
| 90 | +} |
| 91 | + |
| 92 | +static void init_event(log_event_t *ev, void *udata) |
| 93 | +{ |
| 94 | + if (!ev->time) { |
| 95 | + time_t t = time(NULL); |
| 96 | + ev->time = localtime(&t); |
| 97 | + } |
| 98 | + ev->udata = udata; |
| 99 | +} |
| 100 | + |
| 101 | +void log_set_stdout_stream(FILE *stream) |
| 102 | +{ |
| 103 | + L.udata = stream; |
| 104 | +} |
| 105 | + |
| 106 | +void log_impl(int level, const char *file, int line, const char *fmt, ...) |
| 107 | +{ |
| 108 | + log_event_t ev = { |
| 109 | + .fmt = fmt, |
| 110 | + .file = file, |
| 111 | + .line = line, |
| 112 | + .level = level, |
| 113 | + }; |
| 114 | + |
| 115 | + lock(); |
| 116 | + |
| 117 | + if (!L.quiet && level >= L.level) { |
| 118 | + init_event(&ev, L.udata ? L.udata : stdout); |
| 119 | + va_start(ev.ap, fmt); |
| 120 | + stdout_callback(&ev); |
| 121 | + va_end(ev.ap); |
| 122 | + } |
| 123 | + |
| 124 | + unlock(); |
| 125 | +} |
0 commit comments