]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/log.c
Console log: output timestamp (seconds since start of daemon)
[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  * Please note: you sould use LogDebug(...) for debug messages!
206  * @param Level syslog level (LOG_xxx)
207  * @param Format Format string like printf().
208  * @param ... Further arguments.
209  */
210 #ifdef PROTOTYPES
211 GLOBAL void
212 Log( int Level, const char *Format, ... )
213 #else
214 GLOBAL void
215 Log( Level, Format, va_alist )
216 int Level;
217 const char *Format;
218 va_dcl
219 #endif
220 {
221         /* Eintrag in Logfile(s) schreiben */
222         char msg[MAX_LOG_MSG_LEN];
223         bool snotice;
224         va_list ap;
225
226         assert( Format != NULL );
227
228         if( Level & LOG_snotice )
229         {
230                 /* Notice an User mit "s" Mode */
231                 snotice = true;
232                 Level &= ~LOG_snotice;
233         }
234         else snotice = false;
235
236 #ifdef DEBUG
237         if(( Level == LOG_DEBUG ) && ( ! NGIRCd_Debug )) return;
238 #else
239         if( Level == LOG_DEBUG ) return;
240 #endif
241
242         /* String mit variablen Argumenten zusammenbauen ... */
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         {
274                 /* NOTICE an lokale User mit "s"-Mode */
275                 Wall_ServerNotice( msg );
276         }
277 } /* Log */
278
279
280 GLOBAL void
281 Log_Init_Resolver( void )
282 {
283 #ifdef SYSLOG
284         openlog( PACKAGE_NAME, LOG_CONS|LOG_PID, LOG_LOCAL5 );
285 #endif
286 #ifdef DEBUG
287         Log_Resolver( LOG_DEBUG, "Resolver sub-process starting, PID %d.", getpid( ));
288 #endif
289 } /* Log_Init_Resolver */
290
291
292 GLOBAL void
293 Log_Exit_Resolver( void )
294 {
295 #ifdef DEBUG
296         Log_Resolver( LOG_DEBUG, "Resolver sub-process %d done.", getpid( ));
297 #endif
298 #ifdef SYSLOG
299         closelog( );
300 #endif
301 } /* Log_Exit_Resolver */
302
303
304 #ifdef PROTOTYPES
305 GLOBAL void
306 Log_Resolver( const int Level, const char *Format, ... )
307 #else
308 GLOBAL void
309 Log_Resolver( Level, Format, va_alist )
310 const int Level;
311 const char *Format;
312 va_dcl
313 #endif
314 {
315         /* Eintrag des Resolver in Logfile(s) schreiben */
316
317         char msg[MAX_LOG_MSG_LEN];
318         va_list ap;
319
320         assert( Format != NULL );
321
322 #ifdef DEBUG
323         if(( Level == LOG_DEBUG ) && ( ! NGIRCd_Debug )) return;
324 #else
325         if( Level == LOG_DEBUG ) return;
326 #endif
327
328         /* String mit variablen Argumenten zusammenbauen ... */
329 #ifdef PROTOTYPES
330         va_start( ap, Format );
331 #else
332         va_start( ap );
333 #endif
334         vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
335         va_end( ap );
336
337         if( ! Is_Daemon )
338         {
339                 /* Output to console */
340                 fprintf( stdout, "[%d:%d] %s\n", (int)getpid( ), Level, msg );
341                 fflush( stdout );
342         }
343 #ifdef SYSLOG
344         else syslog( Level, "%s", msg );
345 #endif
346 } /* Log_Resolver */
347
348
349 /**
350  * Send log messages to users flagged with the "s" mode.
351  * @param Msg The message to send.
352  */
353 static void
354 Wall_ServerNotice( char *Msg )
355 {
356         CLIENT *c;
357
358         assert( Msg != NULL );
359
360         c = Client_First( );
361         while(c) {
362                 if (Client_Conn(c) > NONE && Client_HasMode(c, 's'))
363                         IRC_WriteStrClient(c, "NOTICE %s :%s%s", Client_ID(c),
364                                                         NOTICE_TXTPREFIX, Msg);
365
366                 c = Client_Next( c );
367         }
368 } /* Wall_ServerNotice */
369
370
371 /* -eof- */