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 "ircd.h" |
31 |
#include "conf.h" |
32 |
#include "send.h" |
33 |
#include "s_misc.h" |
34 |
|
35 |
|
36 |
static struct { |
37 |
char path[PATH_MAX + 1]; |
38 |
size_t size; |
39 |
FBFILE *file; |
40 |
} log_type_table[LOG_TYPE_LAST]; |
41 |
|
42 |
|
43 |
int |
44 |
log_add_file(enum log_type type, size_t size, const char *path) |
45 |
{ |
46 |
if (log_type_table[type].file) |
47 |
fbclose(log_type_table[type].file); |
48 |
|
49 |
strlcpy(log_type_table[type].path, path, sizeof(log_type_table[type].path)); |
50 |
log_type_table[type].size = size; |
51 |
|
52 |
return (log_type_table[type].file = fbopen(path, "a")) != NULL; |
53 |
} |
54 |
|
55 |
void |
56 |
log_close_all(void) |
57 |
{ |
58 |
unsigned int type = 0; |
59 |
|
60 |
while (++type < LOG_TYPE_LAST) |
61 |
{ |
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 |
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(enum log_type type, const char *message) |
87 |
{ |
88 |
char buf[LOG_BUFSIZE]; |
89 |
size_t nbytes = 0; |
90 |
|
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 |
fbputs(buf, log_type_table[type].file, nbytes); |
98 |
} |
99 |
|
100 |
void |
101 |
ilog(enum log_type type, const char *fmt, ...) |
102 |
{ |
103 |
char buf[LOG_BUFSIZE]; |
104 |
va_list args; |
105 |
|
106 |
if (log_type_table[type].file == NULL) |
107 |
return; |
108 |
|
109 |
va_start(args, fmt); |
110 |
vsnprintf(buf, sizeof(buf), fmt, args); |
111 |
va_end(args); |
112 |
|
113 |
if (ConfigLoggingEntry.use_logging) |
114 |
{ |
115 |
write_log(type, buf); |
116 |
|
117 |
if (log_exceed_size(type) <= 0) |
118 |
return; |
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 |
} |