1 | /********************************************************************\ |
---|
2 | * BitlBee -- An IRC to other IM-networks gateway * |
---|
3 | * * |
---|
4 | * Copyright 2002-2005 Wilmer van der Gaast and others * |
---|
5 | \********************************************************************/ |
---|
6 | |
---|
7 | /* Logging services for the bee */ |
---|
8 | |
---|
9 | /* |
---|
10 | This program is free software; you can redistribute it and/or modify |
---|
11 | it under the terms of the GNU General Public License as published by |
---|
12 | the Free Software Foundation; either version 2 of the License, or |
---|
13 | (at your option) any later version. |
---|
14 | |
---|
15 | This program is distributed in the hope that it will be useful, |
---|
16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
18 | GNU General Public License for more details. |
---|
19 | |
---|
20 | You should have received a copy of the GNU General Public License with |
---|
21 | the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL; |
---|
22 | if not, write to the Free Software Foundation, Inc., 51 Franklin St., |
---|
23 | Fifth Floor, Boston, MA 02110-1301 USA |
---|
24 | */ |
---|
25 | |
---|
26 | #define BITLBEE_CORE |
---|
27 | #include "bitlbee.h" |
---|
28 | #include <syslog.h> |
---|
29 | |
---|
30 | static log_t logoutput; |
---|
31 | |
---|
32 | static void log_null(int level, const char *logmessage); |
---|
33 | static void log_irc(int level, const char *logmessage); |
---|
34 | static void log_syslog(int level, const char *logmessage); |
---|
35 | static void log_console(int level, const char *logmessage); |
---|
36 | |
---|
37 | void log_init(void) |
---|
38 | { |
---|
39 | openlog("bitlbee", LOG_PID, LOG_DAEMON); |
---|
40 | |
---|
41 | logoutput.informational = &log_null; |
---|
42 | logoutput.warning = &log_null; |
---|
43 | logoutput.error = &log_null; |
---|
44 | #ifdef DEBUG |
---|
45 | logoutput.debug = &log_null; |
---|
46 | #endif |
---|
47 | |
---|
48 | return; |
---|
49 | } |
---|
50 | |
---|
51 | void log_link(int level, int output) |
---|
52 | { |
---|
53 | /* I know it's ugly, but it works and I didn't feel like messing with pointer to function pointers */ |
---|
54 | |
---|
55 | if (level == LOGLVL_INFO) { |
---|
56 | if (output == LOGOUTPUT_NULL) { |
---|
57 | logoutput.informational = &log_null; |
---|
58 | } else if (output == LOGOUTPUT_IRC) { |
---|
59 | logoutput.informational = &log_irc; |
---|
60 | } else if (output == LOGOUTPUT_SYSLOG) { |
---|
61 | logoutput.informational = &log_syslog; |
---|
62 | } else if (output == LOGOUTPUT_CONSOLE) { |
---|
63 | logoutput.informational = &log_console; |
---|
64 | } |
---|
65 | } else if (level == LOGLVL_WARNING) { |
---|
66 | if (output == LOGOUTPUT_NULL) { |
---|
67 | logoutput.warning = &log_null; |
---|
68 | } else if (output == LOGOUTPUT_IRC) { |
---|
69 | logoutput.warning = &log_irc; |
---|
70 | } else if (output == LOGOUTPUT_SYSLOG) { |
---|
71 | logoutput.warning = &log_syslog; |
---|
72 | } else if (output == LOGOUTPUT_CONSOLE) { |
---|
73 | logoutput.warning = &log_console; |
---|
74 | } |
---|
75 | } else if (level == LOGLVL_ERROR) { |
---|
76 | if (output == LOGOUTPUT_NULL) { |
---|
77 | logoutput.error = &log_null; |
---|
78 | } else if (output == LOGOUTPUT_IRC) { |
---|
79 | logoutput.error = &log_irc; |
---|
80 | } else if (output == LOGOUTPUT_SYSLOG) { |
---|
81 | logoutput.error = &log_syslog; |
---|
82 | } else if (output == LOGOUTPUT_CONSOLE) { |
---|
83 | logoutput.error = &log_console; |
---|
84 | } |
---|
85 | } |
---|
86 | #ifdef DEBUG |
---|
87 | else if (level == LOGLVL_DEBUG) { |
---|
88 | if (output == LOGOUTPUT_NULL) { |
---|
89 | logoutput.debug = &log_null; |
---|
90 | } else if (output == LOGOUTPUT_IRC) { |
---|
91 | logoutput.debug = &log_irc; |
---|
92 | } else if (output == LOGOUTPUT_SYSLOG) { |
---|
93 | logoutput.debug = &log_syslog; |
---|
94 | } else if (output == LOGOUTPUT_CONSOLE) { |
---|
95 | logoutput.debug = &log_console; |
---|
96 | } |
---|
97 | } |
---|
98 | #endif |
---|
99 | return; |
---|
100 | |
---|
101 | } |
---|
102 | |
---|
103 | void log_message(int level, const char *message, ...) |
---|
104 | { |
---|
105 | |
---|
106 | va_list ap; |
---|
107 | char *msgstring; |
---|
108 | |
---|
109 | va_start(ap, message); |
---|
110 | msgstring = g_strdup_vprintf(message, ap); |
---|
111 | va_end(ap); |
---|
112 | |
---|
113 | if (level == LOGLVL_INFO) { |
---|
114 | (*(logoutput.informational))(level, msgstring); |
---|
115 | } |
---|
116 | if (level == LOGLVL_WARNING) { |
---|
117 | (*(logoutput.warning))(level, msgstring); |
---|
118 | } |
---|
119 | if (level == LOGLVL_ERROR) { |
---|
120 | (*(logoutput.error))(level, msgstring); |
---|
121 | } |
---|
122 | #ifdef DEBUG |
---|
123 | if (level == LOGLVL_DEBUG) { |
---|
124 | (*(logoutput.debug))(level, msgstring); |
---|
125 | } |
---|
126 | #endif |
---|
127 | |
---|
128 | g_free(msgstring); |
---|
129 | |
---|
130 | return; |
---|
131 | } |
---|
132 | |
---|
133 | void log_error(const char *functionname) |
---|
134 | { |
---|
135 | log_message(LOGLVL_ERROR, "%s: %s", functionname, strerror(errno)); |
---|
136 | |
---|
137 | return; |
---|
138 | } |
---|
139 | |
---|
140 | static void log_null(int level, const char *message) |
---|
141 | { |
---|
142 | return; |
---|
143 | } |
---|
144 | |
---|
145 | static void log_irc(int level, const char *message) |
---|
146 | { |
---|
147 | if (level == LOGLVL_ERROR) { |
---|
148 | irc_write_all(1, "ERROR :Error: %s", message); |
---|
149 | } |
---|
150 | if (level == LOGLVL_WARNING) { |
---|
151 | irc_write_all(0, "ERROR :Warning: %s", message); |
---|
152 | } |
---|
153 | if (level == LOGLVL_INFO) { |
---|
154 | irc_write_all(0, "ERROR :Informational: %s", message); |
---|
155 | } |
---|
156 | #ifdef DEBUG |
---|
157 | if (level == LOGLVL_DEBUG) { |
---|
158 | irc_write_all(0, "ERROR :Debug: %s", message); |
---|
159 | } |
---|
160 | #endif |
---|
161 | |
---|
162 | return; |
---|
163 | } |
---|
164 | |
---|
165 | static void log_syslog(int level, const char *message) |
---|
166 | { |
---|
167 | if (level == LOGLVL_ERROR) { |
---|
168 | syslog(LOG_ERR, "%s", message); |
---|
169 | } |
---|
170 | if (level == LOGLVL_WARNING) { |
---|
171 | syslog(LOG_WARNING, "%s", message); |
---|
172 | } |
---|
173 | if (level == LOGLVL_INFO) { |
---|
174 | syslog(LOG_INFO, "%s", message); |
---|
175 | } |
---|
176 | #ifdef DEBUG |
---|
177 | if (level == LOGLVL_DEBUG) { |
---|
178 | syslog(LOG_DEBUG, "%s", message); |
---|
179 | } |
---|
180 | #endif |
---|
181 | return; |
---|
182 | } |
---|
183 | |
---|
184 | static void log_console(int level, const char *message) |
---|
185 | { |
---|
186 | if (level == LOGLVL_ERROR) { |
---|
187 | fprintf(stderr, "Error: %s\n", message); |
---|
188 | } |
---|
189 | if (level == LOGLVL_WARNING) { |
---|
190 | fprintf(stderr, "Warning: %s\n", message); |
---|
191 | } |
---|
192 | if (level == LOGLVL_INFO) { |
---|
193 | fprintf(stdout, "Informational: %s\n", message); |
---|
194 | } |
---|
195 | #ifdef DEBUG |
---|
196 | if (level == LOGLVL_DEBUG) { |
---|
197 | fprintf(stdout, "Debug: %s\n", message); |
---|
198 | } |
---|
199 | #endif |
---|
200 | /* Always log stuff in syslogs too. */ |
---|
201 | log_syslog(level, message); |
---|
202 | return; |
---|
203 | } |
---|