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.
27 #include <sys/types.h>
39 #include "irc-write.h"
45 static char Init_Txt[127];
46 static bool Is_Daemon;
49 static char Error_File[FNAME_LEN];
53 static void Wall_ServerNotice PARAMS(( char *Msg ));
56 Log_Message(int Level, const char *msg)
60 fprintf(stdout, "[%ld:%d %4ld] %s\n", (long)getpid(), Level,
61 (long)time(NULL) - NGIRCd_Start, msg);
66 syslog(Level, "%s", msg);
73 Log_Init( bool Daemon_Mode )
75 Is_Daemon = Daemon_Mode;
78 #ifndef LOG_CONS /* Kludge: mips-dec-ultrix4.5 has no LOG_CONS/LOG_LOCAL5 */
84 /* Syslog initialisieren */
85 openlog( PACKAGE_NAME, LOG_CONS|LOG_PID, LOG_LOCAL5 );
89 Log( LOG_NOTICE, "%s started.", NGIRCd_Version );
91 /* Informationen uebern den "Operation Mode" */
96 strlcpy( Init_Txt, "debug-mode", sizeof Init_Txt );
101 if( Init_Txt[0] ) strlcat( Init_Txt, ", ", sizeof Init_Txt );
102 strlcat( Init_Txt, "no-daemon-mode", sizeof Init_Txt );
106 if( Init_Txt[0] ) strlcat( Init_Txt, ", ", sizeof Init_Txt );
107 strlcat( Init_Txt, "passive-mode", sizeof Init_Txt );
112 if( Init_Txt[0] ) strlcat( Init_Txt, ", ", sizeof Init_Txt );
113 strlcat( Init_Txt, "network sniffer", sizeof Init_Txt );
116 if( Init_Txt[0] ) Log( LOG_INFO, "Activating: %s.", Init_Txt );
119 Error_File[0] = '\0';
127 Log_InitErrorfile( void )
129 /* "Error-Log" initialisieren: stderr in Datei umlenken. Dort
130 * landen z.B. alle Ausgaben von assert()-Aufrufen. */
132 /* Dateiname zusammen bauen */
133 snprintf( Error_File, sizeof Error_File, "%s/%s-%ld.err", ERROR_DIR, PACKAGE_NAME, (long)getpid( ));
135 /* stderr umlenken */
137 if( ! freopen( Error_File, "w", stderr ))
139 Log( LOG_ERR, "Can't reopen stderr (\"%s\"): %s", Error_File, strerror( errno ));
143 /* Einige Infos in das Error-File schreiben */
144 fputs( ctime( &NGIRCd_Start ), stderr );
145 fprintf( stderr, "%s started.\n", NGIRCd_Version );
146 fprintf( stderr, "Activating: %s\n\n", Init_Txt[0] ? Init_Txt : "-" );
150 Log( LOG_DEBUG, "Redirected stderr to \"%s\".", Error_File );
152 } /* Log_InitErrfile */
161 if( NGIRCd_SignalRestart ) Log( LOG_NOTICE, "%s done (restarting).", PACKAGE_NAME );
162 else Log( LOG_NOTICE, "%s done.", PACKAGE_NAME );
167 /* Error-File (stderr) loeschen */
168 if( unlink( Error_File ) != 0 ) Log( LOG_ERR, "Can't delete \"%s\": %s", Error_File, strerror( errno ));
173 /* syslog abmelden */
180 * Log function for debug messages.
181 * This function is only functional when the program is compiled with debug
182 * code enabled; otherwise it is an empty function which the compiler will
183 * hopefully mangle down to "nothing" (see log.h). Therefore you should use
184 * LogDebug(...) in favor to Log(LOG_DEBUG, ...).
185 * @param Format Format string like printf().
186 * @param ... Further arguments.
191 LogDebug( const char *Format, ... )
194 LogDebug( Format, va_alist )
197 # endif /* PROTOTYPES */
199 char msg[MAX_LOG_MSG_LEN];
202 if (!NGIRCd_Debug) return;
204 va_start( ap, Format );
208 vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
210 Log(LOG_DEBUG, "%s", msg);
216 * Logging function of ngIRCd.
217 * This function logs messages to the console and/or syslog, whichever is
218 * suitable for the mode ngIRCd is running in (daemon vs. non-daemon).
219 * If LOG_snotice is set, the log messages goes to all user with the mode +s
220 * set and the local &SERVER channel, too.
221 * Please note: you sould use LogDebug(...) for debug messages!
222 * @param Level syslog level (LOG_xxx)
223 * @param Format Format string like printf().
224 * @param ... Further arguments.
228 Log( int Level, const char *Format, ... )
231 Log( Level, Format, va_alist )
237 char msg[MAX_LOG_MSG_LEN];
241 assert( Format != NULL );
243 if( Level & LOG_snotice )
245 /* Notice an User mit "s" Mode */
247 Level &= ~LOG_snotice;
249 else snotice = false;
252 if(( Level == LOG_DEBUG ) && ( ! NGIRCd_Debug )) return;
254 if( Level == LOG_DEBUG ) return;
258 va_start( ap, Format );
262 vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
265 Log_Message(Level, msg);
267 if (Level <= LOG_CRIT) {
268 /* log critical messages to stderr */
269 fprintf(stderr, "%s\n", msg);
274 /* Send NOTICE to all local users with mode +s and to the
275 * local &SERVER channel */
276 Wall_ServerNotice(msg);
277 Channel_LogServer(msg);
283 Log_Init_Resolver( void )
286 openlog( PACKAGE_NAME, LOG_CONS|LOG_PID, LOG_LOCAL5 );
289 Log_Resolver(LOG_DEBUG, "Resolver sub-process starting, PID %ld.", (long)getpid());
291 } /* Log_Init_Resolver */
295 Log_Exit_Resolver( void )
298 Log_Resolver(LOG_DEBUG, "Resolver sub-process %ld done.", (long)getpid());
303 } /* Log_Exit_Resolver */
308 Log_Resolver( const int Level, const char *Format, ... )
311 Log_Resolver( Level, Format, va_alist )
317 /* Eintrag des Resolver in Logfile(s) schreiben */
319 char msg[MAX_LOG_MSG_LEN];
322 assert( Format != NULL );
325 if(( Level == LOG_DEBUG ) && ( ! NGIRCd_Debug )) return;
327 if( Level == LOG_DEBUG ) return;
330 /* String mit variablen Argumenten zusammenbauen ... */
332 va_start( ap, Format );
336 vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
339 Log_Message(Level, msg);
344 * Send log messages to users flagged with the "s" mode.
345 * @param Msg The message to send.
348 Wall_ServerNotice( char *Msg )
352 assert( Msg != NULL );
356 if (Client_Conn(c) > NONE && Client_HasMode(c, 's'))
357 IRC_WriteStrClient(c, "NOTICE %s :%s%s", Client_ID(c),
358 NOTICE_TXTPREFIX, Msg);
360 c = Client_Next( c );
362 } /* Wall_ServerNotice */