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