]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/log.c
Remove "error file" when compiled with debug code enabled
[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
49 static void
50 Log_Message(int Level, const char *msg)
51 {
52         if (!Is_Daemon) {
53                 /* log to console */
54                 fprintf(stdout, "[%ld:%d %4ld] %s\n", (long)getpid(), Level,
55                                 (long)time(NULL) - NGIRCd_Start, msg);
56                 fflush(stdout);
57         }
58 #ifdef SYSLOG
59         else {
60                 syslog(Level, "%s", msg);
61         }
62 #endif
63 }
64
65
66 GLOBAL void
67 Log_Init( bool Daemon_Mode )
68 {
69         Is_Daemon = Daemon_Mode;
70         
71 #ifdef SYSLOG
72 #ifndef LOG_CONS     /* Kludge: mips-dec-ultrix4.5 has no LOG_CONS */
73 #define LOG_CONS 0
74 #endif
75         openlog(PACKAGE_NAME, LOG_CONS|LOG_PID, Conf_SyslogFacility);
76 #endif
77
78         Log( LOG_NOTICE, "%s started.", NGIRCd_Version );
79           
80         /* Information about "Operation Mode" */
81         Init_Txt[0] = '\0';
82 #ifdef DEBUG
83         if( NGIRCd_Debug )
84         {
85                 strlcpy( Init_Txt, "debug-mode", sizeof Init_Txt );
86         }
87 #endif
88         if( ! Is_Daemon )
89         {
90                 if( Init_Txt[0] ) strlcat( Init_Txt, ", ", sizeof Init_Txt );
91                 strlcat( Init_Txt, "no-daemon-mode", sizeof Init_Txt );
92         }
93         if( NGIRCd_Passive )
94         {
95                 if( Init_Txt[0] ) strlcat( Init_Txt, ", ", sizeof Init_Txt );
96                 strlcat( Init_Txt, "passive-mode", sizeof Init_Txt );
97         }
98 #ifdef SNIFFER
99         if( NGIRCd_Sniffer )
100         {
101                 if( Init_Txt[0] ) strlcat( Init_Txt, ", ", sizeof Init_Txt );
102                 strlcat( Init_Txt, "network sniffer", sizeof Init_Txt );
103         }
104 #endif
105         if( Init_Txt[0] ) Log( LOG_INFO, "Activating: %s.", Init_Txt );
106 } /* Log_Init */
107
108
109 GLOBAL void
110 Log_Exit( void )
111 {
112         Log(LOG_NOTICE, "%s done%s, served %lu connections.", PACKAGE_NAME,
113             NGIRCd_SignalRestart ? " (restarting)" : "", Conn_CountAccepted());
114 #ifdef SYSLOG
115         closelog();
116 #endif
117 } /* Log_Exit */
118
119
120 /**
121  * Log function for debug messages.
122  * This function is only functional when the program is compiled with debug
123  * code enabled; otherwise it is an empty function which the compiler will
124  * hopefully mangle down to "nothing" (see log.h). Therefore you should use
125  * LogDebug(...) in favor to Log(LOG_DEBUG, ...).
126  * @param Format Format string like printf().
127  * @param ... Further arguments.
128  */
129 #ifdef DEBUG
130 # ifdef PROTOTYPES
131 GLOBAL void
132 LogDebug( const char *Format, ... )
133 # else
134 GLOBAL void
135 LogDebug( Format, va_alist )
136 const char *Format;
137 va_dcl
138 # endif /* PROTOTYPES */
139 {
140         char msg[MAX_LOG_MSG_LEN];
141         va_list ap;
142
143         if (!NGIRCd_Debug) return;
144 #ifdef PROTOTYPES
145         va_start( ap, Format );
146 #else
147         va_start( ap );
148 #endif
149         vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
150         va_end( ap );
151         Log(LOG_DEBUG, "%s", msg);
152 }
153 #endif  /* DEBUG */
154
155
156 /**
157  * Logging function of ngIRCd.
158  * This function logs messages to the console and/or syslog, whichever is
159  * suitable for the mode ngIRCd is running in (daemon vs. non-daemon).
160  * If LOG_snotice is set, the log messages goes to all user with the mode +s
161  * set and the local &SERVER channel, too.
162  * Please note: you sould use LogDebug(...) for debug messages!
163  * @param Level syslog level (LOG_xxx)
164  * @param Format Format string like printf().
165  * @param ... Further arguments.
166  */
167 #ifdef PROTOTYPES
168 GLOBAL void
169 Log( int Level, const char *Format, ... )
170 #else
171 GLOBAL void
172 Log( Level, Format, va_alist )
173 int Level;
174 const char *Format;
175 va_dcl
176 #endif
177 {
178         char msg[MAX_LOG_MSG_LEN];
179         bool snotice;
180         va_list ap;
181
182         assert( Format != NULL );
183
184         if( Level & LOG_snotice )
185         {
186                 /* Notice an User mit "s" Mode */
187                 snotice = true;
188                 Level &= ~LOG_snotice;
189         }
190         else snotice = false;
191
192 #ifdef DEBUG
193         if(( Level == LOG_DEBUG ) && ( ! NGIRCd_Debug )) return;
194 #else
195         if( Level == LOG_DEBUG ) return;
196 #endif
197
198 #ifdef PROTOTYPES
199         va_start( ap, Format );
200 #else
201         va_start( ap );
202 #endif
203         vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
204         va_end( ap );
205
206         Log_Message(Level, msg);
207
208         if (Level <= LOG_CRIT) {
209                 /* log critical messages to stderr */
210                 fprintf(stderr, "%s\n", msg);
211                 fflush(stderr);
212         }
213
214         if (snotice) {
215                 /* Send NOTICE to all local users with mode +s and to the
216                  * local &SERVER channel */
217                 Log_ServerNotice('s', "%s", msg);
218                 Channel_LogServer(msg);
219         }
220 } /* Log */
221
222
223 GLOBAL void
224 Log_Init_Subprocess(char UNUSED *Name)
225 {
226 #ifdef SYSLOG
227         openlog(PACKAGE_NAME, LOG_CONS|LOG_PID, Conf_SyslogFacility);
228 #endif
229 #ifdef DEBUG
230         Log_Subprocess(LOG_DEBUG, "%s sub-process starting, PID %ld.",
231                      Name, (long)getpid());
232 #endif
233 }
234
235
236 GLOBAL void
237 Log_Exit_Subprocess(char UNUSED *Name)
238 {
239 #ifdef DEBUG
240         Log_Subprocess(LOG_DEBUG, "%s sub-process %ld done.",
241                      Name, (long)getpid());
242 #endif
243 #ifdef SYSLOG
244         closelog( );
245 #endif
246 }
247
248
249 #ifdef PROTOTYPES
250 GLOBAL void
251 Log_Subprocess(const int Level, const char *Format, ...)
252 #else
253 GLOBAL void
254 Log_Subprocess(Level, Format, va_alist)
255 const int Level;
256 const char *Format;
257 va_dcl
258 #endif
259 {
260         char msg[MAX_LOG_MSG_LEN];
261         va_list ap;
262
263         assert(Format != NULL);
264
265 #ifdef DEBUG
266         if ((Level == LOG_DEBUG) && (!NGIRCd_Debug))
267                 return;
268 #else
269         if (Level == LOG_DEBUG)
270                 return;
271 #endif
272
273 #ifdef PROTOTYPES
274         va_start(ap, Format);
275 #else
276         va_start(ap);
277 #endif
278         vsnprintf(msg, MAX_LOG_MSG_LEN, Format, ap);
279         va_end(ap);
280
281         Log_Message(Level, msg);
282 }
283
284
285 /**
286  * Send a log message to all local users flagged with the given user mode.
287  * @param UserMode User mode which the target user must have set,
288  * @param Format The format string.
289  */
290 #ifdef PROTOTYPES
291 GLOBAL void
292 Log_ServerNotice(const char UserMode, const char *Format, ... )
293 #else
294 GLOBAL void
295 Log_ServerNotice(UserMode, Format, va_alist)
296 const char UserMode;
297 const char *Format;
298 va_dcl
299 #endif
300 {
301         CLIENT *c;
302         char msg[MAX_LOG_MSG_LEN];
303         va_list ap;
304
305         assert(Format != NULL);
306
307 #ifdef PROTOTYPES
308         va_start(ap, Format);
309 #else
310         va_start(ap);
311 #endif
312         vsnprintf(msg, MAX_LOG_MSG_LEN, Format, ap);
313         va_end(ap);
314
315         for(c=Client_First(); c != NULL; c=Client_Next(c)) {
316                 if (Client_Conn(c) > NONE && Client_HasMode(c, UserMode))
317                         IRC_WriteStrClient(c, "NOTICE %s :%s%s", Client_ID(c),
318                                                         NOTICE_TXTPREFIX, msg);
319         }
320 } /* Log_ServerNotice */
321
322
323 /* -eof- */