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 43a4bc5b 2002-12-19 alex static char UNUSED id[] = "$Id: log.c,v 1.42 2002/12/19 04:30:00 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 f4ade537 2001-12-11 alex #include <stdarg.h>
23 f4ade537 2001-12-11 alex #include <stdio.h>
24 3be92e9d 2001-12-26 alex #include <string.h>
25 fe2bc90e 2002-03-06 alex #include <sys/types.h>
26 fe2bc90e 2002-03-06 alex #include <unistd.h>
28 50352dc9 2001-12-27 alex #ifdef USE_SYSLOG
29 3d1a0962 2001-12-12 alex #include <syslog.h>
32 006c0328 2002-01-11 alex #include "ngircd.h"
33 ca33cbda 2002-03-12 alex #include "defines.h"
34 c2f60abe 2002-05-27 alex #include "conn.h"
35 c2f60abe 2002-05-27 alex #include "client.h"
36 c2f60abe 2002-05-27 alex #include "channel.h"
37 20a2ffef 2002-03-27 alex #include "irc-write.h"
39 ca33cbda 2002-03-12 alex #include "exp.h"
40 f4ade537 2001-12-11 alex #include "log.h"
43 1c00ddff 2002-03-29 alex LOCAL CHAR Error_File[FNAME_LEN];
44 3d74a9c3 2002-03-29 alex LOCAL CHAR Init_Txt[127];
47 c2f60abe 2002-05-27 alex LOCAL VOID Wall_ServerNotice PARAMS(( CHAR *Msg ));
51 c2f60abe 2002-05-27 alex Log_Init( VOID )
53 50352dc9 2001-12-27 alex #ifdef USE_SYSLOG
54 fcb47ae6 2002-02-19 alex /* Syslog initialisieren */
55 0dc0308d 2001-12-25 alex openlog( PACKAGE, LOG_CONS|LOG_PID, LOG_LOCAL5 );
58 fcb47ae6 2002-02-19 alex /* Hello World! */
59 006c0328 2002-01-11 alex Log( LOG_NOTICE, "%s started.", NGIRCd_Version( ));
61 fcb47ae6 2002-02-19 alex /* Informationen uebern den "Operation Mode" */
62 3d74a9c3 2002-03-29 alex strcpy( Init_Txt, "" );
63 fcb47ae6 2002-02-19 alex #ifdef DEBUG
64 fcb47ae6 2002-02-19 alex if( NGIRCd_Debug )
66 3d74a9c3 2002-03-29 alex if( Init_Txt[0] ) strcat( Init_Txt, ", " );
67 3d74a9c3 2002-03-29 alex strcat( Init_Txt, "debug-mode" );
70 fcb47ae6 2002-02-19 alex if( NGIRCd_NoDaemon )
72 3d74a9c3 2002-03-29 alex if( Init_Txt[0] ) strcat( Init_Txt, ", " );
73 3d74a9c3 2002-03-29 alex strcat( Init_Txt, "no-daemon-mode" );
75 fcb47ae6 2002-02-19 alex if( NGIRCd_Passive )
77 3d74a9c3 2002-03-29 alex if( Init_Txt[0] ) strcat( Init_Txt, ", " );
78 3d74a9c3 2002-03-29 alex strcat( Init_Txt, "passive-mode" );
80 fcb47ae6 2002-02-19 alex #ifdef SNIFFER
81 fcb47ae6 2002-02-19 alex if( NGIRCd_Sniffer )
83 3d74a9c3 2002-03-29 alex if( Init_Txt[0] ) strcat( Init_Txt, ", " );
84 3d74a9c3 2002-03-29 alex strcat( Init_Txt, "network sniffer" );
87 3d74a9c3 2002-03-29 alex if( Init_Txt[0] ) Log( LOG_INFO, "Activating: %s.", Init_Txt );
88 3d74a9c3 2002-03-29 alex } /* Log_Init */
92 c2f60abe 2002-05-27 alex Log_InitErrorfile( VOID )
94 773d886d 2002-03-29 alex /* "Error-Log" initialisieren: stderr in Datei umlenken. Dort
95 773d886d 2002-03-29 alex * landen z.B. alle Ausgaben von assert()-Aufrufen. */
97 b58ab32b 2002-03-31 alex /* Dateiname zusammen bauen */
98 c7b55aa6 2002-10-09 alex sprintf( Error_File, "%s/%s-%ld.err", ERROR_DIR, PACKAGE, (LONG)getpid( ));
100 b58ab32b 2002-03-31 alex /* stderr umlenken */
101 b58ab32b 2002-03-31 alex fflush( stderr );
102 773d886d 2002-03-29 alex if( ! freopen( Error_File, "w", stderr ))
104 773d886d 2002-03-29 alex Log( LOG_ERR, "Can't reopen stderr (\"%s\"): %s", Error_File, strerror( errno ));
108 b58ab32b 2002-03-31 alex /* Einige Infos in das Error-File schreiben */
109 d68fb7a3 2002-03-29 alex fputs( ctime( &NGIRCd_Start ), stderr );
110 773d886d 2002-03-29 alex fprintf( stderr, "%s started.\n", NGIRCd_Version( ));
111 3d74a9c3 2002-03-29 alex fprintf( stderr, "Activating: %s\n\n", Init_Txt[0] ? Init_Txt : "-" );
112 fe2bc90e 2002-03-06 alex fflush( stderr );
114 b58ab32b 2002-03-31 alex Log( LOG_DEBUG, "Redirected stderr to \"%s\".", Error_File );
115 3d74a9c3 2002-03-29 alex } /* Log_InitErrfile */
118 c2f60abe 2002-05-27 alex GLOBAL VOID
119 c2f60abe 2002-05-27 alex Log_Exit( VOID )
121 fcb47ae6 2002-02-19 alex /* Good Bye! */
122 43a4bc5b 2002-12-19 alex if( NGIRCd_SignalRestart ) Log( LOG_NOTICE, "%s done (restarting).", PACKAGE );
123 5cac5404 2002-06-02 alex else Log( LOG_NOTICE, "%s done.", PACKAGE );
125 1c00ddff 2002-03-29 alex /* Error-File (stderr) loeschen */
126 1c00ddff 2002-03-29 alex if( unlink( Error_File ) != 0 ) Log( LOG_ERR, "Can't delete \"%s\": %s", Error_File, strerror( errno ));
128 50352dc9 2001-12-27 alex #ifdef USE_SYSLOG
129 fcb47ae6 2002-02-19 alex /* syslog abmelden */
130 3d1a0962 2001-12-12 alex closelog( );
132 f4ade537 2001-12-11 alex } /* Log_Exit */
135 f7327524 2002-05-30 alex #ifdef PROTOTYPES
136 c2f60abe 2002-05-27 alex GLOBAL VOID
137 c2f60abe 2002-05-27 alex Log( INT Level, CONST CHAR *Format, ... )
139 f7327524 2002-05-30 alex GLOBAL VOID
140 f7327524 2002-05-30 alex Log( Level, Format, va_alist )
142 f7327524 2002-05-30 alex CONST CHAR *Format;
146 f4ade537 2001-12-11 alex /* Eintrag in Logfile(s) schreiben */
148 f4ade537 2001-12-11 alex CHAR msg[MAX_LOG_MSG_LEN];
149 20a2ffef 2002-03-27 alex BOOLEAN snotice;
150 f4ade537 2001-12-11 alex va_list ap;
152 ec1922ef 2001-12-29 alex assert( Format != NULL );
154 20a2ffef 2002-03-27 alex if( Level & LOG_snotice )
156 20a2ffef 2002-03-27 alex /* Notice an User mit "s" Mode */
157 20a2ffef 2002-03-27 alex snotice = TRUE;
158 20a2ffef 2002-03-27 alex Level &= ~LOG_snotice;
160 773d886d 2002-03-29 alex else snotice = FALSE;
162 006c0328 2002-01-11 alex #ifdef DEBUG
163 006c0328 2002-01-11 alex if(( Level == LOG_DEBUG ) && ( ! NGIRCd_Debug )) return;
165 d4a60bd4 2001-12-25 alex if( Level == LOG_DEBUG ) return;
168 f4ade537 2001-12-11 alex /* String mit variablen Argumenten zusammenbauen ... */
169 f7327524 2002-05-30 alex #ifdef PROTOTYPES
170 f4ade537 2001-12-11 alex va_start( ap, Format );
172 f7327524 2002-05-30 alex va_start( ap );
174 10363b39 2002-03-03 alex vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
175 10363b39 2002-03-03 alex va_end( ap );
177 bebf0383 2002-03-30 alex if( NGIRCd_NoDaemon )
179 bebf0383 2002-03-30 alex /* auf Konsole ausgeben */
180 40ebd470 2002-09-03 alex fprintf( stdout, "[%d] %s\n", Level, msg );
181 40ebd470 2002-09-03 alex fflush( stdout );
183 d040fa2a 2002-09-09 alex #ifdef USE_SYSLOG
186 d040fa2a 2002-09-09 alex /* Syslog */
187 ec474a4b 2002-10-04 alex syslog( Level, "%s", msg );
191 bebf0383 2002-03-30 alex if( Level <= LOG_CRIT )
193 bebf0383 2002-03-30 alex /* Kritische Meldungen in Error-File (stderr) */
194 bebf0383 2002-03-30 alex fprintf( stderr, "%s\n", msg );
195 bebf0383 2002-03-30 alex fflush( stderr );
198 bebf0383 2002-03-30 alex if( snotice )
200 bebf0383 2002-03-30 alex /* NOTICE an lokale User mit "s"-Mode */
201 bebf0383 2002-03-30 alex Wall_ServerNotice( msg );
203 f4ade537 2001-12-11 alex } /* Log */
206 c2f60abe 2002-05-27 alex GLOBAL VOID
207 c2f60abe 2002-05-27 alex Log_Init_Resolver( VOID )
209 ec1922ef 2001-12-29 alex #ifdef USE_SYSLOG
210 ec1922ef 2001-12-29 alex openlog( PACKAGE, LOG_CONS|LOG_PID, LOG_LOCAL5 );
212 ec1922ef 2001-12-29 alex } /* Log_Init_Resolver */
215 c2f60abe 2002-05-27 alex GLOBAL VOID
216 c2f60abe 2002-05-27 alex Log_Exit_Resolver( VOID )
218 ec1922ef 2001-12-29 alex #ifdef USE_SYSLOG
219 ec1922ef 2001-12-29 alex closelog( );
221 ec1922ef 2001-12-29 alex } /* Log_Exit_Resolver */
224 f7327524 2002-05-30 alex #ifdef PROTOTYPES
225 c2f60abe 2002-05-27 alex GLOBAL VOID
226 c2f60abe 2002-05-27 alex Log_Resolver( CONST INT Level, CONST CHAR *Format, ... )
228 f7327524 2002-05-30 alex GLOBAL VOID
229 f7327524 2002-05-30 alex Log_Resolver( Level, Format, va_alist )
230 f7327524 2002-05-30 alex CONST INT Level;
231 f7327524 2002-05-30 alex CONST CHAR *Format;
235 ec1922ef 2001-12-29 alex /* Eintrag des Resolver in Logfile(s) schreiben */
237 03783eea 2002-01-05 alex #ifndef USE_SYSLOG
241 ec1922ef 2001-12-29 alex CHAR msg[MAX_LOG_MSG_LEN];
242 ec1922ef 2001-12-29 alex va_list ap;
244 ec1922ef 2001-12-29 alex assert( Format != NULL );
246 d040fa2a 2002-09-09 alex if( NGIRCd_NoDaemon ) return;
248 006c0328 2002-01-11 alex #ifdef DEBUG
249 006c0328 2002-01-11 alex if(( Level == LOG_DEBUG ) && ( ! NGIRCd_Debug )) return;
251 ec1922ef 2001-12-29 alex if( Level == LOG_DEBUG ) return;
254 ec1922ef 2001-12-29 alex /* String mit variablen Argumenten zusammenbauen ... */
255 f7327524 2002-05-30 alex #ifdef PROTOTYPES
256 ec1922ef 2001-12-29 alex va_start( ap, Format );
258 f7327524 2002-05-30 alex va_start( ap );
260 10363b39 2002-03-03 alex vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
261 10363b39 2002-03-03 alex va_end( ap );
263 ec1922ef 2001-12-29 alex /* ... und ausgeben */
264 ec1922ef 2001-12-29 alex syslog( Level, msg );
267 ec1922ef 2001-12-29 alex } /* Log_Resolver */
271 c2f60abe 2002-05-27 alex Wall_ServerNotice( CHAR *Msg )
273 773d886d 2002-03-29 alex /* Server-Notice an entsprechende User verschicken */
277 20a2ffef 2002-03-27 alex assert( Msg != NULL );
279 20a2ffef 2002-03-27 alex c = Client_First( );
282 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 );
283 20a2ffef 2002-03-27 alex c = Client_Next( c );
285 20a2ffef 2002-03-27 alex } /* Wall_ServerNotice */
288 f4ade537 2001-12-11 alex /* -eof- */