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 aee48a3b 2005-04-16 fw static char UNUSED id[] = "$Id: log.c,v 1.54 2005/04/16 09:31:30 fw 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 8adff592 2005-03-19 fw LOCAL char Init_Txt[127];
49 95375426 2005-02-10 alex #ifdef DEBUG
50 8adff592 2005-03-19 fw LOCAL char Error_File[FNAME_LEN];
54 8adff592 2005-03-19 fw LOCAL void Wall_ServerNotice PARAMS(( char *Msg ));
58 8adff592 2005-03-19 fw Log_Init( void )
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
64 aee48a3b 2005-04-16 fw #ifndef LOG_LOCAL5
65 aee48a3b 2005-04-16 fw #define LOG_LOCAL5 0
67 fcb47ae6 2002-02-19 alex /* Syslog initialisieren */
68 62796722 2003-03-31 alex openlog( PACKAGE_NAME, LOG_CONS|LOG_PID, LOG_LOCAL5 );
71 fcb47ae6 2002-02-19 alex /* Hello World! */
72 894cd2cd 2005-02-09 alex Log( LOG_NOTICE, "%s started.", NGIRCd_Version );
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 )
79 d16ce5a7 2005-01-20 alex strcpy( Init_Txt, "debug-mode" );
82 fcb47ae6 2002-02-19 alex if( NGIRCd_NoDaemon )
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" );
87 fcb47ae6 2002-02-19 alex if( NGIRCd_Passive )
89 3d74a9c3 2002-03-29 alex if( Init_Txt[0] ) strcat( Init_Txt, ", " );
90 3d74a9c3 2002-03-29 alex strcat( Init_Txt, "passive-mode" );
92 fcb47ae6 2002-02-19 alex #ifdef SNIFFER
93 fcb47ae6 2002-02-19 alex if( NGIRCd_Sniffer )
95 3d74a9c3 2002-03-29 alex if( Init_Txt[0] ) strcat( Init_Txt, ", " );
96 3d74a9c3 2002-03-29 alex strcat( Init_Txt, "network sniffer" );
99 3d74a9c3 2002-03-29 alex if( Init_Txt[0] ) Log( LOG_INFO, "Activating: %s.", Init_Txt );
101 95375426 2005-02-10 alex #ifdef DEBUG
102 7281b8dd 2004-05-07 alex Error_File[0] = '\0';
104 3d74a9c3 2002-03-29 alex } /* Log_Init */
107 95375426 2005-02-10 alex #ifdef DEBUG
110 8adff592 2005-03-19 fw Log_InitErrorfile( void )
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. */
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( ));
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 ))
122 773d886d 2002-03-29 alex Log( LOG_ERR, "Can't reopen stderr (\"%s\"): %s", Error_File, strerror( errno ));
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 );
132 b58ab32b 2002-03-31 alex Log( LOG_DEBUG, "Redirected stderr to \"%s\".", Error_File );
133 3d74a9c3 2002-03-29 alex } /* Log_InitErrfile */
139 8adff592 2005-03-19 fw Log_Exit( void )
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 );
145 95375426 2005-02-10 alex #ifdef DEBUG
146 7281b8dd 2004-05-07 alex if( Error_File[0] )
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 ));
153 c40592d2 2003-12-26 alex #ifdef SYSLOG
154 fcb47ae6 2002-02-19 alex /* syslog abmelden */
155 3d1a0962 2001-12-12 alex closelog( );
157 f4ade537 2001-12-11 alex } /* Log_Exit */
160 f7327524 2002-05-30 alex #ifdef PROTOTYPES
162 8adff592 2005-03-19 fw Log( int Level, const char *Format, ... )
165 f7327524 2002-05-30 alex Log( Level, Format, va_alist )
167 8adff592 2005-03-19 fw const char *Format;
171 f4ade537 2001-12-11 alex /* Eintrag in Logfile(s) schreiben */
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;
177 ec1922ef 2001-12-29 alex assert( Format != NULL );
179 20a2ffef 2002-03-27 alex if( Level & LOG_snotice )
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;
185 8adff592 2005-03-19 fw else snotice = false;
187 006c0328 2002-01-11 alex #ifdef DEBUG
188 006c0328 2002-01-11 alex if(( Level == LOG_DEBUG ) && ( ! NGIRCd_Debug )) return;
190 d4a60bd4 2001-12-25 alex if( Level == LOG_DEBUG ) return;
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 );
197 f7327524 2002-05-30 alex va_start( ap );
199 10363b39 2002-03-03 alex vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
200 10363b39 2002-03-03 alex va_end( ap );
202 bebf0383 2002-03-30 alex if( NGIRCd_NoDaemon )
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 );
208 c40592d2 2003-12-26 alex #ifdef SYSLOG
211 d040fa2a 2002-09-09 alex /* Syslog */
212 ec474a4b 2002-10-04 alex syslog( Level, "%s", msg );
216 bebf0383 2002-03-30 alex if( Level <= LOG_CRIT )
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 );
223 bebf0383 2002-03-30 alex if( snotice )
225 bebf0383 2002-03-30 alex /* NOTICE an lokale User mit "s"-Mode */
226 bebf0383 2002-03-30 alex Wall_ServerNotice( msg );
228 f4ade537 2001-12-11 alex } /* Log */
232 8adff592 2005-03-19 fw Log_Init_Resolver( void )
234 c40592d2 2003-12-26 alex #ifdef SYSLOG
235 62796722 2003-03-31 alex openlog( PACKAGE_NAME, LOG_CONS|LOG_PID, LOG_LOCAL5 );
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 */
242 8adff592 2005-03-19 fw Log_Exit_Resolver( void )
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( );
248 ec1922ef 2001-12-29 alex } /* Log_Exit_Resolver */
251 f7327524 2002-05-30 alex #ifdef PROTOTYPES
253 8adff592 2005-03-19 fw Log_Resolver( const int Level, const char *Format, ... )
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;
262 ec1922ef 2001-12-29 alex /* Eintrag des Resolver in Logfile(s) schreiben */
264 8adff592 2005-03-19 fw char msg[MAX_LOG_MSG_LEN];
265 ec1922ef 2001-12-29 alex va_list ap;
267 ec1922ef 2001-12-29 alex assert( Format != NULL );
269 006c0328 2002-01-11 alex #ifdef DEBUG
270 006c0328 2002-01-11 alex if(( Level == LOG_DEBUG ) && ( ! NGIRCd_Debug )) return;
272 ec1922ef 2001-12-29 alex if( Level == LOG_DEBUG ) return;
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 );
279 f7327524 2002-05-30 alex va_start( ap );
281 10363b39 2002-03-03 alex vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
282 10363b39 2002-03-03 alex va_end( ap );
284 26390c60 2004-05-10 alex /* Output */
285 26390c60 2004-05-10 alex if( NGIRCd_NoDaemon )
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 );
291 26390c60 2004-05-10 alex #ifdef SYSLOG
292 8308c170 2005-02-03 alex else syslog( Level, "%s", msg );
294 ec1922ef 2001-12-29 alex } /* Log_Resolver */
298 8adff592 2005-03-19 fw Wall_ServerNotice( char *Msg )
300 773d886d 2002-03-29 alex /* Server-Notice an entsprechende User verschicken */
304 20a2ffef 2002-03-27 alex assert( Msg != NULL );
306 20a2ffef 2002-03-27 alex c = Client_First( );
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 );
312 20a2ffef 2002-03-27 alex } /* Wall_ServerNotice */
315 f4ade537 2001-12-11 alex /* -eof- */