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