]> jfr.im git - solanum.git/blob - ircd/logger.c
Support more human friendly k/d/x-line duration format
[solanum.git] / ircd / logger.c
1 /*
2 * Solanum: a slightly advanced ircd
3 *
4 * Copyright (C) 2003 Lee H <lee@leeh.co.uk>
5 * Copyright (C) 2003-2005 ircd-ratbox development team
6 * Copyright (C) 2008 Ariadne Conill <ariadne@dereferenced.org>
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
10 * met:
11 *
12 * 1.Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
14 *
15 * 2.Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * 3.The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
26 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
30 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
31 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 */
34
35 #include "stdinc.h"
36 #include "ircd_defs.h"
37 #include "logger.h"
38 #include "s_conf.h"
39 #include "send.h"
40 #include "client.h"
41 #include "s_serv.h"
42
43 static FILE *log_main;
44 static FILE *log_user;
45 static FILE *log_fuser;
46 static FILE *log_oper;
47 static FILE *log_foper;
48 static FILE *log_server;
49 static FILE *log_kill;
50 static FILE *log_kline;
51 static FILE *log_operspy;
52 static FILE *log_ioerror;
53
54 struct log_struct
55 {
56 char **name;
57 FILE **logfile;
58 };
59
60 static struct log_struct log_table[LAST_LOGFILE] =
61 {
62 { NULL, &log_main },
63 { &ConfigFileEntry.fname_userlog, &log_user },
64 { &ConfigFileEntry.fname_fuserlog, &log_fuser },
65 { &ConfigFileEntry.fname_operlog, &log_oper },
66 { &ConfigFileEntry.fname_foperlog, &log_foper },
67 { &ConfigFileEntry.fname_serverlog, &log_server },
68 { &ConfigFileEntry.fname_killlog, &log_kill },
69 { &ConfigFileEntry.fname_klinelog, &log_kline },
70 { &ConfigFileEntry.fname_operspylog, &log_operspy },
71 { &ConfigFileEntry.fname_ioerrorlog, &log_ioerror }
72 };
73
74 static void
75 verify_logfile_access(const char *filename)
76 {
77 char *dirname, *d;
78 char buf[512];
79 d = rb_dirname(filename);
80 dirname = LOCAL_COPY(d);
81 rb_free(d);
82
83 if(access(dirname, F_OK) == -1)
84 {
85 snprintf(buf, sizeof(buf), "WARNING: Unable to access logfile %s - parent directory %s does not exist", filename, dirname);
86 if(testing_conf || server_state_foreground)
87 fprintf(stderr, "%s\n", buf);
88 sendto_realops_snomask(SNO_GENERAL, L_ALL, "%s", buf);
89 return;
90 }
91
92 if(access(filename, F_OK) == -1)
93 {
94 if(access(dirname, W_OK) == -1)
95 {
96 snprintf(buf, sizeof(buf), "WARNING: Unable to access logfile %s - access to parent directory %s failed: %s",
97 filename, dirname, strerror(errno));
98 if(testing_conf || server_state_foreground)
99 fprintf(stderr, "%s\n", buf);
100 sendto_realops_snomask(SNO_GENERAL, L_ALL, "%s", buf);
101 }
102 return;
103 }
104
105 if(access(filename, W_OK) == -1)
106 {
107 snprintf(buf, sizeof(buf), "WARNING: Access denied for logfile %s: %s", filename, strerror(errno));
108 if(testing_conf || server_state_foreground)
109 fprintf(stderr, "%s\n", buf);
110 sendto_realops_snomask(SNO_GENERAL, L_ALL, "%s", buf);
111 return;
112 }
113 return;
114 }
115
116 void
117 init_main_logfile(void)
118 {
119 verify_logfile_access(logFileName);
120 if(log_main == NULL)
121 {
122 log_main = fopen(logFileName, "a");
123 }
124 }
125
126 void
127 open_logfiles(void)
128 {
129 int i;
130
131 close_logfiles();
132
133 log_main = fopen(logFileName, "a");
134
135 /* log_main is handled above, so just do the rest */
136 for(i = 1; i < LAST_LOGFILE; i++)
137 {
138 /* reopen those with paths */
139 if(!EmptyString(*log_table[i].name))
140 {
141 verify_logfile_access(*log_table[i].name);
142 *log_table[i].logfile = fopen(*log_table[i].name, "a");
143 }
144 }
145 }
146
147 void
148 close_logfiles(void)
149 {
150 int i;
151
152 if(log_main != NULL)
153 fclose(log_main);
154
155 /* log_main is handled above, so just do the rest */
156 for(i = 1; i < LAST_LOGFILE; i++)
157 {
158 if(*log_table[i].logfile != NULL)
159 {
160 fclose(*log_table[i].logfile);
161 *log_table[i].logfile = NULL;
162 }
163 }
164 }
165
166 void
167 ilog(ilogfile dest, const char *format, ...)
168 {
169 FILE *logfile = *log_table[dest].logfile;
170 char buf[BUFSIZE];
171 char buf2[MAX_DATE_STRING + 1 + BUFSIZE + 1];
172 va_list args;
173
174 if(logfile == NULL)
175 return;
176
177 va_start(args, format);
178 vsnprintf(buf, sizeof(buf), format, args);
179 va_end(args);
180
181 snprintf(buf2, sizeof(buf2), "%s %s\n",
182 smalldate(rb_current_time()), buf);
183
184 if(fputs(buf2, logfile) < 0)
185 {
186 fclose(logfile);
187 *log_table[dest].logfile = NULL;
188 return;
189 }
190
191 fflush(logfile);
192 }
193
194 static void
195 _iprint(const char *domain, const char *buf)
196 {
197 if (domain == NULL || buf == NULL)
198 return;
199
200 fprintf(stderr, "%8s: %s\n", domain, buf);
201 }
202
203 void
204 idebug(const char *format, ...)
205 {
206 #ifndef NDEBUG
207 char buf[BUFSIZE];
208 va_list args;
209
210 va_start(args, format);
211 vsnprintf(buf, sizeof(buf), format, args);
212 va_end(args);
213
214 _iprint("debug", buf);
215
216 ilog(L_MAIN, "%s", buf);
217 #endif
218 }
219
220 void
221 inotice(const char *format, ...)
222 {
223 char buf[BUFSIZE];
224 va_list args;
225
226 va_start(args, format);
227 vsnprintf(buf, sizeof(buf), format, args);
228 va_end(args);
229
230 _iprint("notice", buf);
231
232 ilog(L_MAIN, "%s", buf);
233 }
234
235 void
236 iwarn(const char *format, ...)
237 {
238 char buf[BUFSIZE];
239 va_list args;
240
241 va_start(args, format);
242 vsnprintf(buf, sizeof(buf), format, args);
243 va_end(args);
244
245 _iprint("warning", buf);
246
247 ilog(L_MAIN, "%s", buf);
248 }
249
250 void
251 ierror(const char *format, ...)
252 {
253 char buf[BUFSIZE];
254 va_list args;
255
256 va_start(args, format);
257 vsnprintf(buf, sizeof(buf), format, args);
258 va_end(args);
259
260 _iprint("error", buf);
261
262 ilog(L_MAIN, "%s", buf);
263 }
264
265 void
266 report_operspy(struct Client *source_p, const char *token, const char *arg)
267 {
268 /* if its not my client its already propagated */
269 if(MyClient(source_p))
270 sendto_match_servs(source_p, "*", CAP_ENCAP, NOCAPS,
271 "ENCAP * OPERSPY %s %s",
272 token, arg ? arg : "");
273
274 sendto_realops_snomask(SNO_OPERSPY,
275 ConfigFileEntry.operspy_admin_only ? L_ADMIN : L_ALL,
276 "OPERSPY %s %s %s",
277 get_oper_name(source_p), token,
278 arg ? arg : "");
279
280 ilog(L_OPERSPY, "OPERSPY %s %s %s",
281 get_oper_name(source_p), token, arg ? arg : "");
282 }
283
284 const char *
285 smalldate(time_t ltime)
286 {
287 static char buf[MAX_DATE_STRING];
288 struct tm *lt;
289
290 lt = localtime(&ltime);
291
292 snprintf(buf, sizeof(buf), "%d/%d/%d %02d.%02d",
293 lt->tm_year + 1900, lt->tm_mon + 1,
294 lt->tm_mday, lt->tm_hour, lt->tm_min);
295
296 return buf;
297 }
298
299 void
300 ilog_error(const char *error)
301 {
302 int e;
303 const char *errstr;
304
305 e = errno;
306 errstr = strerror(e);
307
308 ilog(L_IOERROR, "%s: %d (%s)", error, e, errstr);
309 sendto_realops_snomask(SNO_DEBUG, L_ALL, "%s: %d (%s)",
310 error, e, errstr);
311 }