Restore stats_spy hook that was removed in commit 401f2454671ca233e35b0e6e4f3fa4c43cd...
[seven-1.x.git] / src / s_log.c
blob9a03575e4e8f19f2b62358ed6d4ffe24257a449b
1 /*
2 * ircd-ratbox: an advanced Internet Relay Chat Daemon(ircd).
4 * Copyright (C) 2003 Lee H <lee@leeh.co.uk>
5 * Copyright (C) 2003-2005 ircd-ratbox development team
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
11 * 1.Redistributions of source code must retain the above copyright notice,
12 * this list of conditions and the following disclaimer.
13 * 2.Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3.The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
23 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
28 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include "stdinc.h"
33 #include "ircd_defs.h"
34 #include "s_log.h"
35 #include "s_conf.h"
36 #include "sprintf_irc.h"
37 #include "send.h"
38 #include "client.h"
39 #include "s_serv.h"
41 static FILE *log_main;
42 static FILE *log_user;
43 static FILE *log_fuser;
44 static FILE *log_oper;
45 static FILE *log_foper;
46 static FILE *log_server;
47 static FILE *log_kill;
48 static FILE *log_kline;
49 static FILE *log_ioerror;
51 struct log_struct
53 char **name;
54 FILE **logfile;
57 static struct log_struct log_table[LAST_LOGFILE] =
59 { NULL, &log_main },
60 { &ConfigFileEntry.fname_userlog, &log_user },
61 { &ConfigFileEntry.fname_fuserlog, &log_fuser },
62 { &ConfigFileEntry.fname_operlog, &log_oper },
63 { &ConfigFileEntry.fname_foperlog, &log_foper },
64 { &ConfigFileEntry.fname_serverlog, &log_server },
65 { &ConfigFileEntry.fname_killlog, &log_kill },
66 { &ConfigFileEntry.fname_klinelog, &log_kline },
67 { &ConfigFileEntry.fname_ioerrorlog, &log_ioerror }
70 void
71 init_main_logfile(void)
73 if(log_main == NULL)
74 log_main = fopen(LPATH, "a");
77 void
78 open_logfiles(void)
80 int i;
82 if(log_main != NULL)
83 fclose(log_main);
85 log_main = fopen(LPATH, "a");
87 /* log_main is handled above, so just do the rest */
88 for(i = 1; i < LAST_LOGFILE; i++)
90 /* close open logfiles */
91 if(*log_table[i].logfile != NULL)
93 fclose(*log_table[i].logfile);
94 *log_table[i].logfile = NULL;
97 /* reopen those with paths */
98 if(!EmptyString(*log_table[i].name))
99 *log_table[i].logfile = fopen(*log_table[i].name, "a");
103 void
104 ilog(ilogfile dest, const char *format, ...)
106 FILE *logfile = *log_table[dest].logfile;
107 char buf[BUFSIZE];
108 char buf2[BUFSIZE];
109 va_list args;
111 if(logfile == NULL)
112 return;
114 va_start(args, format);
115 ircvsnprintf(buf, sizeof(buf), format, args);
116 va_end(args);
118 ircsnprintf(buf2, sizeof(buf2), "%s %s\n", smalldate(), buf);
120 if(fputs(buf2, logfile) < 0)
122 fclose(logfile);
123 *log_table[dest].logfile = NULL;
126 fflush(logfile);
129 static void
130 _iprint(const char *domain, char *buf)
132 if (domain == NULL || buf == NULL)
133 return;
135 fprintf(stderr, "%8s: %s\n", domain, buf);
138 void
139 inotice(const char *format, ...)
141 char buf[BUFSIZE];
142 va_list args;
144 va_start(args, format);
145 ircvsnprintf(buf, sizeof(buf), format, args);
146 va_end(args);
148 _iprint("notice", buf);
150 ilog(L_MAIN, "%s", buf);
153 void
154 iwarn(const char *format, ...)
156 char buf[BUFSIZE];
157 va_list args;
159 va_start(args, format);
160 ircvsnprintf(buf, sizeof(buf), format, args);
161 va_end(args);
163 _iprint("warning", buf);
165 ilog(L_MAIN, "%s", buf);
168 void
169 ierror(const char *format, ...)
171 char buf[BUFSIZE];
172 va_list args;
174 va_start(args, format);
175 ircvsnprintf(buf, sizeof(buf), format, args);
176 va_end(args);
178 _iprint("error", buf);
180 ilog(L_MAIN, "%s", buf);
183 const char *
184 smalldate(void)
186 static char buf[MAX_DATE_STRING];
187 struct tm *lt;
188 time_t ltime = CurrentTime;
190 lt = localtime(&ltime);
192 ircsnprintf(buf, sizeof(buf), "%d/%d/%d %02d.%02d",
193 lt->tm_year + 1900, lt->tm_mon + 1,
194 lt->tm_mday, lt->tm_hour, lt->tm_min);
196 return buf;
200 * report_error - report an error from an errno.
201 * Record error to log and also send a copy to all *LOCAL* opers online.
203 * text is a *format* string for outputing error. It must
204 * contain only two '%s', the first will be replaced
205 * by the sockhost from the client_p, and the latter will
206 * be taken from sys_errlist[errno].
208 * client_p if not NULL, is the *LOCAL* client associated with
209 * the error.
211 * Cannot use perror() within daemon. stderr is closed in
212 * ircd and cannot be used. And, worse yet, it might have
213 * been reassigned to a normal connection...
215 * Actually stderr is still there IFF ircd was run with -s --Rodder
218 void
219 report_error(const char *text, const char *who, const char *wholog, int error)
221 who = (who) ? who : "";
222 wholog = (wholog) ? wholog : "";
224 sendto_realops_snomask(SNO_DEBUG, L_ALL, text, who, strerror(error));
226 ilog(L_IOERROR, text, wholog, strerror(error));