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.
29 #include <sys/types.h>
40 #include "irc-write.h"
47 static bool Is_Daemon;
51 Log_Message(int Level, const char *msg)
55 fprintf(stdout, "[%ld:%d %4ld] %s\n", (long)getpid(), Level,
56 (long)time(NULL) - NGIRCd_Start, msg);
61 syslog(Level, "%s", msg);
68 * Initialitze logging.
69 * This function is called before the configuration file is read in.
71 * @param Daemon_Mode Set to true if ngIRCd is running as daemon.
74 Log_Init(bool Daemon_Mode)
76 Is_Daemon = Daemon_Mode;
79 #ifndef LOG_CONS /* Kludge: mips-dec-ultrix4.5 has no LOG_CONS */
83 openlog(PACKAGE, LOG_CONS|LOG_PID, LOG_DAEMON);
85 openlog(PACKAGE, LOG_CONS|LOG_PID, 0);
92 * Re-init logging after reading the configuration file.
98 #ifndef LOG_CONS /* Kludge: mips-dec-ultrix4.5 has no LOG_CONS */
102 openlog(PACKAGE, LOG_CONS|LOG_PID, Conf_SyslogFacility);
104 Log(LOG_NOTICE, "%s started.", NGIRCd_Version);
105 Log(LOG_INFO, "Using configuration file \"%s\" ...", NGIRCd_ConfFile);
112 Log(LOG_INFO, "%s done%s, served %lu connection%s.", PACKAGE_NAME,
113 NGIRCd_SignalRestart ? " (restarting)" : "", Conn_CountAccepted(),
114 Conn_CountAccepted() == 1 ? "" : "s");
122 * Log function for debug messages.
123 * This function is only functional when the program is compiled with debug
124 * code enabled; otherwise it is an empty function which the compiler will
125 * hopefully mangle down to "nothing" (see log.h). Therefore you should use
126 * LogDebug(...) in favor to Log(LOG_DEBUG, ...).
127 * @param Format Format string like printf().
128 * @param ... Further arguments.
133 LogDebug( const char *Format, ... )
136 LogDebug( Format, va_alist )
139 # endif /* PROTOTYPES */
141 char msg[MAX_LOG_MSG_LEN];
144 if (!NGIRCd_Debug) return;
146 va_start( ap, Format );
150 vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
152 Log(LOG_DEBUG, "%s", msg);
158 * Logging function of ngIRCd.
159 * This function logs messages to the console and/or syslog, whichever is
160 * suitable for the mode ngIRCd is running in (daemon vs. non-daemon).
161 * If LOG_snotice is set, the log messages goes to all user with the mode +s
162 * set and the local &SERVER channel, too.
163 * Please note: you sould use LogDebug(...) for debug messages!
164 * @param Level syslog level (LOG_xxx)
165 * @param Format Format string like printf().
166 * @param ... Further arguments.
170 Log( int Level, const char *Format, ... )
173 Log( Level, Format, va_alist )
179 char msg[MAX_LOG_MSG_LEN];
183 assert( Format != NULL );
185 if( Level & LOG_snotice )
187 /* Notice an User mit "s" Mode */
189 Level &= ~LOG_snotice;
191 else snotice = false;
194 if(( Level == LOG_DEBUG ) && ( ! NGIRCd_Debug )) return;
196 if( Level == LOG_DEBUG ) return;
200 va_start( ap, Format );
204 vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
207 Log_Message(Level, msg);
210 /* Send NOTICE to all local users with mode +s and to the
211 * local &SERVER channel */
212 Log_ServerNotice('s', "%s", msg);
213 Channel_LogServer(msg);
219 Log_Init_Subprocess(char UNUSED *Name)
222 openlog(PACKAGE, LOG_CONS|LOG_PID, Conf_SyslogFacility);
225 Log_Subprocess(LOG_DEBUG, "%s sub-process starting, PID %ld.",
226 Name, (long)getpid());
232 Log_Exit_Subprocess(char UNUSED *Name)
235 Log_Subprocess(LOG_DEBUG, "%s sub-process %ld done.",
236 Name, (long)getpid());
246 Log_Subprocess(const int Level, const char *Format, ...)
249 Log_Subprocess(Level, Format, va_alist)
255 char msg[MAX_LOG_MSG_LEN];
258 assert(Format != NULL);
261 if ((Level == LOG_DEBUG) && (!NGIRCd_Debug))
264 if (Level == LOG_DEBUG)
269 va_start(ap, Format);
273 vsnprintf(msg, MAX_LOG_MSG_LEN, Format, ap);
276 Log_Message(Level, msg);
281 * Send a log message to all local users flagged with the given user mode.
282 * @param UserMode User mode which the target user must have set,
283 * @param Format The format string.
287 Log_ServerNotice(const char UserMode, const char *Format, ... )
290 Log_ServerNotice(UserMode, Format, va_alist)
297 char msg[MAX_LOG_MSG_LEN];
300 assert(Format != NULL);
303 va_start(ap, Format);
307 vsnprintf(msg, MAX_LOG_MSG_LEN, Format, ap);
310 for(c=Client_First(); c != NULL; c=Client_Next(c)) {
311 if (Client_Conn(c) > NONE && Client_HasMode(c, UserMode))
312 IRC_WriteStrClient(c, "NOTICE %s :%s%s", Client_ID(c),
313 NOTICE_TXTPREFIX, msg);
315 } /* Log_ServerNotice */