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 95375426 2005-02-10 alex static char UNUSED id[] = "$Id: log.c,v 1.52 2005/02/10 12:49:04 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 3d74a9c3 2002-03-29 alex LOCAL CHAR Init_Txt[127];
49 95375426 2005-02-10 alex #ifdef DEBUG
50 95375426 2005-02-10 alex LOCAL CHAR Error_File[FNAME_LEN];
54 c2f60abe 2002-05-27 alex LOCAL VOID Wall_ServerNotice PARAMS(( CHAR *Msg ));
58 c2f60abe 2002-05-27 alex Log_Init( VOID )
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 );
65 fcb47ae6 2002-02-19 alex /* Hello World! */
66 894cd2cd 2005-02-09 alex Log( LOG_NOTICE, "%s started.", NGIRCd_Version );
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 )
73 d16ce5a7 2005-01-20 alex strcpy( Init_Txt, "debug-mode" );
76 fcb47ae6 2002-02-19 alex if( NGIRCd_NoDaemon )
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" );
81 fcb47ae6 2002-02-19 alex if( NGIRCd_Passive )
83 3d74a9c3 2002-03-29 alex if( Init_Txt[0] ) strcat( Init_Txt, ", " );
84 3d74a9c3 2002-03-29 alex strcat( Init_Txt, "passive-mode" );
86 fcb47ae6 2002-02-19 alex #ifdef SNIFFER
87 fcb47ae6 2002-02-19 alex if( NGIRCd_Sniffer )
89 3d74a9c3 2002-03-29 alex if( Init_Txt[0] ) strcat( Init_Txt, ", " );
90 3d74a9c3 2002-03-29 alex strcat( Init_Txt, "network sniffer" );
93 3d74a9c3 2002-03-29 alex if( Init_Txt[0] ) Log( LOG_INFO, "Activating: %s.", Init_Txt );
95 95375426 2005-02-10 alex #ifdef DEBUG
96 7281b8dd 2004-05-07 alex Error_File[0] = '\0';
98 3d74a9c3 2002-03-29 alex } /* Log_Init */
101 95375426 2005-02-10 alex #ifdef DEBUG
103 c2f60abe 2002-05-27 alex GLOBAL VOID
104 c2f60abe 2002-05-27 alex Log_InitErrorfile( VOID )
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. */
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( ));
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 ))
116 773d886d 2002-03-29 alex Log( LOG_ERR, "Can't reopen stderr (\"%s\"): %s", Error_File, strerror( errno ));
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 );
126 b58ab32b 2002-03-31 alex Log( LOG_DEBUG, "Redirected stderr to \"%s\".", Error_File );
127 3d74a9c3 2002-03-29 alex } /* Log_InitErrfile */
132 c2f60abe 2002-05-27 alex GLOBAL VOID
133 c2f60abe 2002-05-27 alex Log_Exit( VOID )
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 );
139 95375426 2005-02-10 alex #ifdef DEBUG
140 7281b8dd 2004-05-07 alex if( Error_File[0] )
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 ));
147 c40592d2 2003-12-26 alex #ifdef SYSLOG
148 fcb47ae6 2002-02-19 alex /* syslog abmelden */
149 3d1a0962 2001-12-12 alex closelog( );
151 f4ade537 2001-12-11 alex } /* Log_Exit */
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, ... )
158 f7327524 2002-05-30 alex GLOBAL VOID
159 f7327524 2002-05-30 alex Log( Level, Format, va_alist )
161 f7327524 2002-05-30 alex CONST CHAR *Format;
165 f4ade537 2001-12-11 alex /* Eintrag in Logfile(s) schreiben */
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;
171 ec1922ef 2001-12-29 alex assert( Format != NULL );
173 20a2ffef 2002-03-27 alex if( Level & LOG_snotice )
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;
179 773d886d 2002-03-29 alex else snotice = FALSE;
181 006c0328 2002-01-11 alex #ifdef DEBUG
182 006c0328 2002-01-11 alex if(( Level == LOG_DEBUG ) && ( ! NGIRCd_Debug )) return;
184 d4a60bd4 2001-12-25 alex if( Level == LOG_DEBUG ) return;
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 );
191 f7327524 2002-05-30 alex va_start( ap );
193 10363b39 2002-03-03 alex vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
194 10363b39 2002-03-03 alex va_end( ap );
196 bebf0383 2002-03-30 alex if( NGIRCd_NoDaemon )
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 );
202 c40592d2 2003-12-26 alex #ifdef SYSLOG
205 d040fa2a 2002-09-09 alex /* Syslog */
206 ec474a4b 2002-10-04 alex syslog( Level, "%s", msg );
210 bebf0383 2002-03-30 alex if( Level <= LOG_CRIT )
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 );
217 bebf0383 2002-03-30 alex if( snotice )
219 bebf0383 2002-03-30 alex /* NOTICE an lokale User mit "s"-Mode */
220 bebf0383 2002-03-30 alex Wall_ServerNotice( msg );
222 f4ade537 2001-12-11 alex } /* Log */
225 c2f60abe 2002-05-27 alex GLOBAL VOID
226 c2f60abe 2002-05-27 alex Log_Init_Resolver( VOID )
228 c40592d2 2003-12-26 alex #ifdef SYSLOG
229 62796722 2003-03-31 alex openlog( PACKAGE_NAME, LOG_CONS|LOG_PID, LOG_LOCAL5 );
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 */
235 c2f60abe 2002-05-27 alex GLOBAL VOID
236 c2f60abe 2002-05-27 alex Log_Exit_Resolver( VOID )
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( );
242 ec1922ef 2001-12-29 alex } /* Log_Exit_Resolver */
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, ... )
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;
256 ec1922ef 2001-12-29 alex /* Eintrag des Resolver in Logfile(s) schreiben */
258 ec1922ef 2001-12-29 alex CHAR msg[MAX_LOG_MSG_LEN];
259 ec1922ef 2001-12-29 alex va_list ap;
261 ec1922ef 2001-12-29 alex assert( Format != NULL );
263 006c0328 2002-01-11 alex #ifdef DEBUG
264 006c0328 2002-01-11 alex if(( Level == LOG_DEBUG ) && ( ! NGIRCd_Debug )) return;
266 ec1922ef 2001-12-29 alex if( Level == LOG_DEBUG ) return;
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 );
273 f7327524 2002-05-30 alex va_start( ap );
275 10363b39 2002-03-03 alex vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
276 10363b39 2002-03-03 alex va_end( ap );
278 26390c60 2004-05-10 alex /* Output */
279 26390c60 2004-05-10 alex if( NGIRCd_NoDaemon )
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 );
285 26390c60 2004-05-10 alex #ifdef SYSLOG
286 8308c170 2005-02-03 alex else syslog( Level, "%s", msg );
288 ec1922ef 2001-12-29 alex } /* Log_Resolver */
292 c2f60abe 2002-05-27 alex Wall_ServerNotice( CHAR *Msg )
294 773d886d 2002-03-29 alex /* Server-Notice an entsprechende User verschicken */
298 20a2ffef 2002-03-27 alex assert( Msg != NULL );
300 20a2ffef 2002-03-27 alex c = Client_First( );
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 );
306 20a2ffef 2002-03-27 alex } /* Wall_ServerNotice */
309 f4ade537 2001-12-11 alex /* -eof- */