]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/log.c
Overview of "contributed" files.
[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.44 2003/12/26 15:55:07 alex Exp $";
18
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>
27
28 #ifdef SYSLOG
29 #include <syslog.h>
30 #endif
31
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"
38
39 #include "exp.h"
40 #include "log.h"
41
42
43 LOCAL CHAR Error_File[FNAME_LEN];
44 LOCAL CHAR Init_Txt[127];
45
46
47 LOCAL VOID Wall_ServerNotice PARAMS(( CHAR *Msg ));
48
49
50 GLOBAL VOID
51 Log_Init( VOID )
52 {
53 #ifdef SYSLOG
54         /* Syslog initialisieren */
55         openlog( PACKAGE_NAME, LOG_CONS|LOG_PID, LOG_LOCAL5 );
56 #endif
57
58         /* Hello World! */
59         Log( LOG_NOTICE, "%s started.", NGIRCd_Version( ));
60           
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 */
89
90
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. */
96
97         /* Dateiname zusammen bauen */
98         sprintf( Error_File, "%s/%s-%ld.err", ERROR_DIR, PACKAGE_NAME, (LONG)getpid( ));
99
100         /* stderr umlenken */
101         fflush( stderr );
102         if( ! freopen( Error_File, "w", stderr ))
103         {
104                 Log( LOG_ERR, "Can't reopen stderr (\"%s\"): %s", Error_File, strerror( errno ));
105                 return;
106         }
107
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 );
113
114         Log( LOG_DEBUG, "Redirected stderr to \"%s\".", Error_File );
115 } /* Log_InitErrfile */
116
117
118 GLOBAL VOID
119 Log_Exit( VOID )
120 {
121         /* Good Bye! */
122         if( NGIRCd_SignalRestart ) Log( LOG_NOTICE, "%s done (restarting).", PACKAGE_NAME );
123         else Log( LOG_NOTICE, "%s done.", PACKAGE_NAME );
124
125         /* Error-File (stderr) loeschen */
126         if( unlink( Error_File ) != 0 ) Log( LOG_ERR, "Can't delete \"%s\": %s", Error_File, strerror( errno ));
127
128 #ifdef SYSLOG
129         /* syslog abmelden */
130         closelog( );
131 #endif
132 } /* Log_Exit */
133
134
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
145 {
146         /* Eintrag in Logfile(s) schreiben */
147
148         CHAR msg[MAX_LOG_MSG_LEN];
149         BOOLEAN snotice;
150         va_list ap;
151
152         assert( Format != NULL );
153
154         if( Level & LOG_snotice )
155         {
156                 /* Notice an User mit "s" Mode */
157                 snotice = TRUE;
158                 Level &= ~LOG_snotice;
159         }
160         else snotice = FALSE;
161
162 #ifdef DEBUG
163         if(( Level == LOG_DEBUG ) && ( ! NGIRCd_Debug )) return;
164 #else
165         if( Level == LOG_DEBUG ) return;
166 #endif
167
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 );
176
177         if( NGIRCd_NoDaemon )
178         {
179                 /* auf Konsole ausgeben */
180                 fprintf( stdout, "[%d] %s\n", Level, msg );
181                 fflush( stdout );
182         }
183 #ifdef SYSLOG
184         else
185         {
186                 /* Syslog */
187                 syslog( Level, "%s", msg );
188         }
189 #endif
190
191         if( Level <= LOG_CRIT )
192         {
193                 /* Kritische Meldungen in Error-File (stderr) */
194                 fprintf( stderr, "%s\n", msg );
195                 fflush( stderr );
196         }
197
198         if( snotice )
199         {
200                 /* NOTICE an lokale User mit "s"-Mode */
201                 Wall_ServerNotice( msg );
202         }
203 } /* Log */
204
205
206 GLOBAL VOID
207 Log_Init_Resolver( VOID )
208 {
209 #ifdef SYSLOG
210         openlog( PACKAGE_NAME, LOG_CONS|LOG_PID, LOG_LOCAL5 );
211 #endif
212 } /* Log_Init_Resolver */
213
214
215 GLOBAL VOID
216 Log_Exit_Resolver( VOID )
217 {
218 #ifdef SYSLOG
219         closelog( );
220 #endif
221 } /* Log_Exit_Resolver */
222
223
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
234 {
235         /* Eintrag des Resolver in Logfile(s) schreiben */
236
237 #ifndef SYSLOG
238         return;
239 #else
240
241         CHAR msg[MAX_LOG_MSG_LEN];
242         va_list ap;
243
244         assert( Format != NULL );
245
246         if( NGIRCd_NoDaemon ) return;
247
248 #ifdef DEBUG
249         if(( Level == LOG_DEBUG ) && ( ! NGIRCd_Debug )) return;
250 #else
251         if( Level == LOG_DEBUG ) return;
252 #endif
253
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 );
262
263         /* ... und ausgeben */
264         syslog( Level, msg );
265
266 #endif
267 } /* Log_Resolver */
268
269
270 LOCAL VOID
271 Wall_ServerNotice( CHAR *Msg )
272 {
273         /* Server-Notice an entsprechende User verschicken */
274
275         CLIENT *c;
276
277         assert( Msg != NULL );
278
279         c = Client_First( );
280         while( c )
281         {
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 );
284         }
285 } /* Wall_ServerNotice */
286
287
288 /* -eof- */