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 */
82 openlog(PACKAGE_NAME, LOG_CONS|LOG_PID, Conf_SyslogFacility);
85 Log(LOG_NOTICE, "%s started.", NGIRCd_Version);
90 * Re-init logging after reading the configuration file.
96 #ifndef LOG_CONS /* Kludge: mips-dec-ultrix4.5 has no LOG_CONS */
100 openlog(PACKAGE_NAME, LOG_CONS|LOG_PID, Conf_SyslogFacility);
102 Log(LOG_NOTICE, "%s started.", NGIRCd_Version);
109 Log(LOG_NOTICE, "%s done%s, served %lu connections.", PACKAGE_NAME,
110 NGIRCd_SignalRestart ? " (restarting)" : "", Conn_CountAccepted());
118 * Log function for debug messages.
119 * This function is only functional when the program is compiled with debug
120 * code enabled; otherwise it is an empty function which the compiler will
121 * hopefully mangle down to "nothing" (see log.h). Therefore you should use
122 * LogDebug(...) in favor to Log(LOG_DEBUG, ...).
123 * @param Format Format string like printf().
124 * @param ... Further arguments.
129 LogDebug( const char *Format, ... )
132 LogDebug( Format, va_alist )
135 # endif /* PROTOTYPES */
137 char msg[MAX_LOG_MSG_LEN];
140 if (!NGIRCd_Debug) return;
142 va_start( ap, Format );
146 vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
148 Log(LOG_DEBUG, "%s", msg);
154 * Logging function of ngIRCd.
155 * This function logs messages to the console and/or syslog, whichever is
156 * suitable for the mode ngIRCd is running in (daemon vs. non-daemon).
157 * If LOG_snotice is set, the log messages goes to all user with the mode +s
158 * set and the local &SERVER channel, too.
159 * Please note: you sould use LogDebug(...) for debug messages!
160 * @param Level syslog level (LOG_xxx)
161 * @param Format Format string like printf().
162 * @param ... Further arguments.
166 Log( int Level, const char *Format, ... )
169 Log( Level, Format, va_alist )
175 char msg[MAX_LOG_MSG_LEN];
179 assert( Format != NULL );
181 if( Level & LOG_snotice )
183 /* Notice an User mit "s" Mode */
185 Level &= ~LOG_snotice;
187 else snotice = false;
190 if(( Level == LOG_DEBUG ) && ( ! NGIRCd_Debug )) return;
192 if( Level == LOG_DEBUG ) return;
196 va_start( ap, Format );
200 vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
203 Log_Message(Level, msg);
206 /* Send NOTICE to all local users with mode +s and to the
207 * local &SERVER channel */
208 Log_ServerNotice('s', "%s", msg);
209 Channel_LogServer(msg);
215 Log_Init_Subprocess(char UNUSED *Name)
218 openlog(PACKAGE_NAME, LOG_CONS|LOG_PID, Conf_SyslogFacility);
221 Log_Subprocess(LOG_DEBUG, "%s sub-process starting, PID %ld.",
222 Name, (long)getpid());
228 Log_Exit_Subprocess(char UNUSED *Name)
231 Log_Subprocess(LOG_DEBUG, "%s sub-process %ld done.",
232 Name, (long)getpid());
242 Log_Subprocess(const int Level, const char *Format, ...)
245 Log_Subprocess(Level, Format, va_alist)
251 char msg[MAX_LOG_MSG_LEN];
254 assert(Format != NULL);
257 if ((Level == LOG_DEBUG) && (!NGIRCd_Debug))
260 if (Level == LOG_DEBUG)
265 va_start(ap, Format);
269 vsnprintf(msg, MAX_LOG_MSG_LEN, Format, ap);
272 Log_Message(Level, msg);
277 * Send a log message to all local users flagged with the given user mode.
278 * @param UserMode User mode which the target user must have set,
279 * @param Format The format string.
283 Log_ServerNotice(const char UserMode, const char *Format, ... )
286 Log_ServerNotice(UserMode, Format, va_alist)
293 char msg[MAX_LOG_MSG_LEN];
296 assert(Format != NULL);
299 va_start(ap, Format);
303 vsnprintf(msg, MAX_LOG_MSG_LEN, Format, ap);
306 for(c=Client_First(); c != NULL; c=Client_Next(c)) {
307 if (Client_Conn(c) > NONE && Client_HasMode(c, UserMode))
308 IRC_WriteStrClient(c, "NOTICE %s :%s%s", Client_ID(c),
309 NOTICE_TXTPREFIX, msg);
311 } /* Log_ServerNotice */