2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2010 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 char Init_Txt[127];
48 static bool Is_Daemon;
52 Log_Message(int Level, const char *msg)
56 fprintf(stdout, "[%ld:%d %4ld] %s\n", (long)getpid(), Level,
57 (long)time(NULL) - NGIRCd_Start, msg);
62 syslog(Level, "%s", msg);
69 Log_Init( bool Daemon_Mode )
71 Is_Daemon = Daemon_Mode;
74 #ifndef LOG_CONS /* Kludge: mips-dec-ultrix4.5 has no LOG_CONS */
77 openlog(PACKAGE_NAME, LOG_CONS|LOG_PID, Conf_SyslogFacility);
80 Log( LOG_NOTICE, "%s started.", NGIRCd_Version );
82 /* Information about "Operation Mode" */
87 strlcpy( Init_Txt, "debug-mode", sizeof Init_Txt );
92 if( Init_Txt[0] ) strlcat( Init_Txt, ", ", sizeof Init_Txt );
93 strlcat( Init_Txt, "no-daemon-mode", sizeof Init_Txt );
97 if( Init_Txt[0] ) strlcat( Init_Txt, ", ", sizeof Init_Txt );
98 strlcat( Init_Txt, "passive-mode", sizeof Init_Txt );
103 if( Init_Txt[0] ) strlcat( Init_Txt, ", ", sizeof Init_Txt );
104 strlcat( Init_Txt, "network sniffer", sizeof Init_Txt );
107 if( Init_Txt[0] ) Log( LOG_INFO, "Activating: %s.", Init_Txt );
114 Log(LOG_NOTICE, "%s done%s, served %lu connections.", PACKAGE_NAME,
115 NGIRCd_SignalRestart ? " (restarting)" : "", Conn_CountAccepted());
123 * Log function for debug messages.
124 * This function is only functional when the program is compiled with debug
125 * code enabled; otherwise it is an empty function which the compiler will
126 * hopefully mangle down to "nothing" (see log.h). Therefore you should use
127 * LogDebug(...) in favor to Log(LOG_DEBUG, ...).
128 * @param Format Format string like printf().
129 * @param ... Further arguments.
134 LogDebug( const char *Format, ... )
137 LogDebug( Format, va_alist )
140 # endif /* PROTOTYPES */
142 char msg[MAX_LOG_MSG_LEN];
145 if (!NGIRCd_Debug) return;
147 va_start( ap, Format );
151 vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
153 Log(LOG_DEBUG, "%s", msg);
159 * Logging function of ngIRCd.
160 * This function logs messages to the console and/or syslog, whichever is
161 * suitable for the mode ngIRCd is running in (daemon vs. non-daemon).
162 * If LOG_snotice is set, the log messages goes to all user with the mode +s
163 * set and the local &SERVER channel, too.
164 * Please note: you sould use LogDebug(...) for debug messages!
165 * @param Level syslog level (LOG_xxx)
166 * @param Format Format string like printf().
167 * @param ... Further arguments.
171 Log( int Level, const char *Format, ... )
174 Log( Level, Format, va_alist )
180 char msg[MAX_LOG_MSG_LEN];
184 assert( Format != NULL );
186 if( Level & LOG_snotice )
188 /* Notice an User mit "s" Mode */
190 Level &= ~LOG_snotice;
192 else snotice = false;
195 if(( Level == LOG_DEBUG ) && ( ! NGIRCd_Debug )) return;
197 if( Level == LOG_DEBUG ) return;
201 va_start( ap, Format );
205 vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
208 Log_Message(Level, msg);
211 /* Send NOTICE to all local users with mode +s and to the
212 * local &SERVER channel */
213 Log_ServerNotice('s', "%s", msg);
214 Channel_LogServer(msg);
220 Log_Init_Subprocess(char UNUSED *Name)
223 openlog(PACKAGE_NAME, LOG_CONS|LOG_PID, Conf_SyslogFacility);
226 Log_Subprocess(LOG_DEBUG, "%s sub-process starting, PID %ld.",
227 Name, (long)getpid());
233 Log_Exit_Subprocess(char UNUSED *Name)
236 Log_Subprocess(LOG_DEBUG, "%s sub-process %ld done.",
237 Name, (long)getpid());
247 Log_Subprocess(const int Level, const char *Format, ...)
250 Log_Subprocess(Level, Format, va_alist)
256 char msg[MAX_LOG_MSG_LEN];
259 assert(Format != NULL);
262 if ((Level == LOG_DEBUG) && (!NGIRCd_Debug))
265 if (Level == LOG_DEBUG)
270 va_start(ap, Format);
274 vsnprintf(msg, MAX_LOG_MSG_LEN, Format, ap);
277 Log_Message(Level, msg);
282 * Send a log message to all local users flagged with the given user mode.
283 * @param UserMode User mode which the target user must have set,
284 * @param Format The format string.
288 Log_ServerNotice(const char UserMode, const char *Format, ... )
291 Log_ServerNotice(UserMode, Format, va_alist)
298 char msg[MAX_LOG_MSG_LEN];
301 assert(Format != NULL);
304 va_start(ap, Format);
308 vsnprintf(msg, MAX_LOG_MSG_LEN, Format, ap);
311 for(c=Client_First(); c != NULL; c=Client_Next(c)) {
312 if (Client_Conn(c) > NONE && Client_HasMode(c, UserMode))
313 IRC_WriteStrClient(c, "NOTICE %s :%s%s", Client_ID(c),
314 NOTICE_TXTPREFIX, msg);
316 } /* Log_ServerNotice */