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 openlog( PACKAGE_NAME, LOG_CONS|LOG_PID, LOG_LOCAL5 );
87 Log( LOG_NOTICE, "%s started.", NGIRCd_Version );
89 /* Information about "Operation Mode" */
94 strlcpy( Init_Txt, "debug-mode", sizeof Init_Txt );
99 if( Init_Txt[0] ) strlcat( Init_Txt, ", ", sizeof Init_Txt );
100 strlcat( Init_Txt, "no-daemon-mode", sizeof Init_Txt );
104 if( Init_Txt[0] ) strlcat( Init_Txt, ", ", sizeof Init_Txt );
105 strlcat( Init_Txt, "passive-mode", sizeof Init_Txt );
110 if( Init_Txt[0] ) strlcat( Init_Txt, ", ", sizeof Init_Txt );
111 strlcat( Init_Txt, "network sniffer", sizeof Init_Txt );
114 if( Init_Txt[0] ) Log( LOG_INFO, "Activating: %s.", Init_Txt );
117 Error_File[0] = '\0';
124 Log_InitErrorfile( void )
126 snprintf( Error_File, sizeof Error_File, "%s/%s-%ld.err", ERROR_DIR, PACKAGE_NAME, (long)getpid( ));
129 if( ! freopen( Error_File, "w", stderr ))
131 Log( LOG_ERR, "Can't reopen stderr (\"%s\"): %s", Error_File, strerror( errno ));
135 fputs( ctime( &NGIRCd_Start ), stderr );
136 fprintf( stderr, "%s started.\n", NGIRCd_Version );
137 fprintf( stderr, "Activating: %s\n\n", Init_Txt[0] ? Init_Txt : "-" );
140 Log(LOG_DEBUG, "Redirected stderr to \"%s\".", Error_File);
141 } /* Log_InitErrfile */
149 if( NGIRCd_SignalRestart ) Log( LOG_NOTICE, "%s done (restarting).", PACKAGE_NAME );
150 else Log( LOG_NOTICE, "%s done.", PACKAGE_NAME );
155 /* Error-File (stderr) loeschen */
156 if( unlink( Error_File ) != 0 ) Log( LOG_ERR, "Can't delete \"%s\": %s", Error_File, strerror( errno ));
167 * Log function for debug messages.
168 * This function is only functional when the program is compiled with debug
169 * code enabled; otherwise it is an empty function which the compiler will
170 * hopefully mangle down to "nothing" (see log.h). Therefore you should use
171 * LogDebug(...) in favor to Log(LOG_DEBUG, ...).
172 * @param Format Format string like printf().
173 * @param ... Further arguments.
178 LogDebug( const char *Format, ... )
181 LogDebug( Format, va_alist )
184 # endif /* PROTOTYPES */
186 char msg[MAX_LOG_MSG_LEN];
189 if (!NGIRCd_Debug) return;
191 va_start( ap, Format );
195 vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
197 Log(LOG_DEBUG, "%s", msg);
203 * Logging function of ngIRCd.
204 * This function logs messages to the console and/or syslog, whichever is
205 * suitable for the mode ngIRCd is running in (daemon vs. non-daemon).
206 * If LOG_snotice is set, the log messages goes to all user with the mode +s
207 * set and the local &SERVER channel, too.
208 * Please note: you sould use LogDebug(...) for debug messages!
209 * @param Level syslog level (LOG_xxx)
210 * @param Format Format string like printf().
211 * @param ... Further arguments.
215 Log( int Level, const char *Format, ... )
218 Log( Level, Format, va_alist )
224 char msg[MAX_LOG_MSG_LEN];
228 assert( Format != NULL );
230 if( Level & LOG_snotice )
232 /* Notice an User mit "s" Mode */
234 Level &= ~LOG_snotice;
236 else snotice = false;
239 if(( Level == LOG_DEBUG ) && ( ! NGIRCd_Debug )) return;
241 if( Level == LOG_DEBUG ) return;
245 va_start( ap, Format );
249 vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
252 Log_Message(Level, msg);
254 if (Level <= LOG_CRIT) {
255 /* log critical messages to stderr */
256 fprintf(stderr, "%s\n", msg);
261 /* Send NOTICE to all local users with mode +s and to the
262 * local &SERVER channel */
263 Wall_ServerNotice(msg);
264 Channel_LogServer(msg);
270 Log_Init_Resolver( void )
273 openlog( PACKAGE_NAME, LOG_CONS|LOG_PID, LOG_LOCAL5 );
276 Log_Resolver(LOG_DEBUG, "Resolver sub-process starting, PID %ld.", (long)getpid());
278 } /* Log_Init_Resolver */
282 Log_Exit_Resolver( void )
285 Log_Resolver(LOG_DEBUG, "Resolver sub-process %ld done.", (long)getpid());
290 } /* Log_Exit_Resolver */
295 Log_Resolver( const int Level, const char *Format, ... )
298 Log_Resolver( Level, Format, va_alist )
304 /* Eintrag des Resolver in Logfile(s) schreiben */
306 char msg[MAX_LOG_MSG_LEN];
309 assert( Format != NULL );
312 if(( Level == LOG_DEBUG ) && ( ! NGIRCd_Debug )) return;
314 if( Level == LOG_DEBUG ) return;
317 /* String mit variablen Argumenten zusammenbauen ... */
319 va_start( ap, Format );
323 vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
326 Log_Message(Level, msg);
331 * Send log messages to users flagged with the "s" mode.
332 * @param Msg The message to send.
335 Wall_ServerNotice( char *Msg )
339 assert( Msg != NULL );
343 if (Client_Conn(c) > NONE && Client_HasMode(c, 's'))
344 IRC_WriteStrClient(c, "NOTICE %s :%s%s", Client_ID(c),
345 NOTICE_TXTPREFIX, Msg);
347 c = Client_Next( c );
349 } /* Wall_ServerNotice */