Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 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 <stdio.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <stdarg.h> |
| 26 | #include <string.h> |
| 27 | #include <time.h> |
| 28 | #include <sys/time.h> |
| 29 | |
| 30 | #include "aml_avsync_log.h" |
| 31 | |
| 32 | static struct { |
| 33 | void *udata; |
| 34 | log_LockFn lock; |
| 35 | FILE *fp; |
| 36 | int level; |
| 37 | int quiet; |
yongchun.li | 0ee6e37 | 2021-08-20 04:26:04 -0700 | [diff] [blame^] | 38 | } L = {0}; |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 39 | |
| 40 | |
| 41 | static const char *level_names[] = { |
| 42 | "TRACE", "DEBUG", "INFO", "WARN", "ERROR", "FATAL" |
| 43 | }; |
| 44 | |
| 45 | #ifdef LOG_USE_COLOR |
| 46 | static const char *level_colors[] = { |
| 47 | "\x1b[94m", "\x1b[36m", "\x1b[32m", "\x1b[33m", "\x1b[31m", "\x1b[35m" |
| 48 | }; |
| 49 | #endif |
| 50 | |
| 51 | |
| 52 | static void lock(void) { |
| 53 | if (L.lock) { |
| 54 | L.lock(L.udata, 1); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | |
| 59 | static void unlock(void) { |
| 60 | if (L.lock) { |
| 61 | L.lock(L.udata, 0); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | |
| 66 | void log_set_udata(void *udata) { |
| 67 | L.udata = udata; |
| 68 | } |
| 69 | |
| 70 | |
| 71 | void log_set_lock(log_LockFn fn) { |
| 72 | L.lock = fn; |
| 73 | } |
| 74 | |
| 75 | |
| 76 | void log_set_fp(FILE *fp) { |
| 77 | L.fp = fp; |
| 78 | } |
| 79 | |
| 80 | |
| 81 | void log_set_level(int level) { |
yongchun.li | 12b1454 | 2021-05-19 18:56:31 -0700 | [diff] [blame] | 82 | const char *env= getenv( "AML_AVSYNC_DEBUG_LEVEL" ); |
| 83 | if ( env ) { |
| 84 | L.level = atoi( env ); |
| 85 | } else |
| 86 | L.level = level; |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | |
| 90 | void log_set_quiet(int enable) { |
| 91 | L.quiet = enable ? 1 : 0; |
| 92 | } |
| 93 | |
| 94 | |
| 95 | void log_log(int level, const char *file, int line, const char *fmt, ...) { |
| 96 | if (level < L.level) { |
| 97 | return; |
| 98 | } |
| 99 | |
| 100 | /* Acquire lock */ |
| 101 | lock(); |
| 102 | |
| 103 | /* Get current time */ |
yongchun.li | 0ee6e37 | 2021-08-20 04:26:04 -0700 | [diff] [blame^] | 104 | struct timespec tm; |
| 105 | long second, usec; |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 106 | |
yongchun.li | 0ee6e37 | 2021-08-20 04:26:04 -0700 | [diff] [blame^] | 107 | clock_gettime( CLOCK_MONOTONIC_RAW, &tm ); |
| 108 | second = tm.tv_sec; |
| 109 | usec = tm.tv_nsec/1000LL; |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 110 | /* Log to stderr */ |
| 111 | if (!L.quiet) { |
| 112 | va_list args; |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 113 | #ifdef LOG_USE_COLOR |
| 114 | fprintf( |
yongchun.li | 0ee6e37 | 2021-08-20 04:26:04 -0700 | [diff] [blame^] | 115 | stderr, "[%ld.%06ld]: %s%-5s\x1b[0m \x1b[90m%s:%d:\x1b[0m ", |
| 116 | second, usec, level_colors[level], level_names[level], file, line); |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 117 | #else |
yongchun.li | 0ee6e37 | 2021-08-20 04:26:04 -0700 | [diff] [blame^] | 118 | fprintf(stderr, "[%ld.%06ld]: %-5s %s:%d: ", second, usec, level_names[level], file, line); |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 119 | #endif |
| 120 | va_start(args, fmt); |
| 121 | vfprintf(stderr, fmt, args); |
| 122 | va_end(args); |
| 123 | fprintf(stderr, "\n"); |
| 124 | fflush(stderr); |
| 125 | } |
| 126 | |
| 127 | /* Log to file */ |
| 128 | if (L.fp) { |
| 129 | va_list args; |
yongchun.li | 0ee6e37 | 2021-08-20 04:26:04 -0700 | [diff] [blame^] | 130 | fprintf(L.fp, "[%ld.%06ld]: %-5s %s:%d: ", second, usec, level_names[level], file, line); |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 131 | va_start(args, fmt); |
| 132 | vfprintf(L.fp, fmt, args); |
| 133 | va_end(args); |
| 134 | fprintf(L.fp, "\n"); |
| 135 | fflush(L.fp); |
| 136 | } |
Song Zhao | c03ba12 | 2020-12-23 21:54:02 -0800 | [diff] [blame] | 137 | /* Release lock */ |
| 138 | unlock(); |
| 139 | } |