]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/log.c
Implement the IRC command "SERVLIST"
[ngircd-alex.git] / src / ngircd / log.c
1 /*
2  * ngIRCd -- The Next Generation IRC Daemon
3  * Copyright (c)2001-2005 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.62 2006/08/05 09:16:21 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 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 /**
166  * Log function for debug messages.
167  * This function is only functional when the program is compiled with debug
168  * code enabled; otherwise it is an empty function which the compiler will
169  * hopefully mangle down to "nothing" (see log.h). Therefore you should use
170  * LogDebug(...) in favor to Log(LOG_DEBUG, ...).
171  * @param Format Format string like printf().
172  * @param ... Further arguments.
173  */
174 #ifdef DEBUG
175 # ifdef PROTOTYPES
176 GLOBAL void
177 LogDebug( const char *Format, ... )
178 # else
179 GLOBAL void
180 LogDebug( Format, va_alist )
181 const char *Format;
182 va_dcl
183 # endif /* PROTOTYPES */
184 {
185         char msg[MAX_LOG_MSG_LEN];
186         va_list ap;
187
188         if (!NGIRCd_Debug) return;
189 #ifdef PROTOTYPES
190         va_start( ap, Format );
191 #else
192         va_start( ap );
193 #endif
194         vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
195         va_end( ap );
196         Log(LOG_DEBUG, "%s", msg);
197 }
198 #endif  /* DEBUG */
199
200
201 /**
202  * Logging function of ngIRCd.
203  * This function logs messages to the console and/or syslog, whichever is
204  * suitable for the mode ngIRCd is running in (daemon vs. non-daemon).
205  * If LOG_snotice is set, the log messages goes to all user with the mode +s
206  * set and the local &SERVER channel, too.
207  * Please note: you sould use LogDebug(...) for debug messages!
208  * @param Level syslog level (LOG_xxx)
209  * @param Format Format string like printf().
210  * @param ... Further arguments.
211  */
212 #ifdef PROTOTYPES
213 GLOBAL void
214 Log( int Level, const char *Format, ... )
215 #else
216 GLOBAL void
217 Log( Level, Format, va_alist )
218 int Level;
219 const char *Format;
220 va_dcl
221 #endif
222 {
223         char msg[MAX_LOG_MSG_LEN];
224         bool snotice;
225         va_list ap;
226
227         assert( Format != NULL );
228
229         if( Level & LOG_snotice )
230         {
231                 /* Notice an User mit "s" Mode */
232                 snotice = true;
233                 Level &= ~LOG_snotice;
234         }
235         else snotice = false;
236
237 #ifdef DEBUG
238         if(( Level == LOG_DEBUG ) && ( ! NGIRCd_Debug )) return;
239 #else
240         if( Level == LOG_DEBUG ) return;
241 #endif
242
243 #ifdef PROTOTYPES
244         va_start( ap, Format );
245 #else
246         va_start( ap );
247 #endif
248         vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
249         va_end( ap );
250
251         if (!Is_Daemon) {
252                 /* log to console */
253                 fprintf(stdout, "[%d:%d %4ld] %s\n", (int)getpid( ), Level,
254                         time(NULL) - NGIRCd_Start, msg);
255                 fflush(stdout);
256         }
257 #ifdef SYSLOG
258         else
259         {
260                 /* Syslog */
261                 syslog( Level, "%s", msg );
262         }
263 #endif
264
265         if( Level <= LOG_CRIT )
266         {
267                 /* log critical messages to stderr */
268                 fprintf( stderr, "%s\n", msg );
269                 fflush( stderr );
270         }
271
272         if (snotice) {
273                 /* Send NOTICE to all local users with mode +s and to the
274                  * local &SERVER channel */
275                 Wall_ServerNotice(msg);
276                 Channel_LogServer(msg);
277         }
278 } /* Log */
279
280
281 GLOBAL void
282 Log_Init_Resolver( void )
283 {
284 #ifdef SYSLOG
285         openlog( PACKAGE_NAME, LOG_CONS|LOG_PID, LOG_LOCAL5 );
286 #endif
287 #ifdef DEBUG
288         Log_Resolver( LOG_DEBUG, "Resolver sub-process starting, PID %d.", getpid( ));
289 #endif
290 } /* Log_Init_Resolver */
291
292
293 GLOBAL void
294 Log_Exit_Resolver( void )
295 {
296 #ifdef DEBUG
297         Log_Resolver( LOG_DEBUG, "Resolver sub-process %d done.", getpid( ));
298 #endif
299 #ifdef SYSLOG
300         closelog( );
301 #endif
302 } /* Log_Exit_Resolver */
303
304
305 #ifdef PROTOTYPES
306 GLOBAL void
307 Log_Resolver( const int Level, const char *Format, ... )
308 #else
309 GLOBAL void
310 Log_Resolver( Level, Format, va_alist )
311 const int Level;
312 const char *Format;
313 va_dcl
314 #endif
315 {
316         /* Eintrag des Resolver in Logfile(s) schreiben */
317
318         char msg[MAX_LOG_MSG_LEN];
319         va_list ap;
320
321         assert( Format != NULL );
322
323 #ifdef DEBUG
324         if(( Level == LOG_DEBUG ) && ( ! NGIRCd_Debug )) return;
325 #else
326         if( Level == LOG_DEBUG ) return;
327 #endif
328
329         /* String mit variablen Argumenten zusammenbauen ... */
330 #ifdef PROTOTYPES
331         va_start( ap, Format );
332 #else
333         va_start( ap );
334 #endif
335         vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
336         va_end( ap );
337
338         if (!Is_Daemon) {
339                 /* Output to console */
340                 fprintf(stdout, "[%d:%d %4ld] %s\n", (int)getpid( ), Level,
341                         time(NULL) - NGIRCd_Start, msg);
342                 fflush(stdout);
343         }
344 #ifdef SYSLOG
345         else syslog( Level, "%s", msg );
346 #endif
347 } /* Log_Resolver */
348
349
350 /**
351  * Send log messages to users flagged with the "s" mode.
352  * @param Msg The message to send.
353  */
354 static void
355 Wall_ServerNotice( char *Msg )
356 {
357         CLIENT *c;
358
359         assert( Msg != NULL );
360
361         c = Client_First( );
362         while(c) {
363                 if (Client_Conn(c) > NONE && Client_HasMode(c, 's'))
364                         IRC_WriteStrClient(c, "NOTICE %s :%s%s", Client_ID(c),
365                                                         NOTICE_TXTPREFIX, Msg);
366
367                 c = Client_Next( c );
368         }
369 } /* Wall_ServerNotice */
370
371
372 /* -eof- */