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"
45 static char Init_Txt[127];
46 static bool Is_Daemon;
50 Log_Message(int Level, const char *msg)
54 fprintf(stdout, "[%ld:%d %4ld] %s\n", (long)getpid(), Level,
55 (long)time(NULL) - NGIRCd_Start, msg);
60 syslog(Level, "%s", msg);
67 Log_Init( bool Daemon_Mode )
69 Is_Daemon = Daemon_Mode;
72 #ifndef LOG_CONS /* Kludge: mips-dec-ultrix4.5 has no LOG_CONS */
75 openlog(PACKAGE_NAME, LOG_CONS|LOG_PID, Conf_SyslogFacility);
78 Log( LOG_NOTICE, "%s started.", NGIRCd_Version );
80 /* Information about "Operation Mode" */
85 strlcpy( Init_Txt, "debug-mode", sizeof Init_Txt );
90 if( Init_Txt[0] ) strlcat( Init_Txt, ", ", sizeof Init_Txt );
91 strlcat( Init_Txt, "no-daemon-mode", sizeof Init_Txt );
95 if( Init_Txt[0] ) strlcat( Init_Txt, ", ", sizeof Init_Txt );
96 strlcat( Init_Txt, "passive-mode", sizeof Init_Txt );
101 if( Init_Txt[0] ) strlcat( Init_Txt, ", ", sizeof Init_Txt );
102 strlcat( Init_Txt, "network sniffer", sizeof Init_Txt );
105 if( Init_Txt[0] ) Log( LOG_INFO, "Activating: %s.", Init_Txt );
112 Log(LOG_NOTICE, "%s done%s, served %lu connections.", PACKAGE_NAME,
113 NGIRCd_SignalRestart ? " (restarting)" : "", Conn_CountAccepted());
121 * Log function for debug messages.
122 * This function is only functional when the program is compiled with debug
123 * code enabled; otherwise it is an empty function which the compiler will
124 * hopefully mangle down to "nothing" (see log.h). Therefore you should use
125 * LogDebug(...) in favor to Log(LOG_DEBUG, ...).
126 * @param Format Format string like printf().
127 * @param ... Further arguments.
132 LogDebug( const char *Format, ... )
135 LogDebug( Format, va_alist )
138 # endif /* PROTOTYPES */
140 char msg[MAX_LOG_MSG_LEN];
143 if (!NGIRCd_Debug) return;
145 va_start( ap, Format );
149 vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
151 Log(LOG_DEBUG, "%s", msg);
157 * Logging function of ngIRCd.
158 * This function logs messages to the console and/or syslog, whichever is
159 * suitable for the mode ngIRCd is running in (daemon vs. non-daemon).
160 * If LOG_snotice is set, the log messages goes to all user with the mode +s
161 * set and the local &SERVER channel, too.
162 * Please note: you sould use LogDebug(...) for debug messages!
163 * @param Level syslog level (LOG_xxx)
164 * @param Format Format string like printf().
165 * @param ... Further arguments.
169 Log( int Level, const char *Format, ... )
172 Log( Level, Format, va_alist )
178 char msg[MAX_LOG_MSG_LEN];
182 assert( Format != NULL );
184 if( Level & LOG_snotice )
186 /* Notice an User mit "s" Mode */
188 Level &= ~LOG_snotice;
190 else snotice = false;
193 if(( Level == LOG_DEBUG ) && ( ! NGIRCd_Debug )) return;
195 if( Level == LOG_DEBUG ) return;
199 va_start( ap, Format );
203 vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
206 Log_Message(Level, msg);
208 if (Level <= LOG_CRIT) {
209 /* log critical messages to stderr */
210 fprintf(stderr, "%s\n", msg);
215 /* Send NOTICE to all local users with mode +s and to the
216 * local &SERVER channel */
217 Log_ServerNotice('s', "%s", msg);
218 Channel_LogServer(msg);
224 Log_Init_Subprocess(char UNUSED *Name)
227 openlog(PACKAGE_NAME, LOG_CONS|LOG_PID, Conf_SyslogFacility);
230 Log_Subprocess(LOG_DEBUG, "%s sub-process starting, PID %ld.",
231 Name, (long)getpid());
237 Log_Exit_Subprocess(char UNUSED *Name)
240 Log_Subprocess(LOG_DEBUG, "%s sub-process %ld done.",
241 Name, (long)getpid());
251 Log_Subprocess(const int Level, const char *Format, ...)
254 Log_Subprocess(Level, Format, va_alist)
260 char msg[MAX_LOG_MSG_LEN];
263 assert(Format != NULL);
266 if ((Level == LOG_DEBUG) && (!NGIRCd_Debug))
269 if (Level == LOG_DEBUG)
274 va_start(ap, Format);
278 vsnprintf(msg, MAX_LOG_MSG_LEN, Format, ap);
281 Log_Message(Level, msg);
286 * Send a log message to all local users flagged with the given user mode.
287 * @param UserMode User mode which the target user must have set,
288 * @param Format The format string.
292 Log_ServerNotice(const char UserMode, const char *Format, ... )
295 Log_ServerNotice(UserMode, Format, va_alist)
302 char msg[MAX_LOG_MSG_LEN];
305 assert(Format != NULL);
308 va_start(ap, Format);
312 vsnprintf(msg, MAX_LOG_MSG_LEN, Format, ap);
315 for(c=Client_First(); c != NULL; c=Client_Next(c)) {
316 if (Client_Conn(c) > NONE && Client_HasMode(c, UserMode))
317 IRC_WriteStrClient(c, "NOTICE %s :%s%s", Client_ID(c),
318 NOTICE_TXTPREFIX, msg);
320 } /* Log_ServerNotice */