1 |
|
/* |
2 |
< |
* ircd-hybrid: an advanced Internet Relay Chat Daemon(ircd). |
3 |
< |
* s_log.c: Logger functions. |
2 |
> |
* ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd) |
3 |
|
* |
4 |
< |
* Copyright (C) 2002 by the past and present ircd coders, and others. |
4 |
> |
* Copyright (c) 1997-2014 ircd-hybrid development team |
5 |
|
* |
6 |
|
* This program is free software; you can redistribute it and/or modify |
7 |
|
* it under the terms of the GNU General Public License as published by |
17 |
|
* along with this program; if not, write to the Free Software |
18 |
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
19 |
|
* USA |
21 |
– |
* |
22 |
– |
* $Id$ |
20 |
|
*/ |
21 |
|
|
22 |
< |
#include "stdinc.h" |
22 |
> |
/*! \file log.c |
23 |
> |
* \brief Logger functions. |
24 |
> |
* \version $Id$ |
25 |
> |
*/ |
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" |
40 |
< |
#include "fileio.h" |
27 |
> |
#include "stdinc.h" |
28 |
> |
#include "log.h" |
29 |
|
#include "irc_string.h" |
42 |
– |
#include "sprintf_irc.h" |
30 |
|
#include "ircd.h" |
31 |
< |
#include "s_misc.h" |
32 |
< |
#include "event.h" /* Needed for EVH etc. */ |
46 |
< |
#include "s_conf.h" |
47 |
< |
#include "memory.h" |
48 |
< |
|
49 |
< |
/* some older syslogs would overflow at 2024 */ |
50 |
< |
#define LOG_BUFSIZE 2000 |
31 |
> |
#include "conf.h" |
32 |
> |
#include "misc.h" |
33 |
|
|
52 |
– |
static FBFILE *logFile; |
53 |
– |
static int logLevel = INIT_LOG_LEVEL; |
34 |
|
|
35 |
< |
#ifndef SYSLOG_USERS |
56 |
< |
static EVH user_log_resync; |
57 |
< |
static FBFILE *user_log_fb = NULL; |
58 |
< |
#endif |
59 |
< |
|
60 |
< |
|
61 |
< |
#ifdef USE_SYSLOG |
62 |
< |
static const int sysLogLevel[] = |
35 |
> |
static struct |
36 |
|
{ |
37 |
< |
LOG_CRIT, |
38 |
< |
LOG_ERR, |
39 |
< |
LOG_WARNING, |
40 |
< |
LOG_NOTICE, |
68 |
< |
LOG_INFO, |
69 |
< |
LOG_INFO, |
70 |
< |
LOG_INFO |
71 |
< |
}; |
72 |
< |
#endif |
37 |
> |
char path[HYB_PATH_MAX + 1]; |
38 |
> |
size_t size; |
39 |
> |
FILE *file; |
40 |
> |
} log_type_table[LOG_TYPE_LAST]; |
41 |
|
|
74 |
– |
static const char *logLevelToString[] = |
75 |
– |
{ |
76 |
– |
"L_CRIT", |
77 |
– |
"L_ERROR", |
78 |
– |
"L_WARN", |
79 |
– |
"L_NOTICE", |
80 |
– |
"L_TRACE", |
81 |
– |
"L_INFO", |
82 |
– |
"L_DEBUG" |
83 |
– |
}; |
42 |
|
|
43 |
< |
/* |
44 |
< |
* open_log - open ircd logging file |
87 |
< |
* returns true (1) if successful, false (0) otherwise |
88 |
< |
*/ |
89 |
< |
static int |
90 |
< |
open_log(const char *filename) |
43 |
> |
void |
44 |
> |
log_set_file(enum log_type type, size_t size, const char *path) |
45 |
|
{ |
46 |
< |
logFile = fbopen(filename, "a"); |
46 |
> |
strlcpy(log_type_table[type].path, path, sizeof(log_type_table[type].path)); |
47 |
> |
log_type_table[type].size = size; |
48 |
|
|
49 |
< |
if (logFile == NULL) |
50 |
< |
{ |
96 |
< |
#ifdef USE_SYSLOG |
97 |
< |
syslog(LOG_ERR, "Unable to open log file: %s: %s", |
98 |
< |
filename, strerror(errno)); |
99 |
< |
#endif |
100 |
< |
return (0); |
101 |
< |
} |
102 |
< |
|
103 |
< |
return (1); |
49 |
> |
if (type == LOG_TYPE_IRCD) |
50 |
> |
log_type_table[type].file = fopen(log_type_table[type].path, "a"); |
51 |
|
} |
52 |
|
|
106 |
– |
static void |
107 |
– |
write_log(const char *message) |
108 |
– |
{ |
109 |
– |
char buf[LOG_BUFSIZE]; |
110 |
– |
size_t nbytes = 0; |
111 |
– |
|
112 |
– |
if (logFile == NULL) |
113 |
– |
return; |
114 |
– |
|
115 |
– |
nbytes = snprintf(buf, sizeof(buf), "[%s] %s\n", |
116 |
– |
smalldate(CurrentTime), message); |
117 |
– |
|
118 |
– |
fbputs(buf, logFile, nbytes); |
119 |
– |
} |
120 |
– |
|
53 |
|
void |
54 |
< |
ilog(const int priority, const char *fmt, ...) |
54 |
> |
log_del_all(void) |
55 |
|
{ |
56 |
< |
char buf[LOG_BUFSIZE]; |
125 |
< |
va_list args; |
126 |
< |
|
127 |
< |
assert(priority > -1); |
128 |
< |
|
129 |
< |
if (fmt == NULL) |
130 |
< |
return; |
56 |
> |
unsigned int type = 0; |
57 |
|
|
58 |
< |
if (priority > logLevel) |
59 |
< |
return; |
134 |
< |
|
135 |
< |
va_start(args, fmt); |
136 |
< |
vsnprintf(buf, sizeof(buf), fmt, args); |
137 |
< |
va_end(args); |
138 |
< |
|
139 |
< |
#ifdef USE_SYSLOG |
140 |
< |
if (priority <= L_DEBUG) |
141 |
< |
syslog(sysLogLevel[priority], "%s", buf); |
142 |
< |
#endif |
143 |
< |
if (ConfigLoggingEntry.use_logging) |
144 |
< |
write_log(buf); |
145 |
< |
} |
146 |
< |
|
147 |
< |
void |
148 |
< |
init_log(const char *filename) |
149 |
< |
{ |
150 |
< |
open_log(filename); |
151 |
< |
#ifdef USE_SYSLOG |
152 |
< |
openlog("ircd", LOG_PID | LOG_NDELAY, LOG_FACILITY); |
153 |
< |
#endif |
154 |
< |
#ifndef SYSLOG_USERS |
155 |
< |
eventAddIsh("user_log_resync", user_log_resync, NULL, 60); |
156 |
< |
#endif |
58 |
> |
while (++type < LOG_TYPE_LAST) |
59 |
> |
log_type_table[type].path[0] = '\0'; |
60 |
|
} |
61 |
|
|
62 |
|
void |
63 |
< |
reopen_log(const char *filename) |
63 |
> |
log_reopen_all(void) |
64 |
|
{ |
65 |
< |
if (logFile != NULL) |
163 |
< |
fbclose(logFile); |
164 |
< |
open_log(filename); |
165 |
< |
} |
65 |
> |
unsigned int type = 0; |
66 |
|
|
67 |
< |
void |
68 |
< |
set_log_level(const int level) |
69 |
< |
{ |
70 |
< |
if (L_ERROR < level && level <= L_DEBUG) |
71 |
< |
logLevel = level; |
72 |
< |
} |
67 |
> |
while (++type < LOG_TYPE_LAST) |
68 |
> |
{ |
69 |
> |
if (log_type_table[type].file) |
70 |
> |
{ |
71 |
> |
fclose(log_type_table[type].file); |
72 |
> |
log_type_table[type].file = NULL; |
73 |
> |
} |
74 |
|
|
75 |
< |
int |
76 |
< |
get_log_level(void) |
77 |
< |
{ |
177 |
< |
return(logLevel); |
75 |
> |
if (log_type_table[type].path[0]) |
76 |
> |
log_type_table[type].file = fopen(log_type_table[type].path, "a"); |
77 |
> |
} |
78 |
|
} |
79 |
|
|
80 |
< |
const char * |
81 |
< |
get_log_level_as_string(int level) |
80 |
> |
static int |
81 |
> |
log_exceed_size(unsigned int type) |
82 |
|
{ |
83 |
< |
if (level > L_DEBUG) |
184 |
< |
level = L_DEBUG; |
185 |
< |
else if (level < L_ERROR) |
186 |
< |
level = L_ERROR; |
83 |
> |
struct stat sb; |
84 |
|
|
85 |
< |
return(logLevelToString[level]); |
86 |
< |
} |
85 |
> |
if (!log_type_table[type].size) |
86 |
> |
return 0; |
87 |
|
|
88 |
< |
/* log_user_exit() |
89 |
< |
* |
193 |
< |
* inputs - pointer to connecting client |
194 |
< |
* output - NONE |
195 |
< |
* side effects - Current exiting client is logged to |
196 |
< |
* either SYSLOG or to file. |
197 |
< |
*/ |
198 |
< |
void |
199 |
< |
log_user_exit(struct Client *source_p) |
200 |
< |
{ |
201 |
< |
time_t on_for = CurrentTime - source_p->firsttime; |
202 |
< |
#ifdef SYSLOG_USERS |
203 |
< |
if (IsClient(source_p)) |
204 |
< |
{ |
205 |
< |
ilog(L_INFO, "%s (%3u:%02u:%02u): %s!%s@%s %llu/%llu", |
206 |
< |
myctime(source_p->firsttime), (unsigned int)(on_for / 3600), |
207 |
< |
(unsigned int)((on_for % 3600)/60), (unsigned int)(on_for % 60), |
208 |
< |
source_p->name, source_p->username, source_p->host, |
209 |
< |
source_p->localClient->send.bytes>>10, |
210 |
< |
source_p->localClient->recv.bytes>>10); |
211 |
< |
} |
212 |
< |
#else |
213 |
< |
{ |
214 |
< |
char linebuf[BUFSIZ]; |
88 |
> |
if (stat(log_type_table[type].path, &sb) < 0) |
89 |
> |
return -1; |
90 |
|
|
91 |
< |
/* |
217 |
< |
* This conditional makes the logfile active only after |
218 |
< |
* it's been created - thus logging can be turned off by |
219 |
< |
* removing the file. |
220 |
< |
* -Taner |
221 |
< |
*/ |
222 |
< |
if (IsClient(source_p)) |
223 |
< |
{ |
224 |
< |
if (user_log_fb == NULL) |
225 |
< |
{ |
226 |
< |
if ((ConfigLoggingEntry.userlog[0] != '\0') && |
227 |
< |
(user_log_fb = fbopen(ConfigLoggingEntry.userlog, "r")) != NULL) |
228 |
< |
{ |
229 |
< |
fbclose(user_log_fb); |
230 |
< |
user_log_fb = fbopen(ConfigLoggingEntry.userlog, "a"); |
231 |
< |
} |
232 |
< |
} |
233 |
< |
|
234 |
< |
if (user_log_fb != NULL) |
235 |
< |
{ |
236 |
< |
size_t nbytes = ircsprintf(linebuf, |
237 |
< |
"%s (%3u:%02u:%02u): %s!%s@%s %llu/%llu\n", |
238 |
< |
myctime(source_p->firsttime), |
239 |
< |
(unsigned int)(on_for / 3600), |
240 |
< |
(unsigned int)((on_for % 3600)/60), |
241 |
< |
(unsigned int)(on_for % 60), |
242 |
< |
source_p->name, source_p->username, source_p->host, |
243 |
< |
source_p->localClient->send.bytes>>10, |
244 |
< |
source_p->localClient->recv.bytes>>10); |
245 |
< |
fbputs(linebuf, user_log_fb, nbytes); |
246 |
< |
} |
247 |
< |
} |
248 |
< |
} |
249 |
< |
#endif |
91 |
> |
return (size_t)sb.st_size > log_type_table[type].size; |
92 |
|
} |
93 |
|
|
252 |
– |
#ifndef SYSLOG_USERS |
253 |
– |
/* user_log_resync() |
254 |
– |
* |
255 |
– |
* inputs - NONE |
256 |
– |
* output - NONE |
257 |
– |
* side effects - |
258 |
– |
*/ |
94 |
|
static void |
95 |
< |
user_log_resync(void *notused) |
95 |
> |
log_write(enum log_type type, const char *message) |
96 |
|
{ |
97 |
< |
if (user_log_fb != NULL) |
98 |
< |
{ |
99 |
< |
fbclose(user_log_fb); |
100 |
< |
user_log_fb = NULL; |
101 |
< |
} |
97 |
> |
char buf[IRCD_BUFSIZE] = ""; |
98 |
> |
|
99 |
> |
strftime(buf, sizeof(buf), "[%FT%H:%M:%S%z]", localtime(&CurrentTime)); |
100 |
> |
|
101 |
> |
fprintf(log_type_table[type].file, "%s %s\n", buf, message); |
102 |
> |
fflush(log_type_table[type].file); |
103 |
|
} |
268 |
– |
#endif |
104 |
|
|
270 |
– |
/* log_oper_action() |
271 |
– |
* |
272 |
– |
* inputs - type of oper log entry |
273 |
– |
* - pointer to oper |
274 |
– |
* - const char *pattern == format string |
275 |
– |
* - var args for format string |
276 |
– |
* output - none |
277 |
– |
* side effects - corresponding log is written to, if its present. |
278 |
– |
* |
279 |
– |
* rewritten sept 5 2005 - Dianora |
280 |
– |
*/ |
105 |
|
void |
106 |
< |
log_oper_action(int log_type, const struct Client *source_p, |
283 |
< |
const char *pattern, ...) |
106 |
> |
ilog(enum log_type type, const char *fmt, ...) |
107 |
|
{ |
108 |
+ |
char buf[LOG_BUFSIZE] = ""; |
109 |
|
va_list args; |
286 |
– |
char linebuf[IRCD_BUFSIZE]; |
287 |
– |
FBFILE *log_fb; |
288 |
– |
char *logfile; |
289 |
– |
const char *log_message; |
290 |
– |
size_t nbytes; |
291 |
– |
size_t n_preamble; |
292 |
– |
char *p; |
110 |
|
|
111 |
< |
switch(log_type) |
295 |
< |
{ |
296 |
< |
case LOG_OPER_TYPE: |
297 |
< |
logfile = ConfigLoggingEntry.operlog; |
298 |
< |
log_message = "OPER"; |
299 |
< |
break; |
300 |
< |
case LOG_FAILED_OPER_TYPE: |
301 |
< |
logfile = ConfigLoggingEntry.failed_operlog; |
302 |
< |
log_message = "FAILED OPER"; |
303 |
< |
break; |
304 |
< |
case LOG_KLINE_TYPE: |
305 |
< |
logfile = ConfigLoggingEntry.klinelog; |
306 |
< |
log_message = "KLINE"; |
307 |
< |
break; |
308 |
< |
case LOG_RKLINE_TYPE: |
309 |
< |
logfile = ConfigLoggingEntry.klinelog; |
310 |
< |
log_message = "RKLINE"; |
311 |
< |
break; |
312 |
< |
case LOG_DLINE_TYPE: |
313 |
< |
logfile = ConfigLoggingEntry.klinelog; |
314 |
< |
log_message = "DLINE"; |
315 |
< |
break; |
316 |
< |
case LOG_TEMP_DLINE_TYPE: |
317 |
< |
logfile = ConfigLoggingEntry.klinelog; |
318 |
< |
log_message = "TEMP DLINE"; |
319 |
< |
break; |
320 |
< |
case LOG_TEMP_KLINE_TYPE: |
321 |
< |
logfile = ConfigLoggingEntry.klinelog; |
322 |
< |
log_message = "TEMP KLINE"; |
323 |
< |
break; |
324 |
< |
case LOG_GLINE_TYPE: |
325 |
< |
logfile = ConfigLoggingEntry.glinelog; |
326 |
< |
log_message = "GLINE"; |
327 |
< |
break; |
328 |
< |
case LOG_KILL_TYPE: |
329 |
< |
logfile = ConfigLoggingEntry.killlog; |
330 |
< |
log_message = "KILL"; |
331 |
< |
break; |
332 |
< |
case LOG_IOERR_TYPE: |
333 |
< |
logfile = ConfigLoggingEntry.ioerrlog; |
334 |
< |
log_message = "IO ERR"; |
335 |
< |
break; |
336 |
< |
default: |
111 |
> |
if (!log_type_table[type].file || !ConfigLog.use_logging) |
112 |
|
return; |
338 |
– |
} |
113 |
|
|
114 |
< |
if (*logfile == '\0') |
115 |
< |
return; |
114 |
> |
va_start(args, fmt); |
115 |
> |
vsnprintf(buf, sizeof(buf), fmt, args); |
116 |
> |
va_end(args); |
117 |
|
|
118 |
< |
p = linebuf; |
344 |
< |
if (source_p != NULL) |
345 |
< |
{ |
346 |
< |
n_preamble = ircsprintf(linebuf, "%s %s by (%s!%s@%s) :", |
347 |
< |
myctime(CurrentTime), log_message, |
348 |
< |
source_p->name, source_p->username, source_p->host); |
118 |
> |
log_write(type, buf); |
119 |
|
|
120 |
< |
} |
121 |
< |
else |
352 |
< |
{ |
353 |
< |
n_preamble = ircsprintf(linebuf, "%s %s :", |
354 |
< |
myctime(CurrentTime), log_message); |
355 |
< |
} |
120 |
> |
if (log_exceed_size(type) <= 0) |
121 |
> |
return; |
122 |
|
|
123 |
< |
p += n_preamble; |
123 |
> |
snprintf(buf, sizeof(buf), "Rotating logfile %s", |
124 |
> |
log_type_table[type].path); |
125 |
> |
log_write(type, buf); |
126 |
> |
|
127 |
> |
fclose(log_type_table[type].file); |
128 |
> |
log_type_table[type].file = NULL; |
129 |
> |
|
130 |
> |
snprintf(buf, sizeof(buf), "%s.old", log_type_table[type].path); |
131 |
> |
unlink(buf); |
132 |
> |
rename(log_type_table[type].path, buf); |
133 |
|
|
134 |
< |
if ((log_fb = fbopen(logfile, "r")) != NULL) |
135 |
< |
{ |
361 |
< |
fbclose(log_fb); |
362 |
< |
log_fb = fbopen(logfile, "a"); |
363 |
< |
if (log_fb == NULL) |
364 |
< |
return; |
365 |
< |
va_start(args, pattern); |
366 |
< |
/* XXX add check for IRCD_BUFSIZE-(n_preamble+1) < 0 ? -db */ |
367 |
< |
nbytes = vsnprintf(p, IRCD_BUFSIZE-(n_preamble+1), pattern, args); |
368 |
< |
nbytes += n_preamble; |
369 |
< |
va_end(args); |
370 |
< |
fbputs(linebuf, log_fb, nbytes); |
371 |
< |
fbclose(log_fb); |
372 |
< |
} |
134 |
> |
log_set_file(type, log_type_table[type].size, log_type_table[type].path); |
135 |
> |
log_type_table[type].file = fopen(log_type_table[type].path, "a"); |
136 |
|
} |