ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/libio/misc/log.c
Revision: 61
Committed: Mon Oct 3 21:20:41 2005 UTC (20 years, 10 months ago) by adx
Content type: text/x-csrc
File size: 8116 byte(s)
Log Message:
- split libio Makefiles for easier maintenance
- moved pcre.h to main include/ as it is currently a part
  of libio interface (in the future all such headers should
  be moved to libio/ and integrated...)

Now, libio components like comm, misc etc. should be as independent
as possible (from the rest of ircd and from each other), to allow
easy reuse in other software and to keep the interface clean.


File Contents

# User Rev Content
1 adx 61 /*
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     * $Id: s_log.c 33 2005-10-02 20:50:00Z knight $
23     */
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     #ifdef _WIN32
116     nbytes = snprintf(buf, sizeof(buf), "[%s] %s\r\n",
117     #else
118     nbytes = snprintf(buf, sizeof(buf), "[%s] %s\n",
119     #endif
120     smalldate(CurrentTime), message);
121     fbputs(buf, logFile, nbytes);
122     }
123    
124     void
125     ilog(const int priority, const char *fmt, ...)
126     {
127     char buf[LOG_BUFSIZE];
128     va_list args;
129    
130     assert(priority > -1);
131    
132     if (fmt == NULL)
133     return;
134    
135     if (priority > logLevel)
136     return;
137    
138     va_start(args, fmt);
139     vsprintf(buf, fmt, args);
140     va_end(args);
141    
142     #ifdef USE_SYSLOG
143     if (priority <= L_DEBUG)
144     syslog(sysLogLevel[priority], "%s", buf);
145     #endif
146     if (ConfigLoggingEntry.use_logging)
147     write_log(buf);
148     }
149    
150     void
151     init_log(const char *filename)
152     {
153     open_log(filename);
154     #ifdef USE_SYSLOG
155     openlog("ircd", LOG_PID | LOG_NDELAY, LOG_FACILITY);
156     #endif
157     #ifndef SYSLOG_USERS
158     eventAddIsh("user_log_resync", user_log_resync, NULL, 60);
159     #endif
160     }
161    
162     void
163     reopen_log(const char *filename)
164     {
165     if (logFile != NULL)
166     fbclose(logFile);
167     open_log(filename);
168     }
169    
170     void
171     set_log_level(const int level)
172     {
173     if (L_ERROR < level && level <= L_DEBUG)
174     logLevel = level;
175     }
176    
177     int
178     get_log_level(void)
179     {
180     return(logLevel);
181     }
182    
183     const char *
184     get_log_level_as_string(int level)
185     {
186     if (level > L_DEBUG)
187     level = L_DEBUG;
188     else if (level < L_ERROR)
189     level = L_ERROR;
190    
191     return(logLevelToString[level]);
192     }
193    
194     /* log_user_exit()
195     *
196     * inputs - pointer to connecting client
197     * output - NONE
198     * side effects - Current exiting client is logged to
199     * either SYSLOG or to file.
200     */
201     void
202     log_user_exit(struct Client *source_p)
203     {
204     time_t on_for = CurrentTime - source_p->firsttime;
205     #ifdef SYSLOG_USERS
206     if (IsClient(source_p))
207     {
208     ilog(L_INFO, "%s (%3ld:%02ld:%02ld): %s!%s@%s %llu/%llu\n",
209     myctime(source_p->firsttime),
210     (signed long) on_for / 3600,
211     (signed long) (on_for % 3600)/60,
212     (signed long) on_for % 60,
213     source_p->name, source_p->username, source_p->host,
214     source_p->localClient->send.bytes>>10,
215     source_p->localClient->recv.bytes>>10);
216     }
217     #else
218     {
219     char linebuf[BUFSIZ];
220    
221     /*
222     * This conditional makes the logfile active only after
223     * it's been created - thus logging can be turned off by
224     * removing the file.
225     * -Taner
226     */
227     if (IsClient(source_p))
228     {
229     if (user_log_fb == NULL)
230     {
231     if ((ConfigLoggingEntry.userlog[0] != '\0') &&
232     (user_log_fb = fbopen(ConfigLoggingEntry.userlog, "r")) != NULL)
233     {
234     fbclose(user_log_fb);
235     user_log_fb = fbopen(ConfigLoggingEntry.userlog, "a");
236     }
237     }
238    
239     if (user_log_fb != NULL)
240     {
241     size_t nbytes = ircsprintf(linebuf,
242     "%s (%3ld:%02ld:%02ld): %s!%s@%s %llu/%llu\n",
243     myctime(source_p->firsttime),
244     (signed long) on_for / 3600,
245     (signed long) (on_for % 3600)/60,
246     (signed long) on_for % 60,
247     source_p->name, source_p->username, source_p->host,
248     source_p->localClient->send.bytes>>10,
249     source_p->localClient->recv.bytes>>10);
250     fbputs(linebuf, user_log_fb, nbytes);
251     }
252     }
253     }
254     #endif
255     }
256    
257     #ifndef SYSLOG_USERS
258     /* user_log_resync()
259     *
260     * inputs - NONE
261     * output - NONE
262     * side effects -
263     */
264     static void
265     user_log_resync(void *notused)
266     {
267     if (user_log_fb != NULL)
268     {
269     fbclose(user_log_fb);
270     user_log_fb = NULL;
271     }
272     }
273     #endif
274    
275     /* log_oper_action()
276     *
277     * inputs - type of oper log entry
278     * - pointer to oper
279     * - const char *pattern == format string
280     * - var args for format string
281     * output - none
282     * side effects - corresponding log is written to, if its present.
283     *
284     * rewritten sept 5 2005 - Dianora
285     */
286     void
287     log_oper_action(int log_type, const struct Client *source_p,
288     const char *pattern, ...)
289     {
290     va_list args;
291     char linebuf[IRCD_BUFSIZE];
292     FBFILE *log_fb;
293     char *logfile;
294     const char *log_message;
295     size_t nbytes;
296     size_t n_preamble;
297     char *p;
298    
299     switch(log_type)
300     {
301     case LOG_OPER_TYPE:
302     logfile = ConfigLoggingEntry.operlog;
303     log_message = "OPER";
304     break;
305     case LOG_FAILED_OPER_TYPE:
306     logfile = ConfigLoggingEntry.failed_operlog;
307     log_message = "FAILED OPER";
308     break;
309     case LOG_KLINE_TYPE:
310     logfile = ConfigLoggingEntry.klinelog;
311     log_message = "KLINE";
312     break;
313     case LOG_RKLINE_TYPE:
314     logfile = ConfigLoggingEntry.klinelog;
315     log_message = "RKLINE";
316     break;
317     case LOG_DLINE_TYPE:
318     logfile = ConfigLoggingEntry.klinelog;
319     log_message = "DLINE";
320     break;
321     case LOG_TEMP_DLINE_TYPE:
322     logfile = ConfigLoggingEntry.klinelog;
323     log_message = "TEMP DLINE";
324     break;
325     case LOG_TEMP_KLINE_TYPE:
326     logfile = ConfigLoggingEntry.klinelog;
327     log_message = "TEMP KLINE";
328     break;
329     case LOG_GLINE_TYPE:
330     logfile = ConfigLoggingEntry.glinelog;
331     log_message = "GLINE";
332     break;
333     case LOG_KILL_TYPE:
334     logfile = ConfigLoggingEntry.killlog;
335     log_message = "KILL";
336     break;
337     case LOG_IOERR_TYPE:
338     logfile = ConfigLoggingEntry.ioerrlog;
339     log_message = "IO ERR";
340     break;
341     default:
342     return;
343     }
344    
345     if (*logfile == '\0')
346     return;
347    
348     p = linebuf;
349     if (source_p != NULL)
350     {
351     n_preamble = ircsprintf(linebuf, "%s %s by (%s!%s@%s) :",
352     myctime(CurrentTime), log_message,
353     source_p->name, source_p->username, source_p->host);
354    
355     }
356     else
357     {
358     n_preamble = ircsprintf(linebuf, "%s %s :",
359     myctime(CurrentTime), log_message);
360     }
361    
362     p += n_preamble;
363    
364     if ((log_fb = fbopen(logfile, "r")) != NULL)
365     {
366     fbclose(log_fb);
367     log_fb = fbopen(logfile, "a");
368     if (log_fb == NULL)
369     return;
370     va_start(args, pattern);
371     /* XXX add check for IRCD_BUFSIZE-(n_preamble+1) < 0 ? -db */
372     nbytes = vsnprintf(p, IRCD_BUFSIZE-(n_preamble+1), pattern, args);
373     nbytes += n_preamble;
374     va_end(args);
375     fbputs(linebuf, log_fb, nbytes);
376     fbclose(log_fb);
377     }
378     }

Properties

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