blob: 155d6ce27e48d4553f12bb1ffacc6ab877a70f85 [file] [log] [blame]
Song Zhaoc03ba122020-12-23 21:54:02 -08001/*
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
32static struct {
33 void *udata;
34 log_LockFn lock;
35 FILE *fp;
36 int level;
37 int quiet;
yongchun.li0ee6e372021-08-20 04:26:04 -070038} L = {0};
Song Zhaoc03ba122020-12-23 21:54:02 -080039
40
41static const char *level_names[] = {
42 "TRACE", "DEBUG", "INFO", "WARN", "ERROR", "FATAL"
43};
44
45#ifdef LOG_USE_COLOR
46static const char *level_colors[] = {
47 "\x1b[94m", "\x1b[36m", "\x1b[32m", "\x1b[33m", "\x1b[31m", "\x1b[35m"
48};
49#endif
50
51
52static void lock(void) {
53 if (L.lock) {
54 L.lock(L.udata, 1);
55 }
56}
57
58
59static void unlock(void) {
60 if (L.lock) {
61 L.lock(L.udata, 0);
62 }
63}
64
65
66void log_set_udata(void *udata) {
67 L.udata = udata;
68}
69
70
71void log_set_lock(log_LockFn fn) {
72 L.lock = fn;
73}
74
75
76void log_set_fp(FILE *fp) {
77 L.fp = fp;
78}
79
80
81void log_set_level(int level) {
Song Zhao67c937b2021-10-01 11:21:32 -070082 L.level = level;
Song Zhaoc03ba122020-12-23 21:54:02 -080083}
84
85
86void log_set_quiet(int enable) {
87 L.quiet = enable ? 1 : 0;
88}
89
90
91void log_log(int level, const char *file, int line, const char *fmt, ...) {
92 if (level < L.level) {
93 return;
94 }
95
96 /* Acquire lock */
97 lock();
98
99 /* Get current time */
yongchun.li0ee6e372021-08-20 04:26:04 -0700100 struct timespec tm;
101 long second, usec;
Song Zhaoc03ba122020-12-23 21:54:02 -0800102
yongchun.li0ee6e372021-08-20 04:26:04 -0700103 clock_gettime( CLOCK_MONOTONIC_RAW, &tm );
104 second = tm.tv_sec;
105 usec = tm.tv_nsec/1000LL;
Song Zhaoc03ba122020-12-23 21:54:02 -0800106 /* Log to stderr */
107 if (!L.quiet) {
108 va_list args;
Song Zhaoc03ba122020-12-23 21:54:02 -0800109#ifdef LOG_USE_COLOR
110 fprintf(
yongchun.li0ee6e372021-08-20 04:26:04 -0700111 stderr, "[%ld.%06ld]: %s%-5s\x1b[0m \x1b[90m%s:%d:\x1b[0m ",
112 second, usec, level_colors[level], level_names[level], file, line);
Song Zhaoc03ba122020-12-23 21:54:02 -0800113#else
yongchun.li0ee6e372021-08-20 04:26:04 -0700114 fprintf(stderr, "[%ld.%06ld]: %-5s %s:%d: ", second, usec, level_names[level], file, line);
Song Zhaoc03ba122020-12-23 21:54:02 -0800115#endif
116 va_start(args, fmt);
117 vfprintf(stderr, fmt, args);
118 va_end(args);
119 fprintf(stderr, "\n");
120 fflush(stderr);
121 }
122
123 /* Log to file */
124 if (L.fp) {
125 va_list args;
yongchun.li0ee6e372021-08-20 04:26:04 -0700126 fprintf(L.fp, "[%ld.%06ld]: %-5s %s:%d: ", second, usec, level_names[level], file, line);
Song Zhaoc03ba122020-12-23 21:54:02 -0800127 va_start(args, fmt);
128 vfprintf(L.fp, fmt, args);
129 va_end(args);
130 fprintf(L.fp, "\n");
131 fflush(L.fp);
132 }
Song Zhaoc03ba122020-12-23 21:54:02 -0800133 /* Release lock */
134 unlock();
135}