1 |
|
/* |
2 |
|
* ircd-hybrid: an advanced Internet Relay Chat Daemon(ircd). |
3 |
< |
* s_log.c: Logger functions. |
3 |
> |
* log.c: Logger functions. |
4 |
|
* |
5 |
|
* Copyright (C) 2002 by the past and present ircd coders, and others. |
6 |
|
* |
24 |
|
|
25 |
|
#include "stdinc.h" |
26 |
|
|
27 |
< |
#ifdef USE_SYSLOG |
28 |
< |
# ifdef HAVE_SYS_SYSLOG_H |
29 |
< |
# include <sys/syslog.h> |
30 |
< |
# else |
31 |
< |
# ifdef HAVE_SYSLOG_H |
32 |
< |
# include <syslog.h> |
33 |
< |
# endif |
34 |
< |
# endif |
35 |
< |
#endif |
36 |
< |
|
37 |
< |
#include "client.h" /* Needed for struct Client */ |
38 |
< |
#include "common.h" |
39 |
< |
#include "s_log.h" |
27 |
> |
#include "log.h" |
28 |
|
#include "fileio.h" |
29 |
|
#include "irc_string.h" |
42 |
– |
#include "sprintf_irc.h" |
30 |
|
#include "ircd.h" |
31 |
+ |
#include "conf.h" |
32 |
+ |
#include "send.h" |
33 |
|
#include "s_misc.h" |
45 |
– |
#include "event.h" /* Needed for EVH etc. */ |
46 |
– |
#include "s_conf.h" |
47 |
– |
#include "memory.h" |
34 |
|
|
49 |
– |
/* some older syslogs would overflow at 2024 */ |
50 |
– |
#define LOG_BUFSIZE 2000 |
35 |
|
|
36 |
< |
static FBFILE *logFile; |
37 |
< |
static int logLevel = INIT_LOG_LEVEL; |
36 |
> |
static struct { |
37 |
> |
char path[PATH_MAX + 1]; |
38 |
> |
size_t size; |
39 |
> |
FBFILE *file; |
40 |
> |
} log_type_table[LOG_TYPE_LAST]; |
41 |
|
|
55 |
– |
#ifndef SYSLOG_USERS |
56 |
– |
static EVH user_log_resync; |
57 |
– |
static FBFILE *user_log_fb = NULL; |
58 |
– |
#endif |
42 |
|
|
43 |
< |
|
44 |
< |
#ifdef USE_SYSLOG |
62 |
< |
static const int sysLogLevel[] = |
43 |
> |
int |
44 |
> |
log_add_file(enum log_type type, size_t size, const char *path) |
45 |
|
{ |
46 |
< |
LOG_CRIT, |
47 |
< |
LOG_ERR, |
66 |
< |
LOG_WARNING, |
67 |
< |
LOG_NOTICE, |
68 |
< |
LOG_INFO, |
69 |
< |
LOG_INFO, |
70 |
< |
LOG_INFO |
71 |
< |
}; |
72 |
< |
#endif |
46 |
> |
if (log_type_table[type].file) |
47 |
> |
fbclose(log_type_table[type].file); |
48 |
|
|
49 |
< |
static const char *logLevelToString[] = |
50 |
< |
{ |
76 |
< |
"L_CRIT", |
77 |
< |
"L_ERROR", |
78 |
< |
"L_WARN", |
79 |
< |
"L_NOTICE", |
80 |
< |
"L_TRACE", |
81 |
< |
"L_INFO", |
82 |
< |
"L_DEBUG" |
83 |
< |
}; |
49 |
> |
strlcpy(log_type_table[type].path, path, sizeof(log_type_table[type].path)); |
50 |
> |
log_type_table[type].size = size; |
51 |
|
|
52 |
< |
/* |
53 |
< |
* open_log - open ircd logging file |
54 |
< |
* returns true (1) if successful, false (0) otherwise |
55 |
< |
*/ |
56 |
< |
static int |
90 |
< |
open_log(const char *filename) |
52 |
> |
return (log_type_table[type].file = fbopen(path, "a")) != NULL; |
53 |
> |
} |
54 |
> |
|
55 |
> |
void |
56 |
> |
log_close_all(void) |
57 |
|
{ |
58 |
< |
logFile = fbopen(filename, "a"); |
58 |
> |
unsigned int type = 0; |
59 |
|
|
60 |
< |
if (logFile == NULL) |
60 |
> |
while (++type < LOG_TYPE_LAST) |
61 |
|
{ |
62 |
< |
#ifdef USE_SYSLOG |
63 |
< |
syslog(LOG_ERR, "Unable to open log file: %s: %s", |
64 |
< |
filename, strerror(errno)); |
65 |
< |
#endif |
66 |
< |
return (0); |
62 |
> |
if (log_type_table[type].file == NULL) |
63 |
> |
continue; |
64 |
> |
|
65 |
> |
fbclose(log_type_table[type].file); |
66 |
> |
log_type_table[type].file = NULL; |
67 |
|
} |
68 |
+ |
} |
69 |
|
|
70 |
< |
return (1); |
70 |
> |
static int |
71 |
> |
log_exceed_size(unsigned int type) |
72 |
> |
{ |
73 |
> |
struct stat sb; |
74 |
> |
|
75 |
> |
if (!log_type_table[type].size) |
76 |
> |
return 0; |
77 |
> |
|
78 |
> |
if (stat(log_type_table[type].path, &sb) < 0) |
79 |
> |
return -1; |
80 |
> |
|
81 |
> |
return (size_t)sb.st_size > log_type_table[type].size; |
82 |
|
} |
83 |
|
|
84 |
+ |
|
85 |
|
static void |
86 |
< |
write_log(const char *message) |
86 |
> |
write_log(enum log_type type, const char *message) |
87 |
|
{ |
88 |
|
char buf[LOG_BUFSIZE]; |
89 |
|
size_t nbytes = 0; |
90 |
|
|
91 |
< |
if (logFile == NULL) |
92 |
< |
return; |
91 |
> |
if (ConfigLoggingEntry.timestamp) |
92 |
> |
nbytes = snprintf(buf, sizeof(buf), "[%s] %s\n", |
93 |
> |
smalldate(CurrentTime), message); |
94 |
> |
else |
95 |
> |
nbytes = snprintf(buf, sizeof(buf), "%s\n", message); |
96 |
|
|
97 |
< |
#ifdef _WIN32 |
116 |
< |
nbytes = snprintf(buf, sizeof(buf), "[%s] %s\r\n", |
117 |
< |
smalldate(CurrentTime), message); |
118 |
< |
#else |
119 |
< |
nbytes = snprintf(buf, sizeof(buf), "[%s] %s\n", |
120 |
< |
smalldate(CurrentTime), message); |
121 |
< |
#endif |
122 |
< |
fbputs(buf, logFile, nbytes); |
97 |
> |
fbputs(buf, log_type_table[type].file, nbytes); |
98 |
|
} |
99 |
|
|
100 |
|
void |
101 |
< |
ilog(const int priority, const char *fmt, ...) |
101 |
> |
ilog(enum log_type type, const char *fmt, ...) |
102 |
|
{ |
103 |
|
char buf[LOG_BUFSIZE]; |
104 |
|
va_list args; |
105 |
|
|
106 |
< |
assert(priority > -1); |
132 |
< |
|
133 |
< |
if (fmt == NULL) |
134 |
< |
return; |
135 |
< |
|
136 |
< |
if (priority > logLevel) |
106 |
> |
if (log_type_table[type].file == NULL) |
107 |
|
return; |
108 |
|
|
109 |
|
va_start(args, fmt); |
110 |
< |
vsprintf(buf, fmt, args); |
110 |
> |
vsnprintf(buf, sizeof(buf), fmt, args); |
111 |
|
va_end(args); |
112 |
|
|
143 |
– |
#ifdef USE_SYSLOG |
144 |
– |
if (priority <= L_DEBUG) |
145 |
– |
syslog(sysLogLevel[priority], "%s", buf); |
146 |
– |
#endif |
113 |
|
if (ConfigLoggingEntry.use_logging) |
148 |
– |
write_log(buf); |
149 |
– |
} |
150 |
– |
|
151 |
– |
void |
152 |
– |
init_log(const char *filename) |
153 |
– |
{ |
154 |
– |
open_log(filename); |
155 |
– |
#ifdef USE_SYSLOG |
156 |
– |
openlog("ircd", LOG_PID | LOG_NDELAY, LOG_FACILITY); |
157 |
– |
#endif |
158 |
– |
#ifndef SYSLOG_USERS |
159 |
– |
eventAddIsh("user_log_resync", user_log_resync, NULL, 60); |
160 |
– |
#endif |
161 |
– |
} |
162 |
– |
|
163 |
– |
void |
164 |
– |
reopen_log(const char *filename) |
165 |
– |
{ |
166 |
– |
if (logFile != NULL) |
167 |
– |
fbclose(logFile); |
168 |
– |
open_log(filename); |
169 |
– |
} |
170 |
– |
|
171 |
– |
void |
172 |
– |
set_log_level(const int level) |
173 |
– |
{ |
174 |
– |
if (L_ERROR < level && level <= L_DEBUG) |
175 |
– |
logLevel = level; |
176 |
– |
} |
177 |
– |
|
178 |
– |
int |
179 |
– |
get_log_level(void) |
180 |
– |
{ |
181 |
– |
return(logLevel); |
182 |
– |
} |
183 |
– |
|
184 |
– |
const char * |
185 |
– |
get_log_level_as_string(int level) |
186 |
– |
{ |
187 |
– |
if (level > L_DEBUG) |
188 |
– |
level = L_DEBUG; |
189 |
– |
else if (level < L_ERROR) |
190 |
– |
level = L_ERROR; |
191 |
– |
|
192 |
– |
return(logLevelToString[level]); |
193 |
– |
} |
194 |
– |
|
195 |
– |
/* log_user_exit() |
196 |
– |
* |
197 |
– |
* inputs - pointer to connecting client |
198 |
– |
* output - NONE |
199 |
– |
* side effects - Current exiting client is logged to |
200 |
– |
* either SYSLOG or to file. |
201 |
– |
*/ |
202 |
– |
void |
203 |
– |
log_user_exit(struct Client *source_p) |
204 |
– |
{ |
205 |
– |
time_t on_for = CurrentTime - source_p->firsttime; |
206 |
– |
#ifdef SYSLOG_USERS |
207 |
– |
if (IsClient(source_p)) |
208 |
– |
{ |
209 |
– |
ilog(L_INFO, "%s (%3u:%02u:%02u): %s!%s@%s %llu/%llu", |
210 |
– |
myctime(source_p->firsttime), on_for / 3600, |
211 |
– |
(on_for % 3600)/60, on_for % 60, |
212 |
– |
source_p->name, source_p->username, source_p->host, |
213 |
– |
source_p->localClient->send.bytes>>10, |
214 |
– |
source_p->localClient->recv.bytes>>10); |
215 |
– |
} |
216 |
– |
#else |
217 |
– |
{ |
218 |
– |
char linebuf[BUFSIZ]; |
219 |
– |
|
220 |
– |
/* |
221 |
– |
* This conditional makes the logfile active only after |
222 |
– |
* it's been created - thus logging can be turned off by |
223 |
– |
* removing the file. |
224 |
– |
* -Taner |
225 |
– |
*/ |
226 |
– |
if (IsClient(source_p)) |
227 |
– |
{ |
228 |
– |
if (user_log_fb == NULL) |
229 |
– |
{ |
230 |
– |
if ((ConfigLoggingEntry.userlog[0] != '\0') && |
231 |
– |
(user_log_fb = fbopen(ConfigLoggingEntry.userlog, "r")) != NULL) |
232 |
– |
{ |
233 |
– |
fbclose(user_log_fb); |
234 |
– |
user_log_fb = fbopen(ConfigLoggingEntry.userlog, "a"); |
235 |
– |
} |
236 |
– |
} |
237 |
– |
|
238 |
– |
if (user_log_fb != NULL) |
239 |
– |
{ |
240 |
– |
size_t nbytes = ircsprintf(linebuf, |
241 |
– |
"%s (%3u:%02u:%02u): %s!%s@%s %llu/%llu\n", |
242 |
– |
myctime(source_p->firsttime), |
243 |
– |
on_for / 3600, |
244 |
– |
(on_for % 3600)/60, |
245 |
– |
on_for % 60, |
246 |
– |
source_p->name, source_p->username, source_p->host, |
247 |
– |
source_p->localClient->send.bytes>>10, |
248 |
– |
source_p->localClient->recv.bytes>>10); |
249 |
– |
fbputs(linebuf, user_log_fb, nbytes); |
250 |
– |
} |
251 |
– |
} |
252 |
– |
} |
253 |
– |
#endif |
254 |
– |
} |
255 |
– |
|
256 |
– |
#ifndef SYSLOG_USERS |
257 |
– |
/* user_log_resync() |
258 |
– |
* |
259 |
– |
* inputs - NONE |
260 |
– |
* output - NONE |
261 |
– |
* side effects - |
262 |
– |
*/ |
263 |
– |
static void |
264 |
– |
user_log_resync(void *notused) |
265 |
– |
{ |
266 |
– |
if (user_log_fb != NULL) |
114 |
|
{ |
115 |
< |
fbclose(user_log_fb); |
269 |
< |
user_log_fb = NULL; |
270 |
< |
} |
271 |
< |
} |
272 |
< |
#endif |
115 |
> |
write_log(type, buf); |
116 |
|
|
117 |
< |
/* log_oper_action() |
275 |
< |
* |
276 |
< |
* inputs - type of oper log entry |
277 |
< |
* - pointer to oper |
278 |
< |
* - const char *pattern == format string |
279 |
< |
* - var args for format string |
280 |
< |
* output - none |
281 |
< |
* side effects - corresponding log is written to, if its present. |
282 |
< |
* |
283 |
< |
* rewritten sept 5 2005 - Dianora |
284 |
< |
*/ |
285 |
< |
void |
286 |
< |
log_oper_action(int log_type, const struct Client *source_p, |
287 |
< |
const char *pattern, ...) |
288 |
< |
{ |
289 |
< |
va_list args; |
290 |
< |
char linebuf[IRCD_BUFSIZE]; |
291 |
< |
FBFILE *log_fb; |
292 |
< |
char *logfile; |
293 |
< |
const char *log_message; |
294 |
< |
size_t nbytes; |
295 |
< |
size_t n_preamble; |
296 |
< |
char *p; |
297 |
< |
|
298 |
< |
switch(log_type) |
299 |
< |
{ |
300 |
< |
case LOG_OPER_TYPE: |
301 |
< |
logfile = ConfigLoggingEntry.operlog; |
302 |
< |
log_message = "OPER"; |
303 |
< |
break; |
304 |
< |
case LOG_FAILED_OPER_TYPE: |
305 |
< |
logfile = ConfigLoggingEntry.failed_operlog; |
306 |
< |
log_message = "FAILED OPER"; |
307 |
< |
break; |
308 |
< |
case LOG_KLINE_TYPE: |
309 |
< |
logfile = ConfigLoggingEntry.klinelog; |
310 |
< |
log_message = "KLINE"; |
311 |
< |
break; |
312 |
< |
case LOG_RKLINE_TYPE: |
313 |
< |
logfile = ConfigLoggingEntry.klinelog; |
314 |
< |
log_message = "RKLINE"; |
315 |
< |
break; |
316 |
< |
case LOG_DLINE_TYPE: |
317 |
< |
logfile = ConfigLoggingEntry.klinelog; |
318 |
< |
log_message = "DLINE"; |
319 |
< |
break; |
320 |
< |
case LOG_TEMP_DLINE_TYPE: |
321 |
< |
logfile = ConfigLoggingEntry.klinelog; |
322 |
< |
log_message = "TEMP DLINE"; |
323 |
< |
break; |
324 |
< |
case LOG_TEMP_KLINE_TYPE: |
325 |
< |
logfile = ConfigLoggingEntry.klinelog; |
326 |
< |
log_message = "TEMP KLINE"; |
327 |
< |
break; |
328 |
< |
case LOG_GLINE_TYPE: |
329 |
< |
logfile = ConfigLoggingEntry.glinelog; |
330 |
< |
log_message = "GLINE"; |
331 |
< |
break; |
332 |
< |
case LOG_KILL_TYPE: |
333 |
< |
logfile = ConfigLoggingEntry.killlog; |
334 |
< |
log_message = "KILL"; |
335 |
< |
break; |
336 |
< |
case LOG_IOERR_TYPE: |
337 |
< |
logfile = ConfigLoggingEntry.ioerrlog; |
338 |
< |
log_message = "IO ERR"; |
339 |
< |
break; |
340 |
< |
default: |
341 |
< |
return; |
342 |
< |
} |
343 |
< |
|
344 |
< |
if (*logfile == '\0') |
345 |
< |
return; |
346 |
< |
|
347 |
< |
p = linebuf; |
348 |
< |
if (source_p != NULL) |
349 |
< |
{ |
350 |
< |
n_preamble = ircsprintf(linebuf, "%s %s by (%s!%s@%s) :", |
351 |
< |
myctime(CurrentTime), log_message, |
352 |
< |
source_p->name, source_p->username, source_p->host); |
353 |
< |
|
354 |
< |
} |
355 |
< |
else |
356 |
< |
{ |
357 |
< |
n_preamble = ircsprintf(linebuf, "%s %s :", |
358 |
< |
myctime(CurrentTime), log_message); |
359 |
< |
} |
360 |
< |
|
361 |
< |
p += n_preamble; |
362 |
< |
|
363 |
< |
if ((log_fb = fbopen(logfile, "r")) != NULL) |
364 |
< |
{ |
365 |
< |
fbclose(log_fb); |
366 |
< |
log_fb = fbopen(logfile, "a"); |
367 |
< |
if (log_fb == NULL) |
117 |
> |
if (log_exceed_size(type) <= 0) |
118 |
|
return; |
119 |
< |
va_start(args, pattern); |
120 |
< |
/* XXX add check for IRCD_BUFSIZE-(n_preamble+1) < 0 ? -db */ |
121 |
< |
nbytes = vsnprintf(p, IRCD_BUFSIZE-(n_preamble+1), pattern, args); |
122 |
< |
nbytes += n_preamble; |
123 |
< |
va_end(args); |
124 |
< |
fbputs(linebuf, log_fb, nbytes); |
125 |
< |
fbclose(log_fb); |
119 |
> |
|
120 |
> |
snprintf(buf, sizeof(buf), "Rotating logfile %s", |
121 |
> |
log_type_table[type].path); |
122 |
> |
write_log(type, buf); |
123 |
> |
fbclose(log_type_table[type].file); |
124 |
> |
log_type_table[type].file = NULL; |
125 |
> |
|
126 |
> |
snprintf(buf, sizeof(buf), "%s.old", log_type_table[type].path); |
127 |
> |
unlink(buf); |
128 |
> |
rename(log_type_table[type].path, buf); |
129 |
> |
log_add_file(type, log_type_table[type].size, log_type_table[type].path); |
130 |
|
} |
131 |
|
} |