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)
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.
11 490f28ff 2002-12-12 alex * Logging functions
15 ca33cbda 2002-03-12 alex #include "portab.h"
17 c1f32e82 2004-10-20 alex static char UNUSED id[] = "$Id: log.c,v 1.48 2004/10/20 13:47:32 alex Exp $";
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>
25 c1f32e82 2004-10-20 alex # include <varargs.h>
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>
32 c40592d2 2003-12-26 alex #ifdef SYSLOG
33 3d1a0962 2001-12-12 alex #include <syslog.h>
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"
43 ca33cbda 2002-03-12 alex #include "exp.h"
44 f4ade537 2001-12-11 alex #include "log.h"
47 1c00ddff 2002-03-29 alex LOCAL CHAR Error_File[FNAME_LEN];
48 3d74a9c3 2002-03-29 alex LOCAL CHAR Init_Txt[127];
51 c2f60abe 2002-05-27 alex LOCAL VOID Wall_ServerNotice PARAMS(( CHAR *Msg ));
55 c2f60abe 2002-05-27 alex Log_Init( VOID )
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 );
62 fcb47ae6 2002-02-19 alex /* Hello World! */
63 006c0328 2002-01-11 alex Log( LOG_NOTICE, "%s started.", NGIRCd_Version( ));
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 )
70 3d74a9c3 2002-03-29 alex if( Init_Txt[0] ) strcat( Init_Txt, ", " );
71 3d74a9c3 2002-03-29 alex strcat( Init_Txt, "debug-mode" );
74 fcb47ae6 2002-02-19 alex if( NGIRCd_NoDaemon )
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" );
79 fcb47ae6 2002-02-19 alex if( NGIRCd_Passive )
81 3d74a9c3 2002-03-29 alex if( Init_Txt[0] ) strcat( Init_Txt, ", " );
82 3d74a9c3 2002-03-29 alex strcat( Init_Txt, "passive-mode" );
84 fcb47ae6 2002-02-19 alex #ifdef SNIFFER
85 fcb47ae6 2002-02-19 alex if( NGIRCd_Sniffer )
87 3d74a9c3 2002-03-29 alex if( Init_Txt[0] ) strcat( Init_Txt, ", " );
88 3d74a9c3 2002-03-29 alex strcat( Init_Txt, "network sniffer" );
91 3d74a9c3 2002-03-29 alex if( Init_Txt[0] ) Log( LOG_INFO, "Activating: %s.", Init_Txt );
93 7281b8dd 2004-05-07 alex Error_File[0] = '\0';
94 3d74a9c3 2002-03-29 alex } /* Log_Init */
98 c2f60abe 2002-05-27 alex Log_InitErrorfile( VOID )
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. */
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( ));
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 ))
110 773d886d 2002-03-29 alex Log( LOG_ERR, "Can't reopen stderr (\"%s\"): %s", Error_File, strerror( errno ));
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 );
120 b58ab32b 2002-03-31 alex Log( LOG_DEBUG, "Redirected stderr to \"%s\".", Error_File );
121 3d74a9c3 2002-03-29 alex } /* Log_InitErrfile */
124 c2f60abe 2002-05-27 alex GLOBAL VOID
125 c2f60abe 2002-05-27 alex Log_Exit( VOID )
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 );
131 7281b8dd 2004-05-07 alex if( Error_File[0] )
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 ));
137 c40592d2 2003-12-26 alex #ifdef SYSLOG
138 fcb47ae6 2002-02-19 alex /* syslog abmelden */
139 3d1a0962 2001-12-12 alex closelog( );
141 f4ade537 2001-12-11 alex } /* Log_Exit */
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, ... )
148 f7327524 2002-05-30 alex GLOBAL VOID
149 f7327524 2002-05-30 alex Log( Level, Format, va_alist )
151 f7327524 2002-05-30 alex CONST CHAR *Format;
155 f4ade537 2001-12-11 alex /* Eintrag in Logfile(s) schreiben */
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;
161 ec1922ef 2001-12-29 alex assert( Format != NULL );
163 20a2ffef 2002-03-27 alex if( Level & LOG_snotice )
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;
169 773d886d 2002-03-29 alex else snotice = FALSE;
171 006c0328 2002-01-11 alex #ifdef DEBUG
172 006c0328 2002-01-11 alex if(( Level == LOG_DEBUG ) && ( ! NGIRCd_Debug )) return;
174 d4a60bd4 2001-12-25 alex if( Level == LOG_DEBUG ) return;
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 );
181 f7327524 2002-05-30 alex va_start( ap );
183 10363b39 2002-03-03 alex vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
184 10363b39 2002-03-03 alex va_end( ap );
186 bebf0383 2002-03-30 alex if( NGIRCd_NoDaemon )
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 );
192 c40592d2 2003-12-26 alex #ifdef SYSLOG
195 d040fa2a 2002-09-09 alex /* Syslog */
196 ec474a4b 2002-10-04 alex syslog( Level, "%s", msg );
200 bebf0383 2002-03-30 alex if( Level <= LOG_CRIT )
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 );
207 bebf0383 2002-03-30 alex if( snotice )
209 bebf0383 2002-03-30 alex /* NOTICE an lokale User mit "s"-Mode */
210 bebf0383 2002-03-30 alex Wall_ServerNotice( msg );
212 f4ade537 2001-12-11 alex } /* Log */
215 c2f60abe 2002-05-27 alex GLOBAL VOID
216 c2f60abe 2002-05-27 alex Log_Init_Resolver( VOID )
218 c40592d2 2003-12-26 alex #ifdef SYSLOG
219 62796722 2003-03-31 alex openlog( PACKAGE_NAME, LOG_CONS|LOG_PID, LOG_LOCAL5 );
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 */
225 c2f60abe 2002-05-27 alex GLOBAL VOID
226 c2f60abe 2002-05-27 alex Log_Exit_Resolver( VOID )
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( );
232 ec1922ef 2001-12-29 alex } /* Log_Exit_Resolver */
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, ... )
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;
246 ec1922ef 2001-12-29 alex /* Eintrag des Resolver in Logfile(s) schreiben */
248 ec1922ef 2001-12-29 alex CHAR msg[MAX_LOG_MSG_LEN];
249 ec1922ef 2001-12-29 alex va_list ap;
251 ec1922ef 2001-12-29 alex assert( Format != NULL );
253 006c0328 2002-01-11 alex #ifdef DEBUG
254 006c0328 2002-01-11 alex if(( Level == LOG_DEBUG ) && ( ! NGIRCd_Debug )) return;
256 ec1922ef 2001-12-29 alex if( Level == LOG_DEBUG ) return;
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 );
263 f7327524 2002-05-30 alex va_start( ap );
265 10363b39 2002-03-03 alex vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
266 10363b39 2002-03-03 alex va_end( ap );
268 26390c60 2004-05-10 alex /* Output */
269 26390c60 2004-05-10 alex if( NGIRCd_NoDaemon )
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 );
275 26390c60 2004-05-10 alex #ifdef SYSLOG
276 26390c60 2004-05-10 alex else syslog( Level, msg );
278 ec1922ef 2001-12-29 alex } /* Log_Resolver */
282 c2f60abe 2002-05-27 alex Wall_ServerNotice( CHAR *Msg )
284 773d886d 2002-03-29 alex /* Server-Notice an entsprechende User verschicken */
288 20a2ffef 2002-03-27 alex assert( Msg != NULL );
290 20a2ffef 2002-03-27 alex c = Client_First( );
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 );
296 20a2ffef 2002-03-27 alex } /* Wall_ServerNotice */
299 f4ade537 2001-12-11 alex /* -eof- */