blob: 740da27b30048600b7c60a94c3db98ba435f5143 [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) {
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 */
yongchun.li0ee6e372021-08-20 04:26:04 -0700104 struct timespec tm;
105 long second, usec;
Song Zhaoc03ba122020-12-23 21:54:02 -0800106
yongchun.li0ee6e372021-08-20 04:26:04 -0700107 clock_gettime( CLOCK_MONOTONIC_RAW, &tm );
108 second = tm.tv_sec;
109 usec = tm.tv_nsec/1000LL;
Song Zhaoc03ba122020-12-23 21:54:02 -0800110 /* Log to stderr */
111 if (!L.quiet) {
112 va_list args;
Song Zhaoc03ba122020-12-23 21:54:02 -0800113#ifdef LOG_USE_COLOR
114 fprintf(
yongchun.li0ee6e372021-08-20 04:26:04 -0700115 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 Zhaoc03ba122020-12-23 21:54:02 -0800117#else
yongchun.li0ee6e372021-08-20 04:26:04 -0700118 fprintf(stderr, "[%ld.%06ld]: %-5s %s:%d: ", second, usec, level_names[level], file, line);
Song Zhaoc03ba122020-12-23 21:54:02 -0800119#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.li0ee6e372021-08-20 04:26:04 -0700130 fprintf(L.fp, "[%ld.%06ld]: %-5s %s:%d: ", second, usec, level_names[level], file, line);
Song Zhaoc03ba122020-12-23 21:54:02 -0800131 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 Zhaoc03ba122020-12-23 21:54:02 -0800137 /* Release lock */
138 unlock();
139}