2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2012 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.
28 #include <sys/types.h>
39 #include "irc-write.h"
44 static bool Is_Daemon;
48 Log_Message(int Level, const char *msg)
52 fprintf(stdout, "[%ld:%d %4ld] %s\n", (long)getpid(), Level,
53 (long)(time(NULL) - NGIRCd_Start), msg);
58 syslog(Level, "%s", msg);
65 * Initialitze logging.
66 * This function is called before the configuration file is read in.
68 * @param Daemon_Mode Set to true if ngIRCd is running as daemon.
71 Log_Init(bool Daemon_Mode)
73 Is_Daemon = Daemon_Mode;
76 #ifndef LOG_CONS /* Kludge: mips-dec-ultrix4.5 has no LOG_CONS */
80 openlog(PACKAGE, LOG_CONS|LOG_PID, LOG_DAEMON);
82 openlog(PACKAGE, LOG_CONS|LOG_PID, 0);
89 * Re-init logging after reading the configuration file.
95 #ifndef LOG_CONS /* Kludge: mips-dec-ultrix4.5 has no LOG_CONS */
99 openlog(PACKAGE, LOG_CONS|LOG_PID, Conf_SyslogFacility);
101 Log(LOG_NOTICE, "%s started.", NGIRCd_Version);
102 Log(LOG_INFO, "Using configuration file \"%s\" ...", NGIRCd_ConfFile);
109 Log(LOG_INFO, "%s done%s, served %lu connection%s.", PACKAGE_NAME,
110 NGIRCd_SignalRestart ? " (restarting)" : "", Conn_CountAccepted(),
111 Conn_CountAccepted() == 1 ? "" : "s");
119 * Log function for debug messages.
120 * This function is only functional when the program is compiled with debug
121 * code enabled; otherwise it is an empty function which the compiler will
122 * hopefully mangle down to "nothing" (see log.h). Therefore you should use
123 * LogDebug(...) in favor to Log(LOG_DEBUG, ...).
124 * @param Format Format string like printf().
125 * @param ... Further arguments.
130 LogDebug( const char *Format, ... )
133 LogDebug( Format, va_alist )
136 # endif /* PROTOTYPES */
138 char msg[MAX_LOG_MSG_LEN];
141 if (!NGIRCd_Debug) return;
143 va_start( ap, Format );
147 vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
149 Log(LOG_DEBUG, "%s", msg);
155 * Logging function of ngIRCd.
156 * This function logs messages to the console and/or syslog, whichever is
157 * suitable for the mode ngIRCd is running in (daemon vs. non-daemon).
158 * If LOG_snotice is set, the log messages goes to all user with the mode +s
159 * set and the local &SERVER channel, too.
160 * Please note: you should use LogDebug(...) for debug messages!
161 * @param Level syslog level (LOG_xxx)
162 * @param Format Format string like printf().
163 * @param ... Further arguments.
167 Log( int Level, const char *Format, ... )
170 Log( Level, Format, va_alist )
176 char msg[MAX_LOG_MSG_LEN];
180 assert( Format != NULL );
182 if( Level & LOG_snotice )
184 /* Notice an User mit "s" Mode */
186 Level &= ~LOG_snotice;
188 else snotice = false;
191 if(( Level == LOG_DEBUG ) && ( ! NGIRCd_Debug )) return;
193 if( Level == LOG_DEBUG ) return;
197 va_start( ap, Format );
201 vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
204 Log_Message(Level, msg);
207 /* Send NOTICE to all local users with mode +s and to the
208 * local &SERVER channel */
209 Log_ServerNotice('s', "%s", msg);
210 Channel_LogServer(msg);
216 Log_Init_Subprocess(char UNUSED *Name)
219 openlog(PACKAGE, LOG_CONS|LOG_PID, Conf_SyslogFacility);
222 Log_Subprocess(LOG_DEBUG, "%s sub-process starting, PID %ld.",
223 Name, (long)getpid());
229 Log_Exit_Subprocess(char UNUSED *Name)
232 Log_Subprocess(LOG_DEBUG, "%s sub-process %ld done.",
233 Name, (long)getpid());
243 Log_Subprocess(const int Level, const char *Format, ...)
246 Log_Subprocess(Level, Format, va_alist)
252 char msg[MAX_LOG_MSG_LEN];
255 assert(Format != NULL);
258 if ((Level == LOG_DEBUG) && (!NGIRCd_Debug))
261 if (Level == LOG_DEBUG)
266 va_start(ap, Format);
270 vsnprintf(msg, MAX_LOG_MSG_LEN, Format, ap);
273 Log_Message(Level, msg);
278 * Send a log message to all local users flagged with the given user mode.
279 * @param UserMode User mode which the target user must have set,
280 * @param Format The format string.
284 Log_ServerNotice(const char UserMode, const char *Format, ... )
287 Log_ServerNotice(UserMode, Format, va_alist)
294 char msg[MAX_LOG_MSG_LEN];
297 assert(Format != NULL);
300 va_start(ap, Format);
304 vsnprintf(msg, MAX_LOG_MSG_LEN, Format, ap);
307 for(c=Client_First(); c != NULL; c=Client_Next(c)) {
308 if (Client_Conn(c) > NONE && Client_HasMode(c, UserMode))
309 IRC_WriteStrClient(c, "NOTICE %s :%s%s", Client_ID(c),
310 NOTICE_TXTPREFIX, msg);
312 } /* Log_ServerNotice */