forked from meetecho/janus-gateway
-
Notifications
You must be signed in to change notification settings - Fork 1
/
log.c
276 lines (248 loc) · 6.62 KB
/
log.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*! \file log.c
* \author Jay Ridgeway <[email protected]>
* \copyright GNU General Public License v3
* \brief Buffered logging
* \details Implementation of a simple buffered logger designed to remove
* I/O wait from threads that may be sensitive to such delays. Buffers are
* saved and reused to reduce allocation calls. The logger output can then
* be printed to stdout and/or a log file. If external loggers are added
* to the core, the logger output is passed to those as well.
*
* \ingroup core
* \ref core
*/
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include "log.h"
#include "utils.h"
#include "loggers/logger.h"
#define THREAD_NAME "log"
typedef struct janus_log_buffer janus_log_buffer;
struct janus_log_buffer {
int64_t timestamp;
size_t allocated;
janus_log_buffer *next;
/* str is grown by allocating beyond the struct */
char str[1];
};
#define INITIAL_BUFSZ 2000
static gboolean janus_log_console = TRUE;
static char *janus_log_filepath = NULL;
static FILE *janus_log_file = NULL;
static GHashTable *external_loggers = NULL;
static volatile gint initialized = 0;
static gint stopping = 0;
static gint poolsz = 0;
static gint maxpoolsz = 32;
/* Buffers over this size will be freed */
static size_t maxbuffersz = 8000;
static GMutex lock;
static GCond cond;
static GThread *printthread = NULL;
static janus_log_buffer *printhead = NULL;
static janus_log_buffer *printtail = NULL;
static janus_log_buffer *bufferpool = NULL;
gboolean janus_log_is_stdout_enabled(void) {
return janus_log_console;
}
gboolean janus_log_is_logfile_enabled(void) {
return janus_log_file != NULL;
}
char *janus_log_get_logfile_path(void) {
return janus_log_filepath;
}
static void janus_log_freebuffers(janus_log_buffer **list) {
janus_log_buffer *b, *head = *list;
while (head) {
b = head;
head = b->next;
g_free(b);
}
*list = NULL;
}
static janus_log_buffer *janus_log_getbuf(void) {
janus_log_buffer *b;
g_mutex_lock(&lock);
b = bufferpool;
if (b) {
bufferpool = b->next;
b->next = NULL;
} else {
poolsz++;
}
g_mutex_unlock(&lock);
if (b == NULL) {
b = g_malloc(INITIAL_BUFSZ + sizeof(*b));
b->allocated = INITIAL_BUFSZ;
b->next = NULL;
}
return b;
}
static void *janus_log_thread(void *ctx) {
janus_log_buffer *head, *b, *tofree = NULL;
while (!g_atomic_int_get(&stopping)) {
g_mutex_lock(&lock);
if (!printhead) {
g_cond_wait(&cond, &lock);
}
head = printhead;
printhead = printtail = NULL;
g_mutex_unlock(&lock);
if (head) {
for (b = head; b; b = b->next) {
if(janus_log_console)
fputs(b->str, stdout);
if(janus_log_file)
fputs(b->str, janus_log_file);
if(external_loggers != NULL) {
GHashTableIter iter;
gpointer value;
g_hash_table_iter_init(&iter, external_loggers);
while(g_hash_table_iter_next(&iter, NULL, &value)) {
janus_logger *l = value;
if(l == NULL)
continue;
l->incoming_logline(b->timestamp, b->str);
}
}
}
g_mutex_lock(&lock);
while (head) {
b = head;
head = b->next;
if (poolsz >= maxpoolsz || b->allocated > maxbuffersz) {
b->next = tofree;
tofree = b;
poolsz--;
} else {
b->next = bufferpool;
bufferpool = b;
}
}
g_mutex_unlock(&lock);
if(janus_log_console)
fflush(stdout);
if(janus_log_file)
fflush(janus_log_file);
janus_log_freebuffers(&tofree);
}
}
/* print any remaining messages, stdout flushed on exit */
for (b = printhead; b; b = b->next) {
if(janus_log_console)
fputs(b->str, stdout);
if(janus_log_file)
fputs(b->str, janus_log_file);
if(external_loggers != NULL) {
GHashTableIter iter;
gpointer value;
g_hash_table_iter_init(&iter, external_loggers);
while(g_hash_table_iter_next(&iter, NULL, &value)) {
janus_logger *l = value;
if(l == NULL)
continue;
l->incoming_logline(b->timestamp, b->str);
}
}
}
janus_log_set_loggers(NULL);
if(janus_log_console)
fflush(stdout);
if(janus_log_file)
fflush(janus_log_file);
janus_log_freebuffers(&printhead);
janus_log_freebuffers(&bufferpool);
g_mutex_clear(&lock);
g_cond_clear(&cond);
if(janus_log_file)
fclose(janus_log_file);
janus_log_file = NULL;
g_free(janus_log_filepath);
janus_log_filepath = NULL;
return NULL;
}
void janus_vprintf(const char *format, ...) {
int len;
va_list ap, ap2;
janus_log_buffer *b = janus_log_getbuf();
b->timestamp = janus_get_real_time();
va_start(ap, format);
va_copy(ap2, ap);
/* first try */
len = vsnprintf(b->str, b->allocated, format, ap);
va_end(ap);
if (len >= (int) b->allocated) {
/* buffer wasn't big enough */
b = g_realloc(b, len + 1 + sizeof(*b));
b->allocated = len + 1;
vsnprintf(b->str, b->allocated, format, ap2);
}
va_end(ap2);
g_mutex_lock(&lock);
if (!printhead) {
printhead = printtail = b;
} else {
printtail->next = b;
printtail = b;
}
g_cond_signal(&cond);
g_mutex_unlock(&lock);
}
int janus_log_init(gboolean daemon, gboolean console, const char *logfile) {
if (!g_atomic_int_compare_and_exchange(&initialized, 0, 1)) {
return 0;
}
g_mutex_init(&lock);
g_cond_init(&cond);
if(console) {
/* Set stdout to block buffering, see BUFSIZ in stdio.h */
setvbuf(stdout, NULL, _IOFBF, 0);
}
janus_log_console = console;
if(logfile != NULL) {
/* Open a log file for writing (and append) */
janus_log_file = fopen(logfile, "awt");
if(janus_log_file == NULL) {
g_print("Error opening log file %s: %s\n", logfile, strerror(errno));
return -1;
}
janus_log_filepath = g_strdup(logfile);
}
if(!janus_log_console && logfile == NULL) {
g_print("WARNING: logging completely disabled!\n");
g_print(" (no stdout and no logfile, this may not be what you want...)\n");
}
if(daemon) {
/* Replace the standard file descriptors */
if (freopen("/dev/null", "r", stdin) == NULL) {
g_print("Error replacing stdin with /dev/null\n");
return -1;
}
if (freopen("/dev/null", "w", stdout) == NULL) {
g_print("Error replacing stdout with /dev/null\n");
return -1;
}
if (freopen("/dev/null", "w", stderr) == NULL) {
g_print("Error replacing stderr with /dev/null\n");
return -1;
}
}
printthread = g_thread_new(THREAD_NAME, &janus_log_thread, NULL);
return 0;
}
void janus_log_set_loggers(GHashTable *loggers) {
g_mutex_lock(&lock);
external_loggers = loggers;
if(external_loggers != NULL)
g_print("Adding %d external loggers\n", g_hash_table_size(external_loggers));
g_mutex_unlock(&lock);
}
void janus_log_destroy(void) {
g_atomic_int_set(&stopping, 1);
g_mutex_lock(&lock);
/* Signal print thread to print any remaining message */
g_cond_signal(&cond);
g_mutex_unlock(&lock);
g_thread_join(printthread);
}