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