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