forked from bitlbee/bitlbee
-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.c
181 lines (154 loc) · 4.36 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
/********************************************************************\
* BitlBee -- An IRC to other IM-networks gateway *
* *
* Copyright 2002-2005 Wilmer van der Gaast and others *
\********************************************************************/
/* Logging services for the bee */
/*
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License with
the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL;
if not, write to the Free Software Foundation, Inc., 51 Franklin St.,
Fifth Floor, Boston, MA 02110-1301 USA
*/
#define BITLBEE_CORE
#include "bitlbee.h"
#include <syslog.h>
static log_t logoutput;
static void log_null(int level, const char *logmessage);
static void log_irc(int level, const char *logmessage);
static void log_syslog(int level, const char *logmessage);
static void log_console(int level, const char *logmessage);
void log_init(void)
{
openlog("bitlbee", LOG_PID, LOG_DAEMON);
logoutput.informational = &log_null;
logoutput.warning = &log_null;
logoutput.error = &log_null;
#ifdef DEBUG
logoutput.debug = &log_null;
#endif
return;
}
void log_link(int level, int output)
{
void (*output_function)(int level, const char *logmessage) = &log_null;
if (output == LOGOUTPUT_NULL) {
output_function = &log_null;
} else if (output == LOGOUTPUT_IRC) {
output_function = &log_irc;
} else if (output == LOGOUTPUT_SYSLOG) {
output_function = &log_syslog;
} else if (output == LOGOUTPUT_CONSOLE) {
output_function = &log_console;
}
if (level == LOGLVL_INFO) {
logoutput.informational = output_function;
} else if (level == LOGLVL_WARNING) {
logoutput.warning = output_function;
} else if (level == LOGLVL_ERROR) {
logoutput.error = output_function;
}
#ifdef DEBUG
else if (level == LOGLVL_DEBUG) {
logoutput.debug = output_function;
}
#endif
return;
}
void log_message(int level, const char *message, ...)
{
va_list ap;
char *msgstring;
va_start(ap, message);
msgstring = g_strdup_vprintf(message, ap);
va_end(ap);
if (level == LOGLVL_INFO) {
(*(logoutput.informational))(level, msgstring);
}
if (level == LOGLVL_WARNING) {
(*(logoutput.warning))(level, msgstring);
}
if (level == LOGLVL_ERROR) {
(*(logoutput.error))(level, msgstring);
}
#ifdef DEBUG
if (level == LOGLVL_DEBUG) {
(*(logoutput.debug))(level, msgstring);
}
#endif
g_free(msgstring);
return;
}
void log_error(const char *functionname)
{
log_message(LOGLVL_ERROR, "%s: %s", functionname, strerror(errno));
return;
}
static void log_null(int level, const char *message)
{
return;
}
static void log_irc(int level, const char *message)
{
if (level == LOGLVL_ERROR) {
irc_write_all(1, "ERROR :Error: %s", message);
}
if (level == LOGLVL_WARNING) {
irc_write_all(0, "ERROR :Warning: %s", message);
}
if (level == LOGLVL_INFO) {
irc_write_all(0, "ERROR :Informational: %s", message);
}
#ifdef DEBUG
if (level == LOGLVL_DEBUG) {
irc_write_all(0, "ERROR :Debug: %s", message);
}
#endif
return;
}
static void log_syslog(int level, const char *message)
{
if (level == LOGLVL_ERROR) {
syslog(LOG_ERR, "%s", message);
}
if (level == LOGLVL_WARNING) {
syslog(LOG_WARNING, "%s", message);
}
if (level == LOGLVL_INFO) {
syslog(LOG_INFO, "%s", message);
}
#ifdef DEBUG
if (level == LOGLVL_DEBUG) {
syslog(LOG_DEBUG, "%s", message);
}
#endif
return;
}
static void log_console(int level, const char *message)
{
if (level == LOGLVL_ERROR) {
fprintf(stderr, "Error: %s\n", message);
}
if (level == LOGLVL_WARNING) {
fprintf(stderr, "Warning: %s\n", message);
}
if (level == LOGLVL_INFO) {
fprintf(stdout, "Informational: %s\n", message);
}
#ifdef DEBUG
if (level == LOGLVL_DEBUG) {
fprintf(stdout, "Debug: %s\n", message);
}
#endif
/* Always log stuff in syslogs too. */
log_syslog(level, message);
return;
}