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