]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/log.c
Mac OS X: fix test for packagemaker(1) tool in Makefile
[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         openlog( PACKAGE_NAME, LOG_CONS|LOG_PID, LOG_LOCAL5 );
85 #endif
86
87         Log( LOG_NOTICE, "%s started.", NGIRCd_Version );
88           
89         /* Information about "Operation Mode" */
90         Init_Txt[0] = '\0';
91 #ifdef DEBUG
92         if( NGIRCd_Debug )
93         {
94                 strlcpy( Init_Txt, "debug-mode", sizeof Init_Txt );
95         }
96 #endif
97         if( ! Is_Daemon )
98         {
99                 if( Init_Txt[0] ) strlcat( Init_Txt, ", ", sizeof Init_Txt );
100                 strlcat( Init_Txt, "no-daemon-mode", sizeof Init_Txt );
101         }
102         if( NGIRCd_Passive )
103         {
104                 if( Init_Txt[0] ) strlcat( Init_Txt, ", ", sizeof Init_Txt );
105                 strlcat( Init_Txt, "passive-mode", sizeof Init_Txt );
106         }
107 #ifdef SNIFFER
108         if( NGIRCd_Sniffer )
109         {
110                 if( Init_Txt[0] ) strlcat( Init_Txt, ", ", sizeof Init_Txt );
111                 strlcat( Init_Txt, "network sniffer", sizeof Init_Txt );
112         }
113 #endif
114         if( Init_Txt[0] ) Log( LOG_INFO, "Activating: %s.", Init_Txt );
115
116 #ifdef DEBUG
117         Error_File[0] = '\0';
118 #endif
119 } /* Log_Init */
120
121
122 #ifdef DEBUG
123 GLOBAL void
124 Log_InitErrorfile( void )
125 {
126         snprintf( Error_File, sizeof Error_File, "%s/%s-%ld.err", ERROR_DIR, PACKAGE_NAME, (long)getpid( ));
127
128         fflush( stderr );
129         if( ! freopen( Error_File, "w", stderr ))
130         {
131                 Log( LOG_ERR, "Can't reopen stderr (\"%s\"): %s", Error_File, strerror( errno ));
132                 return;
133         }
134
135         fputs( ctime( &NGIRCd_Start ), stderr );
136         fprintf( stderr, "%s started.\n", NGIRCd_Version );
137         fprintf( stderr, "Activating: %s\n\n", Init_Txt[0] ? Init_Txt : "-" );
138         fflush( stderr );
139
140         Log(LOG_DEBUG, "Redirected stderr to \"%s\".", Error_File);
141 } /* Log_InitErrfile */
142 #endif
143
144
145 GLOBAL void
146 Log_Exit( void )
147 {
148         /* Good Bye! */
149         if( NGIRCd_SignalRestart ) Log( LOG_NOTICE, "%s done (restarting).", PACKAGE_NAME );
150         else Log( LOG_NOTICE, "%s done.", PACKAGE_NAME );
151
152 #ifdef DEBUG
153         if( Error_File[0] )
154         {
155                 /* Error-File (stderr) loeschen */
156                 if( unlink( Error_File ) != 0 ) Log( LOG_ERR, "Can't delete \"%s\": %s", Error_File, strerror( errno ));
157         }
158 #endif
159
160 #ifdef SYSLOG
161         closelog();
162 #endif
163 } /* Log_Exit */
164
165
166 /**
167  * Log function for debug messages.
168  * This function is only functional when the program is compiled with debug
169  * code enabled; otherwise it is an empty function which the compiler will
170  * hopefully mangle down to "nothing" (see log.h). Therefore you should use
171  * LogDebug(...) in favor to Log(LOG_DEBUG, ...).
172  * @param Format Format string like printf().
173  * @param ... Further arguments.
174  */
175 #ifdef DEBUG
176 # ifdef PROTOTYPES
177 GLOBAL void
178 LogDebug( const char *Format, ... )
179 # else
180 GLOBAL void
181 LogDebug( Format, va_alist )
182 const char *Format;
183 va_dcl
184 # endif /* PROTOTYPES */
185 {
186         char msg[MAX_LOG_MSG_LEN];
187         va_list ap;
188
189         if (!NGIRCd_Debug) return;
190 #ifdef PROTOTYPES
191         va_start( ap, Format );
192 #else
193         va_start( ap );
194 #endif
195         vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
196         va_end( ap );
197         Log(LOG_DEBUG, "%s", msg);
198 }
199 #endif  /* DEBUG */
200
201
202 /**
203  * Logging function of ngIRCd.
204  * This function logs messages to the console and/or syslog, whichever is
205  * suitable for the mode ngIRCd is running in (daemon vs. non-daemon).
206  * If LOG_snotice is set, the log messages goes to all user with the mode +s
207  * set and the local &SERVER channel, too.
208  * Please note: you sould use LogDebug(...) for debug messages!
209  * @param Level syslog level (LOG_xxx)
210  * @param Format Format string like printf().
211  * @param ... Further arguments.
212  */
213 #ifdef PROTOTYPES
214 GLOBAL void
215 Log( int Level, const char *Format, ... )
216 #else
217 GLOBAL void
218 Log( Level, Format, va_alist )
219 int Level;
220 const char *Format;
221 va_dcl
222 #endif
223 {
224         char msg[MAX_LOG_MSG_LEN];
225         bool snotice;
226         va_list ap;
227
228         assert( Format != NULL );
229
230         if( Level & LOG_snotice )
231         {
232                 /* Notice an User mit "s" Mode */
233                 snotice = true;
234                 Level &= ~LOG_snotice;
235         }
236         else snotice = false;
237
238 #ifdef DEBUG
239         if(( Level == LOG_DEBUG ) && ( ! NGIRCd_Debug )) return;
240 #else
241         if( Level == LOG_DEBUG ) return;
242 #endif
243
244 #ifdef PROTOTYPES
245         va_start( ap, Format );
246 #else
247         va_start( ap );
248 #endif
249         vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
250         va_end( ap );
251
252         Log_Message(Level, msg);
253
254         if (Level <= LOG_CRIT) {
255                 /* log critical messages to stderr */
256                 fprintf(stderr, "%s\n", msg);
257                 fflush(stderr);
258         }
259
260         if (snotice) {
261                 /* Send NOTICE to all local users with mode +s and to the
262                  * local &SERVER channel */
263                 Wall_ServerNotice(msg);
264                 Channel_LogServer(msg);
265         }
266 } /* Log */
267
268
269 GLOBAL void
270 Log_Init_Resolver( void )
271 {
272 #ifdef SYSLOG
273         openlog( PACKAGE_NAME, LOG_CONS|LOG_PID, LOG_LOCAL5 );
274 #endif
275 #ifdef DEBUG
276         Log_Resolver(LOG_DEBUG, "Resolver sub-process starting, PID %ld.", (long)getpid());
277 #endif
278 } /* Log_Init_Resolver */
279
280
281 GLOBAL void
282 Log_Exit_Resolver( void )
283 {
284 #ifdef DEBUG
285         Log_Resolver(LOG_DEBUG, "Resolver sub-process %ld done.", (long)getpid());
286 #endif
287 #ifdef SYSLOG
288         closelog( );
289 #endif
290 } /* Log_Exit_Resolver */
291
292
293 #ifdef PROTOTYPES
294 GLOBAL void
295 Log_Resolver( const int Level, const char *Format, ... )
296 #else
297 GLOBAL void
298 Log_Resolver( Level, Format, va_alist )
299 const int Level;
300 const char *Format;
301 va_dcl
302 #endif
303 {
304         /* Eintrag des Resolver in Logfile(s) schreiben */
305
306         char msg[MAX_LOG_MSG_LEN];
307         va_list ap;
308
309         assert( Format != NULL );
310
311 #ifdef DEBUG
312         if(( Level == LOG_DEBUG ) && ( ! NGIRCd_Debug )) return;
313 #else
314         if( Level == LOG_DEBUG ) return;
315 #endif
316
317         /* String mit variablen Argumenten zusammenbauen ... */
318 #ifdef PROTOTYPES
319         va_start( ap, Format );
320 #else
321         va_start( ap );
322 #endif
323         vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
324         va_end( ap );
325
326         Log_Message(Level, msg);
327 } /* Log_Resolver */
328
329
330 /**
331  * Send log messages to users flagged with the "s" mode.
332  * @param Msg The message to send.
333  */
334 static void
335 Wall_ServerNotice( char *Msg )
336 {
337         CLIENT *c;
338
339         assert( Msg != NULL );
340
341         c = Client_First( );
342         while(c) {
343                 if (Client_Conn(c) > NONE && Client_HasMode(c, 's'))
344                         IRC_WriteStrClient(c, "NOTICE %s :%s%s", Client_ID(c),
345                                                         NOTICE_TXTPREFIX, Msg);
346
347                 c = Client_Next( c );
348         }
349 } /* Wall_ServerNotice */
350
351
352 /* -eof- */