]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/log.c
Clarify that "CAFile" is not set by default
[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 Use_Syslog;
43
44
45 static void
46 Log_Message(int Level, const char *msg)
47 {
48         if (!Use_Syslog) {
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  * Initialize logging.
64  * This function is called before the configuration file is read in.
65  *
66  * @param Syslog_Mode Set to true if ngIRCd is configured to log to the syslog.
67  */
68 GLOBAL void
69 Log_Init(bool Syslog_Mode)
70 {
71         Use_Syslog = Syslog_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 PROTOTYPES
125 GLOBAL void
126 LogDebug( const char *Format, ... )
127 # else
128 GLOBAL void
129 LogDebug( Format, va_alist )
130 const char *Format;
131 va_dcl
132 # endif /* PROTOTYPES */
133 {
134         char msg[MAX_LOG_MSG_LEN];
135         va_list ap;
136
137         if (!NGIRCd_Debug) return;
138 #ifdef PROTOTYPES
139         va_start( ap, Format );
140 #else
141         va_start( ap );
142 #endif
143         vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
144         va_end( ap );
145         Log(LOG_DEBUG, "%s", msg);
146 }
147
148
149 /**
150  * Logging function of ngIRCd.
151  * This function logs messages to the console and/or syslog, whichever is
152  * suitable for the mode ngIRCd is running in (daemon vs. non-daemon).
153  * If LOG_snotice is set, the log messages goes to all user with the mode +s
154  * set and the local &SERVER channel, too.
155  * Please note: you should use LogDebug(...) for debug messages!
156  * @param Level syslog level (LOG_xxx)
157  * @param Format Format string like printf().
158  * @param ... Further arguments.
159  */
160 #ifdef PROTOTYPES
161 GLOBAL void
162 Log( int Level, const char *Format, ... )
163 #else
164 GLOBAL void
165 Log( Level, Format, va_alist )
166 int Level;
167 const char *Format;
168 va_dcl
169 #endif
170 {
171         char msg[MAX_LOG_MSG_LEN];
172         bool snotice;
173         va_list ap;
174
175         assert( Format != NULL );
176
177         if( Level & LOG_snotice )
178         {
179                 /* Notice an User mit "s" Mode */
180                 snotice = true;
181                 Level &= ~LOG_snotice;
182         }
183         else snotice = false;
184
185         if(( Level == LOG_DEBUG ) && ( ! NGIRCd_Debug )) return;
186
187 #ifdef PROTOTYPES
188         va_start( ap, Format );
189 #else
190         va_start( ap );
191 #endif
192         vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
193         va_end( ap );
194
195         Log_Message(Level, msg);
196
197         if (snotice) {
198                 /* Send NOTICE to all local users with mode +s and to the
199                  * local &SERVER channel */
200                 Log_ServerNotice('s', "%s", msg);
201                 Channel_LogServer(msg);
202         }
203 } /* Log */
204
205
206 GLOBAL void
207 Log_Init_Subprocess(char UNUSED *Name)
208 {
209 #ifdef SYSLOG
210         openlog(PACKAGE, LOG_CONS|LOG_PID, Conf_SyslogFacility);
211 #endif
212         Log_Subprocess(LOG_DEBUG, "%s sub-process starting, PID %ld.",
213                      Name, (long)getpid());
214 }
215
216
217 GLOBAL void
218 Log_Exit_Subprocess(char UNUSED *Name)
219 {
220         Log_Subprocess(LOG_DEBUG, "%s sub-process %ld done.",
221                      Name, (long)getpid());
222 #ifdef SYSLOG
223         closelog( );
224 #endif
225 }
226
227
228 #ifdef PROTOTYPES
229 GLOBAL void
230 Log_Subprocess(const int Level, const char *Format, ...)
231 #else
232 GLOBAL void
233 Log_Subprocess(Level, Format, va_alist)
234 const int Level;
235 const char *Format;
236 va_dcl
237 #endif
238 {
239         char msg[MAX_LOG_MSG_LEN];
240         va_list ap;
241
242         assert(Format != NULL);
243
244         if ((Level == LOG_DEBUG) && (!NGIRCd_Debug))
245                 return;
246
247 #ifdef PROTOTYPES
248         va_start(ap, Format);
249 #else
250         va_start(ap);
251 #endif
252         vsnprintf(msg, MAX_LOG_MSG_LEN, Format, ap);
253         va_end(ap);
254
255         Log_Message(Level, msg);
256 }
257
258
259 /**
260  * Send a log message to all local users flagged with the given user mode.
261  * @param UserMode User mode which the target user must have set,
262  * @param Format The format string.
263  */
264 #ifdef PROTOTYPES
265 GLOBAL void
266 Log_ServerNotice(const char UserMode, const char *Format, ... )
267 #else
268 GLOBAL void
269 Log_ServerNotice(UserMode, Format, va_alist)
270 const char UserMode;
271 const char *Format;
272 va_dcl
273 #endif
274 {
275         CLIENT *c;
276         char msg[MAX_LOG_MSG_LEN];
277         va_list ap;
278
279         assert(Format != NULL);
280
281 #ifdef PROTOTYPES
282         va_start(ap, Format);
283 #else
284         va_start(ap);
285 #endif
286         vsnprintf(msg, MAX_LOG_MSG_LEN, Format, ap);
287         va_end(ap);
288
289         for(c=Client_First(); c != NULL; c=Client_Next(c)) {
290                 if (Client_Conn(c) > NONE && Client_HasMode(c, UserMode))
291                         IRC_WriteStrClient(c, "NOTICE %s :%s%s", Client_ID(c),
292                                                         NOTICE_TXTPREFIX, msg);
293         }
294 } /* Log_ServerNotice */
295
296
297 /* -eof- */