2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2005 Alexander Barton (alex@barton.de)
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.
17 static char UNUSED id[] = "$Id: log.c,v 1.62 2006/08/05 09:16:21 fw Exp $";
29 #include <sys/types.h>
41 #include "irc-write.h"
47 static char Init_Txt[127];
48 static bool Is_Daemon;
51 static char Error_File[FNAME_LEN];
55 static void Wall_ServerNotice PARAMS(( char *Msg ));
59 Log_Init( bool Daemon_Mode )
61 Is_Daemon = Daemon_Mode;
64 #ifndef LOG_CONS /* Kludge: mips-dec-ultrix4.5 has no LOG_CONS/LOG_LOCAL5 */
70 /* Syslog initialisieren */
71 openlog( PACKAGE_NAME, LOG_CONS|LOG_PID, LOG_LOCAL5 );
75 Log( LOG_NOTICE, "%s started.", NGIRCd_Version );
77 /* Informationen uebern den "Operation Mode" */
82 strlcpy( Init_Txt, "debug-mode", sizeof Init_Txt );
87 if( Init_Txt[0] ) strlcat( Init_Txt, ", ", sizeof Init_Txt );
88 strlcat( Init_Txt, "no-daemon-mode", sizeof Init_Txt );
92 if( Init_Txt[0] ) strlcat( Init_Txt, ", ", sizeof Init_Txt );
93 strlcat( Init_Txt, "passive-mode", sizeof Init_Txt );
98 if( Init_Txt[0] ) strlcat( Init_Txt, ", ", sizeof Init_Txt );
99 strlcat( Init_Txt, "network sniffer", sizeof Init_Txt );
102 if( Init_Txt[0] ) Log( LOG_INFO, "Activating: %s.", Init_Txt );
105 Error_File[0] = '\0';
113 Log_InitErrorfile( void )
115 /* "Error-Log" initialisieren: stderr in Datei umlenken. Dort
116 * landen z.B. alle Ausgaben von assert()-Aufrufen. */
118 /* Dateiname zusammen bauen */
119 snprintf( Error_File, sizeof Error_File, "%s/%s-%ld.err", ERROR_DIR, PACKAGE_NAME, (long)getpid( ));
121 /* stderr umlenken */
123 if( ! freopen( Error_File, "w", stderr ))
125 Log( LOG_ERR, "Can't reopen stderr (\"%s\"): %s", Error_File, strerror( errno ));
129 /* Einige Infos in das Error-File schreiben */
130 fputs( ctime( &NGIRCd_Start ), stderr );
131 fprintf( stderr, "%s started.\n", NGIRCd_Version );
132 fprintf( stderr, "Activating: %s\n\n", Init_Txt[0] ? Init_Txt : "-" );
136 Log( LOG_DEBUG, "Redirected stderr to \"%s\".", Error_File );
138 } /* Log_InitErrfile */
147 if( NGIRCd_SignalRestart ) Log( LOG_NOTICE, "%s done (restarting).", PACKAGE_NAME );
148 else Log( LOG_NOTICE, "%s done.", PACKAGE_NAME );
153 /* Error-File (stderr) loeschen */
154 if( unlink( Error_File ) != 0 ) Log( LOG_ERR, "Can't delete \"%s\": %s", Error_File, strerror( errno ));
159 /* syslog abmelden */
166 * Log function for debug messages.
167 * This function is only functional when the program is compiled with debug
168 * code enabled; otherwise it is an empty function which the compiler will
169 * hopefully mangle down to "nothing" (see log.h). Therefore you should use
170 * LogDebug(...) in favor to Log(LOG_DEBUG, ...).
171 * @param Format Format string like printf().
172 * @param ... Further arguments.
177 LogDebug( const char *Format, ... )
180 LogDebug( Format, va_alist )
183 # endif /* PROTOTYPES */
185 char msg[MAX_LOG_MSG_LEN];
188 if (!NGIRCd_Debug) return;
190 va_start( ap, Format );
194 vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
196 Log(LOG_DEBUG, "%s", msg);
202 * Logging function of ngIRCd.
203 * This function logs messages to the console and/or syslog, whichever is
204 * suitable for the mode ngIRCd is running in (daemon vs. non-daemon).
205 * Please note: you sould use LogDebug(...) for debug messages!
206 * @param Level syslog level (LOG_xxx)
207 * @param Format Format string like printf().
208 * @param ... Further arguments.
212 Log( int Level, const char *Format, ... )
215 Log( Level, Format, va_alist )
221 /* Eintrag in Logfile(s) schreiben */
222 char msg[MAX_LOG_MSG_LEN];
226 assert( Format != NULL );
228 if( Level & LOG_snotice )
230 /* Notice an User mit "s" Mode */
232 Level &= ~LOG_snotice;
234 else snotice = false;
237 if(( Level == LOG_DEBUG ) && ( ! NGIRCd_Debug )) return;
239 if( Level == LOG_DEBUG ) return;
242 /* String mit variablen Argumenten zusammenbauen ... */
244 va_start( ap, Format );
248 vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
253 /* auf Konsole ausgeben */
254 fprintf( stdout, "[%d:%d] %s\n", (int)getpid( ), Level, msg );
261 syslog( Level, "%s", msg );
265 if( Level <= LOG_CRIT )
267 /* log critical messages to stderr */
268 fprintf( stderr, "%s\n", msg );
274 /* NOTICE an lokale User mit "s"-Mode */
275 Wall_ServerNotice( msg );
281 Log_Init_Resolver( void )
284 openlog( PACKAGE_NAME, LOG_CONS|LOG_PID, LOG_LOCAL5 );
287 Log_Resolver( LOG_DEBUG, "Resolver sub-process starting, PID %d.", getpid( ));
289 } /* Log_Init_Resolver */
293 Log_Exit_Resolver( void )
296 Log_Resolver( LOG_DEBUG, "Resolver sub-process %d done.", getpid( ));
301 } /* Log_Exit_Resolver */
306 Log_Resolver( const int Level, const char *Format, ... )
309 Log_Resolver( Level, Format, va_alist )
315 /* Eintrag des Resolver in Logfile(s) schreiben */
317 char msg[MAX_LOG_MSG_LEN];
320 assert( Format != NULL );
323 if(( Level == LOG_DEBUG ) && ( ! NGIRCd_Debug )) return;
325 if( Level == LOG_DEBUG ) return;
328 /* String mit variablen Argumenten zusammenbauen ... */
330 va_start( ap, Format );
334 vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
339 /* Output to console */
340 fprintf( stdout, "[%d:%d] %s\n", (int)getpid( ), Level, msg );
344 else syslog( Level, "%s", msg );
350 * Send log messages to users flagged with the "s" mode.
351 * @param Msg The message to send.
354 Wall_ServerNotice( char *Msg )
358 assert( Msg != NULL );
362 if (Client_Conn(c) > NONE && Client_HasMode(c, 's'))
363 IRC_WriteStrClient(c, "NOTICE %s :%s%s", Client_ID(c),
364 NOTICE_TXTPREFIX, Msg);
366 c = Client_Next( c );
368 } /* Wall_ServerNotice */