1 |
/* |
2 |
* ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd) |
3 |
* |
4 |
* Copyright (c) 1997-2018 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 |
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 LogFile |
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 |
struct LogFile *log = &log_type_table[type]; |
47 |
|
48 |
strlcpy(log->path, path, sizeof(log->path)); |
49 |
log->size = size; |
50 |
|
51 |
if (type == LOG_TYPE_IRCD) |
52 |
log->file = fopen(log->path, "a"); |
53 |
} |
54 |
|
55 |
void |
56 |
log_del_all(void) |
57 |
{ |
58 |
unsigned int type = 0; |
59 |
|
60 |
while (++type < LOG_TYPE_LAST) |
61 |
log_type_table[type].path[0] = '\0'; |
62 |
} |
63 |
|
64 |
void |
65 |
log_reopen_all(void) |
66 |
{ |
67 |
unsigned int type = 0; |
68 |
|
69 |
while (++type < LOG_TYPE_LAST) |
70 |
{ |
71 |
struct LogFile *log = &log_type_table[type]; |
72 |
|
73 |
if (log->file) |
74 |
{ |
75 |
fclose(log->file); |
76 |
log->file = NULL; |
77 |
} |
78 |
|
79 |
if (log->path[0]) |
80 |
log->file = fopen(log->path, "a"); |
81 |
} |
82 |
} |
83 |
|
84 |
static int |
85 |
log_exceed_size(struct LogFile *log) |
86 |
{ |
87 |
struct stat sb; |
88 |
|
89 |
if (log->size == 0) |
90 |
return 0; |
91 |
|
92 |
if (stat(log->path, &sb) < 0) |
93 |
return 0; |
94 |
|
95 |
return (size_t)sb.st_size > log->size; |
96 |
} |
97 |
|
98 |
static void |
99 |
log_write(struct LogFile *log, const char *message) |
100 |
{ |
101 |
fprintf(log->file, "[%s] %s\n", date_iso8601(0), message); |
102 |
fflush(log->file); |
103 |
} |
104 |
|
105 |
void |
106 |
ilog(enum log_type type, const char *fmt, ...) |
107 |
{ |
108 |
struct LogFile *log = &log_type_table[type]; |
109 |
char buf[LOG_BUFSIZE] = ""; |
110 |
va_list args; |
111 |
|
112 |
if (log->file == NULL || ConfigLog.use_logging == 0) |
113 |
return; |
114 |
|
115 |
va_start(args, fmt); |
116 |
vsnprintf(buf, sizeof(buf), fmt, args); |
117 |
va_end(args); |
118 |
|
119 |
log_write(log, buf); |
120 |
|
121 |
if (log_exceed_size(log) == 0) |
122 |
return; |
123 |
|
124 |
snprintf(buf, sizeof(buf), "Rotating logfile %s", log->path); |
125 |
log_write(log, buf); |
126 |
|
127 |
fclose(log->file); |
128 |
log->file = NULL; |
129 |
|
130 |
snprintf(buf, sizeof(buf), "%s.old", log->path); |
131 |
unlink(buf); |
132 |
rename(log->path, buf); |
133 |
|
134 |
log->file = fopen(log->path, "a"); |
135 |
} |