Blame


1 f4ade537 2001-12-11 alex /*
2 f4ade537 2001-12-11 alex * ngIRCd -- The Next Generation IRC Daemon
3 1547f76c 2002-01-02 alex * Copyright (c)2001,2002 by Alexander Barton (alex@barton.de)
4 f4ade537 2001-12-11 alex *
5 490f28ff 2002-12-12 alex * This program is free software; you can redistribute it and/or modify
6 490f28ff 2002-12-12 alex * it under the terms of the GNU General Public License as published by
7 490f28ff 2002-12-12 alex * the Free Software Foundation; either version 2 of the License, or
8 490f28ff 2002-12-12 alex * (at your option) any later version.
9 490f28ff 2002-12-12 alex * Please read the file COPYING, README and AUTHORS for more information.
10 f4ade537 2001-12-11 alex *
11 490f28ff 2002-12-12 alex * Logging functions
12 f4ade537 2001-12-11 alex */
13 f4ade537 2001-12-11 alex
14 f4ade537 2001-12-11 alex
15 ca33cbda 2002-03-12 alex #include "portab.h"
16 490f28ff 2002-12-12 alex
17 43a4bc5b 2002-12-19 alex static char UNUSED id[] = "$Id: log.c,v 1.42 2002/12/19 04:30:00 alex Exp $";
18 f4ade537 2001-12-11 alex
19 ca33cbda 2002-03-12 alex #include "imp.h"
20 f4ade537 2001-12-11 alex #include <assert.h>
21 fe2bc90e 2002-03-06 alex #include <errno.h>
22 f4ade537 2001-12-11 alex #include <stdarg.h>
23 f4ade537 2001-12-11 alex #include <stdio.h>
24 3be92e9d 2001-12-26 alex #include <string.h>
25 fe2bc90e 2002-03-06 alex #include <sys/types.h>
26 fe2bc90e 2002-03-06 alex #include <unistd.h>
27 50352dc9 2001-12-27 alex
28 50352dc9 2001-12-27 alex #ifdef USE_SYSLOG
29 3d1a0962 2001-12-12 alex #include <syslog.h>
30 50352dc9 2001-12-27 alex #endif
31 f4ade537 2001-12-11 alex
32 006c0328 2002-01-11 alex #include "ngircd.h"
33 ca33cbda 2002-03-12 alex #include "defines.h"
34 c2f60abe 2002-05-27 alex #include "conn.h"
35 c2f60abe 2002-05-27 alex #include "client.h"
36 c2f60abe 2002-05-27 alex #include "channel.h"
37 20a2ffef 2002-03-27 alex #include "irc-write.h"
38 006c0328 2002-01-11 alex
39 ca33cbda 2002-03-12 alex #include "exp.h"
40 f4ade537 2001-12-11 alex #include "log.h"
41 f4ade537 2001-12-11 alex
42 f4ade537 2001-12-11 alex
43 1c00ddff 2002-03-29 alex LOCAL CHAR Error_File[FNAME_LEN];
44 3d74a9c3 2002-03-29 alex LOCAL CHAR Init_Txt[127];
45 1c00ddff 2002-03-29 alex
46 1c00ddff 2002-03-29 alex
47 c2f60abe 2002-05-27 alex LOCAL VOID Wall_ServerNotice PARAMS(( CHAR *Msg ));
48 20a2ffef 2002-03-27 alex
49 20a2ffef 2002-03-27 alex
50 c2f60abe 2002-05-27 alex GLOBAL VOID
51 c2f60abe 2002-05-27 alex Log_Init( VOID )
52 f4ade537 2001-12-11 alex {
53 50352dc9 2001-12-27 alex #ifdef USE_SYSLOG
54 fcb47ae6 2002-02-19 alex /* Syslog initialisieren */
55 0dc0308d 2001-12-25 alex openlog( PACKAGE, LOG_CONS|LOG_PID, LOG_LOCAL5 );
56 50352dc9 2001-12-27 alex #endif
57 fcb47ae6 2002-02-19 alex
58 fcb47ae6 2002-02-19 alex /* Hello World! */
59 006c0328 2002-01-11 alex Log( LOG_NOTICE, "%s started.", NGIRCd_Version( ));
60 fe2bc90e 2002-03-06 alex
61 fcb47ae6 2002-02-19 alex /* Informationen uebern den "Operation Mode" */
62 3d74a9c3 2002-03-29 alex strcpy( Init_Txt, "" );
63 fcb47ae6 2002-02-19 alex #ifdef DEBUG
64 fcb47ae6 2002-02-19 alex if( NGIRCd_Debug )
65 fcb47ae6 2002-02-19 alex {
66 3d74a9c3 2002-03-29 alex if( Init_Txt[0] ) strcat( Init_Txt, ", " );
67 3d74a9c3 2002-03-29 alex strcat( Init_Txt, "debug-mode" );
68 fcb47ae6 2002-02-19 alex }
69 fcb47ae6 2002-02-19 alex #endif
70 fcb47ae6 2002-02-19 alex if( NGIRCd_NoDaemon )
71 fcb47ae6 2002-02-19 alex {
72 3d74a9c3 2002-03-29 alex if( Init_Txt[0] ) strcat( Init_Txt, ", " );
73 3d74a9c3 2002-03-29 alex strcat( Init_Txt, "no-daemon-mode" );
74 fcb47ae6 2002-02-19 alex }
75 fcb47ae6 2002-02-19 alex if( NGIRCd_Passive )
76 fcb47ae6 2002-02-19 alex {
77 3d74a9c3 2002-03-29 alex if( Init_Txt[0] ) strcat( Init_Txt, ", " );
78 3d74a9c3 2002-03-29 alex strcat( Init_Txt, "passive-mode" );
79 fcb47ae6 2002-02-19 alex }
80 fcb47ae6 2002-02-19 alex #ifdef SNIFFER
81 fcb47ae6 2002-02-19 alex if( NGIRCd_Sniffer )
82 fcb47ae6 2002-02-19 alex {
83 3d74a9c3 2002-03-29 alex if( Init_Txt[0] ) strcat( Init_Txt, ", " );
84 3d74a9c3 2002-03-29 alex strcat( Init_Txt, "network sniffer" );
85 fcb47ae6 2002-02-19 alex }
86 fcb47ae6 2002-02-19 alex #endif
87 3d74a9c3 2002-03-29 alex if( Init_Txt[0] ) Log( LOG_INFO, "Activating: %s.", Init_Txt );
88 3d74a9c3 2002-03-29 alex } /* Log_Init */
89 fe2bc90e 2002-03-06 alex
90 3d74a9c3 2002-03-29 alex
91 c2f60abe 2002-05-27 alex GLOBAL VOID
92 c2f60abe 2002-05-27 alex Log_InitErrorfile( VOID )
93 3d74a9c3 2002-03-29 alex {
94 773d886d 2002-03-29 alex /* "Error-Log" initialisieren: stderr in Datei umlenken. Dort
95 773d886d 2002-03-29 alex * landen z.B. alle Ausgaben von assert()-Aufrufen. */
96 3d74a9c3 2002-03-29 alex
97 b58ab32b 2002-03-31 alex /* Dateiname zusammen bauen */
98 c7b55aa6 2002-10-09 alex sprintf( Error_File, "%s/%s-%ld.err", ERROR_DIR, PACKAGE, (LONG)getpid( ));
99 b58ab32b 2002-03-31 alex
100 b58ab32b 2002-03-31 alex /* stderr umlenken */
101 b58ab32b 2002-03-31 alex fflush( stderr );
102 773d886d 2002-03-29 alex if( ! freopen( Error_File, "w", stderr ))
103 773d886d 2002-03-29 alex {
104 773d886d 2002-03-29 alex Log( LOG_ERR, "Can't reopen stderr (\"%s\"): %s", Error_File, strerror( errno ));
105 773d886d 2002-03-29 alex return;
106 773d886d 2002-03-29 alex }
107 fe2bc90e 2002-03-06 alex
108 b58ab32b 2002-03-31 alex /* Einige Infos in das Error-File schreiben */
109 d68fb7a3 2002-03-29 alex fputs( ctime( &NGIRCd_Start ), stderr );
110 773d886d 2002-03-29 alex fprintf( stderr, "%s started.\n", NGIRCd_Version( ));
111 3d74a9c3 2002-03-29 alex fprintf( stderr, "Activating: %s\n\n", Init_Txt[0] ? Init_Txt : "-" );
112 fe2bc90e 2002-03-06 alex fflush( stderr );
113 b58ab32b 2002-03-31 alex
114 b58ab32b 2002-03-31 alex Log( LOG_DEBUG, "Redirected stderr to \"%s\".", Error_File );
115 3d74a9c3 2002-03-29 alex } /* Log_InitErrfile */
116 f4ade537 2001-12-11 alex
117 f4ade537 2001-12-11 alex
118 c2f60abe 2002-05-27 alex GLOBAL VOID
119 c2f60abe 2002-05-27 alex Log_Exit( VOID )
120 f4ade537 2001-12-11 alex {
121 fcb47ae6 2002-02-19 alex /* Good Bye! */
122 43a4bc5b 2002-12-19 alex if( NGIRCd_SignalRestart ) Log( LOG_NOTICE, "%s done (restarting).", PACKAGE );
123 5cac5404 2002-06-02 alex else Log( LOG_NOTICE, "%s done.", PACKAGE );
124 b9a2c4a8 2002-03-21 alex
125 1c00ddff 2002-03-29 alex /* Error-File (stderr) loeschen */
126 1c00ddff 2002-03-29 alex if( unlink( Error_File ) != 0 ) Log( LOG_ERR, "Can't delete \"%s\": %s", Error_File, strerror( errno ));
127 1c00ddff 2002-03-29 alex
128 50352dc9 2001-12-27 alex #ifdef USE_SYSLOG
129 fcb47ae6 2002-02-19 alex /* syslog abmelden */
130 3d1a0962 2001-12-12 alex closelog( );
131 50352dc9 2001-12-27 alex #endif
132 f4ade537 2001-12-11 alex } /* Log_Exit */
133 f4ade537 2001-12-11 alex
134 f4ade537 2001-12-11 alex
135 f7327524 2002-05-30 alex #ifdef PROTOTYPES
136 c2f60abe 2002-05-27 alex GLOBAL VOID
137 c2f60abe 2002-05-27 alex Log( INT Level, CONST CHAR *Format, ... )
138 f7327524 2002-05-30 alex #else
139 f7327524 2002-05-30 alex GLOBAL VOID
140 f7327524 2002-05-30 alex Log( Level, Format, va_alist )
141 f7327524 2002-05-30 alex INT Level;
142 f7327524 2002-05-30 alex CONST CHAR *Format;
143 f7327524 2002-05-30 alex va_dcl
144 f7327524 2002-05-30 alex #endif
145 f4ade537 2001-12-11 alex {
146 f4ade537 2001-12-11 alex /* Eintrag in Logfile(s) schreiben */
147 f4ade537 2001-12-11 alex
148 f4ade537 2001-12-11 alex CHAR msg[MAX_LOG_MSG_LEN];
149 20a2ffef 2002-03-27 alex BOOLEAN snotice;
150 f4ade537 2001-12-11 alex va_list ap;
151 f4ade537 2001-12-11 alex
152 ec1922ef 2001-12-29 alex assert( Format != NULL );
153 ec1922ef 2001-12-29 alex
154 20a2ffef 2002-03-27 alex if( Level & LOG_snotice )
155 20a2ffef 2002-03-27 alex {
156 20a2ffef 2002-03-27 alex /* Notice an User mit "s" Mode */
157 20a2ffef 2002-03-27 alex snotice = TRUE;
158 20a2ffef 2002-03-27 alex Level &= ~LOG_snotice;
159 20a2ffef 2002-03-27 alex }
160 773d886d 2002-03-29 alex else snotice = FALSE;
161 20a2ffef 2002-03-27 alex
162 006c0328 2002-01-11 alex #ifdef DEBUG
163 006c0328 2002-01-11 alex if(( Level == LOG_DEBUG ) && ( ! NGIRCd_Debug )) return;
164 006c0328 2002-01-11 alex #else
165 d4a60bd4 2001-12-25 alex if( Level == LOG_DEBUG ) return;
166 d4a60bd4 2001-12-25 alex #endif
167 d4a60bd4 2001-12-25 alex
168 f4ade537 2001-12-11 alex /* String mit variablen Argumenten zusammenbauen ... */
169 f7327524 2002-05-30 alex #ifdef PROTOTYPES
170 f4ade537 2001-12-11 alex va_start( ap, Format );
171 f7327524 2002-05-30 alex #else
172 f7327524 2002-05-30 alex va_start( ap );
173 f7327524 2002-05-30 alex #endif
174 10363b39 2002-03-03 alex vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
175 10363b39 2002-03-03 alex va_end( ap );
176 f4ade537 2001-12-11 alex
177 bebf0383 2002-03-30 alex if( NGIRCd_NoDaemon )
178 bebf0383 2002-03-30 alex {
179 bebf0383 2002-03-30 alex /* auf Konsole ausgeben */
180 40ebd470 2002-09-03 alex fprintf( stdout, "[%d] %s\n", Level, msg );
181 40ebd470 2002-09-03 alex fflush( stdout );
182 d040fa2a 2002-09-09 alex }
183 d040fa2a 2002-09-09 alex #ifdef USE_SYSLOG
184 d040fa2a 2002-09-09 alex else
185 d040fa2a 2002-09-09 alex {
186 d040fa2a 2002-09-09 alex /* Syslog */
187 ec474a4b 2002-10-04 alex syslog( Level, "%s", msg );
188 bebf0383 2002-03-30 alex }
189 d040fa2a 2002-09-09 alex #endif
190 fe2bc90e 2002-03-06 alex
191 bebf0383 2002-03-30 alex if( Level <= LOG_CRIT )
192 bebf0383 2002-03-30 alex {
193 bebf0383 2002-03-30 alex /* Kritische Meldungen in Error-File (stderr) */
194 bebf0383 2002-03-30 alex fprintf( stderr, "%s\n", msg );
195 bebf0383 2002-03-30 alex fflush( stderr );
196 bebf0383 2002-03-30 alex }
197 bebf0383 2002-03-30 alex
198 bebf0383 2002-03-30 alex if( snotice )
199 bebf0383 2002-03-30 alex {
200 bebf0383 2002-03-30 alex /* NOTICE an lokale User mit "s"-Mode */
201 bebf0383 2002-03-30 alex Wall_ServerNotice( msg );
202 bebf0383 2002-03-30 alex }
203 f4ade537 2001-12-11 alex } /* Log */
204 f4ade537 2001-12-11 alex
205 f4ade537 2001-12-11 alex
206 c2f60abe 2002-05-27 alex GLOBAL VOID
207 c2f60abe 2002-05-27 alex Log_Init_Resolver( VOID )
208 ec1922ef 2001-12-29 alex {
209 ec1922ef 2001-12-29 alex #ifdef USE_SYSLOG
210 ec1922ef 2001-12-29 alex openlog( PACKAGE, LOG_CONS|LOG_PID, LOG_LOCAL5 );
211 ec1922ef 2001-12-29 alex #endif
212 ec1922ef 2001-12-29 alex } /* Log_Init_Resolver */
213 ec1922ef 2001-12-29 alex
214 ec1922ef 2001-12-29 alex
215 c2f60abe 2002-05-27 alex GLOBAL VOID
216 c2f60abe 2002-05-27 alex Log_Exit_Resolver( VOID )
217 ec1922ef 2001-12-29 alex {
218 ec1922ef 2001-12-29 alex #ifdef USE_SYSLOG
219 ec1922ef 2001-12-29 alex closelog( );
220 ec1922ef 2001-12-29 alex #endif
221 ec1922ef 2001-12-29 alex } /* Log_Exit_Resolver */
222 ec1922ef 2001-12-29 alex
223 ec1922ef 2001-12-29 alex
224 f7327524 2002-05-30 alex #ifdef PROTOTYPES
225 c2f60abe 2002-05-27 alex GLOBAL VOID
226 c2f60abe 2002-05-27 alex Log_Resolver( CONST INT Level, CONST CHAR *Format, ... )
227 f7327524 2002-05-30 alex #else
228 f7327524 2002-05-30 alex GLOBAL VOID
229 f7327524 2002-05-30 alex Log_Resolver( Level, Format, va_alist )
230 f7327524 2002-05-30 alex CONST INT Level;
231 f7327524 2002-05-30 alex CONST CHAR *Format;
232 f7327524 2002-05-30 alex va_dcl
233 f7327524 2002-05-30 alex #endif
234 ec1922ef 2001-12-29 alex {
235 ec1922ef 2001-12-29 alex /* Eintrag des Resolver in Logfile(s) schreiben */
236 ec1922ef 2001-12-29 alex
237 03783eea 2002-01-05 alex #ifndef USE_SYSLOG
238 03783eea 2002-01-05 alex return;
239 03783eea 2002-01-05 alex #else
240 03783eea 2002-01-05 alex
241 ec1922ef 2001-12-29 alex CHAR msg[MAX_LOG_MSG_LEN];
242 ec1922ef 2001-12-29 alex va_list ap;
243 ec1922ef 2001-12-29 alex
244 ec1922ef 2001-12-29 alex assert( Format != NULL );
245 ec1922ef 2001-12-29 alex
246 d040fa2a 2002-09-09 alex if( NGIRCd_NoDaemon ) return;
247 d040fa2a 2002-09-09 alex
248 006c0328 2002-01-11 alex #ifdef DEBUG
249 006c0328 2002-01-11 alex if(( Level == LOG_DEBUG ) && ( ! NGIRCd_Debug )) return;
250 006c0328 2002-01-11 alex #else
251 ec1922ef 2001-12-29 alex if( Level == LOG_DEBUG ) return;
252 ec1922ef 2001-12-29 alex #endif
253 ec1922ef 2001-12-29 alex
254 ec1922ef 2001-12-29 alex /* String mit variablen Argumenten zusammenbauen ... */
255 f7327524 2002-05-30 alex #ifdef PROTOTYPES
256 ec1922ef 2001-12-29 alex va_start( ap, Format );
257 f7327524 2002-05-30 alex #else
258 f7327524 2002-05-30 alex va_start( ap );
259 f7327524 2002-05-30 alex #endif
260 10363b39 2002-03-03 alex vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
261 10363b39 2002-03-03 alex va_end( ap );
262 ec1922ef 2001-12-29 alex
263 ec1922ef 2001-12-29 alex /* ... und ausgeben */
264 ec1922ef 2001-12-29 alex syslog( Level, msg );
265 ec1922ef 2001-12-29 alex
266 03783eea 2002-01-05 alex #endif
267 ec1922ef 2001-12-29 alex } /* Log_Resolver */
268 ec1922ef 2001-12-29 alex
269 ec1922ef 2001-12-29 alex
270 c2f60abe 2002-05-27 alex LOCAL VOID
271 c2f60abe 2002-05-27 alex Wall_ServerNotice( CHAR *Msg )
272 20a2ffef 2002-03-27 alex {
273 773d886d 2002-03-29 alex /* Server-Notice an entsprechende User verschicken */
274 773d886d 2002-03-29 alex
275 20a2ffef 2002-03-27 alex CLIENT *c;
276 20a2ffef 2002-03-27 alex
277 20a2ffef 2002-03-27 alex assert( Msg != NULL );
278 773d886d 2002-03-29 alex
279 20a2ffef 2002-03-27 alex c = Client_First( );
280 20a2ffef 2002-03-27 alex while( c )
281 20a2ffef 2002-03-27 alex {
282 856f24eb 2002-10-09 alex if(( Client_Conn( c ) > NONE ) && ( Client_HasMode( c, 's' ))) IRC_WriteStrClient( c, "NOTICE %s :%s%s", Client_ThisServer( ), NOTICE_TXTPREFIX, Msg );
283 20a2ffef 2002-03-27 alex c = Client_Next( c );
284 20a2ffef 2002-03-27 alex }
285 20a2ffef 2002-03-27 alex } /* Wall_ServerNotice */
286 20a2ffef 2002-03-27 alex
287 20a2ffef 2002-03-27 alex
288 f4ade537 2001-12-11 alex /* -eof- */