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