2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2014 Alexander Barton (alex@barton.de) and Contributors.
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.
26 #include <sys/types.h>
37 #include "irc-write.h"
42 static bool Is_Daemon;
46 Log_Message(int Level, const char *msg)
50 fprintf(stdout, "[%ld:%d %4ld] %s\n", (long)getpid(), Level,
51 (long)(time(NULL) - NGIRCd_Start), msg);
56 syslog(Level, "%s", msg);
63 * Initialitze logging.
64 * This function is called before the configuration file is read in.
66 * @param Daemon_Mode Set to true if ngIRCd is running as daemon.
69 Log_Init(bool Daemon_Mode)
71 Is_Daemon = Daemon_Mode;
74 #ifndef LOG_CONS /* Kludge: mips-dec-ultrix4.5 has no LOG_CONS */
78 openlog(PACKAGE, LOG_CONS|LOG_PID, LOG_DAEMON);
80 openlog(PACKAGE, LOG_CONS|LOG_PID, 0);
87 * Re-init logging after reading the configuration file.
93 #ifndef LOG_CONS /* Kludge: mips-dec-ultrix4.5 has no LOG_CONS */
97 openlog(PACKAGE, LOG_CONS|LOG_PID, Conf_SyslogFacility);
99 Log(LOG_NOTICE, "%s started.", NGIRCd_Version);
100 Log(LOG_INFO, "Using configuration file \"%s\" ...", NGIRCd_ConfFile);
107 Log(LOG_INFO, "%s done%s, served %lu connection%s.", PACKAGE_NAME,
108 NGIRCd_SignalRestart ? " (restarting)" : "", Conn_CountAccepted(),
109 Conn_CountAccepted() == 1 ? "" : "s");
117 * Log function for debug messages.
118 * This function is only functional when the program is compiled with debug
119 * code enabled; otherwise it is an empty function which the compiler will
120 * hopefully mangle down to "nothing" (see log.h). Therefore you should use
121 * LogDebug(...) in favor to Log(LOG_DEBUG, ...).
122 * @param Format Format string like printf().
123 * @param ... Further arguments.
128 LogDebug( const char *Format, ... )
131 LogDebug( Format, va_alist )
134 # endif /* PROTOTYPES */
136 char msg[MAX_LOG_MSG_LEN];
139 if (!NGIRCd_Debug) return;
141 va_start( ap, Format );
145 vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
147 Log(LOG_DEBUG, "%s", msg);
153 * Logging function of ngIRCd.
154 * This function logs messages to the console and/or syslog, whichever is
155 * suitable for the mode ngIRCd is running in (daemon vs. non-daemon).
156 * If LOG_snotice is set, the log messages goes to all user with the mode +s
157 * set and the local &SERVER channel, too.
158 * Please note: you should use LogDebug(...) for debug messages!
159 * @param Level syslog level (LOG_xxx)
160 * @param Format Format string like printf().
161 * @param ... Further arguments.
165 Log( int Level, const char *Format, ... )
168 Log( Level, Format, va_alist )
174 char msg[MAX_LOG_MSG_LEN];
178 assert( Format != NULL );
180 if( Level & LOG_snotice )
182 /* Notice an User mit "s" Mode */
184 Level &= ~LOG_snotice;
186 else snotice = false;
189 if(( Level == LOG_DEBUG ) && ( ! NGIRCd_Debug )) return;
191 if( Level == LOG_DEBUG ) return;
195 va_start( ap, Format );
199 vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
202 Log_Message(Level, msg);
205 /* Send NOTICE to all local users with mode +s and to the
206 * local &SERVER channel */
207 Log_ServerNotice('s', "%s", msg);
208 Channel_LogServer(msg);
214 Log_Init_Subprocess(char UNUSED *Name)
217 openlog(PACKAGE, LOG_CONS|LOG_PID, Conf_SyslogFacility);
220 Log_Subprocess(LOG_DEBUG, "%s sub-process starting, PID %ld.",
221 Name, (long)getpid());
227 Log_Exit_Subprocess(char UNUSED *Name)
230 Log_Subprocess(LOG_DEBUG, "%s sub-process %ld done.",
231 Name, (long)getpid());
241 Log_Subprocess(const int Level, const char *Format, ...)
244 Log_Subprocess(Level, Format, va_alist)
250 char msg[MAX_LOG_MSG_LEN];
253 assert(Format != NULL);
256 if ((Level == LOG_DEBUG) && (!NGIRCd_Debug))
259 if (Level == LOG_DEBUG)
264 va_start(ap, Format);
268 vsnprintf(msg, MAX_LOG_MSG_LEN, Format, ap);
271 Log_Message(Level, msg);
276 * Send a log message to all local users flagged with the given user mode.
277 * @param UserMode User mode which the target user must have set,
278 * @param Format The format string.
282 Log_ServerNotice(const char UserMode, const char *Format, ... )
285 Log_ServerNotice(UserMode, Format, va_alist)
292 char msg[MAX_LOG_MSG_LEN];
295 assert(Format != NULL);
298 va_start(ap, Format);
302 vsnprintf(msg, MAX_LOG_MSG_LEN, Format, ap);
305 for(c=Client_First(); c != NULL; c=Client_Next(c)) {
306 if (Client_Conn(c) > NONE && Client_HasMode(c, UserMode))
307 IRC_WriteStrClient(c, "NOTICE %s :%s%s", Client_ID(c),
308 NOTICE_TXTPREFIX, msg);
310 } /* Log_ServerNotice */