1 |
/* |
2 |
* ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd) |
3 |
* |
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 |
8 |
* the Free Software Foundation; either version 2 of the License, or |
9 |
* (at your option) any later version. |
10 |
* |
11 |
* This program is distributed in the hope that it will be useful, |
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
* GNU General Public License for more details. |
15 |
* |
16 |
* You should have received a copy of the GNU General Public License |
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 |
20 |
*/ |
21 |
|
22 |
/*! \file log.c |
23 |
* \brief Logger functions. |
24 |
* \version $Id$ |
25 |
*/ |
26 |
|
27 |
#include "stdinc.h" |
28 |
#include "log.h" |
29 |
#include "irc_string.h" |
30 |
#include "ircd.h" |
31 |
#include "conf.h" |
32 |
#include "misc.h" |
33 |
|
34 |
|
35 |
static struct |
36 |
{ |
37 |
char path[HYB_PATH_MAX + 1]; |
38 |
size_t size; |
39 |
FILE *file; |
40 |
} log_type_table[LOG_TYPE_LAST]; |
41 |
|
42 |
|
43 |
void |
44 |
log_set_file(enum log_type type, size_t size, const char *path) |
45 |
{ |
46 |
strlcpy(log_type_table[type].path, path, sizeof(log_type_table[type].path)); |
47 |
log_type_table[type].size = size; |
48 |
|
49 |
if (type == LOG_TYPE_IRCD) |
50 |
log_type_table[type].file = fopen(log_type_table[type].path, "a"); |
51 |
} |
52 |
|
53 |
void |
54 |
log_del_all(void) |
55 |
{ |
56 |
unsigned int type = 0; |
57 |
|
58 |
while (++type < LOG_TYPE_LAST) |
59 |
log_type_table[type].path[0] = '\0'; |
60 |
} |
61 |
|
62 |
void |
63 |
log_reopen_all(void) |
64 |
{ |
65 |
unsigned int type = 0; |
66 |
|
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 |
if (log_type_table[type].path[0]) |
76 |
log_type_table[type].file = fopen(log_type_table[type].path, "a"); |
77 |
} |
78 |
} |
79 |
|
80 |
static int |
81 |
log_exceed_size(unsigned int type) |
82 |
{ |
83 |
struct stat sb; |
84 |
|
85 |
if (!log_type_table[type].size) |
86 |
return 0; |
87 |
|
88 |
if (stat(log_type_table[type].path, &sb) < 0) |
89 |
return -1; |
90 |
|
91 |
return (size_t)sb.st_size > log_type_table[type].size; |
92 |
} |
93 |
|
94 |
static void |
95 |
log_write(enum log_type type, const char *message) |
96 |
{ |
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 |
} |
104 |
|
105 |
void |
106 |
ilog(enum log_type type, const char *fmt, ...) |
107 |
{ |
108 |
char buf[LOG_BUFSIZE] = ""; |
109 |
va_list args; |
110 |
|
111 |
if (!log_type_table[type].file || !ConfigLoggingEntry.use_logging) |
112 |
return; |
113 |
|
114 |
va_start(args, fmt); |
115 |
vsnprintf(buf, sizeof(buf), fmt, args); |
116 |
va_end(args); |
117 |
|
118 |
log_write(type, buf); |
119 |
|
120 |
if (log_exceed_size(type) <= 0) |
121 |
return; |
122 |
|
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 |
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 |
} |