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.54 2005/04/16 09:31:30 fw Exp $";
19 #include "imp.h"
20 #include <assert.h>
21 #include <errno.h>
22 #ifdef PROTOTYPES
23 # include <stdarg.h>
24 #else
25 # include <varargs.h>
26 #endif
27 #include <stdio.h>
28 #include <string.h>
29 #include <sys/types.h>
30 #include <unistd.h>
32 #ifdef SYSLOG
33 #include <syslog.h>
34 #endif
36 #include "ngircd.h"
37 #include "defines.h"
38 #include "conn.h"
39 #include "client.h"
40 #include "channel.h"
41 #include "irc-write.h"
43 #include "exp.h"
44 #include "log.h"
47 LOCAL char Init_Txt[127];
49 #ifdef DEBUG
50 LOCAL char Error_File[FNAME_LEN];
51 #endif
54 LOCAL void Wall_ServerNotice PARAMS(( char *Msg ));
57 GLOBAL void
58 Log_Init( void )
59 {
60 #ifdef SYSLOG
61 #ifndef LOG_CONS /* Kludge: mips-dec-ultrix4.5 has no LOG_CONS/LOG_LOCAL5 */
62 #define LOG_CONS 0
63 #endif
64 #ifndef LOG_LOCAL5
65 #define LOG_LOCAL5 0
66 #endif
67 /* Syslog initialisieren */
68 openlog( PACKAGE_NAME, LOG_CONS|LOG_PID, LOG_LOCAL5 );
69 #endif
71 /* Hello World! */
72 Log( LOG_NOTICE, "%s started.", NGIRCd_Version );
74 /* Informationen uebern den "Operation Mode" */
75 Init_Txt[0] = '\0';
76 #ifdef DEBUG
77 if( NGIRCd_Debug )
78 {
79 strcpy( Init_Txt, "debug-mode" );
80 }
81 #endif
82 if( NGIRCd_NoDaemon )
83 {
84 if( Init_Txt[0] ) strcat( Init_Txt, ", " );
85 strcat( Init_Txt, "no-daemon-mode" );
86 }
87 if( NGIRCd_Passive )
88 {
89 if( Init_Txt[0] ) strcat( Init_Txt, ", " );
90 strcat( Init_Txt, "passive-mode" );
91 }
92 #ifdef SNIFFER
93 if( NGIRCd_Sniffer )
94 {
95 if( Init_Txt[0] ) strcat( Init_Txt, ", " );
96 strcat( Init_Txt, "network sniffer" );
97 }
98 #endif
99 if( Init_Txt[0] ) Log( LOG_INFO, "Activating: %s.", Init_Txt );
101 #ifdef DEBUG
102 Error_File[0] = '\0';
103 #endif
104 } /* Log_Init */
107 #ifdef DEBUG
109 GLOBAL void
110 Log_InitErrorfile( void )
112 /* "Error-Log" initialisieren: stderr in Datei umlenken. Dort
113 * landen z.B. alle Ausgaben von assert()-Aufrufen. */
115 /* Dateiname zusammen bauen */
116 sprintf( Error_File, "%s/%s-%ld.err", ERROR_DIR, PACKAGE_NAME, (long)getpid( ));
118 /* stderr umlenken */
119 fflush( stderr );
120 if( ! freopen( Error_File, "w", stderr ))
122 Log( LOG_ERR, "Can't reopen stderr (\"%s\"): %s", Error_File, strerror( errno ));
123 return;
126 /* Einige Infos in das Error-File schreiben */
127 fputs( ctime( &NGIRCd_Start ), stderr );
128 fprintf( stderr, "%s started.\n", NGIRCd_Version );
129 fprintf( stderr, "Activating: %s\n\n", Init_Txt[0] ? Init_Txt : "-" );
130 fflush( stderr );
132 Log( LOG_DEBUG, "Redirected stderr to \"%s\".", Error_File );
133 } /* Log_InitErrfile */
135 #endif
138 GLOBAL void
139 Log_Exit( void )
141 /* Good Bye! */
142 if( NGIRCd_SignalRestart ) Log( LOG_NOTICE, "%s done (restarting).", PACKAGE_NAME );
143 else Log( LOG_NOTICE, "%s done.", PACKAGE_NAME );
145 #ifdef DEBUG
146 if( Error_File[0] )
148 /* Error-File (stderr) loeschen */
149 if( unlink( Error_File ) != 0 ) Log( LOG_ERR, "Can't delete \"%s\": %s", Error_File, strerror( errno ));
151 #endif
153 #ifdef SYSLOG
154 /* syslog abmelden */
155 closelog( );
156 #endif
157 } /* Log_Exit */
160 #ifdef PROTOTYPES
161 GLOBAL void
162 Log( int Level, const char *Format, ... )
163 #else
164 GLOBAL void
165 Log( Level, Format, va_alist )
166 int Level;
167 const char *Format;
168 va_dcl
169 #endif
171 /* Eintrag in Logfile(s) schreiben */
173 char msg[MAX_LOG_MSG_LEN];
174 bool snotice;
175 va_list ap;
177 assert( Format != NULL );
179 if( Level & LOG_snotice )
181 /* Notice an User mit "s" Mode */
182 snotice = true;
183 Level &= ~LOG_snotice;
185 else snotice = false;
187 #ifdef DEBUG
188 if(( Level == LOG_DEBUG ) && ( ! NGIRCd_Debug )) return;
189 #else
190 if( Level == LOG_DEBUG ) return;
191 #endif
193 /* String mit variablen Argumenten zusammenbauen ... */
194 #ifdef PROTOTYPES
195 va_start( ap, Format );
196 #else
197 va_start( ap );
198 #endif
199 vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
200 va_end( ap );
202 if( NGIRCd_NoDaemon )
204 /* auf Konsole ausgeben */
205 fprintf( stdout, "[%d:%d] %s\n", (int)getpid( ), Level, msg );
206 fflush( stdout );
208 #ifdef SYSLOG
209 else
211 /* Syslog */
212 syslog( Level, "%s", msg );
214 #endif
216 if( Level <= LOG_CRIT )
218 /* log critical messages to stderr */
219 fprintf( stderr, "%s\n", msg );
220 fflush( stderr );
223 if( snotice )
225 /* NOTICE an lokale User mit "s"-Mode */
226 Wall_ServerNotice( msg );
228 } /* Log */
231 GLOBAL void
232 Log_Init_Resolver( void )
234 #ifdef SYSLOG
235 openlog( PACKAGE_NAME, LOG_CONS|LOG_PID, LOG_LOCAL5 );
236 #endif
237 Log_Resolver( LOG_DEBUG, "Resolver sub-process starting, PID %d.", getpid( ));
238 } /* Log_Init_Resolver */
241 GLOBAL void
242 Log_Exit_Resolver( void )
244 Log_Resolver( LOG_DEBUG, "Resolver sub-process %d done.", getpid( ));
245 #ifdef SYSLOG
246 closelog( );
247 #endif
248 } /* Log_Exit_Resolver */
251 #ifdef PROTOTYPES
252 GLOBAL void
253 Log_Resolver( const int Level, const char *Format, ... )
254 #else
255 GLOBAL void
256 Log_Resolver( Level, Format, va_alist )
257 const int Level;
258 const char *Format;
259 va_dcl
260 #endif
262 /* Eintrag des Resolver in Logfile(s) schreiben */
264 char msg[MAX_LOG_MSG_LEN];
265 va_list ap;
267 assert( Format != NULL );
269 #ifdef DEBUG
270 if(( Level == LOG_DEBUG ) && ( ! NGIRCd_Debug )) return;
271 #else
272 if( Level == LOG_DEBUG ) return;
273 #endif
275 /* String mit variablen Argumenten zusammenbauen ... */
276 #ifdef PROTOTYPES
277 va_start( ap, Format );
278 #else
279 va_start( ap );
280 #endif
281 vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
282 va_end( ap );
284 /* Output */
285 if( NGIRCd_NoDaemon )
287 /* Output to console */
288 fprintf( stdout, "[%d:%d] %s\n", (int)getpid( ), Level, msg );
289 fflush( stdout );
291 #ifdef SYSLOG
292 else syslog( Level, "%s", msg );
293 #endif
294 } /* Log_Resolver */
297 LOCAL void
298 Wall_ServerNotice( char *Msg )
300 /* Server-Notice an entsprechende User verschicken */
302 CLIENT *c;
304 assert( Msg != NULL );
306 c = Client_First( );
307 while( c )
309 if(( Client_Conn( c ) > NONE ) && ( Client_HasMode( c, 's' ))) IRC_WriteStrClient( c, "NOTICE %s :%s%s", Client_ThisServer( ), NOTICE_TXTPREFIX, Msg );
310 c = Client_Next( c );
312 } /* Wall_ServerNotice */
315 /* -eof- */