]> jfr.im git - solanum.git/blame - src/logger.c
Automated merge with ssh://hg.atheme.org//hg/charybdis
[solanum.git] / src / logger.c
CommitLineData
212380e3 1/*
ba1a1399 2 * charybdis: an advanced Internet Relay Chat Daemon(ircd).
212380e3
AC
3 *
4 * Copyright (C) 2003 Lee H <lee@leeh.co.uk>
5 * Copyright (C) 2003-2005 ircd-ratbox development team
ba1a1399 6 * Copyright (C) 2008 William Pitcock <nenolod@sacredspiral.co.uk>
212380e3
AC
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.
ba1a1399 14 *
212380e3
AC
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.
ba1a1399 18 *
212380e3
AC
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 *
9b6ff0c8 34 * $Id: s_log.c 3209 2007-02-11 16:54:43Z jilles $
212380e3
AC
35 */
36
37#include "stdinc.h"
38#include "ircd_defs.h"
4016731b 39#include "logger.h"
212380e3 40#include "s_conf.h"
212380e3
AC
41#include "send.h"
42#include "client.h"
43#include "s_serv.h"
44
45static FILE *log_main;
46static FILE *log_user;
47static FILE *log_fuser;
48static FILE *log_oper;
49static FILE *log_foper;
50static FILE *log_server;
51static FILE *log_kill;
212380e3
AC
52static FILE *log_kline;
53static FILE *log_operspy;
54static FILE *log_ioerror;
55
56struct log_struct
57{
58 char **name;
59 FILE **logfile;
60};
61
62static struct log_struct log_table[LAST_LOGFILE] =
63{
64 { NULL, &log_main },
65 { &ConfigFileEntry.fname_userlog, &log_user },
66 { &ConfigFileEntry.fname_fuserlog, &log_fuser },
67 { &ConfigFileEntry.fname_operlog, &log_oper },
68 { &ConfigFileEntry.fname_foperlog, &log_foper },
69 { &ConfigFileEntry.fname_serverlog, &log_server },
70 { &ConfigFileEntry.fname_killlog, &log_kill },
71 { &ConfigFileEntry.fname_klinelog, &log_kline },
212380e3
AC
72 { &ConfigFileEntry.fname_operspylog, &log_operspy },
73 { &ConfigFileEntry.fname_ioerrorlog, &log_ioerror }
74};
75
76void
77init_main_logfile(void)
78{
79 if(log_main == NULL)
9b6ff0c8 80 log_main = fopen(logFileName, "a");
212380e3
AC
81}
82
83void
84open_logfiles(void)
85{
86 int i;
87
ea82a3ca 88 close_logfiles();
212380e3 89
9b6ff0c8 90 log_main = fopen(logFileName, "a");
212380e3
AC
91
92 /* log_main is handled above, so just do the rest */
93 for(i = 1; i < LAST_LOGFILE; i++)
94 {
ea82a3ca
VY
95 /* reopen those with paths */
96 if(!EmptyString(*log_table[i].name))
97 *log_table[i].logfile = fopen(*log_table[i].name, "a");
98 }
99}
100
101void
102close_logfiles(void)
103{
104 int i;
105
106 if(log_main != NULL)
107 fclose(log_main);
108
109 /* log_main is handled above, so just do the rest */
110 for(i = 1; i < LAST_LOGFILE; i++)
111 {
212380e3
AC
112 if(*log_table[i].logfile != NULL)
113 {
114 fclose(*log_table[i].logfile);
115 *log_table[i].logfile = NULL;
116 }
212380e3 117 }
ea82a3ca 118}
212380e3
AC
119
120void
121ilog(ilogfile dest, const char *format, ...)
122{
123 FILE *logfile = *log_table[dest].logfile;
124 char buf[BUFSIZE];
125 char buf2[BUFSIZE];
126 va_list args;
127
128 if(logfile == NULL)
129 return;
130
131 va_start(args, format);
6aae811e 132 rb_vsnprintf(buf, sizeof(buf), format, args);
212380e3
AC
133 va_end(args);
134
b2f0da88 135 rb_snprintf(buf2, sizeof(buf2), "%s %s\n", smalldate(), buf);
212380e3
AC
136
137 if(fputs(buf2, logfile) < 0)
138 {
139 fclose(logfile);
140 *log_table[dest].logfile = NULL;
141 }
142
143 fflush(logfile);
144}
145
146static void
ba1a1399 147_iprint(const char *domain, const char *buf)
212380e3
AC
148{
149 if (domain == NULL || buf == NULL)
150 return;
151
152 fprintf(stderr, "%8s: %s\n", domain, buf);
153}
154
155void
156inotice(const char *format, ...)
157{
158 char buf[BUFSIZE];
159 va_list args;
160
161 va_start(args, format);
6c528b8e 162 rb_vsnprintf(buf, sizeof(buf), format, args);
212380e3
AC
163 va_end(args);
164
165 _iprint("notice", buf);
166
167 ilog(L_MAIN, "%s", buf);
168}
169
170void
171iwarn(const char *format, ...)
172{
173 char buf[BUFSIZE];
174 va_list args;
175
176 va_start(args, format);
6c528b8e 177 rb_vsnprintf(buf, sizeof(buf), format, args);
212380e3
AC
178 va_end(args);
179
180 _iprint("warning", buf);
181
182 ilog(L_MAIN, "%s", buf);
183}
184
185void
186ierror(const char *format, ...)
187{
188 char buf[BUFSIZE];
189 va_list args;
190
191 va_start(args, format);
6c528b8e 192 rb_vsnprintf(buf, sizeof(buf), format, args);
212380e3
AC
193 va_end(args);
194
195 _iprint("error", buf);
196
197 ilog(L_MAIN, "%s", buf);
198}
199
200void
201report_operspy(struct Client *source_p, const char *token, const char *arg)
202{
203 /* if its not my client its already propagated */
204 if(MyClient(source_p))
205 sendto_match_servs(source_p, "*", CAP_ENCAP, NOCAPS,
206 "ENCAP * OPERSPY %s %s",
207 token, arg ? arg : "");
208
209 sendto_realops_snomask(SNO_OPERSPY,
210 ConfigFileEntry.operspy_admin_only ? L_ADMIN : L_ALL,
211 "OPERSPY %s %s %s",
212 get_oper_name(source_p), token,
213 arg ? arg : "");
214
215 ilog(L_OPERSPY, "OPERSPY %s %s %s",
216 get_oper_name(source_p), token, arg ? arg : "");
217}
218
219const char *
220smalldate(void)
221{
222 static char buf[MAX_DATE_STRING];
223 struct tm *lt;
e3354945 224 time_t ltime = rb_current_time();
212380e3
AC
225
226 lt = localtime(&ltime);
227
b2f0da88 228 rb_snprintf(buf, sizeof(buf), "%d/%d/%d %02d.%02d",
212380e3
AC
229 lt->tm_year + 1900, lt->tm_mon + 1,
230 lt->tm_mday, lt->tm_hour, lt->tm_min);
231
232 return buf;
233}
234
212380e3 235void
ba1a1399 236ilog_error(const char *error)
212380e3 237{
825ddf13 238 ilog(L_IOERROR, "%s: %d (%s)", error, errno, strerror(errno));
212380e3 239
825ddf13 240 sendto_realops_snomask(SNO_DEBUG, L_ALL, "%s: %d (%s)", error, errno, strerror(errno));
212380e3 241}