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