]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/log.c
Refactor Wall_ServerNotice() into more generic Log_ServerNotice()
[ngircd-alex.git] / src / ngircd / log.c
1 /*
2  * ngIRCd -- The Next Generation IRC Daemon
3  * Copyright (c)2001-2010 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
54 Log_Message(int Level, const char *msg)
55 {
56         if (!Is_Daemon) {
57                 /* log to console */
58                 fprintf(stdout, "[%ld:%d %4ld] %s\n", (long)getpid(), Level,
59                                 (long)time(NULL) - NGIRCd_Start, msg);
60                 fflush(stdout);
61         }
62 #ifdef SYSLOG
63         else {
64                 syslog(Level, "%s", msg);
65         }
66 #endif
67 }
68
69
70 GLOBAL void
71 Log_Init( bool Daemon_Mode )
72 {
73         Is_Daemon = Daemon_Mode;
74         
75 #ifdef SYSLOG
76 #ifndef LOG_CONS     /* Kludge: mips-dec-ultrix4.5 has no LOG_CONS/LOG_LOCAL5 */
77 #define LOG_CONS 0
78 #endif
79 #ifndef LOG_LOCAL5
80 #define LOG_LOCAL5 0
81 #endif
82         openlog( PACKAGE_NAME, LOG_CONS|LOG_PID, LOG_LOCAL5 );
83 #endif
84
85         Log( LOG_NOTICE, "%s started.", NGIRCd_Version );
86           
87         /* Information about "Operation Mode" */
88         Init_Txt[0] = '\0';
89 #ifdef DEBUG
90         if( NGIRCd_Debug )
91         {
92                 strlcpy( Init_Txt, "debug-mode", sizeof Init_Txt );
93         }
94 #endif
95         if( ! Is_Daemon )
96         {
97                 if( Init_Txt[0] ) strlcat( Init_Txt, ", ", sizeof Init_Txt );
98                 strlcat( Init_Txt, "no-daemon-mode", sizeof Init_Txt );
99         }
100         if( NGIRCd_Passive )
101         {
102                 if( Init_Txt[0] ) strlcat( Init_Txt, ", ", sizeof Init_Txt );
103                 strlcat( Init_Txt, "passive-mode", sizeof Init_Txt );
104         }
105 #ifdef SNIFFER
106         if( NGIRCd_Sniffer )
107         {
108                 if( Init_Txt[0] ) strlcat( Init_Txt, ", ", sizeof Init_Txt );
109                 strlcat( Init_Txt, "network sniffer", sizeof Init_Txt );
110         }
111 #endif
112         if( Init_Txt[0] ) Log( LOG_INFO, "Activating: %s.", Init_Txt );
113
114 #ifdef DEBUG
115         Error_File[0] = '\0';
116 #endif
117 } /* Log_Init */
118
119
120 #ifdef DEBUG
121 GLOBAL void
122 Log_InitErrorfile( void )
123 {
124         snprintf( Error_File, sizeof Error_File, "%s/%s-%ld.err", ERROR_DIR, PACKAGE_NAME, (long)getpid( ));
125
126         fflush( stderr );
127         if( ! freopen( Error_File, "w", stderr ))
128         {
129                 Log( LOG_ERR, "Can't reopen stderr (\"%s\"): %s", Error_File, strerror( errno ));
130                 return;
131         }
132
133         fputs( ctime( &NGIRCd_Start ), stderr );
134         fprintf( stderr, "%s started.\n", NGIRCd_Version );
135         fprintf( stderr, "Activating: %s\n\n", Init_Txt[0] ? Init_Txt : "-" );
136         fflush( stderr );
137
138         Log(LOG_DEBUG, "Redirected stderr to \"%s\".", Error_File);
139 } /* Log_InitErrfile */
140 #endif
141
142
143 GLOBAL void
144 Log_Exit( void )
145 {
146         /* Good Bye! */
147         Log(LOG_NOTICE, "%s done%s, served %lu connections.", PACKAGE_NAME,
148             NGIRCd_SignalRestart ? " (restarting)" : "", Conn_CountAccepted());
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         closelog();
160 #endif
161 } /* Log_Exit */
162
163
164 /**
165  * Log function for debug messages.
166  * This function is only functional when the program is compiled with debug
167  * code enabled; otherwise it is an empty function which the compiler will
168  * hopefully mangle down to "nothing" (see log.h). Therefore you should use
169  * LogDebug(...) in favor to Log(LOG_DEBUG, ...).
170  * @param Format Format string like printf().
171  * @param ... Further arguments.
172  */
173 #ifdef DEBUG
174 # ifdef PROTOTYPES
175 GLOBAL void
176 LogDebug( const char *Format, ... )
177 # else
178 GLOBAL void
179 LogDebug( Format, va_alist )
180 const char *Format;
181 va_dcl
182 # endif /* PROTOTYPES */
183 {
184         char msg[MAX_LOG_MSG_LEN];
185         va_list ap;
186
187         if (!NGIRCd_Debug) return;
188 #ifdef PROTOTYPES
189         va_start( ap, Format );
190 #else
191         va_start( ap );
192 #endif
193         vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
194         va_end( ap );
195         Log(LOG_DEBUG, "%s", msg);
196 }
197 #endif  /* DEBUG */
198
199
200 /**
201  * Logging function of ngIRCd.
202  * This function logs messages to the console and/or syslog, whichever is
203  * suitable for the mode ngIRCd is running in (daemon vs. non-daemon).
204  * If LOG_snotice is set, the log messages goes to all user with the mode +s
205  * set and the local &SERVER channel, too.
206  * Please note: you sould use LogDebug(...) for debug messages!
207  * @param Level syslog level (LOG_xxx)
208  * @param Format Format string like printf().
209  * @param ... Further arguments.
210  */
211 #ifdef PROTOTYPES
212 GLOBAL void
213 Log( int Level, const char *Format, ... )
214 #else
215 GLOBAL void
216 Log( Level, Format, va_alist )
217 int Level;
218 const char *Format;
219 va_dcl
220 #endif
221 {
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 #ifdef PROTOTYPES
243         va_start( ap, Format );
244 #else
245         va_start( ap );
246 #endif
247         vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
248         va_end( ap );
249
250         Log_Message(Level, msg);
251
252         if (Level <= LOG_CRIT) {
253                 /* log critical messages to stderr */
254                 fprintf(stderr, "%s\n", msg);
255                 fflush(stderr);
256         }
257
258         if (snotice) {
259                 /* Send NOTICE to all local users with mode +s and to the
260                  * local &SERVER channel */
261                 Log_ServerNotice('s', "%s", msg);
262                 Channel_LogServer(msg);
263         }
264 } /* Log */
265
266
267 GLOBAL void
268 Log_Init_Resolver( void )
269 {
270 #ifdef SYSLOG
271         openlog( PACKAGE_NAME, LOG_CONS|LOG_PID, LOG_LOCAL5 );
272 #endif
273 #ifdef DEBUG
274         Log_Resolver(LOG_DEBUG, "Resolver sub-process starting, PID %ld.", (long)getpid());
275 #endif
276 } /* Log_Init_Resolver */
277
278
279 GLOBAL void
280 Log_Exit_Resolver( void )
281 {
282 #ifdef DEBUG
283         Log_Resolver(LOG_DEBUG, "Resolver sub-process %ld done.", (long)getpid());
284 #endif
285 #ifdef SYSLOG
286         closelog( );
287 #endif
288 } /* Log_Exit_Resolver */
289
290
291 #ifdef PROTOTYPES
292 GLOBAL void
293 Log_Resolver( const int Level, const char *Format, ... )
294 #else
295 GLOBAL void
296 Log_Resolver( Level, Format, va_alist )
297 const int Level;
298 const char *Format;
299 va_dcl
300 #endif
301 {
302         /* Eintrag des Resolver in Logfile(s) schreiben */
303
304         char msg[MAX_LOG_MSG_LEN];
305         va_list ap;
306
307         assert( Format != NULL );
308
309 #ifdef DEBUG
310         if(( Level == LOG_DEBUG ) && ( ! NGIRCd_Debug )) return;
311 #else
312         if( Level == LOG_DEBUG ) return;
313 #endif
314
315         /* String mit variablen Argumenten zusammenbauen ... */
316 #ifdef PROTOTYPES
317         va_start( ap, Format );
318 #else
319         va_start( ap );
320 #endif
321         vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
322         va_end( ap );
323
324         Log_Message(Level, msg);
325 } /* Log_Resolver */
326
327
328 /**
329  * Send a log message to all local users flagged with the given user mode.
330  * @param UserMode User mode which the target user must have set,
331  * @param Format The format string.
332  */
333 #ifdef PROTOTYPES
334 GLOBAL void
335 Log_ServerNotice(const char UserMode, const char *Format, ... )
336 #else
337 GLOBAL void
338 Log_ServerNotice(UserMode, Format, va_alist)
339 const char UserMode;
340 const char *Format;
341 va_dcl
342 #endif
343 {
344         CLIENT *c;
345         char msg[MAX_LOG_MSG_LEN];
346         va_list ap;
347
348         assert(Format != NULL);
349
350 #ifdef PROTOTYPES
351         va_start(ap, Format);
352 #else
353         va_start(ap);
354 #endif
355         vsnprintf(msg, MAX_LOG_MSG_LEN, Format, ap);
356         va_end(ap);
357
358         for(c=Client_First(); c != NULL; c=Client_Next(c)) {
359                 if (Client_Conn(c) > NONE && Client_HasMode(c, UserMode))
360                         IRC_WriteStrClient(c, "NOTICE %s :%s%s", Client_ID(c),
361                                                         NOTICE_TXTPREFIX, msg);
362         }
363 } /* Log_ServerNotice */
364
365
366 /* -eof- */