blob: 89789775144598dc08b6c6b1e1ac6e3dd6262612 [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;
38} L;
39
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) {
82 L.level = level;
83}
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 */
100 time_t t = time(NULL);
101 struct tm *lt = localtime(&t);
102 struct timeval tv;
103
104 gettimeofday(&tv, NULL);
105 /* Log to stderr */
106 if (!L.quiet) {
107 va_list args;
108 char buf[16];
109 buf[strftime(buf, sizeof(buf), "%H:%M:%S", lt)] = '\0';
110#ifdef LOG_USE_COLOR
111 fprintf(
112 stderr, "%s:%03ld %s%-5s\x1b[0m \x1b[90m%s:%d:\x1b[0m ",
113 buf, tv.tv_usec/1000, level_colors[level], level_names[level], file, line);
114#else
115 fprintf(stderr, "%s:%03ld %-5s %s:%d: ", buf, tv.tv_usec/1000, level_names[level], file, line);
116#endif
117 va_start(args, fmt);
118 vfprintf(stderr, fmt, args);
119 va_end(args);
120 fprintf(stderr, "\n");
121 fflush(stderr);
122 }
123
124 /* Log to file */
125 if (L.fp) {
126 va_list args;
127 char buf[32];
128 buf[strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", lt)] = '\0';
129 fprintf(L.fp, "%s:%03ld %-5s %s:%d: ", buf, tv.tv_usec/1000, level_names[level], file, line);
130 va_start(args, fmt);
131 vfprintf(L.fp, fmt, args);
132 va_end(args);
133 fprintf(L.fp, "\n");
134 fflush(L.fp);
135 }
136
137 /* Release lock */
138 unlock();
139}