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
|
/**
* @file syslog.c Syslog module
*
* Copyright (C) 2010 Creytiv.com
*/
#define _BSD_SOURCE 1
#define _GNU_SOURCE 1
#include <stdio.h>
#include <syslog.h>
#include <re.h>
#include <baresip.h>
#define DEBUG_MODULE "syslog"
#define DEBUG_LEVEL 6
#include <re_dbg.h>
#if defined (DARWIN) || defined (__GLIBC__)
static FILE *fv[2];
static int writer(void *cookie, const char *p, int len)
{
(void)cookie;
syslog(LOG_NOTICE, "%.*s", (int)len, p);
return len;
}
static void tolog(int ix, FILE **pfp)
{
#if defined (__GLIBC__)
static cookie_io_functions_t memfile_func = {
.write = (cookie_write_function_t *)writer
};
#endif
FILE *f;
#if defined (__GLIBC__)
f = fopencookie(NULL, "w+", memfile_func);
#else
f = fwopen(NULL, writer);
#endif
if (!f)
return;
setvbuf(f, NULL, _IOLBF, 0);
fv[ix] = *pfp = f;
}
static void restore(int ix, FILE **fp)
{
if (fv[ix]) {
*fp = fv[ix];
fv[ix] = NULL;
}
}
#endif
static void syslog_handler(int level, const char *p, size_t len, void *arg)
{
(void)arg;
syslog(level, "%.*s", (int)len, p);
}
static int module_init(void)
{
openlog("baresip", LOG_NDELAY | LOG_PID, LOG_LOCAL0);
#if defined (DARWIN) || defined (__GLIBC__)
/* Redirect stdout/stderr to syslog */
tolog(0, &stdout);
tolog(1, &stderr);
#endif
dbg_init(DBG_INFO, DBG_NONE);
dbg_handler_set(syslog_handler, NULL);
return 0;
}
static int module_close(void)
{
dbg_handler_set(NULL, NULL);
#if defined (DARWIN) || defined (__GLIBC__)
restore(0, &stdout);
restore(1, &stderr);
#endif
closelog();
return 0;
}
const struct mod_export DECL_EXPORTS(syslog) = {
"syslog",
"syslog",
module_init,
module_close
};
|