Blame


1 f4ade537 2001-12-11 alex /*
2 f4ade537 2001-12-11 alex * ngIRCd -- The Next Generation IRC Daemon
3 1547f76c 2002-01-02 alex * Copyright (c)2001,2002 by Alexander Barton (alex@barton.de)
4 f4ade537 2001-12-11 alex *
5 490f28ff 2002-12-12 alex * This program is free software; you can redistribute it and/or modify
6 490f28ff 2002-12-12 alex * it under the terms of the GNU General Public License as published by
7 490f28ff 2002-12-12 alex * the Free Software Foundation; either version 2 of the License, or
8 490f28ff 2002-12-12 alex * (at your option) any later version.
9 490f28ff 2002-12-12 alex * Please read the file COPYING, README and AUTHORS for more information.
10 f4ade537 2001-12-11 alex *
11 490f28ff 2002-12-12 alex * Logging functions
12 f4ade537 2001-12-11 alex */
13 f4ade537 2001-12-11 alex
14 f4ade537 2001-12-11 alex
15 ca33cbda 2002-03-12 alex #include "portab.h"
16 490f28ff 2002-12-12 alex
17 aee48a3b 2005-04-16 fw static char UNUSED id[] = "$Id: log.c,v 1.54 2005/04/16 09:31:30 fw Exp $";
18 f4ade537 2001-12-11 alex
19 ca33cbda 2002-03-12 alex #include "imp.h"
20 f4ade537 2001-12-11 alex #include <assert.h>
21 fe2bc90e 2002-03-06 alex #include <errno.h>
22 c1f32e82 2004-10-20 alex #ifdef PROTOTYPES
23 c1f32e82 2004-10-20 alex # include <stdarg.h>
24 c1f32e82 2004-10-20 alex #else
25 c1f32e82 2004-10-20 alex # include <varargs.h>
26 c1f32e82 2004-10-20 alex #endif
27 f4ade537 2001-12-11 alex #include <stdio.h>
28 3be92e9d 2001-12-26 alex #include <string.h>
29 fe2bc90e 2002-03-06 alex #include <sys/types.h>
30 fe2bc90e 2002-03-06 alex #include <unistd.h>
31 50352dc9 2001-12-27 alex
32 c40592d2 2003-12-26 alex #ifdef SYSLOG
33 3d1a0962 2001-12-12 alex #include <syslog.h>
34 50352dc9 2001-12-27 alex #endif
35 f4ade537 2001-12-11 alex
36 006c0328 2002-01-11 alex #include "ngircd.h"
37 ca33cbda 2002-03-12 alex #include "defines.h"
38 c2f60abe 2002-05-27 alex #include "conn.h"
39 c2f60abe 2002-05-27 alex #include "client.h"
40 c2f60abe 2002-05-27 alex #include "channel.h"
41 20a2ffef 2002-03-27 alex #include "irc-write.h"
42 006c0328 2002-01-11 alex
43 ca33cbda 2002-03-12 alex #include "exp.h"
44 f4ade537 2001-12-11 alex #include "log.h"
45 f4ade537 2001-12-11 alex
46 f4ade537 2001-12-11 alex
47 8adff592 2005-03-19 fw LOCAL char Init_Txt[127];
48 1c00ddff 2002-03-29 alex
49 95375426 2005-02-10 alex #ifdef DEBUG
50 8adff592 2005-03-19 fw LOCAL char Error_File[FNAME_LEN];
51 95375426 2005-02-10 alex #endif
52 1c00ddff 2002-03-29 alex
53 95375426 2005-02-10 alex
54 8adff592 2005-03-19 fw LOCAL void Wall_ServerNotice PARAMS(( char *Msg ));
55 20a2ffef 2002-03-27 alex
56 20a2ffef 2002-03-27 alex
57 8adff592 2005-03-19 fw GLOBAL void
58 8adff592 2005-03-19 fw Log_Init( void )
59 f4ade537 2001-12-11 alex {
60 c40592d2 2003-12-26 alex #ifdef SYSLOG
61 aee48a3b 2005-04-16 fw #ifndef LOG_CONS /* Kludge: mips-dec-ultrix4.5 has no LOG_CONS/LOG_LOCAL5 */
62 aee48a3b 2005-04-16 fw #define LOG_CONS 0
63 aee48a3b 2005-04-16 fw #endif
64 aee48a3b 2005-04-16 fw #ifndef LOG_LOCAL5
65 aee48a3b 2005-04-16 fw #define LOG_LOCAL5 0
66 aee48a3b 2005-04-16 fw #endif
67 fcb47ae6 2002-02-19 alex /* Syslog initialisieren */
68 62796722 2003-03-31 alex openlog( PACKAGE_NAME, LOG_CONS|LOG_PID, LOG_LOCAL5 );
69 50352dc9 2001-12-27 alex #endif
70 fcb47ae6 2002-02-19 alex
71 fcb47ae6 2002-02-19 alex /* Hello World! */
72 894cd2cd 2005-02-09 alex Log( LOG_NOTICE, "%s started.", NGIRCd_Version );
73 fe2bc90e 2002-03-06 alex
74 fcb47ae6 2002-02-19 alex /* Informationen uebern den "Operation Mode" */
75 d16ce5a7 2005-01-20 alex Init_Txt[0] = '\0';
76 fcb47ae6 2002-02-19 alex #ifdef DEBUG
77 fcb47ae6 2002-02-19 alex if( NGIRCd_Debug )
78 fcb47ae6 2002-02-19 alex {
79 d16ce5a7 2005-01-20 alex strcpy( Init_Txt, "debug-mode" );
80 fcb47ae6 2002-02-19 alex }
81 fcb47ae6 2002-02-19 alex #endif
82 fcb47ae6 2002-02-19 alex if( NGIRCd_NoDaemon )
83 fcb47ae6 2002-02-19 alex {
84 3d74a9c3 2002-03-29 alex if( Init_Txt[0] ) strcat( Init_Txt, ", " );
85 3d74a9c3 2002-03-29 alex strcat( Init_Txt, "no-daemon-mode" );
86 fcb47ae6 2002-02-19 alex }
87 fcb47ae6 2002-02-19 alex if( NGIRCd_Passive )
88 fcb47ae6 2002-02-19 alex {
89 3d74a9c3 2002-03-29 alex if( Init_Txt[0] ) strcat( Init_Txt, ", " );
90 3d74a9c3 2002-03-29 alex strcat( Init_Txt, "passive-mode" );
91 fcb47ae6 2002-02-19 alex }
92 fcb47ae6 2002-02-19 alex #ifdef SNIFFER
93 fcb47ae6 2002-02-19 alex if( NGIRCd_Sniffer )
94 fcb47ae6 2002-02-19 alex {
95 3d74a9c3 2002-03-29 alex if( Init_Txt[0] ) strcat( Init_Txt, ", " );
96 3d74a9c3 2002-03-29 alex strcat( Init_Txt, "network sniffer" );
97 fcb47ae6 2002-02-19 alex }
98 fcb47ae6 2002-02-19 alex #endif
99 3d74a9c3 2002-03-29 alex if( Init_Txt[0] ) Log( LOG_INFO, "Activating: %s.", Init_Txt );
100 7281b8dd 2004-05-07 alex
101 95375426 2005-02-10 alex #ifdef DEBUG
102 7281b8dd 2004-05-07 alex Error_File[0] = '\0';
103 95375426 2005-02-10 alex #endif
104 3d74a9c3 2002-03-29 alex } /* Log_Init */
105 fe2bc90e 2002-03-06 alex
106 3d74a9c3 2002-03-29 alex
107 95375426 2005-02-10 alex #ifdef DEBUG
108 95375426 2005-02-10 alex
109 8adff592 2005-03-19 fw GLOBAL void
110 8adff592 2005-03-19 fw Log_InitErrorfile( void )
111 3d74a9c3 2002-03-29 alex {
112 773d886d 2002-03-29 alex /* "Error-Log" initialisieren: stderr in Datei umlenken. Dort
113 773d886d 2002-03-29 alex * landen z.B. alle Ausgaben von assert()-Aufrufen. */
114 3d74a9c3 2002-03-29 alex
115 b58ab32b 2002-03-31 alex /* Dateiname zusammen bauen */
116 8adff592 2005-03-19 fw sprintf( Error_File, "%s/%s-%ld.err", ERROR_DIR, PACKAGE_NAME, (long)getpid( ));
117 b58ab32b 2002-03-31 alex
118 b58ab32b 2002-03-31 alex /* stderr umlenken */
119 b58ab32b 2002-03-31 alex fflush( stderr );
120 773d886d 2002-03-29 alex if( ! freopen( Error_File, "w", stderr ))
121 773d886d 2002-03-29 alex {
122 773d886d 2002-03-29 alex Log( LOG_ERR, "Can't reopen stderr (\"%s\"): %s", Error_File, strerror( errno ));
123 773d886d 2002-03-29 alex return;
124 773d886d 2002-03-29 alex }
125 fe2bc90e 2002-03-06 alex
126 b58ab32b 2002-03-31 alex /* Einige Infos in das Error-File schreiben */
127 d68fb7a3 2002-03-29 alex fputs( ctime( &NGIRCd_Start ), stderr );
128 894cd2cd 2005-02-09 alex fprintf( stderr, "%s started.\n", NGIRCd_Version );
129 3d74a9c3 2002-03-29 alex fprintf( stderr, "Activating: %s\n\n", Init_Txt[0] ? Init_Txt : "-" );
130 fe2bc90e 2002-03-06 alex fflush( stderr );
131 b58ab32b 2002-03-31 alex
132 b58ab32b 2002-03-31 alex Log( LOG_DEBUG, "Redirected stderr to \"%s\".", Error_File );
133 3d74a9c3 2002-03-29 alex } /* Log_InitErrfile */
134 f4ade537 2001-12-11 alex
135 95375426 2005-02-10 alex #endif
136 f4ade537 2001-12-11 alex
137 95375426 2005-02-10 alex
138 8adff592 2005-03-19 fw GLOBAL void
139 8adff592 2005-03-19 fw Log_Exit( void )
140 f4ade537 2001-12-11 alex {
141 fcb47ae6 2002-02-19 alex /* Good Bye! */
142 62796722 2003-03-31 alex if( NGIRCd_SignalRestart ) Log( LOG_NOTICE, "%s done (restarting).", PACKAGE_NAME );
143 62796722 2003-03-31 alex else Log( LOG_NOTICE, "%s done.", PACKAGE_NAME );
144 b9a2c4a8 2002-03-21 alex
145 95375426 2005-02-10 alex #ifdef DEBUG
146 7281b8dd 2004-05-07 alex if( Error_File[0] )
147 7281b8dd 2004-05-07 alex {
148 7281b8dd 2004-05-07 alex /* Error-File (stderr) loeschen */
149 7281b8dd 2004-05-07 alex if( unlink( Error_File ) != 0 ) Log( LOG_ERR, "Can't delete \"%s\": %s", Error_File, strerror( errno ));
150 7281b8dd 2004-05-07 alex }
151 95375426 2005-02-10 alex #endif
152 1c00ddff 2002-03-29 alex
153 c40592d2 2003-12-26 alex #ifdef SYSLOG
154 fcb47ae6 2002-02-19 alex /* syslog abmelden */
155 3d1a0962 2001-12-12 alex closelog( );
156 50352dc9 2001-12-27 alex #endif
157 f4ade537 2001-12-11 alex } /* Log_Exit */
158 f4ade537 2001-12-11 alex
159 f4ade537 2001-12-11 alex
160 f7327524 2002-05-30 alex #ifdef PROTOTYPES
161 8adff592 2005-03-19 fw GLOBAL void
162 8adff592 2005-03-19 fw Log( int Level, const char *Format, ... )
163 f7327524 2002-05-30 alex #else
164 8adff592 2005-03-19 fw GLOBAL void
165 f7327524 2002-05-30 alex Log( Level, Format, va_alist )
166 8adff592 2005-03-19 fw int Level;
167 8adff592 2005-03-19 fw const char *Format;
168 f7327524 2002-05-30 alex va_dcl
169 f7327524 2002-05-30 alex #endif
170 f4ade537 2001-12-11 alex {
171 f4ade537 2001-12-11 alex /* Eintrag in Logfile(s) schreiben */
172 f4ade537 2001-12-11 alex
173 8adff592 2005-03-19 fw char msg[MAX_LOG_MSG_LEN];
174 8adff592 2005-03-19 fw bool snotice;
175 f4ade537 2001-12-11 alex va_list ap;
176 f4ade537 2001-12-11 alex
177 ec1922ef 2001-12-29 alex assert( Format != NULL );
178 ec1922ef 2001-12-29 alex
179 20a2ffef 2002-03-27 alex if( Level & LOG_snotice )
180 20a2ffef 2002-03-27 alex {
181 20a2ffef 2002-03-27 alex /* Notice an User mit "s" Mode */
182 8adff592 2005-03-19 fw snotice = true;
183 20a2ffef 2002-03-27 alex Level &= ~LOG_snotice;
184 20a2ffef 2002-03-27 alex }
185 8adff592 2005-03-19 fw else snotice = false;
186 20a2ffef 2002-03-27 alex
187 006c0328 2002-01-11 alex #ifdef DEBUG
188 006c0328 2002-01-11 alex if(( Level == LOG_DEBUG ) && ( ! NGIRCd_Debug )) return;
189 006c0328 2002-01-11 alex #else
190 d4a60bd4 2001-12-25 alex if( Level == LOG_DEBUG ) return;
191 d4a60bd4 2001-12-25 alex #endif
192 d4a60bd4 2001-12-25 alex
193 f4ade537 2001-12-11 alex /* String mit variablen Argumenten zusammenbauen ... */
194 f7327524 2002-05-30 alex #ifdef PROTOTYPES
195 f4ade537 2001-12-11 alex va_start( ap, Format );
196 f7327524 2002-05-30 alex #else
197 f7327524 2002-05-30 alex va_start( ap );
198 f7327524 2002-05-30 alex #endif
199 10363b39 2002-03-03 alex vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
200 10363b39 2002-03-03 alex va_end( ap );
201 f4ade537 2001-12-11 alex
202 bebf0383 2002-03-30 alex if( NGIRCd_NoDaemon )
203 bebf0383 2002-03-30 alex {
204 bebf0383 2002-03-30 alex /* auf Konsole ausgeben */
205 8adff592 2005-03-19 fw fprintf( stdout, "[%d:%d] %s\n", (int)getpid( ), Level, msg );
206 40ebd470 2002-09-03 alex fflush( stdout );
207 d040fa2a 2002-09-09 alex }
208 c40592d2 2003-12-26 alex #ifdef SYSLOG
209 d040fa2a 2002-09-09 alex else
210 d040fa2a 2002-09-09 alex {
211 d040fa2a 2002-09-09 alex /* Syslog */
212 ec474a4b 2002-10-04 alex syslog( Level, "%s", msg );
213 bebf0383 2002-03-30 alex }
214 d040fa2a 2002-09-09 alex #endif
215 fe2bc90e 2002-03-06 alex
216 bebf0383 2002-03-30 alex if( Level <= LOG_CRIT )
217 bebf0383 2002-03-30 alex {
218 95375426 2005-02-10 alex /* log critical messages to stderr */
219 bebf0383 2002-03-30 alex fprintf( stderr, "%s\n", msg );
220 bebf0383 2002-03-30 alex fflush( stderr );
221 bebf0383 2002-03-30 alex }
222 bebf0383 2002-03-30 alex
223 bebf0383 2002-03-30 alex if( snotice )
224 bebf0383 2002-03-30 alex {
225 bebf0383 2002-03-30 alex /* NOTICE an lokale User mit "s"-Mode */
226 bebf0383 2002-03-30 alex Wall_ServerNotice( msg );
227 bebf0383 2002-03-30 alex }
228 f4ade537 2001-12-11 alex } /* Log */
229 f4ade537 2001-12-11 alex
230 f4ade537 2001-12-11 alex
231 8adff592 2005-03-19 fw GLOBAL void
232 8adff592 2005-03-19 fw Log_Init_Resolver( void )
233 ec1922ef 2001-12-29 alex {
234 c40592d2 2003-12-26 alex #ifdef SYSLOG
235 62796722 2003-03-31 alex openlog( PACKAGE_NAME, LOG_CONS|LOG_PID, LOG_LOCAL5 );
236 ec1922ef 2001-12-29 alex #endif
237 26390c60 2004-05-10 alex Log_Resolver( LOG_DEBUG, "Resolver sub-process starting, PID %d.", getpid( ));
238 ec1922ef 2001-12-29 alex } /* Log_Init_Resolver */
239 ec1922ef 2001-12-29 alex
240 ec1922ef 2001-12-29 alex
241 8adff592 2005-03-19 fw GLOBAL void
242 8adff592 2005-03-19 fw Log_Exit_Resolver( void )
243 ec1922ef 2001-12-29 alex {
244 26390c60 2004-05-10 alex Log_Resolver( LOG_DEBUG, "Resolver sub-process %d done.", getpid( ));
245 c40592d2 2003-12-26 alex #ifdef SYSLOG
246 ec1922ef 2001-12-29 alex closelog( );
247 ec1922ef 2001-12-29 alex #endif
248 ec1922ef 2001-12-29 alex } /* Log_Exit_Resolver */
249 ec1922ef 2001-12-29 alex
250 ec1922ef 2001-12-29 alex
251 f7327524 2002-05-30 alex #ifdef PROTOTYPES
252 8adff592 2005-03-19 fw GLOBAL void
253 8adff592 2005-03-19 fw Log_Resolver( const int Level, const char *Format, ... )
254 f7327524 2002-05-30 alex #else
255 8adff592 2005-03-19 fw GLOBAL void
256 f7327524 2002-05-30 alex Log_Resolver( Level, Format, va_alist )
257 8adff592 2005-03-19 fw const int Level;
258 8adff592 2005-03-19 fw const char *Format;
259 f7327524 2002-05-30 alex va_dcl
260 f7327524 2002-05-30 alex #endif
261 ec1922ef 2001-12-29 alex {
262 ec1922ef 2001-12-29 alex /* Eintrag des Resolver in Logfile(s) schreiben */
263 ec1922ef 2001-12-29 alex
264 8adff592 2005-03-19 fw char msg[MAX_LOG_MSG_LEN];
265 ec1922ef 2001-12-29 alex va_list ap;
266 ec1922ef 2001-12-29 alex
267 ec1922ef 2001-12-29 alex assert( Format != NULL );
268 d040fa2a 2002-09-09 alex
269 006c0328 2002-01-11 alex #ifdef DEBUG
270 006c0328 2002-01-11 alex if(( Level == LOG_DEBUG ) && ( ! NGIRCd_Debug )) return;
271 006c0328 2002-01-11 alex #else
272 ec1922ef 2001-12-29 alex if( Level == LOG_DEBUG ) return;
273 ec1922ef 2001-12-29 alex #endif
274 ec1922ef 2001-12-29 alex
275 ec1922ef 2001-12-29 alex /* String mit variablen Argumenten zusammenbauen ... */
276 f7327524 2002-05-30 alex #ifdef PROTOTYPES
277 ec1922ef 2001-12-29 alex va_start( ap, Format );
278 f7327524 2002-05-30 alex #else
279 f7327524 2002-05-30 alex va_start( ap );
280 f7327524 2002-05-30 alex #endif
281 10363b39 2002-03-03 alex vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
282 10363b39 2002-03-03 alex va_end( ap );
283 ec1922ef 2001-12-29 alex
284 26390c60 2004-05-10 alex /* Output */
285 26390c60 2004-05-10 alex if( NGIRCd_NoDaemon )
286 26390c60 2004-05-10 alex {
287 26390c60 2004-05-10 alex /* Output to console */
288 8adff592 2005-03-19 fw fprintf( stdout, "[%d:%d] %s\n", (int)getpid( ), Level, msg );
289 26390c60 2004-05-10 alex fflush( stdout );
290 26390c60 2004-05-10 alex }
291 26390c60 2004-05-10 alex #ifdef SYSLOG
292 8308c170 2005-02-03 alex else syslog( Level, "%s", msg );
293 03783eea 2002-01-05 alex #endif
294 ec1922ef 2001-12-29 alex } /* Log_Resolver */
295 ec1922ef 2001-12-29 alex
296 ec1922ef 2001-12-29 alex
297 8adff592 2005-03-19 fw LOCAL void
298 8adff592 2005-03-19 fw Wall_ServerNotice( char *Msg )
299 20a2ffef 2002-03-27 alex {
300 773d886d 2002-03-29 alex /* Server-Notice an entsprechende User verschicken */
301 773d886d 2002-03-29 alex
302 20a2ffef 2002-03-27 alex CLIENT *c;
303 20a2ffef 2002-03-27 alex
304 20a2ffef 2002-03-27 alex assert( Msg != NULL );
305 773d886d 2002-03-29 alex
306 20a2ffef 2002-03-27 alex c = Client_First( );
307 20a2ffef 2002-03-27 alex while( c )
308 20a2ffef 2002-03-27 alex {
309 856f24eb 2002-10-09 alex if(( Client_Conn( c ) > NONE ) && ( Client_HasMode( c, 's' ))) IRC_WriteStrClient( c, "NOTICE %s :%s%s", Client_ThisServer( ), NOTICE_TXTPREFIX, Msg );
310 20a2ffef 2002-03-27 alex c = Client_Next( c );
311 20a2ffef 2002-03-27 alex }
312 20a2ffef 2002-03-27 alex } /* Wall_ServerNotice */
313 20a2ffef 2002-03-27 alex
314 20a2ffef 2002-03-27 alex
315 f4ade537 2001-12-11 alex /* -eof- */