blob: 6c4071ea63a3f86b3325844dfe3256d785239957 [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) {
yongchun.li12b14542021-05-19 18:56:31 -070082 const char *env= getenv( "AML_AVSYNC_DEBUG_LEVEL" );
83 if ( env ) {
84 L.level = atoi( env );
85 } else
86 L.level = level;
Song Zhaoc03ba122020-12-23 21:54:02 -080087}
88
89
90void log_set_quiet(int enable) {
91 L.quiet = enable ? 1 : 0;
92}
93
94
95void 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 */
104 time_t t = time(NULL);
105 struct tm *lt = localtime(&t);
106 struct timeval tv;
107
108 gettimeofday(&tv, NULL);
109 /* Log to stderr */
110 if (!L.quiet) {
111 va_list args;
112 char buf[16];
113 buf[strftime(buf, sizeof(buf), "%H:%M:%S", lt)] = '\0';
114#ifdef LOG_USE_COLOR
115 fprintf(
116 stderr, "%s:%03ld %s%-5s\x1b[0m \x1b[90m%s:%d:\x1b[0m ",
117 buf, tv.tv_usec/1000, level_colors[level], level_names[level], file, line);
118#else
119 fprintf(stderr, "%s:%03ld %-5s %s:%d: ", buf, tv.tv_usec/1000, level_names[level], file, line);
120#endif
121 va_start(args, fmt);
122 vfprintf(stderr, fmt, args);
123 va_end(args);
124 fprintf(stderr, "\n");
125 fflush(stderr);
126 }
127
128 /* Log to file */
129 if (L.fp) {
130 va_list args;
131 char buf[32];
132 buf[strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", lt)] = '\0';
133 fprintf(L.fp, "%s:%03ld %-5s %s:%d: ", buf, tv.tv_usec/1000, level_names[level], file, line);
134 va_start(args, fmt);
135 vfprintf(L.fp, fmt, args);
136 va_end(args);
137 fprintf(L.fp, "\n");
138 fflush(L.fp);
139 }
140
141 /* Release lock */
142 unlock();
143}