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