]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/log.c
Don't log critical (or worse) messages to stderr
[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 (snotice) {
209                 /* Send NOTICE to all local users with mode +s and to the
210                  * local &SERVER channel */
211                 Log_ServerNotice('s', "%s", msg);
212                 Channel_LogServer(msg);
213         }
214 } /* Log */
215
216
217 GLOBAL void
218 Log_Init_Subprocess(char UNUSED *Name)
219 {
220 #ifdef SYSLOG
221         openlog(PACKAGE_NAME, LOG_CONS|LOG_PID, Conf_SyslogFacility);
222 #endif
223 #ifdef DEBUG
224         Log_Subprocess(LOG_DEBUG, "%s sub-process starting, PID %ld.",
225                      Name, (long)getpid());
226 #endif
227 }
228
229
230 GLOBAL void
231 Log_Exit_Subprocess(char UNUSED *Name)
232 {
233 #ifdef DEBUG
234         Log_Subprocess(LOG_DEBUG, "%s sub-process %ld done.",
235                      Name, (long)getpid());
236 #endif
237 #ifdef SYSLOG
238         closelog( );
239 #endif
240 }
241
242
243 #ifdef PROTOTYPES
244 GLOBAL void
245 Log_Subprocess(const int Level, const char *Format, ...)
246 #else
247 GLOBAL void
248 Log_Subprocess(Level, Format, va_alist)
249 const int Level;
250 const char *Format;
251 va_dcl
252 #endif
253 {
254         char msg[MAX_LOG_MSG_LEN];
255         va_list ap;
256
257         assert(Format != NULL);
258
259 #ifdef DEBUG
260         if ((Level == LOG_DEBUG) && (!NGIRCd_Debug))
261                 return;
262 #else
263         if (Level == LOG_DEBUG)
264                 return;
265 #endif
266
267 #ifdef PROTOTYPES
268         va_start(ap, Format);
269 #else
270         va_start(ap);
271 #endif
272         vsnprintf(msg, MAX_LOG_MSG_LEN, Format, ap);
273         va_end(ap);
274
275         Log_Message(Level, msg);
276 }
277
278
279 /**
280  * Send a log message to all local users flagged with the given user mode.
281  * @param UserMode User mode which the target user must have set,
282  * @param Format The format string.
283  */
284 #ifdef PROTOTYPES
285 GLOBAL void
286 Log_ServerNotice(const char UserMode, const char *Format, ... )
287 #else
288 GLOBAL void
289 Log_ServerNotice(UserMode, Format, va_alist)
290 const char UserMode;
291 const char *Format;
292 va_dcl
293 #endif
294 {
295         CLIENT *c;
296         char msg[MAX_LOG_MSG_LEN];
297         va_list ap;
298
299         assert(Format != NULL);
300
301 #ifdef PROTOTYPES
302         va_start(ap, Format);
303 #else
304         va_start(ap);
305 #endif
306         vsnprintf(msg, MAX_LOG_MSG_LEN, Format, ap);
307         va_end(ap);
308
309         for(c=Client_First(); c != NULL; c=Client_Next(c)) {
310                 if (Client_Conn(c) > NONE && Client_HasMode(c, UserMode))
311                         IRC_WriteStrClient(c, "NOTICE %s :%s%s", Client_ID(c),
312                                                         NOTICE_TXTPREFIX, msg);
313         }
314 } /* Log_ServerNotice */
315
316
317 /* -eof- */