blob: 892dda5b2a7bad15ccdc6906a709bbea7315408e [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>
Song Zhao47961d72022-08-29 14:04:44 -070029#ifdef ENABLE_SYSLOG
30#include <syslog.h>
31#elif ENABLE_LOGCAT
32#define LOG_TAG "avs"
33#include <cutils/log.h>
34#endif
Song Zhaoc03ba122020-12-23 21:54:02 -080035
36#include "aml_avsync_log.h"
37
38static struct {
39 void *udata;
40 log_LockFn lock;
41 FILE *fp;
42 int level;
43 int quiet;
yongchun.li0ee6e372021-08-20 04:26:04 -070044} L = {0};
Song Zhaoc03ba122020-12-23 21:54:02 -080045
46
47static const char *level_names[] = {
48 "TRACE", "DEBUG", "INFO", "WARN", "ERROR", "FATAL"
49};
50
51#ifdef LOG_USE_COLOR
52static const char *level_colors[] = {
53 "\x1b[94m", "\x1b[36m", "\x1b[32m", "\x1b[33m", "\x1b[31m", "\x1b[35m"
54};
55#endif
56
57
58static void lock(void) {
59 if (L.lock) {
60 L.lock(L.udata, 1);
61 }
62}
63
64
65static void unlock(void) {
66 if (L.lock) {
67 L.lock(L.udata, 0);
68 }
69}
70
71
72void log_set_udata(void *udata) {
73 L.udata = udata;
74}
75
76
77void log_set_lock(log_LockFn fn) {
78 L.lock = fn;
79}
80
81
82void log_set_fp(FILE *fp) {
83 L.fp = fp;
84}
85
86
87void log_set_level(int level) {
Song Zhao67c937b2021-10-01 11:21:32 -070088 L.level = level;
Song Zhaoc03ba122020-12-23 21:54:02 -080089}
90
91
92void log_set_quiet(int enable) {
93 L.quiet = enable ? 1 : 0;
94}
95
96
97void log_log(int level, const char *file, int line, const char *fmt, ...) {
98 if (level < L.level) {
99 return;
100 }
101
102 /* Acquire lock */
103 lock();
104
105 /* Get current time */
yongchun.li0ee6e372021-08-20 04:26:04 -0700106 struct timespec tm;
107 long second, usec;
Song Zhaoc03ba122020-12-23 21:54:02 -0800108
yongchun.li0ee6e372021-08-20 04:26:04 -0700109 clock_gettime( CLOCK_MONOTONIC_RAW, &tm );
110 second = tm.tv_sec;
111 usec = tm.tv_nsec/1000LL;
Song Zhao47961d72022-08-29 14:04:44 -0700112
113#if defined(ENABLE_SYSLOG)
114 {
115 va_list args;
116 int l;
117 char content[512];
118 switch (level) {
119 case AVS_LOG_FATAL:
120 l = LOG_CRIT;
121 break;
122 case AVS_LOG_ERROR:
123 l = LOG_ERR;
124 break;
125 case AVS_LOG_WARN:
126 l = LOG_WARNING;
127 break;
128 case AVS_LOG_INFO:
129 l = LOG_NOTICE;
130 break;
131 case AVS_LOG_DEBUG:
132 l = LOG_INFO;
133 break;
134 case AVS_LOG_TRACE:
135 default:
136 l = LOG_DEBUG;
137 break;
138 }
139 va_start(args, fmt);
140 vsnprintf(content, 512, fmt, args);
141 va_end(args);
142 syslog(l, "[%ld.%06ld]: %-5s %s:%d: %s", second, usec, level_names[level], file, line, content);
143
144 unlock();
145 return;
146 }
147#elif defined(ENABLE_LOGCAT)
148 {
149 va_list args;
150 int l;
151 char content[512];
152 switch (level) {
153 case AVS_LOG_FATAL:
154 l = ANDROID_LOG_ERROR;
155 break;
156 case AVS_LOG_ERROR:
157 l = ANDROID_LOG_ERROR;
158 break;
159 case AVS_LOG_WARN:
160 l = ANDROID_LOG_WARN;
161 break;
162 case AVS_LOG_INFO:
163 l = ANDROID_LOG_INFO;
164 break;
165 case AVS_LOG_DEBUG:
166 l = ANDROID_LOG_DEBUG;
167 break;
168 case AVS_LOG_TRACE:
169 default:
170 l = ANDROID_LOG_VERBOSE;
171 break;
172 }
173 va_start(args, fmt);
174 vsnprintf(content, 512, fmt, args);
175 va_end(args);
176 LOG_PRI(l, LOG_TAG, "[%ld.%06ld]: %-5s %s:%d: %s", second, usec, level_names[level], file, line, content);
177
178 unlock();
179 return;
180 }
181#endif
Song Zhaoc03ba122020-12-23 21:54:02 -0800182 /* Log to stderr */
183 if (!L.quiet) {
184 va_list args;
Song Zhaoc03ba122020-12-23 21:54:02 -0800185#ifdef LOG_USE_COLOR
186 fprintf(
yongchun.li0ee6e372021-08-20 04:26:04 -0700187 stderr, "[%ld.%06ld]: %s%-5s\x1b[0m \x1b[90m%s:%d:\x1b[0m ",
188 second, usec, level_colors[level], level_names[level], file, line);
Song Zhaoc03ba122020-12-23 21:54:02 -0800189#else
yongchun.li0ee6e372021-08-20 04:26:04 -0700190 fprintf(stderr, "[%ld.%06ld]: %-5s %s:%d: ", second, usec, level_names[level], file, line);
Song Zhaoc03ba122020-12-23 21:54:02 -0800191#endif
192 va_start(args, fmt);
193 vfprintf(stderr, fmt, args);
194 va_end(args);
195 fprintf(stderr, "\n");
196 fflush(stderr);
197 }
198
199 /* Log to file */
200 if (L.fp) {
201 va_list args;
yongchun.li0ee6e372021-08-20 04:26:04 -0700202 fprintf(L.fp, "[%ld.%06ld]: %-5s %s:%d: ", second, usec, level_names[level], file, line);
Song Zhaoc03ba122020-12-23 21:54:02 -0800203 va_start(args, fmt);
204 vfprintf(L.fp, fmt, args);
205 va_end(args);
206 fprintf(L.fp, "\n");
207 fflush(L.fp);
208 }
Song Zhaoc03ba122020-12-23 21:54:02 -0800209 /* Release lock */
210 unlock();
211}