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>
38 #include "irc-write.h"
44 static char Init_Txt[127];
45 static bool Is_Daemon;
48 static char Error_File[FNAME_LEN];
53 Log_Message(int Level, const char *msg)
57 fprintf(stdout, "[%ld:%d %4ld] %s\n", (long)getpid(), Level,
58 (long)time(NULL) - NGIRCd_Start, msg);
63 syslog(Level, "%s", msg);
70 Log_Init( bool Daemon_Mode )
72 Is_Daemon = Daemon_Mode;
75 #ifndef LOG_CONS /* Kludge: mips-dec-ultrix4.5 has no LOG_CONS/LOG_LOCAL5 */
81 openlog( PACKAGE_NAME, LOG_CONS|LOG_PID, LOG_LOCAL5 );
84 Log( LOG_NOTICE, "%s started.", NGIRCd_Version );
86 /* Information about "Operation Mode" */
91 strlcpy( Init_Txt, "debug-mode", sizeof Init_Txt );
96 if( Init_Txt[0] ) strlcat( Init_Txt, ", ", sizeof Init_Txt );
97 strlcat( Init_Txt, "no-daemon-mode", sizeof Init_Txt );
101 if( Init_Txt[0] ) strlcat( Init_Txt, ", ", sizeof Init_Txt );
102 strlcat( Init_Txt, "passive-mode", sizeof Init_Txt );
107 if( Init_Txt[0] ) strlcat( Init_Txt, ", ", sizeof Init_Txt );
108 strlcat( Init_Txt, "network sniffer", sizeof Init_Txt );
111 if( Init_Txt[0] ) Log( LOG_INFO, "Activating: %s.", Init_Txt );
114 Error_File[0] = '\0';
121 Log_InitErrorfile( void )
123 snprintf( Error_File, sizeof Error_File, "%s/%s-%ld.err", ERROR_DIR, PACKAGE_NAME, (long)getpid( ));
126 if( ! freopen( Error_File, "w", stderr ))
128 Log( LOG_ERR, "Can't reopen stderr (\"%s\"): %s", Error_File, strerror( errno ));
132 fputs( ctime( &NGIRCd_Start ), stderr );
133 fprintf( stderr, "%s started.\n", NGIRCd_Version );
134 fprintf( stderr, "Activating: %s\n\n", Init_Txt[0] ? Init_Txt : "-" );
137 Log(LOG_DEBUG, "Redirected stderr to \"%s\".", Error_File);
138 } /* Log_InitErrfile */
146 Log(LOG_NOTICE, "%s done%s, served %lu connections.", PACKAGE_NAME,
147 NGIRCd_SignalRestart ? " (restarting)" : "", Conn_CountAccepted());
152 /* Error-File (stderr) loeschen */
153 if( unlink( Error_File ) != 0 ) Log( LOG_ERR, "Can't delete \"%s\": %s", Error_File, strerror( errno ));
164 * Log function for debug messages.
165 * This function is only functional when the program is compiled with debug
166 * code enabled; otherwise it is an empty function which the compiler will
167 * hopefully mangle down to "nothing" (see log.h). Therefore you should use
168 * LogDebug(...) in favor to Log(LOG_DEBUG, ...).
169 * @param Format Format string like printf().
170 * @param ... Further arguments.
175 LogDebug( const char *Format, ... )
178 LogDebug( Format, va_alist )
181 # endif /* PROTOTYPES */
183 char msg[MAX_LOG_MSG_LEN];
186 if (!NGIRCd_Debug) return;
188 va_start( ap, Format );
192 vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
194 Log(LOG_DEBUG, "%s", msg);
200 * Logging function of ngIRCd.
201 * This function logs messages to the console and/or syslog, whichever is
202 * suitable for the mode ngIRCd is running in (daemon vs. non-daemon).
203 * If LOG_snotice is set, the log messages goes to all user with the mode +s
204 * set and the local &SERVER channel, too.
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 char msg[MAX_LOG_MSG_LEN];
225 assert( Format != NULL );
227 if( Level & LOG_snotice )
229 /* Notice an User mit "s" Mode */
231 Level &= ~LOG_snotice;
233 else snotice = false;
236 if(( Level == LOG_DEBUG ) && ( ! NGIRCd_Debug )) return;
238 if( Level == LOG_DEBUG ) return;
242 va_start( ap, Format );
246 vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
249 Log_Message(Level, msg);
251 if (Level <= LOG_CRIT) {
252 /* log critical messages to stderr */
253 fprintf(stderr, "%s\n", msg);
258 /* Send NOTICE to all local users with mode +s and to the
259 * local &SERVER channel */
260 Log_ServerNotice('s', "%s", msg);
261 Channel_LogServer(msg);
267 Log_Init_Subprocess(char *Name)
270 openlog( PACKAGE_NAME, LOG_CONS|LOG_PID, LOG_LOCAL5 );
273 Log_Subprocess(LOG_DEBUG, "%s sub-process starting, PID %ld.",
274 Name, (long)getpid());
280 Log_Exit_Subprocess(char *Name)
283 Log_Subprocess(LOG_DEBUG, "%s sub-process %ld done.",
284 Name, (long)getpid());
294 Log_Subprocess(const int Level, const char *Format, ...)
297 Log_Subprocess(Level, Format, va_alist)
303 char msg[MAX_LOG_MSG_LEN];
306 assert(Format != NULL);
309 if ((Level == LOG_DEBUG) && (!NGIRCd_Debug))
312 if (Level == LOG_DEBUG)
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 */