]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/log.c
added kludge to make ngircd (with syslog) compile on mips-dec-ultrix4.5
[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.54 2005/04/16 09:31:30 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
54 LOCAL void Wall_ServerNotice PARAMS(( char *Msg ));
55
56
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
70
71         /* Hello World! */
72         Log( LOG_NOTICE, "%s started.", NGIRCd_Version );
73           
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 );
100
101 #ifdef DEBUG
102         Error_File[0] = '\0';
103 #endif
104 } /* Log_Init */
105
106
107 #ifdef DEBUG
108
109 GLOBAL void
110 Log_InitErrorfile( void )
111 {
112         /* "Error-Log" initialisieren: stderr in Datei umlenken. Dort
113          * landen z.B. alle Ausgaben von assert()-Aufrufen. */
114
115         /* Dateiname zusammen bauen */
116         sprintf( Error_File, "%s/%s-%ld.err", ERROR_DIR, PACKAGE_NAME, (long)getpid( ));
117
118         /* stderr umlenken */
119         fflush( stderr );
120         if( ! freopen( Error_File, "w", stderr ))
121         {
122                 Log( LOG_ERR, "Can't reopen stderr (\"%s\"): %s", Error_File, strerror( errno ));
123                 return;
124         }
125
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 );
131
132         Log( LOG_DEBUG, "Redirected stderr to \"%s\".", Error_File );
133 } /* Log_InitErrfile */
134
135 #endif
136
137
138 GLOBAL void
139 Log_Exit( void )
140 {
141         /* Good Bye! */
142         if( NGIRCd_SignalRestart ) Log( LOG_NOTICE, "%s done (restarting).", PACKAGE_NAME );
143         else Log( LOG_NOTICE, "%s done.", PACKAGE_NAME );
144
145 #ifdef DEBUG
146         if( Error_File[0] )
147         {
148                 /* Error-File (stderr) loeschen */
149                 if( unlink( Error_File ) != 0 ) Log( LOG_ERR, "Can't delete \"%s\": %s", Error_File, strerror( errno ));
150         }
151 #endif
152
153 #ifdef SYSLOG
154         /* syslog abmelden */
155         closelog( );
156 #endif
157 } /* Log_Exit */
158
159
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
170 {
171         /* Eintrag in Logfile(s) schreiben */
172
173         char msg[MAX_LOG_MSG_LEN];
174         bool snotice;
175         va_list ap;
176
177         assert( Format != NULL );
178
179         if( Level & LOG_snotice )
180         {
181                 /* Notice an User mit "s" Mode */
182                 snotice = true;
183                 Level &= ~LOG_snotice;
184         }
185         else snotice = false;
186
187 #ifdef DEBUG
188         if(( Level == LOG_DEBUG ) && ( ! NGIRCd_Debug )) return;
189 #else
190         if( Level == LOG_DEBUG ) return;
191 #endif
192
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 );
201
202         if( NGIRCd_NoDaemon )
203         {
204                 /* auf Konsole ausgeben */
205                 fprintf( stdout, "[%d:%d] %s\n", (int)getpid( ), Level, msg );
206                 fflush( stdout );
207         }
208 #ifdef SYSLOG
209         else
210         {
211                 /* Syslog */
212                 syslog( Level, "%s", msg );
213         }
214 #endif
215
216         if( Level <= LOG_CRIT )
217         {
218                 /* log critical messages to stderr */
219                 fprintf( stderr, "%s\n", msg );
220                 fflush( stderr );
221         }
222
223         if( snotice )
224         {
225                 /* NOTICE an lokale User mit "s"-Mode */
226                 Wall_ServerNotice( msg );
227         }
228 } /* Log */
229
230
231 GLOBAL void
232 Log_Init_Resolver( void )
233 {
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 */
239
240
241 GLOBAL void
242 Log_Exit_Resolver( void )
243 {
244         Log_Resolver( LOG_DEBUG, "Resolver sub-process %d done.", getpid( ));
245 #ifdef SYSLOG
246         closelog( );
247 #endif
248 } /* Log_Exit_Resolver */
249
250
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
261 {
262         /* Eintrag des Resolver in Logfile(s) schreiben */
263
264         char msg[MAX_LOG_MSG_LEN];
265         va_list ap;
266
267         assert( Format != NULL );
268
269 #ifdef DEBUG
270         if(( Level == LOG_DEBUG ) && ( ! NGIRCd_Debug )) return;
271 #else
272         if( Level == LOG_DEBUG ) return;
273 #endif
274
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 );
283
284         /* Output */
285         if( NGIRCd_NoDaemon )
286         {
287                 /* Output to console */
288                 fprintf( stdout, "[%d:%d] %s\n", (int)getpid( ), Level, msg );
289                 fflush( stdout );
290         }
291 #ifdef SYSLOG
292         else syslog( Level, "%s", msg );
293 #endif
294 } /* Log_Resolver */
295
296
297 LOCAL void
298 Wall_ServerNotice( char *Msg )
299 {
300         /* Server-Notice an entsprechende User verschicken */
301
302         CLIENT *c;
303
304         assert( Msg != NULL );
305
306         c = Client_First( );
307         while( c )
308         {
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 );
311         }
312 } /* Wall_ServerNotice */
313
314
315 /* -eof- */