]> arthur.barton.de Git - ngircd-alex.git/blobdiff - src/ngircd/log.c
Slightly reoder startup steps, and enhance logging
[ngircd-alex.git] / src / ngircd / log.c
index 7fa57255ca7b957373db701c29865ca3f223e3de..a47ce92cd81571025a446ecd0f54b8553dcf9020 100644 (file)
 /*
  * ngIRCd -- The Next Generation IRC Daemon
- * Copyright (c)2001,2002 by Alexander Barton (alex@barton.de)
+ * Copyright (c)2001-2019 Alexander Barton (alex@barton.de) and Contributors.
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
  * (at your option) any later version.
  * Please read the file COPYING, README and AUTHORS for more information.
- *
- * Logging functions
  */
 
-
 #include "portab.h"
 
-static char UNUSED id[] = "$Id: log.c,v 1.47 2004/06/26 08:50:44 alex Exp $";
+/**
+ * @file
+ * Logging functions
+ */
 
-#include "imp.h"
 #include <assert.h>
-#include <errno.h>
-#include <stdarg.h>
+#ifdef PROTOTYPES
+# include <stdarg.h>
+#else
+# include <varargs.h>
+#endif
 #include <stdio.h>
-#include <string.h>
 #include <sys/types.h>
+#include <time.h>
 #include <unistd.h>
 
 #ifdef SYSLOG
-#include <syslog.h>
+# include <syslog.h>
 #endif
 
 #include "ngircd.h"
-#include "defines.h"
 #include "conn.h"
-#include "client.h"
 #include "channel.h"
 #include "irc-write.h"
+#include "conf.h"
 
-#include "exp.h"
 #include "log.h"
 
-
-LOCAL CHAR Error_File[FNAME_LEN];
-LOCAL CHAR Init_Txt[127];
+static bool Is_Daemon;
 
 
-LOCAL VOID Wall_ServerNotice PARAMS(( CHAR *Msg ));
+static void
+Log_Message(int Level, const char *msg)
+{
+       if (!Is_Daemon) {
+               /* log to console */
+               fprintf(stdout, "[%ld:%d %4ld] %s\n", (long)getpid(), Level,
+                               (long)(time(NULL) - NGIRCd_Start), msg);
+               fflush(stdout);
+       }
+#ifdef SYSLOG
+       else {
+               syslog(Level, "%s", msg);
+       }
+#endif
+}
 
 
-GLOBAL VOID
-Log_Init( VOID )
+/**
+ * Initialitze logging.
+ * This function is called before the configuration file is read in.
+ *
+ * @param Daemon_Mode Set to true if ngIRCd is running as daemon.
+ */
+GLOBAL void
+Log_Init(bool Daemon_Mode)
 {
+       Is_Daemon = Daemon_Mode;
+
 #ifdef SYSLOG
-       /* Syslog initialisieren */
-       openlog( PACKAGE_NAME, LOG_CONS|LOG_PID, LOG_LOCAL5 );
+#ifndef LOG_CONS     /* Kludge: mips-dec-ultrix4.5 has no LOG_CONS */
+#define LOG_CONS 0
 #endif
-
-       /* Hello World! */
-       Log( LOG_NOTICE, "%s started.", NGIRCd_Version( ));
-         
-       /* Informationen uebern den "Operation Mode" */
-       strcpy( Init_Txt, "" );
-#ifdef DEBUG
-       if( NGIRCd_Debug )
-       {
-               if( Init_Txt[0] ) strcat( Init_Txt, ", " );
-               strcat( Init_Txt, "debug-mode" );
-       }
+#ifdef LOG_DAEMON
+       openlog(PACKAGE, LOG_CONS|LOG_PID, LOG_DAEMON);
+#else
+       openlog(PACKAGE, LOG_CONS|LOG_PID, 0);
 #endif
-       if( NGIRCd_NoDaemon )
-       {
-               if( Init_Txt[0] ) strcat( Init_Txt, ", " );
-               strcat( Init_Txt, "no-daemon-mode" );
-       }
-       if( NGIRCd_Passive )
-       {
-               if( Init_Txt[0] ) strcat( Init_Txt, ", " );
-               strcat( Init_Txt, "passive-mode" );
-       }
-#ifdef SNIFFER
-       if( NGIRCd_Sniffer )
-       {
-               if( Init_Txt[0] ) strcat( Init_Txt, ", " );
-               strcat( Init_Txt, "network sniffer" );
-       }
 #endif
-       if( Init_Txt[0] ) Log( LOG_INFO, "Activating: %s.", Init_Txt );
-
-       Error_File[0] = '\0';
+       Log(LOG_NOTICE, "%s starting ...", NGIRCd_Version);
 } /* Log_Init */
 
 
