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