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