2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2019 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 Use_Syslog;
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 Syslog_Mode Set to true if ngIRCd is configured to log to the syslog.
69 Log_Init(bool Syslog_Mode)
71 Use_Syslog = Syslog_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);
83 Log(LOG_NOTICE, "%s starting ...", NGIRCd_Version);
88 * Re-init logging after reading the configuration file.
94 #ifndef LOG_CONS /* Kludge: mips-dec-ultrix4.5 has no LOG_CONS */
98 openlog(PACKAGE, LOG_CONS|LOG_PID, Conf_SyslogFacility);
106 Log(LOG_NOTICE, "%s done%s, served %lu connection%s.", PACKAGE_NAME,
107 NGIRCd_SignalRestart ? " (restarting)" : "", Conn_CountAccepted(),
108 Conn_CountAccepted() == 1 ? "" : "s");
116 * Log function for debug messages.
117 * This function is only functional when the program is compiled with debug
118 * code enabled; otherwise it is an empty function which the compiler will
119 * hopefully mangle down to "nothing" (see log.h). Therefore you should use
120 * LogDebug(...) in favor to Log(LOG_DEBUG, ...).
121 * @param Format Format string like printf().
122 * @param ... Further arguments.
126 LogDebug( const char *Format, ... )
129 LogDebug( Format, va_alist )
132 # endif /* PROTOTYPES */
134 char msg[MAX_LOG_MSG_LEN];
137 if (!NGIRCd_Debug) return;
139 va_start( ap, Format );
143 vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
145 Log(LOG_DEBUG, "%s", msg);
150 * Logging function of ngIRCd.
151 * This function logs messages to the console and/or syslog, whichever is
152 * suitable for the mode ngIRCd is running in (daemon vs. non-daemon).
153 * If LOG_snotice is set, the log messages goes to all user with the mode +s
154 * set and the local &SERVER channel, too.
155 * Please note: you should use LogDebug(...) for debug messages!
156 * @param Level syslog level (LOG_xxx)
157 * @param Format Format string like printf().
158 * @param ... Further arguments.
162 Log( int Level, const char *Format, ... )
165 Log( Level, Format, va_alist )
171 char msg[MAX_LOG_MSG_LEN];
175 assert( Format != NULL );
177 if( Level & LOG_snotice )
179 /* Notice an User mit "s" Mode */
181 Level &= ~LOG_snotice;
183 else snotice = false;
185 if(( Level == LOG_DEBUG ) && ( ! NGIRCd_Debug )) return;
188 va_start( ap, Format );
192 vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
195 Log_Message(Level, msg);
198 /* Send NOTICE to all local users with mode +s and to the
199 * local &SERVER channel */
200 Log_ServerNotice('s', "%s", msg);
201 Channel_LogServer(msg);
207 Log_Init_Subprocess(char UNUSED *Name)
210 openlog(PACKAGE, LOG_CONS|LOG_PID, Conf_SyslogFacility);
212 Log_Subprocess(LOG_DEBUG, "%s sub-process starting, PID %ld.",
213 Name, (long)getpid());
218 Log_Exit_Subprocess(char UNUSED *Name)
220 Log_Subprocess(LOG_DEBUG, "%s sub-process %ld done.",
221 Name, (long)getpid());
230 Log_Subprocess(const int Level, const char *Format, ...)
233 Log_Subprocess(Level, Format, va_alist)
239 char msg[MAX_LOG_MSG_LEN];
242 assert(Format != NULL);
244 if ((Level == LOG_DEBUG) && (!NGIRCd_Debug))
248 va_start(ap, Format);
252 vsnprintf(msg, MAX_LOG_MSG_LEN, Format, ap);
255 Log_Message(Level, msg);
260 * Send a log message to all local users flagged with the given user mode.
261 * @param UserMode User mode which the target user must have set,
262 * @param Format The format string.
266 Log_ServerNotice(const char UserMode, const char *Format, ... )
269 Log_ServerNotice(UserMode, Format, va_alist)
276 char msg[MAX_LOG_MSG_LEN];
279 assert(Format != NULL);
282 va_start(ap, Format);
286 vsnprintf(msg, MAX_LOG_MSG_LEN, Format, ap);
289 for(c=Client_First(); c != NULL; c=Client_Next(c)) {
290 if (Client_Conn(c) > NONE && Client_HasMode(c, UserMode))
291 IRC_WriteStrClient(c, "NOTICE %s :%s%s", Client_ID(c),
292 NOTICE_TXTPREFIX, msg);
294 } /* Log_ServerNotice */