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