]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/log.c
e00c7ce7eff94c4aa6ad6b039728de869e87beda
[ngircd-alex.git] / src / ngircd / log.c
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  */
13
14
15 #include "portab.h"
16
17 static char UNUSED id[] = "$Id: log.c,v 1.58 2005/07/31 20:13:08 alex Exp $";
18
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>
31
32 #ifdef SYSLOG
33 #include <syslog.h>
34 #endif
35
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"
42
43 #include "exp.h"
44 #include "log.h"
45
46
47 static char Init_Txt[127];
48 static bool Is_Daemon;
49
50 #ifdef DEBUG
51 static char Error_File[FNAME_LEN];
52 #endif
53
54
55 static void Wall_ServerNotice PARAMS(( char *Msg ));
56
57
58 GLOBAL void
59 Log_Init( bool Daemon_Mode )
60 {
61         Is_Daemon = Daemon_Mode;
62         
63 #ifdef SYSLOG
64 #ifndef LOG_CONS        /* Kludge: mips-dec-ultrix4.5 has no LOG_CONS/LOG_LOCAL5 */
65 #define LOG_CONS 0
66 #endif
67 #ifndef LOG_LOCAL5
68 #define LOG_LOCAL5 0
69 #endif
70         /* Syslog initialisieren */
71         openlog( PACKAGE_NAME, LOG_CONS|LOG_PID, LOG_LOCAL5 );
72 #endif
73
74         /* Hello World! */
75         Log( LOG_NOTICE, "%s started.", NGIRCd_Version );
76           
77         /* Informationen uebern den "Operation Mode" */
78         Init_Txt[0] = '\0';
79 #ifdef DEBUG
80         if( NGIRCd_Debug )
81         {
82                 strlcpy( Init_Txt, "debug-mode", sizeof Init_Txt );
83         }
84 #endif
85         if( ! Is_Daemon )
86         {
87                 if( Init_Txt[0] ) strlcat( Init_Txt, ", ", sizeof Init_Txt );
88                 strlcat( Init_Txt, "no-daemon-mode", sizeof Init_Txt );
89         }
90         if( NGIRCd_Passive )
91         {
92                 if( Init_Txt[0] ) strlcat( Init_Txt, ", ", sizeof Init_Txt );
93                 strlcat( Init_Txt, "passive-mode", sizeof Init_Txt );
94         }
95 #ifdef SNIFFER
96         if( NGIRCd_Sniffer )
97         {
98                 if( Init_Txt[0] ) strlcat( Init_Txt, ", ", sizeof Init_Txt );
99                 strlcat( Init_Txt, "network sniffer", sizeof Init_Txt );
100         }
101 #endif
102         if( Init_Txt[0] ) Log( LOG_INFO, "Activating: %s.", Init_Txt );
103
104 #ifdef DEBUG
105         Error_File[0] = '\0';
106 #endif
107 } /* Log_Init */
108
109
110 #ifdef DEBUG
111
112 GLOBAL void
113 Log_InitErrorfile( void )
114 {
115         /* "Error-Log" initialisieren: stderr in Datei umlenken. Dort
116          * landen z.B. alle Ausgaben von assert()-Aufrufen. */
117
118         /* Dateiname zusammen bauen */
119         snprintf( Error_File, sizeof Error_File, "%s/%s-%ld.err", ERROR_DIR, PACKAGE_NAME, (long)getpid( ));
120
121         /* stderr umlenken */
122         fflush( stderr );
123         if( ! freopen( Error_File, "w", stderr ))
124         {
125                 Log( LOG_ERR, "Can't reopen stderr (\"%s\"): %s", Error_File, strerror( errno ));
126                 return;
127         }
128
129         /* Einige Infos in das Error-File schreiben */
130         fputs( ctime( &NGIRCd_Start ), stderr );
131         fprintf( stderr, "%s started.\n", NGIRCd_Version );
132         fprintf( stderr, "Activating: %s\n\n", Init_Txt[0] ? Init_Txt : "-" );
133         fflush( stderr );
134
135 #ifdef DEBUG
136         Log( LOG_DEBUG, "Redirected stderr to \"%s\".", Error_File );
137 #endif
138 } /* Log_InitErrfile */
139
140 #endif
141
142
143 GLOBAL void
144 Log_Exit( void )
145 {
146         /* Good Bye! */
147         if( NGIRCd_SignalRestart ) Log( LOG_NOTICE, "%s done (restarting).", PACKAGE_NAME );
148         else Log( LOG_NOTICE, "%s done.", PACKAGE_NAME );
149
150 #ifdef DEBUG
151         if( Error_File[0] )
152         {
153                 /* Error-File (stderr) loeschen */
154                 if( unlink( Error_File ) != 0 ) Log( LOG_ERR, "Can't delete \"%s\": %s", Error_File, strerror( errno ));
155         }
156 #endif
157
158 #ifdef SYSLOG
159         /* syslog abmelden */
160         closelog( );
161 #endif
162 } /* Log_Exit */
163
164
165 #ifdef PROTOTYPES
166 GLOBAL void
167 Log( int Level, const char *Format, ... )
168 #else
169 GLOBAL void
170 Log( Level, Format, va_alist )
171 int Level;
172 const char *Format;
173 va_dcl
174 #endif
175 {
176         /* Eintrag in Logfile(s) schreiben */
177
178         char msg[MAX_LOG_MSG_LEN];
179         bool snotice;
180         va_list ap;
181
182         assert( Format != NULL );
183
184         if( Level & LOG_snotice )
185         {
186                 /* Notice an User mit "s" Mode */
187                 snotice = true;
188                 Level &= ~LOG_snotice;
189         }
190         else snotice = false;
191
192 #ifdef DEBUG
193         if(( Level == LOG_DEBUG ) && ( ! NGIRCd_Debug )) return;
194 #else
195         if( Level == LOG_DEBUG ) return;
196 #endif
197
198         /* String mit variablen Argumenten zusammenbauen ... */
199 #ifdef PROTOTYPES
200         va_start( ap, Format );
201 #else
202         va_start( ap );
203 #endif
204         vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
205         va_end( ap );
206
207         if( ! Is_Daemon )
208         {
209                 /* auf Konsole ausgeben */
210                 fprintf( stdout, "[%d:%d] %s\n", (int)getpid( ), Level, msg );
211                 fflush( stdout );
212         }
213 #ifdef SYSLOG
214         else
215         {
216                 /* Syslog */
217                 syslog( Level, "%s", msg );
218         }
219 #endif
220
221         if( Level <= LOG_CRIT )
222         {
223                 /* log critical messages to stderr */
224                 fprintf( stderr, "%s\n", msg );
225                 fflush( stderr );
226         }
227
228         if( snotice )
229         {
230                 /* NOTICE an lokale User mit "s"-Mode */
231                 Wall_ServerNotice( msg );
232         }
233 } /* Log */
234
235
236 GLOBAL void
237 Log_Init_Resolver( void )
238 {
239 #ifdef SYSLOG
240         openlog( PACKAGE_NAME, LOG_CONS|LOG_PID, LOG_LOCAL5 );
241 #endif
242 #ifdef DEBUG
243         Log_Resolver( LOG_DEBUG, "Resolver sub-process starting, PID %d.", getpid( ));
244 #endif
245 } /* Log_Init_Resolver */
246
247
248 GLOBAL void
249 Log_Exit_Resolver( void )
250 {
251 #ifdef DEBUG
252         Log_Resolver( LOG_DEBUG, "Resolver sub-process %d done.", getpid( ));
253 #endif
254 #ifdef SYSLOG
255         closelog( );
256 #endif
257 } /* Log_Exit_Resolver */
258
259
260 #ifdef PROTOTYPES
261 GLOBAL void
262 Log_Resolver( const int Level, const char *Format, ... )
263 #else
264 GLOBAL void
265 Log_Resolver( Level, Format, va_alist )
266 const int Level;
267 const char *Format;
268 va_dcl
269 #endif
270 {
271         /* Eintrag des Resolver in Logfile(s) schreiben */
272
273         char msg[MAX_LOG_MSG_LEN];
274         va_list ap;
275
276         assert( Format != NULL );
277
278 #ifdef DEBUG
279         if(( Level == LOG_DEBUG ) && ( ! NGIRCd_Debug )) return;
280 #else
281         if( Level == LOG_DEBUG ) return;
282 #endif
283
284         /* String mit variablen Argumenten zusammenbauen ... */
285 #ifdef PROTOTYPES
286         va_start( ap, Format );
287 #else
288         va_start( ap );
289 #endif
290         vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
291         va_end( ap );
292
293         if( ! Is_Daemon )
294         {
295                 /* Output to console */
296                 fprintf( stdout, "[%d:%d] %s\n", (int)getpid( ), Level, msg );
297                 fflush( stdout );
298         }
299 #ifdef SYSLOG
300         else syslog( Level, "%s", msg );
301 #endif
302 } /* Log_Resolver */
303
304
305 static void
306 Wall_ServerNotice( char *Msg )
307 {
308         /* Server-Notice an entsprechende User verschicken */
309
310         CLIENT *c;
311
312         assert( Msg != NULL );
313
314         c = Client_First( );
315         while( c )
316         {
317                 if(( Client_Conn( c ) > NONE ) && ( Client_HasMode( c, 's' ))) IRC_WriteStrClient( c, "NOTICE %s :%s%s", Client_ThisServer( ), NOTICE_TXTPREFIX, Msg );
318                 c = Client_Next( c );
319         }
320 } /* Wall_ServerNotice */
321
322
323 /* -eof- */