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.61 2006/07/23 23:23:45 alex 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". Therefore you should use LogDebug(...)
170 * in favor to Log(LOG_DEBUG, ...).
171 * @param Format Format string like printf().
172 * @param ... Further arguments.
177 LogDebug( const char *Format, ... )
179 LogDebug( UNUSED const char *Format, ... )
183 LogDebug( Format, va_alist )
186 # endif /* PROTOTYPES */
189 char msg[MAX_LOG_MSG_LEN];
192 if (!NGIRCd_Debug) return;
194 va_start( ap, Format );
198 vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
200 Log(LOG_DEBUG, "%s", msg);
205 * The compiler should optimize this out, please ;-) */
211 * Logging function of ngIRCd.
212 * This function logs messages to the console and/or syslog, whichever is
213 * suitable for the mode ngIRCd is running in (daemon vs. non-daemon).
214 * Please note: you sould use LogDebug(...) for debug messages!
215 * @param Level syslog level (LOG_xxx)
216 * @param Format Format string like printf().
217 * @param ... Further arguments.
221 Log( int Level, const char *Format, ... )
224 Log( Level, Format, va_alist )
230 /* Eintrag in Logfile(s) schreiben */
231 char msg[MAX_LOG_MSG_LEN];
235 assert( Format != NULL );
237 if( Level & LOG_snotice )
239 /* Notice an User mit "s" Mode */
241 Level &= ~LOG_snotice;
243 else snotice = false;
246 if(( Level == LOG_DEBUG ) && ( ! NGIRCd_Debug )) return;
248 if( Level == LOG_DEBUG ) return;
251 /* String mit variablen Argumenten zusammenbauen ... */
253 va_start( ap, Format );
257 vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
262 /* auf Konsole ausgeben */
263 fprintf( stdout, "[%d:%d] %s\n", (int)getpid( ), Level, msg );
270 syslog( Level, "%s", msg );
274 if( Level <= LOG_CRIT )
276 /* log critical messages to stderr */
277 fprintf( stderr, "%s\n", msg );
283 /* NOTICE an lokale User mit "s"-Mode */
284 Wall_ServerNotice( msg );
290 Log_Init_Resolver( void )
293 openlog( PACKAGE_NAME, LOG_CONS|LOG_PID, LOG_LOCAL5 );
296 Log_Resolver( LOG_DEBUG, "Resolver sub-process starting, PID %d.", getpid( ));
298 } /* Log_Init_Resolver */
302 Log_Exit_Resolver( void )
305 Log_Resolver( LOG_DEBUG, "Resolver sub-process %d done.", getpid( ));
310 } /* Log_Exit_Resolver */
315 Log_Resolver( const int Level, const char *Format, ... )
318 Log_Resolver( Level, Format, va_alist )
324 /* Eintrag des Resolver in Logfile(s) schreiben */
326 char msg[MAX_LOG_MSG_LEN];
329 assert( Format != NULL );
332 if(( Level == LOG_DEBUG ) && ( ! NGIRCd_Debug )) return;
334 if( Level == LOG_DEBUG ) return;
337 /* String mit variablen Argumenten zusammenbauen ... */
339 va_start( ap, Format );
343 vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
348 /* Output to console */
349 fprintf( stdout, "[%d:%d] %s\n", (int)getpid( ), Level, msg );
353 else syslog( Level, "%s", msg );
359 * Send log messages to users flagged with the "s" mode.
360 * @param Msg The message to send.
363 Wall_ServerNotice( char *Msg )
367 assert( Msg != NULL );
371 if (Client_Conn(c) > NONE && Client_HasMode(c, 's'))
372 IRC_WriteStrClient(c, "NOTICE %s :%s%s", Client_ID(c),
373 NOTICE_TXTPREFIX, Msg);
375 c = Client_Next( c );
377 } /* Wall_ServerNotice */