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 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);
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.
127 LogDebug( const char *Format, ... )
130 LogDebug( Format, va_alist )
133 # endif /* PROTOTYPES */
135 char msg[MAX_LOG_MSG_LEN];
138 if (!NGIRCd_Debug) return;
140 va_start( ap, Format );
144 vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
146 Log(LOG_DEBUG, "%s", msg);
152 * Logging function of ngIRCd.
153 * This function logs messages to the console and/or syslog, whichever is
154 * suitable for the mode ngIRCd is running in (daemon vs. non-daemon).
155 * If LOG_snotice is set, the log messages goes to all user with the mode +s
156 * set and the local &SERVER channel, too.
157 * Please note: you should use LogDebug(...) for debug messages!
158 * @param Level syslog level (LOG_xxx)
159 * @param Format Format string like printf().
160 * @param ... Further arguments.
164 Log( int Level, const char *Format, ... )
167 Log( Level, Format, va_alist )
173 char msg[MAX_LOG_MSG_LEN];
177 assert( Format != NULL );
179 if( Level & LOG_snotice )
181 /* Notice an User mit "s" Mode */
183 Level &= ~LOG_snotice;
185 else snotice = false;
188 if(( Level == LOG_DEBUG ) && ( ! NGIRCd_Debug )) return;
190 if( Level == LOG_DEBUG ) return;
194 va_start( ap, Format );
198 vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
201 Log_Message(Level, msg);
204 /* Send NOTICE to all local users with mode +s and to the
205 * local &SERVER channel */
206 Log_ServerNotice('s', "%s", msg);
207 Channel_LogServer(msg);
213 Log_Init_Subprocess(char UNUSED *Name)
216 openlog(PACKAGE, LOG_CONS|LOG_PID, Conf_SyslogFacility);
219 Log_Subprocess(LOG_DEBUG, "%s sub-process starting, PID %ld.",
220 Name, (long)getpid());
226 Log_Exit_Subprocess(char UNUSED *Name)
229 Log_Subprocess(LOG_DEBUG, "%s sub-process %ld done.",
230 Name, (long)getpid());
240 Log_Subprocess(const int Level, const char *Format, ...)
243 Log_Subprocess(Level, Format, va_alist)
249 char msg[MAX_LOG_MSG_LEN];
252 assert(Format != NULL);
255 if ((Level == LOG_DEBUG) && (!NGIRCd_Debug))
258 if (Level == LOG_DEBUG)
263 va_start(ap, Format);
267 vsnprintf(msg, MAX_LOG_MSG_LEN, Format, ap);
270 Log_Message(Level, msg);
275 * Send a log message to all local users flagged with the given user mode.
276 * @param UserMode User mode which the target user must have set,
277 * @param Format The format string.
281 Log_ServerNotice(const char UserMode, const char *Format, ... )
284 Log_ServerNotice(UserMode, Format, va_alist)
291 char msg[MAX_LOG_MSG_LEN];
294 assert(Format != NULL);
297 va_start(ap, Format);
301 vsnprintf(msg, MAX_LOG_MSG_LEN, Format, ap);
304 for(c=Client_First(); c != NULL; c=Client_Next(c)) {
305 if (Client_Conn(c) > NONE && Client_HasMode(c, UserMode))
306 IRC_WriteStrClient(c, "NOTICE %s :%s%s", Client_ID(c),
307 NOTICE_TXTPREFIX, msg);
309 } /* Log_ServerNotice */