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 Log_Init(bool Daemon_Mode)
70 Is_Daemon = Daemon_Mode;
73 #ifndef LOG_CONS /* Kludge: mips-dec-ultrix4.5 has no LOG_CONS */
76 openlog(PACKAGE_NAME, LOG_CONS|LOG_PID, Conf_SyslogFacility);
79 Log(LOG_NOTICE, "%s started.", NGIRCd_Version);
86 Log(LOG_NOTICE, "%s done%s, served %lu connections.", PACKAGE_NAME,
87 NGIRCd_SignalRestart ? " (restarting)" : "", Conn_CountAccepted());
95 * Log function for debug messages.
96 * This function is only functional when the program is compiled with debug
97 * code enabled; otherwise it is an empty function which the compiler will
98 * hopefully mangle down to "nothing" (see log.h). Therefore you should use
99 * LogDebug(...) in favor to Log(LOG_DEBUG, ...).
100 * @param Format Format string like printf().
101 * @param ... Further arguments.
106 LogDebug( const char *Format, ... )
109 LogDebug( Format, va_alist )
112 # endif /* PROTOTYPES */
114 char msg[MAX_LOG_MSG_LEN];
117 if (!NGIRCd_Debug) return;
119 va_start( ap, Format );
123 vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
125 Log(LOG_DEBUG, "%s", msg);
131 * Logging function of ngIRCd.
132 * This function logs messages to the console and/or syslog, whichever is
133 * suitable for the mode ngIRCd is running in (daemon vs. non-daemon).
134 * If LOG_snotice is set, the log messages goes to all user with the mode +s
135 * set and the local &SERVER channel, too.
136 * Please note: you sould use LogDebug(...) for debug messages!
137 * @param Level syslog level (LOG_xxx)
138 * @param Format Format string like printf().
139 * @param ... Further arguments.
143 Log( int Level, const char *Format, ... )
146 Log( Level, Format, va_alist )
152 char msg[MAX_LOG_MSG_LEN];
156 assert( Format != NULL );
158 if( Level & LOG_snotice )
160 /* Notice an User mit "s" Mode */
162 Level &= ~LOG_snotice;
164 else snotice = false;
167 if(( Level == LOG_DEBUG ) && ( ! NGIRCd_Debug )) return;
169 if( Level == LOG_DEBUG ) return;
173 va_start( ap, Format );
177 vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
180 Log_Message(Level, msg);
183 /* Send NOTICE to all local users with mode +s and to the
184 * local &SERVER channel */
185 Log_ServerNotice('s', "%s", msg);
186 Channel_LogServer(msg);
192 Log_Init_Subprocess(char UNUSED *Name)
195 openlog(PACKAGE_NAME, LOG_CONS|LOG_PID, Conf_SyslogFacility);
198 Log_Subprocess(LOG_DEBUG, "%s sub-process starting, PID %ld.",
199 Name, (long)getpid());
205 Log_Exit_Subprocess(char UNUSED *Name)
208 Log_Subprocess(LOG_DEBUG, "%s sub-process %ld done.",
209 Name, (long)getpid());
219 Log_Subprocess(const int Level, const char *Format, ...)
222 Log_Subprocess(Level, Format, va_alist)
228 char msg[MAX_LOG_MSG_LEN];
231 assert(Format != NULL);
234 if ((Level == LOG_DEBUG) && (!NGIRCd_Debug))
237 if (Level == LOG_DEBUG)
242 va_start(ap, Format);
246 vsnprintf(msg, MAX_LOG_MSG_LEN, Format, ap);
249 Log_Message(Level, msg);
254 * Send a log message to all local users flagged with the given user mode.
255 * @param UserMode User mode which the target user must have set,
256 * @param Format The format string.
260 Log_ServerNotice(const char UserMode, const char *Format, ... )
263 Log_ServerNotice(UserMode, Format, va_alist)
270 char msg[MAX_LOG_MSG_LEN];
273 assert(Format != NULL);
276 va_start(ap, Format);
280 vsnprintf(msg, MAX_LOG_MSG_LEN, Format, ap);
283 for(c=Client_First(); c != NULL; c=Client_Next(c)) {
284 if (Client_Conn(c) > NONE && Client_HasMode(c, UserMode))
285 IRC_WriteStrClient(c, "NOTICE %s :%s%s", Client_ID(c),
286 NOTICE_TXTPREFIX, msg);
288 } /* Log_ServerNotice */