Blob


1 /*
2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001,2002 by Alexander Barton (alex@barton.de)
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 * Please read the file COPYING, README and AUTHORS for more information.
10 *
11 * Logging functions
12 */
15 #include "portab.h"
17 static char UNUSED id[] = "$Id: log.c,v 1.42 2002/12/19 04:30:00 alex Exp $";
19 #include "imp.h"
20 #include <assert.h>
21 #include <errno.h>
22 #include <stdarg.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <sys/types.h>
26 #include <unistd.h>
28 #ifdef USE_SYSLOG
29 #include <syslog.h>
30 #endif
32 #include "ngircd.h"
33 #include "defines.h"
34 #include "conn.h"
35 #include "client.h"
36 #include "channel.h"
37 #include "irc-write.h"
39 #include "exp.h"
40 #include "log.h"
43 LOCAL CHAR Error_File[FNAME_LEN];
44 LOCAL CHAR Init_Txt[127];
47 LOCAL VOID Wall_ServerNotice PARAMS(( CHAR *Msg ));
50 GLOBAL VOID
51 Log_Init( VOID )
52 {
53 #ifdef USE_SYSLOG
54 /* Syslog initialisieren */
55 openlog( PACKAGE, LOG_CONS|LOG_PID, LOG_LOCAL5 );
56 #endif
58 /* Hello World! */
59 Log( LOG_NOTICE, "%s started.", NGIRCd_Version( ));
61 /* Informationen uebern den "Operation Mode" */
62 strcpy( Init_Txt, "" );
63 #ifdef DEBUG
64 if( NGIRCd_Debug )
65 {
66 if( Init_Txt[0] ) strcat( Init_Txt, ", " );
67 strcat( Init_Txt, "debug-mode" );
68 }
69 #endif
70 if( NGIRCd_NoDaemon )
71 {
72 if( Init_Txt[0] ) strcat( Init_Txt, ", " );
73 strcat( Init_Txt, "no-daemon-mode" );
74 }
75 if( NGIRCd_Passive )
76 {
77 if( Init_Txt[0] ) strcat( Init_Txt, ", " );
78 strcat( Init_Txt, "passive-mode" );
79 }
80 #ifdef SNIFFER
81 if( NGIRCd_Sniffer )
82 {
83 if( Init_Txt[0] ) strcat( Init_Txt, ", " );
84 strcat( Init_Txt, "network sniffer" );
85 }
86 #endif
87 if( Init_Txt[0] ) Log( LOG_INFO, "Activating: %s.", Init_Txt );
88 } /* Log_Init */
91 GLOBAL VOID
92 Log_InitErrorfile( VOID )
93 {
94 /* "Error-Log" initialisieren: stderr in Datei umlenken. Dort
95 * landen z.B. alle Ausgaben von assert()-Aufrufen. */
97 /* Dateiname zusammen bauen */
98 sprintf( Error_File, "%s/%s-%ld.err", ERROR_DIR, PACKAGE, (LONG)getpid( ));
100 /* stderr umlenken */
101 fflush( stderr );
102 if( ! freopen( Error_File, "w", stderr ))
104 Log( LOG_ERR, "Can't reopen stderr (\"%s\"): %s", Error_File, strerror( errno ));
105 return;
108 /* Einige Infos in das Error-File schreiben */
109 fputs( ctime( &NGIRCd_Start ), stderr );
110 fprintf( stderr, "%s started.\n", NGIRCd_Version( ));
111 fprintf( stderr, "Activating: %s\n\n", Init_Txt[0] ? Init_Txt : "-" );
112 fflush( stderr );
114 Log( LOG_DEBUG, "Redirected stderr to \"%s\".", Error_File );
115 } /* Log_InitErrfile */
118 GLOBAL VOID
119 Log_Exit( VOID )
121 /* Good Bye! */
122 if( NGIRCd_SignalRestart ) Log( LOG_NOTICE, "%s done (restarting).", PACKAGE );
123 else Log( LOG_NOTICE, "%s done.", PACKAGE );
125 /* Error-File (stderr) loeschen */
126 if( unlink( Error_File ) != 0 ) Log( LOG_ERR, "Can't delete \"%s\": %s", Error_File, strerror( errno ));
128 #ifdef USE_SYSLOG
129 /* syslog abmelden */
130 closelog( );
131 #endif
132 } /* Log_Exit */
135 #ifdef PROTOTYPES
136 GLOBAL VOID
137 Log( INT Level, CONST CHAR *Format, ... )
138 #else
139 GLOBAL VOID
140 Log( Level, Format, va_alist )
141 INT Level;
142 CONST CHAR *Format;
143 va_dcl
144 #endif
146 /* Eintrag in Logfile(s) schreiben */
148 CHAR msg[MAX_LOG_MSG_LEN];
149 BOOLEAN snotice;
150 va_list ap;
152 assert( Format != NULL );
154 if( Level & LOG_snotice )
156 /* Notice an User mit "s" Mode */
157 snotice = TRUE;
158 Level &= ~LOG_snotice;
160 else snotice = FALSE;
162 #ifdef DEBUG
163 if(( Level == LOG_DEBUG ) && ( ! NGIRCd_Debug )) return;
164 #else
165 if( Level == LOG_DEBUG ) return;
166 #endif
168 /* String mit variablen Argumenten zusammenbauen ... */
169 #ifdef PROTOTYPES
170 va_start( ap, Format );
171 #else
172 va_start( ap );
173 #endif
174 vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
175 va_end( ap );
177 if( NGIRCd_NoDaemon )
179 /* auf Konsole ausgeben */
180 fprintf( stdout, "[%d] %s\n", Level, msg );
181 fflush( stdout );
183 #ifdef USE_SYSLOG
184 else
186 /* Syslog */
187 syslog( Level, "%s", msg );
189 #endif
191 if( Level <= LOG_CRIT )
193 /* Kritische Meldungen in Error-File (stderr) */
194 fprintf( stderr, "%s\n", msg );
195 fflush( stderr );
198 if( snotice )
200 /* NOTICE an lokale User mit "s"-Mode */
201 Wall_ServerNotice( msg );
203 } /* Log */
206 GLOBAL VOID
207 Log_Init_Resolver( VOID )
209 #ifdef USE_SYSLOG
210 openlog( PACKAGE, LOG_CONS|LOG_PID, LOG_LOCAL5 );
211 #endif
212 } /* Log_Init_Resolver */
215 GLOBAL VOID
216 Log_Exit_Resolver( VOID )
218 #ifdef USE_SYSLOG
219 closelog( );
220 #endif
221 } /* Log_Exit_Resolver */
224 #ifdef PROTOTYPES
225 GLOBAL VOID
226 Log_Resolver( CONST INT Level, CONST CHAR *Format, ... )
227 #else
228 GLOBAL VOID
229 Log_Resolver( Level, Format, va_alist )
230 CONST INT Level;
231 CONST CHAR *Format;
232 va_dcl
233 #endif
235 /* Eintrag des Resolver in Logfile(s) schreiben */
237 #ifndef USE_SYSLOG
238 return;
239 #else
241 CHAR msg[MAX_LOG_MSG_LEN];
242 va_list ap;
244 assert( Format != NULL );
246 if( NGIRCd_NoDaemon ) return;
248 #ifdef DEBUG
249 if(( Level == LOG_DEBUG ) && ( ! NGIRCd_Debug )) return;
250 #else
251 if( Level == LOG_DEBUG ) return;
252 #endif
254 /* String mit variablen Argumenten zusammenbauen ... */
255 #ifdef PROTOTYPES
256 va_start( ap, Format );
257 #else
258 va_start( ap );
259 #endif
260 vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
261 va_end( ap );
263 /* ... und ausgeben */
264 syslog( Level, msg );
266 #endif
267 } /* Log_Resolver */
270 LOCAL VOID
271 Wall_ServerNotice( CHAR *Msg )
273 /* Server-Notice an entsprechende User verschicken */
275 CLIENT *c;
277 assert( Msg != NULL );
279 c = Client_First( );
280 while( c )
282 if(( Client_Conn( c ) > NONE ) && ( Client_HasMode( c, 's' ))) IRC_WriteStrClient( c, "NOTICE %s :%s%s", Client_ThisServer( ), NOTICE_TXTPREFIX, Msg );
283 c = Client_Next( c );
285 } /* Wall_ServerNotice */
288 /* -eof- */