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