/[svn]/ircd-hybrid-7.2/src/s_log.c
ViewVC logotype

Annotation of /ircd-hybrid-7.2/src/s_log.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1001 - (hide annotations)
Sat Aug 29 22:44:44 2009 UTC (11 years, 7 months ago) by michael
File MIME type: text/x-chdr
File size: 8018 byte(s)
- remove half done and broken win32 support

1 adx 30 /*
2     * ircd-hybrid: an advanced Internet Relay Chat Daemon(ircd).
3     * s_log.c: Logger functions.
4     *
5     * Copyright (C) 2002 by the past and present ircd coders, and others.
6     *
7     * This program is free software; you can redistribute it and/or modify
8     * it under the terms of the GNU General Public License as published by
9     * the Free Software Foundation; either version 2 of the License, or
10     * (at your option) any later version.
11     *
12     * This program is distributed in the hope that it will be useful,
13     * but WITHOUT ANY WARRANTY; without even the implied warranty of
14     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15     * GNU General Public License for more details.
16     *
17     * You should have received a copy of the GNU General Public License
18     * along with this program; if not, write to the Free Software
19     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20     * USA
21     *
22 knight 31 * $Id$
23 adx 30 */
24    
25     #include "stdinc.h"
26    
27     #ifdef USE_SYSLOG
28     # ifdef HAVE_SYS_SYSLOG_H
29     # include <sys/syslog.h>
30     # else
31     # ifdef HAVE_SYSLOG_H
32     # include <syslog.h>
33     # endif
34     # endif
35     #endif
36    
37     #include "client.h" /* Needed for struct Client */
38     #include "common.h"
39     #include "s_log.h"
40     #include "fileio.h"
41     #include "irc_string.h"
42     #include "sprintf_irc.h"
43     #include "ircd.h"
44     #include "s_misc.h"
45     #include "event.h" /* Needed for EVH etc. */
46     #include "s_conf.h"
47     #include "memory.h"
48    
49     /* some older syslogs would overflow at 2024 */
50     #define LOG_BUFSIZE 2000
51    
52     static FBFILE *logFile;
53     static int logLevel = INIT_LOG_LEVEL;
54    
55     #ifndef SYSLOG_USERS
56     static EVH user_log_resync;
57     static FBFILE *user_log_fb = NULL;
58     #endif
59    
60    
61     #ifdef USE_SYSLOG
62     static const int sysLogLevel[] =
63     {
64     LOG_CRIT,
65     LOG_ERR,
66     LOG_WARNING,
67     LOG_NOTICE,
68     LOG_INFO,
69     LOG_INFO,
70     LOG_INFO
71     };
72     #endif
73    
74     static const char *logLevelToString[] =
75     {
76     "L_CRIT",
77     "L_ERROR",
78     "L_WARN",
79     "L_NOTICE",
80     "L_TRACE",
81     "L_INFO",
82     "L_DEBUG"
83     };
84    
85     /*
86     * open_log - open ircd logging file
87     * returns true (1) if successful, false (0) otherwise
88     */
89     static int
90     open_log(const char *filename)
91     {
92     logFile = fbopen(filename, "a");
93    
94     if (logFile == NULL)
95     {
96     #ifdef USE_SYSLOG
97     syslog(LOG_ERR, "Unable to open log file: %s: %s",
98     filename, strerror(errno));
99     #endif
100     return (0);
101     }
102    
103     return (1);
104     }
105    
106     static void
107     write_log(const char *message)
108     {
109     char buf[LOG_BUFSIZE];
110     size_t nbytes = 0;
111    
112     if (logFile == NULL)
113     return;
114    
115     nbytes = snprintf(buf, sizeof(buf), "[%s] %s\n",
116 michael 581 smalldate(CurrentTime), message);
117 michael 1001
118 adx 30 fbputs(buf, logFile, nbytes);
119     }
120    
121     void
122     ilog(const int priority, const char *fmt, ...)
123     {
124     char buf[LOG_BUFSIZE];
125     va_list args;
126    
127     assert(priority > -1);
128    
129     if (fmt == NULL)
130     return;
131    
132     if (priority > logLevel)
133     return;
134    
135     va_start(args, fmt);
136     vsprintf(buf, fmt, args);
137     va_end(args);
138    
139     #ifdef USE_SYSLOG
140     if (priority <= L_DEBUG)
141     syslog(sysLogLevel[priority], "%s", buf);
142     #endif
143     if (ConfigLoggingEntry.use_logging)
144     write_log(buf);
145     }
146    
147     void
148     init_log(const char *filename)
149     {
150     open_log(filename);
151     #ifdef USE_SYSLOG
152     openlog("ircd", LOG_PID | LOG_NDELAY, LOG_FACILITY);
153     #endif
154     #ifndef SYSLOG_USERS
155     eventAddIsh("user_log_resync", user_log_resync, NULL, 60);
156     #endif
157     }
158    
159     void
160     reopen_log(const char *filename)
161     {
162     if (logFile != NULL)
163     fbclose(logFile);
164     open_log(filename);
165     }
166    
167     void
168     set_log_level(const int level)
169     {
170     if (L_ERROR < level && level <= L_DEBUG)
171     logLevel = level;
172     }
173    
174     int
175     get_log_level(void)
176     {
177     return(logLevel);
178     }
179    
180     const char *
181     get_log_level_as_string(int level)
182     {
183     if (level > L_DEBUG)
184     level = L_DEBUG;
185     else if (level < L_ERROR)
186     level = L_ERROR;
187    
188     return(logLevelToString[level]);
189     }
190    
191     /* log_user_exit()
192     *
193     * inputs - pointer to connecting client
194     * output - NONE
195     * side effects - Current exiting client is logged to
196     * either SYSLOG or to file.
197     */
198     void
199     log_user_exit(struct Client *source_p)
200     {
201     time_t on_for = CurrentTime - source_p->firsttime;
202     #ifdef SYSLOG_USERS
203     if (IsClient(source_p))
204     {
205 michael 581 ilog(L_INFO, "%s (%3u:%02u:%02u): %s!%s@%s %llu/%llu",
206 michael 582 myctime(source_p->firsttime), (unsigned int)(on_for / 3600),
207     (unsigned int)((on_for % 3600)/60), (unsigned int)(on_for % 60),
208 michael 581 source_p->name, source_p->username, source_p->host,
209     source_p->localClient->send.bytes>>10,
210     source_p->localClient->recv.bytes>>10);
211 adx 30 }
212     #else
213     {
214     char linebuf[BUFSIZ];
215    
216     /*
217     * This conditional makes the logfile active only after
218     * it's been created - thus logging can be turned off by
219     * removing the file.
220     * -Taner
221     */
222     if (IsClient(source_p))
223     {
224     if (user_log_fb == NULL)
225     {
226     if ((ConfigLoggingEntry.userlog[0] != '\0') &&
227     (user_log_fb = fbopen(ConfigLoggingEntry.userlog, "r")) != NULL)
228     {
229     fbclose(user_log_fb);
230     user_log_fb = fbopen(ConfigLoggingEntry.userlog, "a");
231     }
232     }
233    
234     if (user_log_fb != NULL)
235     {
236     size_t nbytes = ircsprintf(linebuf,
237 michael 581 "%s (%3u:%02u:%02u): %s!%s@%s %llu/%llu\n",
238 adx 30 myctime(source_p->firsttime),
239 michael 582 (unsigned int)(on_for / 3600),
240     (unsigned int)((on_for % 3600)/60),
241     (unsigned int)(on_for % 60),
242 adx 30 source_p->name, source_p->username, source_p->host,
243     source_p->localClient->send.bytes>>10,
244     source_p->localClient->recv.bytes>>10);
245     fbputs(linebuf, user_log_fb, nbytes);
246     }
247     }
248     }
249     #endif
250     }
251    
252     #ifndef SYSLOG_USERS
253     /* user_log_resync()
254     *
255     * inputs - NONE
256     * output - NONE
257     * side effects -
258     */
259     static void
260     user_log_resync(void *notused)
261     {
262     if (user_log_fb != NULL)
263     {
264     fbclose(user_log_fb);
265     user_log_fb = NULL;
266     }
267     }
268     #endif
269    
270     /* log_oper_action()
271     *
272     * inputs - type of oper log entry
273     * - pointer to oper
274     * - const char *pattern == format string
275     * - var args for format string
276     * output - none
277     * side effects - corresponding log is written to, if its present.
278     *
279     * rewritten sept 5 2005 - Dianora
280     */
281     void
282     log_oper_action(int log_type, const struct Client *source_p,
283     const char *pattern, ...)
284     {
285     va_list args;
286     char linebuf[IRCD_BUFSIZE];
287     FBFILE *log_fb;
288     char *logfile;
289     const char *log_message;
290     size_t nbytes;
291     size_t n_preamble;
292     char *p;
293    
294     switch(log_type)
295     {
296     case LOG_OPER_TYPE:
297     logfile = ConfigLoggingEntry.operlog;
298     log_message = "OPER";
299     break;
300     case LOG_FAILED_OPER_TYPE:
301     logfile = ConfigLoggingEntry.failed_operlog;
302     log_message = "FAILED OPER";
303     break;
304     case LOG_KLINE_TYPE:
305     logfile = ConfigLoggingEntry.klinelog;
306     log_message = "KLINE";
307     break;
308     case LOG_RKLINE_TYPE:
309     logfile = ConfigLoggingEntry.klinelog;
310     log_message = "RKLINE";
311     break;
312     case LOG_DLINE_TYPE:
313     logfile = ConfigLoggingEntry.klinelog;
314     log_message = "DLINE";
315     break;
316     case LOG_TEMP_DLINE_TYPE:
317     logfile = ConfigLoggingEntry.klinelog;
318     log_message = "TEMP DLINE";
319     break;
320     case LOG_TEMP_KLINE_TYPE:
321     logfile = ConfigLoggingEntry.klinelog;
322     log_message = "TEMP KLINE";
323     break;
324     case LOG_GLINE_TYPE:
325     logfile = ConfigLoggingEntry.glinelog;
326     log_message = "GLINE";
327     break;
328     case LOG_KILL_TYPE:
329     logfile = ConfigLoggingEntry.killlog;
330     log_message = "KILL";
331     break;
332     case LOG_IOERR_TYPE:
333     logfile = ConfigLoggingEntry.ioerrlog;
334     log_message = "IO ERR";
335     break;
336     default:
337     return;
338     }
339    
340     if (*logfile == '\0')
341     return;
342    
343     p = linebuf;
344     if (source_p != NULL)
345     {
346     n_preamble = ircsprintf(linebuf, "%s %s by (%s!%s@%s) :",
347     myctime(CurrentTime), log_message,
348     source_p->name, source_p->username, source_p->host);
349    
350     }
351     else
352     {
353     n_preamble = ircsprintf(linebuf, "%s %s :",
354     myctime(CurrentTime), log_message);
355     }
356    
357     p += n_preamble;
358    
359     if ((log_fb = fbopen(logfile, "r")) != NULL)
360     {
361     fbclose(log_fb);
362     log_fb = fbopen(logfile, "a");
363     if (log_fb == NULL)
364     return;
365     va_start(args, pattern);
366     /* XXX add check for IRCD_BUFSIZE-(n_preamble+1) < 0 ? -db */
367     nbytes = vsnprintf(p, IRCD_BUFSIZE-(n_preamble+1), pattern, args);
368     nbytes += n_preamble;
369     va_end(args);
370     fbputs(linebuf, log_fb, nbytes);
371     fbclose(log_fb);
372     }
373     }

Properties

Name Value
svn:eol-style native
svn:keywords Id Revision

svnadmin@ircd-hybrid.org
ViewVC Help
Powered by ViewVC 1.1.28