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 "irc_string.h" |
29 |
#include "ircd.h" |
30 |
#include "conf.h" |
31 |
#include "send.h" |
32 |
#include "s_misc.h" |
33 |
|
34 |
|
35 |
static struct { |
36 |
char path[PATH_MAX + 1]; |
37 |
size_t size; |
38 |
FILE *file; |
39 |
} log_type_table[LOG_TYPE_LAST]; |
40 |
|
41 |
|
42 |
int |
43 |
log_add_file(enum log_type type, size_t size, const char *path) |
44 |
{ |
45 |
if (log_type_table[type].file) |
46 |
fclose(log_type_table[type].file); |
47 |
|
48 |
strlcpy(log_type_table[type].path, path, sizeof(log_type_table[type].path)); |
49 |
log_type_table[type].size = size; |
50 |
|
51 |
return (log_type_table[type].file = fopen(path, "a")) != NULL; |
52 |
} |
53 |
|
54 |
void |
55 |
log_close_all(void) |
56 |
{ |
57 |
unsigned int type = 0; |
58 |
|
59 |
while (++type < LOG_TYPE_LAST) |
60 |
{ |
61 |
if (log_type_table[type].file == NULL) |
62 |
continue; |
63 |
|
64 |
fclose(log_type_table[type].file); |
65 |
log_type_table[type].file = NULL; |
66 |
} |
67 |
} |
68 |
|
69 |
static int |
70 |
log_exceed_size(unsigned int type) |
71 |
{ |
72 |
struct stat sb; |
73 |
|
74 |
if (!log_type_table[type].size) |
75 |
return 0; |
76 |
|
77 |
if (stat(log_type_table[type].path, &sb) < 0) |
78 |
return -1; |
79 |
|
80 |
return (size_t)sb.st_size > log_type_table[type].size; |
81 |
} |
82 |
|
83 |
|
84 |
static void |
85 |
write_log(enum log_type type, const char *message) |
86 |
{ |
87 |
char buf[LOG_BUFSIZE]; |
88 |
size_t nbytes = 0; |
89 |
struct tm *lt = localtime(&CurrentTime); |
90 |
|
91 |
nbytes = strftime(buf, sizeof(buf), "[%FT%H:%M:%S%z] ", lt); |
92 |
snprintf(buf+nbytes, sizeof(buf)-nbytes, "%s\n", message); |
93 |
|
94 |
fputs(buf, log_type_table[type].file); |
95 |
fflush(log_type_table[type].file); |
96 |
} |
97 |
|
98 |
void |
99 |
ilog(enum log_type type, const char *fmt, ...) |
100 |
{ |
101 |
char buf[LOG_BUFSIZE]; |
102 |
va_list args; |
103 |
|
104 |
if (log_type_table[type].file == NULL) |
105 |
return; |
106 |
|
107 |
va_start(args, fmt); |
108 |
vsnprintf(buf, sizeof(buf), fmt, args); |
109 |
va_end(args); |
110 |
|
111 |
if (ConfigLoggingEntry.use_logging) |
112 |
{ |
113 |
write_log(type, buf); |
114 |
|
115 |
if (log_exceed_size(type) <= 0) |
116 |
return; |
117 |
|
118 |
snprintf(buf, sizeof(buf), "Rotating logfile %s", |
119 |
log_type_table[type].path); |
120 |
write_log(type, buf); |
121 |
fclose(log_type_table[type].file); |
122 |
log_type_table[type].file = NULL; |
123 |
|
124 |
snprintf(buf, sizeof(buf), "%s.old", log_type_table[type].path); |
125 |
unlink(buf); |
126 |
rename(log_type_table[type].path, buf); |
127 |
log_add_file(type, log_type_table[type].size, log_type_table[type].path); |
128 |
} |
129 |
} |