forked from DeaDBeeF-Player/deadbeef
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.c
246 lines (206 loc) · 6.3 KB
/
logger.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/*
DeaDBeeF -- the music player
Copyright (C) 2009-2016 Alexey Yakovenko and other contributors
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <stdlib.h>
#include "logger.h"
#include "deadbeef.h"
#include "threading.h"
typedef struct logger_s {
void (*log) (DB_plugin_t *plugin, uint32_t layers, const char *text, void *ctx);
void *ctx;
struct logger_s *next;
} logger_t;
static uint32_t _log_layers = DDB_LOG_LAYER_DEFAULT | DDB_LOG_LAYER_INFO;
static logger_t *_loggers;
static uint64_t _mutex;
// This buffer is used during the initialization time, before there are any listeners.
// As soon as the first listener registers -- it gets a callback with this buffer, then buffering stops.
// Only error messages are buffered (DDB_LOG_LAYER_DEFAULT)
#define INIT_BUFFER_SIZE 32768
static char *init_buffer;
static char *init_buffer_ptr;
static char *init_buffer_info;
static char *init_buffer_info_ptr;
#ifdef ANDROID
#include <android/log.h>
static void
console_write (const char *text) {
__android_log_write(ANDROID_LOG_INFO,ANDROID_LOGGER_TAG,text);
}
#else
static void
console_write (const char *text) {
fwrite (text, strlen(text), 1, stderr);
fflush (stderr);
}
#endif
static void
_log_internal (DB_plugin_t *plugin, uint32_t layers, const char *text) {
mutex_lock (_mutex);
console_write (text);
size_t len = strlen (text);
for (logger_t *l = _loggers; l; l = l->next) {
l->log (plugin, layers, text, l->ctx);
}
if (init_buffer && (layers == DDB_LOG_LAYER_DEFAULT)) {
if (init_buffer_ptr - init_buffer + len + 1 < INIT_BUFFER_SIZE) {
memcpy (init_buffer_ptr, text, len);
init_buffer_ptr += len;
*init_buffer_ptr = 0;
}
}
if (init_buffer_info && (layers == DDB_LOG_LAYER_INFO)) {
if (init_buffer_info_ptr - init_buffer_info + len + 1 < INIT_BUFFER_SIZE) {
memcpy (init_buffer_info_ptr, text, len);
init_buffer_info_ptr += len;
*init_buffer_info_ptr = 0;
}
}
mutex_unlock(_mutex);
}
static int
_is_log_visible (DB_plugin_t *plugin, uint32_t layers) {
if (plugin && !(plugin->flags&DDB_PLUGIN_FLAG_LOGGING)) {
return 0;
}
if (layers && !(layers & _log_layers)) {
return 0;
}
return 1;
}
int
ddb_logger_init (void) {
_mutex = mutex_create ();
if (!_mutex) {
return -1;
}
init_buffer = calloc(1, INIT_BUFFER_SIZE);
init_buffer_ptr = init_buffer;
init_buffer_info = calloc(1, INIT_BUFFER_SIZE);
init_buffer_info_ptr = init_buffer_info;
return 0;
}
void
ddb_logger_stop_buffering (void) {
mutex_lock (_mutex);
if (init_buffer) {
free (init_buffer);
init_buffer = init_buffer_ptr = NULL;
}
if (init_buffer_info) {
free (init_buffer_info);
init_buffer_info = init_buffer_info_ptr = NULL;
}
mutex_unlock (_mutex);
}
void
ddb_logger_free (void) {
if (_mutex) {
mutex_lock (_mutex);
ddb_logger_stop_buffering ();
while (_loggers) {
ddb_log_viewer_unregister(_loggers->log, _loggers->ctx);
}
mutex_free (_mutex);
_mutex = 0;
}
}
void
ddb_log_detailed (DB_plugin_t *plugin, uint32_t layers, const char *fmt, ...) {
if (!_is_log_visible(plugin, layers)) {
return;
}
char text[2048];
va_list ap;
va_start(ap, fmt);
(void) vsnprintf(text, sizeof (text), fmt, ap);
va_end(ap);
_log_internal (plugin, layers, text);
}
void
ddb_vlog_detailed (DB_plugin_t *plugin, uint32_t layers, const char *fmt, va_list ap) {
if (!_is_log_visible(plugin, layers)) {
return;
}
char text[2048];
(void) vsnprintf(text, sizeof (text), fmt, ap);
_log_internal (plugin, layers, text);
}
void
ddb_log (const char *fmt, ...) {
char text[2048];
va_list ap;
va_start(ap, fmt);
(void) vsnprintf(text, sizeof (text), fmt, ap);
va_end(ap);
_log_internal (NULL, 0, text);
}
void
ddb_vlog (const char *fmt, va_list ap) {
char text[2048];
(void) vsnprintf(text, sizeof (text), fmt, ap);
_log_internal (NULL, 0, text);
}
void
ddb_log_viewer_register (void (*callback)(DB_plugin_t *plugin, uint32_t layers, const char *text, void *ctx), void *ctx) {
mutex_lock(_mutex);
for (logger_t *l = _loggers; l; l = l->next) {
if (l->log == callback && l->ctx == ctx) {
mutex_unlock(_mutex);
return;
}
}
logger_t *logger = calloc (sizeof (logger_t), 1);
logger->log = callback;
logger->ctx = ctx;
logger->next = _loggers;
_loggers = logger;
if (init_buffer) {
if (*init_buffer) {
callback (NULL, DDB_LOG_LAYER_DEFAULT, init_buffer, ctx);
}
}
if (init_buffer_info) {
if (*init_buffer_info) {
callback (NULL, DDB_LOG_LAYER_INFO, init_buffer_info, ctx);
}
}
ddb_logger_stop_buffering ();
mutex_unlock(_mutex);
}
void
ddb_log_viewer_unregister (void (*callback)(DB_plugin_t *plugin, uint32_t layers, const char *text, void *ctx), void *ctx) {
mutex_lock(_mutex);
logger_t *prev = NULL;
for (logger_t *l = _loggers; l; l = l->next) {
if (l->log == callback && l->ctx == ctx) {
if (prev) {
prev->next = l->next;
}
else {
_loggers = l->next;
}
free (l);
break;
}
prev = l;
}
mutex_unlock(_mutex);
}