2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2010 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];
54 Log_Message(int Level, const char *msg)
58 fprintf(stdout, "[%ld:%d %4ld] %s\n", (long)getpid(), Level,
59 (long)time(NULL) - NGIRCd_Start, msg);
64 syslog(Level, "%s", msg);
71 Log_Init( bool Daemon_Mode )
73 Is_Daemon = Daemon_Mode;
76 #ifndef LOG_CONS /* Kludge: mips-dec-ultrix4.5 has no LOG_CONS/LOG_LOCAL5 */
82 openlog( PACKAGE_NAME, LOG_CONS|LOG_PID, LOG_LOCAL5 );
85 Log( LOG_NOTICE, "%s started.", NGIRCd_Version );
87 /* Information about "Operation Mode" */
92 strlcpy( Init_Txt, "debug-mode", sizeof Init_Txt );
97 if( Init_Txt[0] ) strlcat( Init_Txt, ", ", sizeof Init_Txt );
98 strlcat( Init_Txt, "no-daemon-mode", sizeof Init_Txt );
102 if( Init_Txt[0] ) strlcat( Init_Txt, ", ", sizeof Init_Txt );
103 strlcat( Init_Txt, "passive-mode", sizeof Init_Txt );
108 if( Init_Txt[0] ) strlcat( Init_Txt, ", ", sizeof Init_Txt );
109 strlcat( Init_Txt, "network sniffer", sizeof Init_Txt );
112 if( Init_Txt[0] ) Log( LOG_INFO, "Activating: %s.", Init_Txt );
115 Error_File[0] = '\0';
122 Log_InitErrorfile( void )
124 snprintf( Error_File, sizeof Error_File, "%s/%s-%ld.err", ERROR_DIR, PACKAGE_NAME, (long)getpid( ));
127 if( ! freopen( Error_File, "w", stderr ))
129 Log( LOG_ERR, "Can't reopen stderr (\"%s\"): %s", Error_File, strerror( errno ));
133 fputs( ctime( &NGIRCd_Start ), stderr );
134 fprintf( stderr, "%s started.\n", NGIRCd_Version );
135 fprintf( stderr, "Activating: %s\n\n", Init_Txt[0] ? Init_Txt : "-" );
138 Log(LOG_DEBUG, "Redirected stderr to \"%s\".", Error_File);
139 } /* Log_InitErrfile */
147 Log(LOG_NOTICE, "%s done%s, served %lu connections.", PACKAGE_NAME,
148 NGIRCd_SignalRestart ? " (restarting)" : "", Conn_CountAccepted());
153 /* Error-File (stderr) loeschen */
154 if( unlink( Error_File ) != 0 ) Log( LOG_ERR, "Can't delete \"%s\": %s", Error_File, strerror( errno ));
165 * Log function for debug messages.
166 * This function is only functional when the program is compiled with debug
167 * code enabled; otherwise it is an empty function which the compiler will
168 * hopefully mangle down to "nothing" (see log.h). Therefore you should use
169 * LogDebug(...) in favor to Log(LOG_DEBUG, ...).
170 * @param Format Format string like printf().
171 * @param ... Further arguments.
176 LogDebug( const char *Format, ... )
179 LogDebug( Format, va_alist )
182 # endif /* PROTOTYPES */
184 char msg[MAX_LOG_MSG_LEN];
187 if (!NGIRCd_Debug) return;
189 va_start( ap, Format );
193 vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
195 Log(LOG_DEBUG, "%s", msg);
201 * Logging function of ngIRCd.
202 * This function logs messages to the console and/or syslog, whichever is
203 * suitable for the mode ngIRCd is running in (daemon vs. non-daemon).
204 * If LOG_snotice is set, the log messages goes to all user with the mode +s
205 * set and the local &SERVER channel, too.
206 * Please note: you sould use LogDebug(...) for debug messages!
207 * @param Level syslog level (LOG_xxx)
208 * @param Format Format string like printf().
209 * @param ... Further arguments.
213 Log( int Level, const char *Format, ... )
216 Log( Level, Format, va_alist )
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;
243 va_start( ap, Format );
247 vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
250 Log_Message(Level, msg);
252 if (Level <= LOG_CRIT) {
253 /* log critical messages to stderr */
254 fprintf(stderr, "%s\n", msg);
259 /* Send NOTICE to all local users with mode +s and to the
260 * local &SERVER channel */
261 Log_ServerNotice('s', "%s", msg);
262 Channel_LogServer(msg);
268 Log_Init_Resolver( void )
271 openlog( PACKAGE_NAME, LOG_CONS|LOG_PID, LOG_LOCAL5 );
274 Log_Resolver(LOG_DEBUG, "Resolver sub-process starting, PID %ld.", (long)getpid());
276 } /* Log_Init_Resolver */
280 Log_Exit_Resolver( void )
283 Log_Resolver(LOG_DEBUG, "Resolver sub-process %ld done.", (long)getpid());
288 } /* Log_Exit_Resolver */
293 Log_Resolver( const int Level, const char *Format, ... )
296 Log_Resolver( Level, Format, va_alist )
302 /* Eintrag des Resolver in Logfile(s) schreiben */
304 char msg[MAX_LOG_MSG_LEN];
307 assert( Format != NULL );
310 if(( Level == LOG_DEBUG ) && ( ! NGIRCd_Debug )) return;
312 if( Level == LOG_DEBUG ) return;
315 /* String mit variablen Argumenten zusammenbauen ... */
317 va_start( ap, Format );
321 vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
324 Log_Message(Level, msg);
329 * Send a log message to all local users flagged with the given user mode.
330 * @param UserMode User mode which the target user must have set,
331 * @param Format The format string.
335 Log_ServerNotice(const char UserMode, const char *Format, ... )
338 Log_ServerNotice(UserMode, Format, va_alist)
345 char msg[MAX_LOG_MSG_LEN];
348 assert(Format != NULL);
351 va_start(ap, Format);
355 vsnprintf(msg, MAX_LOG_MSG_LEN, Format, ap);
358 for(c=Client_First(); c != NULL; c=Client_Next(c)) {
359 if (Client_Conn(c) > NONE && Client_HasMode(c, UserMode))
360 IRC_WriteStrClient(c, "NOTICE %s :%s%s", Client_ID(c),
361 NOTICE_TXTPREFIX, msg);
363 } /* Log_ServerNotice */