-GLOBAL VOID
-Log_InitErrorfile( VOID )
+/**
+ * Re-init logging after reading the configuration file.
+ */
+GLOBAL void
+Log_ReInit(void)
 {
-       /* "Error-Log" initialisieren: stderr in Datei umlenken. Dort
-        * landen z.B. alle Ausgaben von assert()-Aufrufen. */
-
-       /* Dateiname zusammen bauen */
-       sprintf( Error_File, "%s/%s-%ld.err", ERROR_DIR, PACKAGE_NAME, (LONG)getpid( ));
-
-       /* stderr umlenken */
-       fflush( stderr );
-       if( ! freopen( Error_File, "w", stderr ))
-       {
-               Log( LOG_ERR, "Can't reopen stderr (\"%s\"): %s", Error_File, strerror( errno ));
-               return;
-       }
-
-       /* Einige Infos in das Error-File schreiben */
-       fputs( ctime( &NGIRCd_Start ), stderr );
-       fprintf( stderr, "%s started.\n", NGIRCd_Version( ));
-       fprintf( stderr, "Activating: %s\n\n", Init_Txt[0] ? Init_Txt : "-" );
-       fflush( stderr );
-
-       Log( LOG_DEBUG, "Redirected stderr to \"%s\".", Error_File );
-} /* Log_InitErrfile */
+#ifdef SYSLOG
+#ifndef LOG_CONS     /* Kludge: mips-dec-ultrix4.5 has no LOG_CONS */
+#define LOG_CONS 0
+#endif
+       closelog();
+       openlog(PACKAGE, LOG_CONS|LOG_PID, Conf_SyslogFacility);
+#endif
+}
 
 
-GLOBAL VOID
-Log_Exit( VOID )
+GLOBAL void
+Log_Exit( void )
 {
-       /* Good Bye! */
-       if( NGIRCd_SignalRestart ) Log( LOG_NOTICE, "%s done (restarting).", PACKAGE_NAME );
-       else Log( LOG_NOTICE, "%s done.", PACKAGE_NAME );
-
-       if( Error_File[0] )
-       {
-               /* Error-File (stderr) loeschen */
-               if( unlink( Error_File ) != 0 ) Log( LOG_ERR, "Can't delete \"%s\": %s", Error_File, strerror( errno ));
-       }
-
+       Log(LOG_NOTICE, "%s done%s, served %lu connection%s.", PACKAGE_NAME,
+           NGIRCd_SignalRestart ? " (restarting)" : "", Conn_CountAccepted(),
+           Conn_CountAccepted() == 1 ? "" : "s");
 #ifdef SYSLOG
-       /* syslog abmelden */
-       closelog( );
+       closelog();
 #endif
 } /* Log_Exit */
 
 
