]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/log.c
use %ld as format specifier for posix data types
[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 #include "imp.h"
18 #include <assert.h>
19 #include <errno.h>
20 #ifdef PROTOTYPES
21 #       include <stdarg.h>
22 #else
23 #       include <varargs.h>
24 #endif
25 #include <stdio.h>
26 #include <string.h>
27 #include <sys/types.h>
28 #include <unistd.h>
29
30 #ifdef SYSLOG
31 #include <syslog.h>
32 #endif
33
34 #include "ngircd.h"
35 #include "defines.h"
36 #include "conn.h"
37 #include "client.h"
38 #include "channel.h"
39 #include "irc-write.h"
40
41 #include "exp.h"
42 #include "log.h"
43
44
45 static char Init_Txt[127];
46 static bool Is_Daemon;
47
48 #ifdef DEBUG
49 static char Error_File[FNAME_LEN];
50 #endif
51
52
53 static void Wall_ServerNotice PARAMS(( char *Msg ));
54
55 static void
56 Log_Message(int Level, const char *msg)
57 {
58         if (!Is_Daemon) {
59                 /* log to console */
60                 fprintf(stdout, "[%ld:%d %4ld] %s\n", (long)getpid(), Level,
61                                 (long)time(NULL) - NGIRCd_Start, msg);
62                 fflush(stdout);
63         }
64 #ifdef SYSLOG
65         else {
66                 syslog(Level, "%s", msg);
67         }
68 #endif
69 }
70
71
72 GLOBAL void
73 Log_Init( bool Daemon_Mode )
74 {
75         Is_Daemon = Daemon_Mode;
76         
77 #ifdef SYSLOG
78 #ifndef LOG_CONS     /* Kludge: mips-dec-ultrix4.5 has no LOG_CONS/LOG_LOCAL5 */
79 #define LOG_CONS 0
80 #endif
81 #ifndef LOG_LOCAL5
82 #define LOG_LOCAL5 0
83 #endif
84         /* Syslog initialisieren */
85         openlog( PACKAGE_NAME, LOG_CONS|LOG_PID, LOG_LOCAL5 );
86 #endif
87
88         /* Hello World! */
89         Log( LOG_NOTICE, "%s started.", NGIRCd_Version );
90           
91         /* Informationen uebern den "Operation Mode" */
92         Init_Txt[0] = '\0';
93 #ifdef DEBUG
94         if( NGIRCd_Debug )
95         {
96                 strlcpy( Init_Txt, "debug-mode", sizeof Init_Txt );
97         }
98 #endif
99         if( ! Is_Daemon )
100         {
101                 if( Init_Txt[0] ) strlcat( Init_Txt, ", ", sizeof Init_Txt );
102                 strlcat( Init_Txt, "no-daemon-mode", sizeof Init_Txt );
103         }
104         if( NGIRCd_Passive )
105         {
106                 if( Init_Txt[0] ) strlcat( Init_Txt, ", ", sizeof Init_Txt );
107                 strlcat( Init_Txt, "passive-mode", sizeof Init_Txt );
108         }
109 #ifdef SNIFFER
110         if( NGIRCd_Sniffer )
111         {
112                 if( Init_Txt[0] ) strlcat( Init_Txt, ", ", sizeof Init_Txt );
113                 strlcat( Init_Txt, "network sniffer", sizeof Init_Txt );
114         }
115 #endif
116         if( Init_Txt[0] ) Log( LOG_INFO, "Activating: %s.", Init_Txt );
117
118 #ifdef DEBUG
119         Error_File[0] = '\0';
120 #endif
121 } /* Log_Init */
122
123
124 #ifdef DEBUG
125
126 GLOBAL void
127 Log_InitErrorfile( void )
128 {
129         /* "Error-Log" initialisieren: stderr in Datei umlenken. Dort
130          * landen z.B. alle Ausgaben von assert()-Aufrufen. */
131
132         /* Dateiname zusammen bauen */
133         snprintf( Error_File, sizeof Error_File, "%s/%s-%ld.err", ERROR_DIR, PACKAGE_NAME, (long)getpid( ));
134
135         /* stderr umlenken */
136         fflush( stderr );
137         if( ! freopen( Error_File, "w", stderr ))
138         {
139                 Log( LOG_ERR, "Can't reopen stderr (\"%s\"): %s", Error_File, strerror( errno ));
140                 return;
141         }
142
143         /* Einige Infos in das Error-File schreiben */
144         fputs( ctime( &NGIRCd_Start ), stderr );
145         fprintf( stderr, "%s started.\n", NGIRCd_Version );
146         fprintf( stderr, "Activating: %s\n\n", Init_Txt[0] ? Init_Txt : "-" );
147         fflush( stderr );
148
149 #ifdef DEBUG
150         Log( LOG_DEBUG, "Redirected stderr to \"%s\".", Error_File );
151 #endif
152 } /* Log_InitErrfile */
153
154 #endif
155
156
157 GLOBAL void
158 Log_Exit( void )
159 {
160         /* Good Bye! */
161         if( NGIRCd_SignalRestart ) Log( LOG_NOTICE, "%s done (restarting).", PACKAGE_NAME );
162         else Log( LOG_NOTICE, "%s done.", PACKAGE_NAME );
163
164 #ifdef DEBUG
165         if( Error_File[0] )
166         {
167                 /* Error-File (stderr) loeschen */
168                 if( unlink( Error_File ) != 0 ) Log( LOG_ERR, "Can't delete \"%s\": %s", Error_File, strerror( errno ));
169         }
170 #endif
171
172 #ifdef SYSLOG
173         /* syslog abmelden */
174         closelog( );
175 #endif
176 } /* Log_Exit */
177
178
179 /**
180  * Log function for debug messages.
181  * This function is only functional when the program is compiled with debug
182  * code enabled; otherwise it is an empty function which the compiler will
183  * hopefully mangle down to "nothing" (see log.h). Therefore you should use
184  * LogDebug(...) in favor to Log(LOG_DEBUG, ...).
185  * @param Format Format string like printf().
186  * @param ... Further arguments.
187  */
188 #ifdef DEBUG
189 # ifdef PROTOTYPES
190 GLOBAL void
191 LogDebug( const char *Format, ... )
192 # else
193 GLOBAL void
194 LogDebug( Format, va_alist )
195 const char *Format;
196 va_dcl
197 # endif /* PROTOTYPES */
198 {
199         char msg[MAX_LOG_MSG_LEN];
200         va_list ap;
201
202         if (!NGIRCd_Debug) return;
203 #ifdef PROTOTYPES
204         va_start( ap, Format );
205 #else
206         va_start( ap );
207 #endif
208         vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
209         va_end( ap );
210         Log(LOG_DEBUG, "%s", msg);
211 }
212 #endif  /* DEBUG */
213
214
215 /**
216  * Logging function of ngIRCd.
217  * This function logs messages to the console and/or syslog, whichever is
218  * suitable for the mode ngIRCd is running in (daemon vs. non-daemon).
219  * If LOG_snotice is set, the log messages goes to all user with the mode +s
220  * set and the local &SERVER channel, too.
221  * Please note: you sould use LogDebug(...) for debug messages!
222  * @param Level syslog level (LOG_xxx)
223  * @param Format Format string like printf().
224  * @param ... Further arguments.
225  */
226 #ifdef PROTOTYPES
227 GLOBAL void
228 Log( int Level, const char *Format, ... )
229 #else
230 GLOBAL void
231 Log( Level, Format, va_alist )
232 int Level;
233 const char *Format;
234 va_dcl
235 #endif
236 {
237         char msg[MAX_LOG_MSG_LEN];
238         bool snotice;
239         va_list ap;
240
241         assert( Format != NULL );
242
243         if( Level & LOG_snotice )
244         {
245                 /* Notice an User mit "s" Mode */
246                 snotice = true;
247                 Level &= ~LOG_snotice;
248         }
249         else snotice = false;
250
251 #ifdef DEBUG
252         if(( Level == LOG_DEBUG ) && ( ! NGIRCd_Debug )) return;
253 #else
254         if( Level == LOG_DEBUG ) return;
255 #endif
256
257 #ifdef PROTOTYPES
258         va_start( ap, Format );
259 #else
260         va_start( ap );
261 #endif
262         vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
263         va_end( ap );
264
265         Log_Message(Level, msg);
266
267         if (Level <= LOG_CRIT) {
268                 /* log critical messages to stderr */
269                 fprintf(stderr, "%s\n", msg);
270                 fflush(stderr);
271         }
272
273         if (snotice) {
274                 /* Send NOTICE to all local users with mode +s and to the
275                  * local &SERVER channel */
276                 Wall_ServerNotice(msg);
277                 Channel_LogServer(msg);
278         }
279 } /* Log */
280
281
282 GLOBAL void
283 Log_Init_Resolver( void )
284 {
285 #ifdef SYSLOG
286         openlog( PACKAGE_NAME, LOG_CONS|LOG_PID, LOG_LOCAL5 );
287 #endif
288 #ifdef DEBUG
289         Log_Resolver(LOG_DEBUG, "Resolver sub-process starting, PID %ld.", (long)getpid());
290 #endif
291 } /* Log_Init_Resolver */
292
293
294 GLOBAL void
295 Log_Exit_Resolver( void )
296 {
297 #ifdef DEBUG
298         Log_Resolver(LOG_DEBUG, "Resolver sub-process %ld done.", (long)getpid());
299 #endif
300 #ifdef SYSLOG
301         closelog( );
302 #endif
303 } /* Log_Exit_Resolver */
304
305
306 #ifdef PROTOTYPES
307 GLOBAL void
308 Log_Resolver( const int Level, const char *Format, ... )
309 #else
310 GLOBAL void
311 Log_Resolver( Level, Format, va_alist )
312 const int Level;
313 const char *Format;
314 va_dcl
315 #endif
316 {
317         /* Eintrag des Resolver in Logfile(s) schreiben */
318
319         char msg[MAX_LOG_MSG_LEN];
320         va_list ap;
321
322         assert( Format != NULL );
323
324 #ifdef DEBUG
325         if(( Level == LOG_DEBUG ) && ( ! NGIRCd_Debug )) return;
326 #else
327         if( Level == LOG_DEBUG ) return;
328 #endif
329
330         /* String mit variablen Argumenten zusammenbauen ... */
331 #ifdef PROTOTYPES
332         va_start( ap, Format );
333 #else
334         va_start( ap );
335 #endif
336         vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
337         va_end( ap );
338
339         Log_Message(Level, msg);
340 } /* Log_Resolver */
341
342
343 /**
344  * Send log messages to users flagged with the "s" mode.
345  * @param Msg The message to send.
346  */
347 static void
348 Wall_ServerNotice( char *Msg )
349 {
350         CLIENT *c;
351
352         assert( Msg != NULL );
353
354         c = Client_First( );
355         while(c) {
356                 if (Client_Conn(c) > NONE && Client_HasMode(c, 's'))
357                         IRC_WriteStrClient(c, "NOTICE %s :%s%s", Client_ID(c),
358                                                         NOTICE_TXTPREFIX, Msg);
359
360                 c = Client_Next( c );
361         }
362 } /* Wall_ServerNotice */
363
364
365 /* -eof- */