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