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 c1f32e82 2004-10-20 alex static char UNUSED id[] = "$Id: log.c,v 1.48 2004/10/20 13:47:32 alex 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 1c00ddff 2002-03-29 alex LOCAL CHAR Error_File[FNAME_LEN];
48 3d74a9c3 2002-03-29 alex LOCAL CHAR Init_Txt[127];
49 1c00ddff 2002-03-29 alex
50 1c00ddff 2002-03-29 alex
51 c2f60abe 2002-05-27 alex LOCAL VOID Wall_ServerNotice PARAMS(( CHAR *Msg ));
52 20a2ffef 2002-03-27 alex
53 20a2ffef 2002-03-27 alex
54 c2f60abe 2002-05-27 alex GLOBAL VOID
55 c2f60abe 2002-05-27 alex Log_Init( VOID )
56 f4ade537 2001-12-11 alex {
57 c40592d2 2003-12-26 alex #ifdef SYSLOG
58 fcb47ae6 2002-02-19 alex /* Syslog initialisieren */
59 62796722 2003-03-31 alex openlog( PACKAGE_NAME, LOG_CONS|LOG_PID, LOG_LOCAL5 );
60 50352dc9 2001-12-27 alex #endif
61 fcb47ae6 2002-02-19 alex
62 fcb47ae6 2002-02-19 alex /* Hello World! */
63 006c0328 2002-01-11 alex Log( LOG_NOTICE, "%s started.", NGIRCd_Version( ));
64 fe2bc90e 2002-03-06 alex
65 fcb47ae6 2002-02-19 alex /* Informationen uebern den "Operation Mode" */
66 3d74a9c3 2002-03-29 alex strcpy( Init_Txt, "" );
67 fcb47ae6 2002-02-19 alex #ifdef DEBUG
68 fcb47ae6 2002-02-19 alex if( NGIRCd_Debug )
69 fcb47ae6 2002-02-19 alex {
70 3d74a9c3 2002-03-29 alex if( Init_Txt[0] ) strcat( Init_Txt, ", " );
71 3d74a9c3 2002-03-29 alex strcat( Init_Txt, "debug-mode" );
72 fcb47ae6 2002-02-19 alex }
73 fcb47ae6 2002-02-19 alex #endif
74 fcb47ae6 2002-02-19 alex if( NGIRCd_NoDaemon )
75 fcb47ae6 2002-02-19 alex {
76 3d74a9c3 2002-03-29 alex if( Init_Txt[0] ) strcat( Init_Txt, ", " );
77 3d74a9c3 2002-03-29 alex strcat( Init_Txt, "no-daemon-mode" );
78 fcb47ae6 2002-02-19 alex }
79 fcb47ae6 2002-02-19 alex if( NGIRCd_Passive )
80 fcb47ae6 2002-02-19 alex {
81 3d74a9c3 2002-03-29 alex if( Init_Txt[0] ) strcat( Init_Txt, ", " );
82 3d74a9c3 2002-03-29 alex strcat( Init_Txt, "passive-mode" );
83 fcb47ae6 2002-02-19 alex }
84 fcb47ae6 2002-02-19 alex #ifdef SNIFFER
85 fcb47ae6 2002-02-19 alex if( NGIRCd_Sniffer )
86 fcb47ae6 2002-02-19 alex {
87 3d74a9c3 2002-03-29 alex if( Init_Txt[0] ) strcat( Init_Txt, ", " );
88 3d74a9c3 2002-03-29 alex strcat( Init_Txt, "network sniffer" );
89 fcb47ae6 2002-02-19 alex }
90 fcb47ae6 2002-02-19 alex #endif
91 3d74a9c3 2002-03-29 alex if( Init_Txt[0] ) Log( LOG_INFO, "Activating: %s.", Init_Txt );
92 7281b8dd 2004-05-07 alex
93 7281b8dd 2004-05-07 alex Error_File[0] = '\0';
94 3d74a9c3 2002-03-29 alex } /* Log_Init */
95 fe2bc90e 2002-03-06 alex
96 3d74a9c3 2002-03-29 alex
97 c2f60abe 2002-05-27 alex GLOBAL VOID
98 c2f60abe 2002-05-27 alex Log_InitErrorfile( VOID )
99 3d74a9c3 2002-03-29 alex {
100 773d886d 2002-03-29 alex /* "Error-Log" initialisieren: stderr in Datei umlenken. Dort
101 773d886d 2002-03-29 alex * landen z.B. alle Ausgaben von assert()-Aufrufen. */
102 3d74a9c3 2002-03-29 alex
103 b58ab32b 2002-03-31 alex /* Dateiname zusammen bauen */
104 62796722 2003-03-31 alex sprintf( Error_File, "%s/%s-%ld.err", ERROR_DIR, PACKAGE_NAME, (LONG)getpid( ));
105 b58ab32b 2002-03-31 alex
106 b58ab32b 2002-03-31 alex /* stderr umlenken */
107 b58ab32b 2002-03-31 alex fflush( stderr );
108 773d886d 2002-03-29 alex if( ! freopen( Error_File, "w", stderr ))
109 773d886d 2002-03-29 alex {
110 773d886d 2002-03-29 alex Log( LOG_ERR, "Can't reopen stderr (\"%s\"): %s", Error_File, strerror( errno ));
111 773d886d 2002-03-29 alex return;
112 773d886d 2002-03-29 alex }
113 fe2bc90e 2002-03-06 alex
114 b58ab32b 2002-03-31 alex /* Einige Infos in das Error-File schreiben */
115 d68fb7a3 2002-03-29 alex fputs( ctime( &NGIRCd_Start ), stderr );
116 773d886d 2002-03-29 alex fprintf( stderr, "%s started.\n", NGIRCd_Version( ));
117 3d74a9c3 2002-03-29 alex fprintf( stderr, "Activating: %s\n\n", Init_Txt[0] ? Init_Txt : "-" );
118 fe2bc90e 2002-03-06 alex fflush( stderr );
119 b58ab32b 2002-03-31 alex
120 b58ab32b 2002-03-31 alex Log( LOG_DEBUG, "Redirected stderr to \"%s\".", Error_File );
121 3d74a9c3 2002-03-29 alex } /* Log_InitErrfile */
122 f4ade537 2001-12-11 alex
123 f4ade537 2001-12-11 alex
124 c2f60abe 2002-05-27 alex GLOBAL VOID
125 c2f60abe 2002-05-27 alex Log_Exit( VOID )
126 f4ade537 2001-12-11 alex {
127 fcb47ae6 2002-02-19 alex /* Good Bye! */
128 62796722 2003-03-31 alex if( NGIRCd_SignalRestart ) Log( LOG_NOTICE, "%s done (restarting).", PACKAGE_NAME );
129 62796722 2003-03-31 alex else Log( LOG_NOTICE, "%s done.", PACKAGE_NAME );
130 b9a2c4a8 2002-03-21 alex
131 7281b8dd 2004-05-07 alex if( Error_File[0] )
132 7281b8dd 2004-05-07 alex {
133 7281b8dd 2004-05-07 alex /* Error-File (stderr) loeschen */
134 7281b8dd 2004-05-07 alex if( unlink( Error_File ) != 0 ) Log( LOG_ERR, "Can't delete \"%s\": %s", Error_File, strerror( errno ));
135 7281b8dd 2004-05-07 alex }
136 1c00ddff 2002-03-29 alex
137 c40592d2 2003-12-26 alex #ifdef SYSLOG
138 fcb47ae6 2002-02-19 alex /* syslog abmelden */
139 3d1a0962 2001-12-12 alex closelog( );
140 50352dc9 2001-12-27 alex #endif
141 f4ade537 2001-12-11 alex } /* Log_Exit */
142 f4ade537 2001-12-11 alex
143 f4ade537 2001-12-11 alex
144 f7327524 2002-05-30 alex #ifdef PROTOTYPES
145 c2f60abe 2002-05-27 alex GLOBAL VOID
146 c2f60abe 2002-05-27 alex Log( INT Level, CONST CHAR *Format, ... )
147 f7327524 2002-05-30 alex #else
148 f7327524 2002-05-30 alex GLOBAL VOID
149 f7327524 2002-05-30 alex Log( Level, Format, va_alist )
150 f7327524 2002-05-30 alex INT Level;
151 f7327524 2002-05-30 alex CONST CHAR *Format;
152 f7327524 2002-05-30 alex va_dcl
153 f7327524 2002-05-30 alex #endif
154 f4ade537 2001-12-11 alex {
155 f4ade537 2001-12-11 alex /* Eintrag in Logfile(s) schreiben */
156 f4ade537 2001-12-11 alex
157 f4ade537 2001-12-11 alex CHAR msg[MAX_LOG_MSG_LEN];
158 20a2ffef 2002-03-27 alex BOOLEAN snotice;
159 f4ade537 2001-12-11 alex va_list ap;
160 f4ade537 2001-12-11 alex
161 ec1922ef 2001-12-29 alex assert( Format != NULL );
162 ec1922ef 2001-12-29 alex
163 20a2ffef 2002-03-27 alex if( Level & LOG_snotice )
164 20a2ffef 2002-03-27 alex {
165 20a2ffef 2002-03-27 alex /* Notice an User mit "s" Mode */
166 20a2ffef 2002-03-27 alex snotice = TRUE;
167 20a2ffef 2002-03-27 alex Level &= ~LOG_snotice;
168 20a2ffef 2002-03-27 alex }
169 773d886d 2002-03-29 alex else snotice = FALSE;
170 20a2ffef 2002-03-27 alex
171 006c0328 2002-01-11 alex #ifdef DEBUG
172 006c0328 2002-01-11 alex if(( Level == LOG_DEBUG ) && ( ! NGIRCd_Debug )) return;
173 006c0328 2002-01-11 alex #else
174 d4a60bd4 2001-12-25 alex if( Level == LOG_DEBUG ) return;
175 d4a60bd4 2001-12-25 alex #endif
176 d4a60bd4 2001-12-25 alex
177 f4ade537 2001-12-11 alex /* String mit variablen Argumenten zusammenbauen ... */
178 f7327524 2002-05-30 alex #ifdef PROTOTYPES
179 f4ade537 2001-12-11 alex va_start( ap, Format );
180 f7327524 2002-05-30 alex #else
181 f7327524 2002-05-30 alex va_start( ap );
182 f7327524 2002-05-30 alex #endif
183 10363b39 2002-03-03 alex vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
184 10363b39 2002-03-03 alex va_end( ap );
185 f4ade537 2001-12-11 alex
186 bebf0383 2002-03-30 alex if( NGIRCd_NoDaemon )
187 bebf0383 2002-03-30 alex {
188 bebf0383 2002-03-30 alex /* auf Konsole ausgeben */
189 364ff708 2004-06-26 alex fprintf( stdout, "[%d:%d] %s\n", (INT)getpid( ), Level, msg );
190 40ebd470 2002-09-03 alex fflush( stdout );
191 d040fa2a 2002-09-09 alex }
192 c40592d2 2003-12-26 alex #ifdef SYSLOG
193 d040fa2a 2002-09-09 alex else
194 d040fa2a 2002-09-09 alex {
195 d040fa2a 2002-09-09 alex /* Syslog */
196 ec474a4b 2002-10-04 alex syslog( Level, "%s", msg );
197 bebf0383 2002-03-30 alex }
198 d040fa2a 2002-09-09 alex #endif
199 fe2bc90e 2002-03-06 alex
200 bebf0383 2002-03-30 alex if( Level <= LOG_CRIT )
201 bebf0383 2002-03-30 alex {
202 bebf0383 2002-03-30 alex /* Kritische Meldungen in Error-File (stderr) */
203 bebf0383 2002-03-30 alex fprintf( stderr, "%s\n", msg );
204 bebf0383 2002-03-30 alex fflush( stderr );
205 bebf0383 2002-03-30 alex }
206 bebf0383 2002-03-30 alex
207 bebf0383 2002-03-30 alex if( snotice )
208 bebf0383 2002-03-30 alex {
209 bebf0383 2002-03-30 alex /* NOTICE an lokale User mit "s"-Mode */
210 bebf0383 2002-03-30 alex Wall_ServerNotice( msg );
211 bebf0383 2002-03-30 alex }
212 f4ade537 2001-12-11 alex } /* Log */
213 f4ade537 2001-12-11 alex
214 f4ade537 2001-12-11 alex
215 c2f60abe 2002-05-27 alex GLOBAL VOID
216 c2f60abe 2002-05-27 alex Log_Init_Resolver( VOID )
217 ec1922ef 2001-12-29 alex {
218 c40592d2 2003-12-26 alex #ifdef SYSLOG
219 62796722 2003-03-31 alex openlog( PACKAGE_NAME, LOG_CONS|LOG_PID, LOG_LOCAL5 );
220 ec1922ef 2001-12-29 alex #endif
221 26390c60 2004-05-10 alex Log_Resolver( LOG_DEBUG, "Resolver sub-process starting, PID %d.", getpid( ));
222 ec1922ef 2001-12-29 alex } /* Log_Init_Resolver */
223 ec1922ef 2001-12-29 alex
224 ec1922ef 2001-12-29 alex
225 c2f60abe 2002-05-27 alex GLOBAL VOID
226 c2f60abe 2002-05-27 alex Log_Exit_Resolver( VOID )
227 ec1922ef 2001-12-29 alex {
228 26390c60 2004-05-10 alex Log_Resolver( LOG_DEBUG, "Resolver sub-process %d done.", getpid( ));
229 c40592d2 2003-12-26 alex #ifdef SYSLOG
230 ec1922ef 2001-12-29 alex closelog( );
231 ec1922ef 2001-12-29 alex #endif
232 ec1922ef 2001-12-29 alex } /* Log_Exit_Resolver */
233 ec1922ef 2001-12-29 alex
234 ec1922ef 2001-12-29 alex
235 f7327524 2002-05-30 alex #ifdef PROTOTYPES
236 c2f60abe 2002-05-27 alex GLOBAL VOID
237 c2f60abe 2002-05-27 alex Log_Resolver( CONST INT Level, CONST CHAR *Format, ... )
238 f7327524 2002-05-30 alex #else
239 f7327524 2002-05-30 alex GLOBAL VOID
240 f7327524 2002-05-30 alex Log_Resolver( Level, Format, va_alist )
241 f7327524 2002-05-30 alex CONST INT Level;
242 f7327524 2002-05-30 alex CONST CHAR *Format;
243 f7327524 2002-05-30 alex va_dcl
244 f7327524 2002-05-30 alex #endif
245 ec1922ef 2001-12-29 alex {
246 ec1922ef 2001-12-29 alex /* Eintrag des Resolver in Logfile(s) schreiben */
247 ec1922ef 2001-12-29 alex
248 ec1922ef 2001-12-29 alex CHAR msg[MAX_LOG_MSG_LEN];
249 ec1922ef 2001-12-29 alex va_list ap;
250 ec1922ef 2001-12-29 alex
251 ec1922ef 2001-12-29 alex assert( Format != NULL );
252 d040fa2a 2002-09-09 alex
253 006c0328 2002-01-11 alex #ifdef DEBUG
254 006c0328 2002-01-11 alex if(( Level == LOG_DEBUG ) && ( ! NGIRCd_Debug )) return;
255 006c0328 2002-01-11 alex #else
256 ec1922ef 2001-12-29 alex if( Level == LOG_DEBUG ) return;
257 ec1922ef 2001-12-29 alex #endif
258 ec1922ef 2001-12-29 alex
259 ec1922ef 2001-12-29 alex /* String mit variablen Argumenten zusammenbauen ... */
260 f7327524 2002-05-30 alex #ifdef PROTOTYPES
261 ec1922ef 2001-12-29 alex va_start( ap, Format );
262 f7327524 2002-05-30 alex #else
263 f7327524 2002-05-30 alex va_start( ap );
264 f7327524 2002-05-30 alex #endif
265 10363b39 2002-03-03 alex vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
266 10363b39 2002-03-03 alex va_end( ap );
267 ec1922ef 2001-12-29 alex
268 26390c60 2004-05-10 alex /* Output */
269 26390c60 2004-05-10 alex if( NGIRCd_NoDaemon )
270 26390c60 2004-05-10 alex {
271 26390c60 2004-05-10 alex /* Output to console */
272 364ff708 2004-06-26 alex fprintf( stdout, "[%d:%d] %s\n", (INT)getpid( ), Level, msg );
273 26390c60 2004-05-10 alex fflush( stdout );
274 26390c60 2004-05-10 alex }
275 26390c60 2004-05-10 alex #ifdef SYSLOG
276 26390c60 2004-05-10 alex else syslog( Level, msg );
277 03783eea 2002-01-05 alex #endif
278 ec1922ef 2001-12-29 alex } /* Log_Resolver */
279 ec1922ef 2001-12-29 alex
280 ec1922ef 2001-12-29 alex
281 c2f60abe 2002-05-27 alex LOCAL VOID
282 c2f60abe 2002-05-27 alex Wall_ServerNotice( CHAR *Msg )
283 20a2ffef 2002-03-27 alex {
284 773d886d 2002-03-29 alex /* Server-Notice an entsprechende User verschicken */
285 773d886d 2002-03-29 alex
286 20a2ffef 2002-03-27 alex CLIENT *c;
287 20a2ffef 2002-03-27 alex
288 20a2ffef 2002-03-27 alex assert( Msg != NULL );
289 773d886d 2002-03-29 alex
290 20a2ffef 2002-03-27 alex c = Client_First( );
291 20a2ffef 2002-03-27 alex while( c )
292 20a2ffef 2002-03-27 alex {
293 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 );
294 20a2ffef 2002-03-27 alex c = Client_Next( c );
295 20a2ffef 2002-03-27 alex }
296 20a2ffef 2002-03-27 alex } /* Wall_ServerNotice */
297 20a2ffef 2002-03-27 alex
298 20a2ffef 2002-03-27 alex
299 f4ade537 2001-12-11 alex /* -eof- */