1 |
/* |
2 |
* ircd-hybrid: an advanced Internet Relay Chat Daemon(ircd). |
3 |
* log.c: Logger functions. |
4 |
* |
5 |
* Copyright (C) 2002 by the past and present ircd coders, and others. |
6 |
* |
7 |
* This program is free software; you can redistribute it and/or modify |
8 |
* it under the terms of the GNU General Public License as published by |
9 |
* the Free Software Foundation; either version 2 of the License, or |
10 |
* (at your option) any later version. |
11 |
* |
12 |
* This program is distributed in the hope that it will be useful, |
13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
* GNU General Public License for more details. |
16 |
* |
17 |
* You should have received a copy of the GNU General Public License |
18 |
* along with this program; if not, write to the Free Software |
19 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
20 |
* USA |
21 |
* |
22 |
* $Id$ |
23 |
*/ |
24 |
|
25 |
#include "stdinc.h" |
26 |
|
27 |
#include "log.h" |
28 |
#include "fileio.h" |
29 |
#include "irc_string.h" |
30 |
#include "sprintf_irc.h" |
31 |
#include "ircd.h" |
32 |
#include "s_misc.h" |
33 |
#include "conf.h" |
34 |
#include "memory.h" |
35 |
#include "send.h" |
36 |
|
37 |
/* some older syslogs would overflow at 2024 */ |
38 |
#define LOG_BUFSIZE 2000 |
39 |
|
40 |
static struct { |
41 |
char path[PATH_MAX + 1]; |
42 |
size_t size; |
43 |
FBFILE *file; |
44 |
} log_type_table[LOG_TYPE_LAST]; |
45 |
|
46 |
|
47 |
int |
48 |
log_add_file(unsigned int type, size_t size, const char *path) |
49 |
{ |
50 |
if (log_type_table[type].file) |
51 |
fbclose(log_type_table[type].file); |
52 |
|
53 |
strlcpy(log_type_table[type].path, path, sizeof(log_type_table[type].path)); |
54 |
log_type_table[type].size = size; |
55 |
|
56 |
return (log_type_table[type].file = fbopen(path, "a")) != NULL; |
57 |
} |
58 |
|
59 |
void |
60 |
log_close_all(void) |
61 |
{ |
62 |
unsigned int type = 0; |
63 |
|
64 |
while (++type < LOG_TYPE_LAST) |
65 |
{ |
66 |
if (log_type_table[type].file == NULL) |
67 |
continue; |
68 |
|
69 |
fbclose(log_type_table[type].file); |
70 |
log_type_table[type].file = NULL; |
71 |
} |
72 |
} |
73 |
|
74 |
static int |
75 |
log_exceed_size(unsigned int type) |
76 |
{ |
77 |
struct stat sb; |
78 |
|
79 |
if (!log_type_table[type].size) |
80 |
return 0; |
81 |
|
82 |
if (stat(log_type_table[type].path, &sb) < 0) |
83 |
return -1; |
84 |
|
85 |
return (size_t)sb.st_size > log_type_table[type].size; |
86 |
} |
87 |
|
88 |
|
89 |
static void |
90 |
write_log(unsigned int type, const char *message) |
91 |
{ |
92 |
char buf[LOG_BUFSIZE]; |
93 |
size_t nbytes = 0; |
94 |
|
95 |
if (log_type_table[type].file == NULL) |
96 |
return; |
97 |
|
98 |
if (ConfigLoggingEntry.timestamp) |
99 |
nbytes = snprintf(buf, sizeof(buf), "[%s] %s\n", |
100 |
smalldate(CurrentTime), message); |
101 |
else |
102 |
nbytes = snprintf(buf, sizeof(buf), "%s\n", message); |
103 |
|
104 |
fbputs(buf, log_type_table[type].file, nbytes); |
105 |
} |
106 |
|
107 |
void |
108 |
ilog(unsigned int type, const char *fmt, ...) |
109 |
{ |
110 |
char buf[LOG_BUFSIZE]; |
111 |
va_list args; |
112 |
|
113 |
assert(fmt); |
114 |
assert(type >= 0 && type < LOG_TYPE_LAST); |
115 |
|
116 |
va_start(args, fmt); |
117 |
vsnprintf(buf, sizeof(buf), fmt, args); |
118 |
va_end(args); |
119 |
|
120 |
if (ConfigLoggingEntry.use_logging) |
121 |
{ |
122 |
write_log(type, buf); |
123 |
|
124 |
if (log_exceed_size(type) <= 0) |
125 |
return; |
126 |
|
127 |
snprintf(buf, sizeof(buf), "Rotating logfile %s", |
128 |
log_type_table[type].path); |
129 |
write_log(type, buf); |
130 |
fbclose(log_type_table[type].file); |
131 |
log_type_table[type].file = NULL; |
132 |
|
133 |
snprintf(buf, sizeof(buf), "%s.old", log_type_table[type].path); |
134 |
unlink(buf); |
135 |
rename(log_type_table[type].path, buf); |
136 |
log_add_file(type, log_type_table[type].size, log_type_table[type].path); |
137 |
} |
138 |
} |