]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/log.c
Mark some variables as "unused" to prevent compiler warnings
[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_Subprocess(char UNUSED *Name)
268 {
269 #ifdef SYSLOG
270         openlog( PACKAGE_NAME, LOG_CONS|LOG_PID, LOG_LOCAL5 );
271 #endif
272 #ifdef DEBUG
273         Log_Subprocess(LOG_DEBUG, "%s sub-process starting, PID %ld.",
274                      Name, (long)getpid());
275 #endif
276 }
277
278
279 GLOBAL void
280 Log_Exit_Subprocess(char UNUSED *Name)
281 {
282 #ifdef DEBUG
283         Log_Subprocess(LOG_DEBUG, "%s sub-process %ld done.",
284                      Name, (long)getpid());
285 #endif
286 #ifdef SYSLOG
287         closelog( );
288 #endif
289 }
290
291
292 #ifdef PROTOTYPES
293 GLOBAL void
294 Log_Subprocess(const int Level, const char *Format, ...)
295 #else
296 GLOBAL void
297 Log_Subprocess(Level, Format, va_alist)
298 const int Level;
299 const char *Format;
300 va_dcl
301 #endif
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))
310                 return;
311 #else
312         if (Level == LOG_DEBUG)
313                 return;
314 #endif
315
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 }
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- */