+/**
+ * Log function for debug messages.
+ * This function is only functional when the program is compiled with debug
+ * code enabled; otherwise it is an empty function which the compiler will
+ * hopefully mangle down to "nothing" (see log.h). Therefore you should use
+ * LogDebug(...) in favor to Log(LOG_DEBUG, ...).
+ * @param Format Format string like printf().
+ * @param ... Further arguments.
+ */
+#ifdef DEBUG
+# ifdef PROTOTYPES
+GLOBAL void
+LogDebug( const char *Format, ... )
+# else
+GLOBAL void
+LogDebug( Format, va_alist )
+const char *Format;
+va_dcl
+# endif /* PROTOTYPES */
+{
+       char msg[MAX_LOG_MSG_LEN];
+       va_list ap;
+
+       if (!NGIRCd_Debug) return;
 #ifdef PROTOTYPES
-GLOBAL VOID
-Log( INT Level, CONST CHAR *Format, ... )
+       va_start( ap, Format );
 #else
-GLOBAL VOID
+       va_start( ap );
+#endif
+       vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
+       va_end( ap );
+       Log(LOG_DEBUG, "%s", msg);
+}
+#endif /* DEBUG */
+
+
+/**
+ * Logging function of ngIRCd.
+ * This function logs messages to the console and/or syslog, whichever is
+ * suitable for the mode ngIRCd is running in (daemon vs. non-daemon).
+ * If LOG_snotice is set, the log messages goes to all user with the mode +s
+ * set and the local &SERVER channel, too.
+ * Please note: you should use LogDebug(...) for debug messages!
+ * @param Level syslog level (LOG_xxx)
+ * @param Format Format string like printf().
+ * @param ... Further arguments.
+ */
+#ifdef PROTOTYPES
+GLOBAL void
+Log( int Level, const char *Format, ... )
+#else
+GLOBAL void
 Log( Level, Format, va_alist )
-INT Level;
-CONST CHAR *Format;
+int Level;
+const char *Format;
 va_dcl
 #endif
 {
-       /* Eintrag in Logfile(s) schreiben */
-
-       CHAR msg[MAX_LOG_MSG_LEN];
-       BOOLEAN snotice;
+       char msg[MAX_LOG_MSG_LEN];
+       bool snotice;
        va_list ap;
 
        assert( Format != NULL );
@@ -159,10 +179,10 @@ va_dcl
        if( Level & LOG_snotice )
        {
                /* Notice an User mit "s" Mode */
-               snotice = TRUE;
+               snotice = true;
                Level &= ~LOG_snotice;
        }
-       else snotice = FALSE;
+       else snotice = false;
 
 #ifdef DEBUG
        if(( Level == LOG_DEBUG ) && ( ! NGIRCd_Debug )) return;
@@ -170,7 +190,6 @@ va_dcl
        if( Level == LOG_DEBUG ) return;
 #endif
 
-       /* String mit variablen Argumenten zusammenbauen ... */
 #ifdef PROTOTYPES
        va_start( ap, Format );
 #else
@@ -179,117 +198,115 @@ va_dcl
        vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
        va_end( ap );
 
-       if( NGIRCd_NoDaemon )
-       {
-               /* auf Konsole ausgeben */
-               fprintf( stdout, "[%d:%d] %s\n", (INT)getpid( ), Level, msg );
-               fflush( stdout );
-       }
-#ifdef SYSLOG
-       else
-       {
-               /* Syslog */
-               syslog( Level, "%s", msg );
-       }
-#endif
-
-       if( Level <= LOG_CRIT )
-       {
-               /* Kritische Meldungen in Error-File (stderr) */
-               fprintf( stderr, "%s\n", msg );
-               fflush( stderr );
-       }
+       Log_Message(Level, msg);
 
-       if( snotice )
-       {
-               /* NOTICE an lokale User mit "s"-Mode */
-               Wall_ServerNotice( msg );
+       if (snotice) {
+               /* Send NOTICE to all local users with mode +s and to the
+                * local &SERVER channel */
+               Log_ServerNotice('s', "%s", msg);
+               Channel_LogServer(msg);
        }
 } /* Log */
 
 
-GLOBAL VOID
-Log_Init_Resolver( VOID )
+GLOBAL void
+Log_Init_Subprocess(char UNUSED *Name)
 {
 #ifdef SYSLOG
-       openlog( PACKAGE_NAME, LOG_CONS|LOG_PID, LOG_LOCAL5 );
+       openlog(PACKAGE, LOG_CONS|LOG_PID, Conf_SyslogFacility);
+#endif
+#ifdef DEBUG
+       Log_Subprocess(LOG_DEBUG, "%s sub-process starting, PID %ld.",
+                    Name, (long)getpid());
 #endif
-       Log_Resolver( LOG_DEBUG, "Resolver sub-process starting, PID %d.", getpid( ));
-} /* Log_Init_Resolver */
+}
 
 
-GLOBAL VOID
-Log_Exit_Resolver( VOID )
+GLOBAL void
+Log_Exit_Subprocess(char UNUSED *Name)
 {
-       Log_Resolver( LOG_DEBUG, "Resolver sub-process %d done.", getpid( ));
+#ifdef DEBUG
+       Log_Subprocess(LOG_DEBUG, "%s sub-process %ld done.",
+                    Name, (long)getpid());
+#endif
 #ifdef SYSLOG
        closelog( );
 #endif
-} /* Log_Exit_Resolver */
+}
 
 
 #ifdef PROTOTYPES
