]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/log.c
Debian: require "telnet" or "telnet-ssl" for building
[ngircd-alex.git] / src / ngircd / log.c
1 /*
2  * ngIRCd -- The Next Generation IRC Daemon
3  * Copyright (c)2001-2012 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
12 #include "portab.h"
13
14 /**
15  * @file
16  * Logging functions
17  */
18
19 #include "imp.h"
20 #include <assert.h>
21 #include <errno.h>
22 #ifdef PROTOTYPES
23 #       include <stdarg.h>
24 #else
25 #       include <varargs.h>
26 #endif
27 #include <stdio.h>
28 #include <string.h>
29 #include <sys/types.h>
30 #include <unistd.h>
31
32 #ifdef SYSLOG
33 #include <syslog.h>
34 #endif
35
36 #include "ngircd.h"
37 #include "defines.h"
38 #include "conn.h"
39 #include "channel.h"
40 #include "irc-write.h"
41 #include "conf.h"
42
43 #include "exp.h"
44 #include "log.h"
45
46
47 static bool Is_Daemon;
48
49
50 static void
51 Log_Message(int Level, const char *msg)
52 {
53         if (!Is_Daemon) {
54                 /* log to console */
55                 fprintf(stdout, "[%ld:%d %4ld] %s\n", (long)getpid(), Level,
56                                 (long)time(NULL) - NGIRCd_Start, msg);
57                 fflush(stdout);
58         }
59 #ifdef SYSLOG
60         else {
61                 syslog(Level, "%s", msg);
62         }
63 #endif
64 }
65
66
67 /**
68  * Initialitze logging.
69  * This function is called before the configuration file is read in.
70  *
71  * @param Daemon_Mode Set to true if ngIRCd is running as daemon.
72  */
73 GLOBAL void
74 Log_Init(bool Daemon_Mode)
75 {
76         Is_Daemon = Daemon_Mode;
77
78 #ifdef SYSLOG
79 #ifndef LOG_CONS     /* Kludge: mips-dec-ultrix4.5 has no LOG_CONS */
80 #define LOG_CONS 0
81 #endif
82 #ifdef LOG_DAEMON
83         openlog(PACKAGE_NAME, LOG_CONS|LOG_PID, LOG_DAEMON);
84 #else
85         openlog(PACKAGE_NAME, LOG_CONS|LOG_PID, 0);
86 #endif
87 #endif
88 } /* Log_Init */
89
90
91 /**
92  * Re-init logging after reading the configuration file.
93  */
94 GLOBAL void
95 Log_ReInit(void)
96 {
97 #ifdef SYSLOG
98 #ifndef LOG_CONS     /* Kludge: mips-dec-ultrix4.5 has no LOG_CONS */
99 #define LOG_CONS 0
100 #endif
101         closelog();
102         openlog(PACKAGE_NAME, LOG_CONS|LOG_PID, Conf_SyslogFacility);
103 #endif
104         Log(LOG_NOTICE, "%s started.", NGIRCd_Version);
105         Log(LOG_INFO, "Using configuration file \"%s\" ...", NGIRCd_ConfFile);
106 }
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- */