1 |
adx |
30 |
/* |
2 |
michael |
2916 |
* ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd) |
3 |
adx |
30 |
* |
4 |
michael |
8752 |
* Copyright (c) 1997-2019 ircd-hybrid development team |
5 |
adx |
30 |
* |
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 |
michael |
4565 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 |
19 |
adx |
30 |
* USA |
20 |
|
|
*/ |
21 |
|
|
|
22 |
michael |
2916 |
/*! \file log.c |
23 |
|
|
* \brief Logger functions. |
24 |
|
|
* \version $Id$ |
25 |
|
|
*/ |
26 |
|
|
|
27 |
adx |
30 |
#include "stdinc.h" |
28 |
michael |
1309 |
#include "log.h" |
29 |
|
|
#include "conf.h" |
30 |
michael |
3347 |
#include "misc.h" |
31 |
michael |
8508 |
#include "memory.h" |
32 |
adx |
30 |
|
33 |
|
|
|
34 |
michael |
8510 |
static struct LogFile log_type_table[LOG_TYPE_LAST]; |
35 |
adx |
30 |
|
36 |
|
|
|
37 |
michael |
1831 |
void |
38 |
|
|
log_set_file(enum log_type type, size_t size, const char *path) |
39 |
adx |
30 |
{ |
40 |
michael |
8506 |
struct LogFile *log = &log_type_table[type]; |
41 |
adx |
30 |
|
42 |
michael |
8508 |
if (log->path) |
43 |
|
|
xfree(log->path); |
44 |
|
|
|
45 |
|
|
log->path = xstrdup(path); |
46 |
michael |
8506 |
log->size = size; |
47 |
|
|
|
48 |
michael |
1831 |
if (type == LOG_TYPE_IRCD) |
49 |
michael |
8506 |
log->file = fopen(log->path, "a"); |
50 |
michael |
1247 |
} |
51 |
|
|
|
52 |
|
|
void |
53 |
michael |
8510 |
log_free(struct LogFile *log) |
54 |
adx |
30 |
{ |
55 |
michael |
8510 |
xfree(log->path); |
56 |
|
|
log->path = NULL; |
57 |
|
|
} |
58 |
|
|
|
59 |
|
|
void |
60 |
|
|
log_reopen(struct LogFile *log) |
61 |
|
|
{ |
62 |
|
|
if (log->file) |
63 |
michael |
8508 |
{ |
64 |
michael |
8510 |
fclose(log->file); |
65 |
|
|
log->file = NULL; |
66 |
|
|
} |
67 |
adx |
30 |
|
68 |
michael |
8510 |
if (log->path) |
69 |
|
|
log->file = fopen(log->path, "a"); |
70 |
michael |
1831 |
} |
71 |
|
|
|
72 |
|
|
void |
73 |
michael |
8510 |
log_iterate(void (*func)(struct LogFile *)) |
74 |
michael |
1831 |
{ |
75 |
michael |
8510 |
/* type = 1 to skip LOG_TYPE_IRCD */ |
76 |
michael |
8508 |
for (unsigned int type = 1; type < LOG_TYPE_LAST; ++type) |
77 |
michael |
8510 |
func(&log_type_table[type]); |
78 |
adx |
30 |
} |
79 |
|
|
|
80 |
michael |
8660 |
static bool |
81 |
michael |
8506 |
log_exceed_size(struct LogFile *log) |
82 |
michael |
1248 |
{ |
83 |
|
|
struct stat sb; |
84 |
|
|
|
85 |
michael |
8506 |
if (log->size == 0) |
86 |
michael |
8660 |
return false; |
87 |
michael |
1248 |
|
88 |
michael |
8506 |
if (stat(log->path, &sb) < 0) |
89 |
michael |
8660 |
return false; |
90 |
michael |
1248 |
|
91 |
michael |
8506 |
return (size_t)sb.st_size > log->size; |
92 |
michael |
1248 |
} |
93 |
|
|
|
94 |
michael |
2916 |
static void |
95 |
michael |
8506 |
log_write(struct LogFile *log, const char *message) |
96 |
adx |
30 |
{ |
97 |
michael |
8506 |
fprintf(log->file, "[%s] %s\n", date_iso8601(0), message); |
98 |
|
|
fflush(log->file); |
99 |
adx |
30 |
} |
100 |
michael |
1325 |
|
101 |
adx |
30 |
void |
102 |
michael |
1319 |
ilog(enum log_type type, const char *fmt, ...) |
103 |
adx |
30 |
{ |
104 |
michael |
8506 |
struct LogFile *log = &log_type_table[type]; |
105 |
michael |
3215 |
char buf[LOG_BUFSIZE] = ""; |
106 |
adx |
30 |
va_list args; |
107 |
|
|
|
108 |
michael |
8506 |
if (log->file == NULL || ConfigLog.use_logging == 0) |
109 |
michael |
1319 |
return; |
110 |
adx |
30 |
|
111 |
|
|
va_start(args, fmt); |
112 |
michael |
1086 |
vsnprintf(buf, sizeof(buf), fmt, args); |
113 |
adx |
30 |
va_end(args); |
114 |
|
|
|
115 |
michael |
8506 |
log_write(log, buf); |
116 |
michael |
1248 |
|
117 |
michael |
8660 |
if (log_exceed_size(log) == false) |
118 |
michael |
1327 |
return; |
119 |
michael |
1248 |
|
120 |
michael |
8506 |
snprintf(buf, sizeof(buf), "Rotating logfile %s", log->path); |
121 |
|
|
log_write(log, buf); |
122 |
michael |
1248 |
|
123 |
michael |
8506 |
fclose(log->file); |
124 |
|
|
log->file = NULL; |
125 |
michael |
1327 |
|
126 |
michael |
8506 |
snprintf(buf, sizeof(buf), "%s.old", log->path); |
127 |
michael |
1327 |
unlink(buf); |
128 |
michael |
8506 |
rename(log->path, buf); |
129 |
michael |
1327 |
|
130 |
michael |
8506 |
log->file = fopen(log->path, "a"); |
131 |
michael |
1248 |
} |