2 * Copyright (c) 1993, 1994, 1995, 1996, 1998
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that: (1) source code distributions
7 * retain the above copyright notice and this paragraph in its entirety, (2)
8 * distributions including binary code include the above copyright notice and
9 * this paragraph in its entirety in the documentation or other materials
10 * provided with the distribution, and (3) all advertising materials mentioning
11 * features or use of this software display the following acknowledgement:
12 * ``This product includes software developed by the University of California,
13 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14 * the University nor the names of its contributors may be used to endorse
15 * or promote products derived from this software without specific prior
17 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
36 #include "portability.h"
40 static int log_to_systemlog;
41 static int log_debug_messages;
43 static void rpcapd_vlog_stderr(log_priority,
44 PCAP_FORMAT_STRING(const char *), va_list) PCAP_PRINTFLIKE(2, 0);
46 static void rpcapd_vlog_stderr(log_priority priority, const char *message, va_list ap)
51 * Squelch warnings from compilers that *don't* assume that
52 * priority always has a valid enum value and therefore don't
53 * assume that we'll always go through one of the case arms.
55 * If we have a default case, compilers that *do* assume that
56 * will then complain about the default case code being
59 * Damned if you do, damned if you don't.
82 fprintf(stderr, "rpcapd: %s", tag);
83 vfprintf(stderr, message, ap);
87 static void rpcapd_vlog_systemlog(log_priority,
88 PCAP_FORMAT_STRING(const char *), va_list) PCAP_PRINTFLIKE(2, 0);
91 #define MESSAGE_SUBKEY \
92 "SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\rpcapd"
94 static void rpcapd_vlog_systemlog(log_priority priority, const char *message,
98 static int initialized = 0;
100 static HANDLE log_handle;
108 * Register our message stuff in the Registry.
110 * First, create the registry key for us. If the key
111 * already exists, this succeeds and returns a handle
114 if (RegCreateKey(HKEY_LOCAL_MACHINE, MESSAGE_SUBKEY,
115 &key_handle) != ERROR_SUCCESS) {
117 * Failed - give up and just log this message,
118 * and all subsequent messages, to the
121 log_to_systemlog = 0;
123 rpcapd_vlog_stderr(priority, message, ap);
126 log_handle = RegisterEventSource(NULL, "rpcapd");
134 // XXX - what *should* we do about debug messages?
136 eventlog_type = EVENTLOG_INFORMATION_TYPE;
137 event_id = RPCAPD_INFO_ID;
141 eventlog_type = EVENTLOG_INFORMATION_TYPE;
142 event_id = RPCAPD_INFO_ID;
145 case LOGPRIO_WARNING:
146 eventlog_type = EVENTLOG_WARNING_TYPE;
147 event_id = RPCAPD_WARNING_ID;
151 eventlog_type = EVENTLOG_ERROR_TYPE;
152 event_id = RPCAPD_ERROR_ID;
160 vsprintf(msgbuf, message, ap);
164 * If this fails, how are we going to report it?
166 (void) ReportEvent(log_handle, eventlog_type, 0, event_id, NULL, 1, 0,
169 rpcapd_vlog_stderr(priority, message, ap);
173 static void rpcapd_vlog_systemlog(log_priority priority, const char *message,
176 static int initialized = 0;
183 openlog("rpcapd", LOG_PID, LOG_DAEMON);
190 syslog_priority = LOG_DEBUG;
194 syslog_priority = LOG_INFO;
197 case LOGPRIO_WARNING:
198 syslog_priority = LOG_WARNING;
202 syslog_priority = LOG_ERR;
211 vsyslog(syslog_priority, message, ap);
214 * Thanks, IBM, for not providing vsyslog() in AIX!
216 * They also warn that the syslog functions shouldn't
217 * be used in multithreaded programs, but the only thing
218 * obvious that seems to make the syslog_r functions
219 * better is that they have an additional argument
220 * that points to the information that's static to
221 * the syslog code in non-thread-safe versions. Most
222 * of that data is set by openlog(); since we already
223 * do an openlog before doing logging, and don't
224 * change that data afterwards, I suspect that, in
225 * practice, the regular syslog routines are OK for
226 * us (especially given that we'd end up having one
227 * static struct syslog_data anyway, which means we'd
228 * just be like the non-thread-safe version).
232 pcap_vsnprintf(logbuf, sizeof logbuf, message, ap);
233 syslog(syslog_priority, "%s", logbuf);
238 void rpcapd_log_set(int log_to_systemlog_arg, int log_debug_messages_arg)
240 log_debug_messages = log_debug_messages_arg;
241 log_to_systemlog = log_to_systemlog_arg;
244 void rpcapd_log(log_priority priority, const char *message, ...)
248 if (priority != LOGPRIO_DEBUG || log_debug_messages) {
249 va_start(ap, message);
250 if (log_to_systemlog)
252 rpcapd_vlog_systemlog(priority, message, ap);
256 rpcapd_vlog_stderr(priority, message, ap);