-GLOBAL VOID
-Log_Resolver( CONST INT Level, CONST CHAR *Format, ... )
+GLOBAL void
+Log_Subprocess(const int Level, const char *Format, ...)
 #else
-GLOBAL VOID
-Log_Resolver( Level, Format, va_alist )
-CONST INT Level;
-CONST CHAR *Format;
+GLOBAL void
+Log_Subprocess(Level, Format, va_alist)
+const int Level;
+const char *Format;
 va_dcl
 #endif
 {
-       /* Eintrag des Resolver in Logfile(s) schreiben */
-
-       CHAR msg[MAX_LOG_MSG_LEN];
+       char msg[MAX_LOG_MSG_LEN];
        va_list ap;
 
-       assert( Format != NULL );
+       assert(Format != NULL);
 
 #ifdef DEBUG
-       if(( Level == LOG_DEBUG ) && ( ! NGIRCd_Debug )) return;
+       if ((Level == LOG_DEBUG) && (!NGIRCd_Debug))
+               return;
 #else
-       if( Level == LOG_DEBUG ) return;
+       if (Level == LOG_DEBUG)
+               return;
 #endif
 
-       /* String mit variablen Argumenten zusammenbauen ... */
 #ifdef PROTOTYPES
-       va_start( ap, Format );
+       va_start(ap, Format);
 #else
-       va_start( ap );
+       va_start(ap);
 #endif
-       vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
-       va_end( ap );
+       vsnprintf(msg, MAX_LOG_MSG_LEN, Format, ap);
+       va_end(ap);
 
-       /* Output */
-       if( NGIRCd_NoDaemon )
-       {
-               /* Output to console */
-               fprintf( stdout, "[%d:%d] %s\n", (INT)getpid( ), Level, msg );
-               fflush( stdout );
-       }
-#ifdef SYSLOG
-       else syslog( Level, msg );
-#endif
-} /* Log_Resolver */
+       Log_Message(Level, msg);
+}
 
 
-LOCAL VOID
-Wall_ServerNotice( CHAR *Msg )
+/**
+ * Send a log message to all local users flagged with the given user mode.
+ * @param UserMode User mode which the target user must have set,
+ * @param Format The format string.
+ */
+#ifdef PROTOTYPES
+GLOBAL void
+Log_ServerNotice(const char UserMode, const char *Format, ... )
+#else
+GLOBAL void
+Log_ServerNotice(UserMode, Format, va_alist)
+const char UserMode;
+const char *Format;
+va_dcl
+#endif
 {
-       /* Server-Notice an entsprechende User verschicken */
-
        CLIENT *c;
+       char msg[MAX_LOG_MSG_LEN];
+       va_list ap;
 
-       assert( Msg != NULL );
+       assert(Format != NULL);
 
-       c = Client_First( );
-       while( c )
-       {
-               if(( Client_Conn( c ) > NONE ) && ( Client_HasMode( c, 's' ))) IRC_WriteStrClient( c, "NOTICE %s :%s%s", Client_ThisServer( ), NOTICE_TXTPREFIX, Msg );
-               c = Client_Next( c );
+#ifdef PROTOTYPES
+       va_start(ap, Format);
+#else
+       va_start(ap);
+#endif
+       vsnprintf(msg, MAX_LOG_MSG_LEN, Format, ap);
+       va_end(ap);
+
+       for(c=Client_First(); c != NULL; c=Client_Next(c)) {
+               if (Client_Conn(c) > NONE && Client_HasMode(c, UserMode))
+                       IRC_WriteStrClient(c, "NOTICE %s :%s%s", Client_ID(c),
+                                                       NOTICE_TXTPREFIX, msg);
        }
-} /* Wall_ServerNotice */
+} /* Log_ServerNotice */
 
 
 /* -eof- */