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.
30 #include <sys/types.h>
41 #include "irc-write.h"
48 static char Init_Txt[127];
49 static bool Is_Daemon;
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 */
78 openlog(PACKAGE_NAME, LOG_CONS|LOG_PID, Conf_SyslogFacility);
81 Log( LOG_NOTICE, "%s started.", NGIRCd_Version );
83 /* Information about "Operation Mode" */
88 strlcpy( Init_Txt, "debug-mode", sizeof Init_Txt );
93 if( Init_Txt[0] ) strlcat( Init_Txt, ", ", sizeof Init_Txt );
94 strlcat( Init_Txt, "no-daemon-mode", sizeof Init_Txt );
98 if( Init_Txt[0] ) strlcat( Init_Txt, ", ", sizeof Init_Txt );
99 strlcat( Init_Txt, "passive-mode", sizeof Init_Txt );
104 if( Init_Txt[0] ) strlcat( Init_Txt, ", ", sizeof Init_Txt );
105 strlcat( Init_Txt, "network sniffer", sizeof Init_Txt );
108 if( Init_Txt[0] ) Log( LOG_INFO, "Activating: %s.", Init_Txt );
115 Log(LOG_NOTICE, "%s done%s, served %lu connections.", PACKAGE_NAME,
116 NGIRCd_SignalRestart ? " (restarting)" : "", Conn_CountAccepted());
124 * Log function for debug messages.
125 * This function is only functional when the program is compiled with debug
126 * code enabled; otherwise it is an empty function which the compiler will
127 * hopefully mangle down to "nothing" (see log.h). Therefore you should use
128 * LogDebug(...) in favor to Log(LOG_DEBUG, ...).
129 * @param Format Format string like printf().
130 * @param ... Further arguments.
135 LogDebug( const char *Format, ... )
138 LogDebug( Format, va_alist )
141 # endif /* PROTOTYPES */
143 char msg[MAX_LOG_MSG_LEN];
146 if (!NGIRCd_Debug) return;
148 va_start( ap, Format );
152 vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
154 Log(LOG_DEBUG, "%s", msg);
160 * Logging function of ngIRCd.
161 * This function logs messages to the console and/or syslog, whichever is
162 * suitable for the mode ngIRCd is running in (daemon vs. non-daemon).
163 * If LOG_snotice is set, the log messages goes to all user with the mode +s
164 * set and the local &SERVER channel, too.
165 * Please note: you sould use LogDebug(...) for debug messages!
166 * @param Level syslog level (LOG_xxx)
167 * @param Format Format string like printf().
168 * @param ... Further arguments.
172 Log( int Level, const char *Format, ... )
175 Log( Level, Format, va_alist )
181 char msg[MAX_LOG_MSG_LEN];
185 assert( Format != NULL );
187 if( Level & LOG_snotice )
189 /* Notice an User mit "s" Mode */
191 Level &= ~LOG_snotice;
193 else snotice = false;
196 if(( Level == LOG_DEBUG ) && ( ! NGIRCd_Debug )) return;
198 if( Level == LOG_DEBUG ) return;
202 va_start( ap, Format );
206 vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
209 Log_Message(Level, msg);
212 /* Send NOTICE to all local users with mode +s and to the
213 * local &SERVER channel */
214 Log_ServerNotice('s', "%s", msg);
215 Channel_LogServer(msg);
221 Log_Init_Subprocess(char UNUSED *Name)
224 openlog(PACKAGE_NAME, LOG_CONS|LOG_PID, Conf_SyslogFacility);
227 Log_Subprocess(LOG_DEBUG, "%s sub-process starting, PID %ld.",
228 Name, (long)getpid());
234 Log_Exit_Subprocess(char UNUSED *Name)
237 Log_Subprocess(LOG_DEBUG, "%s sub-process %ld done.",
238 Name, (long)getpid());
248 Log_Subprocess(const int Level, const char *Format, ...)
251 Log_Subprocess(Level, Format, va_alist)
257 char msg[MAX_LOG_MSG_LEN];
260 assert(Format != NULL);
263 if ((Level == LOG_DEBUG) && (!NGIRCd_Debug))
266 if (Level == LOG_DEBUG)
271 va_start(ap, Format);
275 vsnprintf(msg, MAX_LOG_MSG_LEN, Format, ap);
278 Log_Message(Level, msg);
283 * Send a log message to all local users flagged with the given user mode.
284 * @param UserMode User mode which the target user must have set,
285 * @param Format The format string.
289 Log_ServerNotice(const char UserMode, const char *Format, ... )
292 Log_ServerNotice(UserMode, Format, va_alist)
299 char msg[MAX_LOG_MSG_LEN];
302 assert(Format != NULL);
305 va_start(ap, Format);
309 vsnprintf(msg, MAX_LOG_MSG_LEN, Format, ap);
312 for(c=Client_First(); c != NULL; c=Client_Next(c)) {
313 if (Client_Conn(c) > NONE && Client_HasMode(c, UserMode))
314 IRC_WriteStrClient(c, "NOTICE %s :%s%s", Client_ID(c),
315 NOTICE_TXTPREFIX, msg);
317 } /* Log_ServerNotice */