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.
29 /* Prevent inclusion of winsock.h, which causes redefinition errors when
30 * winsock2.h is included later */
31 #define WIN32_LEAN_AND_MEAN
37 #include "portability.h"
41 static int log_to_systemlog
;
42 static int log_debug_messages
;
44 static void rpcapd_vlog_stderr(log_priority
,
45 PCAP_FORMAT_STRING(const char *), va_list) PCAP_PRINTFLIKE(2, 0);
47 static void rpcapd_vlog_stderr(log_priority priority
, const char *message
, va_list ap
)
52 * Squelch warnings from compilers that *don't* assume that
53 * priority always has a valid enum value and therefore don't
54 * assume that we'll always go through one of the case arms.
56 * If we have a default case, compilers that *do* assume that
57 * will then complain about the default case code being
60 * Damned if you do, damned if you don't.
83 fprintf(stderr
, "rpcapd: %s", tag
);
84 vfprintf(stderr
, message
, ap
);
88 static void rpcapd_vlog_systemlog(log_priority
,
89 PCAP_FORMAT_STRING(const char *), va_list) PCAP_PRINTFLIKE(2, 0);
92 #define MESSAGE_SUBKEY \
93 "SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\rpcapd"
95 static void rpcapd_vlog_systemlog(log_priority priority
, const char *message
,
99 static int initialized
= 0;
101 static HANDLE log_handle
;
109 * Register our message stuff in the Registry.
111 * First, create the registry key for us. If the key
112 * already exists, this succeeds and returns a handle
115 if (RegCreateKey(HKEY_LOCAL_MACHINE
, MESSAGE_SUBKEY
,
116 &key_handle
) != ERROR_SUCCESS
) {
118 * Failed - give up and just log this message,
119 * and all subsequent messages, to the
122 log_to_systemlog
= 0;
124 rpcapd_vlog_stderr(priority
, message
, ap
);
127 log_handle
= RegisterEventSource(NULL
, "rpcapd");
135 // XXX - what *should* we do about debug messages?
137 eventlog_type
= EVENTLOG_INFORMATION_TYPE
;
138 event_id
= RPCAPD_INFO_ID
;
142 eventlog_type
= EVENTLOG_INFORMATION_TYPE
;
143 event_id
= RPCAPD_INFO_ID
;
146 case LOGPRIO_WARNING
:
147 eventlog_type
= EVENTLOG_WARNING_TYPE
;
148 event_id
= RPCAPD_WARNING_ID
;
152 eventlog_type
= EVENTLOG_ERROR_TYPE
;
153 event_id
= RPCAPD_ERROR_ID
;
161 vsprintf(msgbuf
, message
, ap
);
165 * If this fails, how are we going to report it?
167 (void) ReportEvent(log_handle
, eventlog_type
, 0, event_id
, NULL
, 1, 0,
170 rpcapd_vlog_stderr(priority
, message
, ap
);
174 static void rpcapd_vlog_systemlog(log_priority priority
, const char *message
,
177 static int initialized
= 0;
184 openlog("rpcapd", LOG_PID
, LOG_DAEMON
);
191 syslog_priority
= LOG_DEBUG
;
195 syslog_priority
= LOG_INFO
;
198 case LOGPRIO_WARNING
:
199 syslog_priority
= LOG_WARNING
;
203 syslog_priority
= LOG_ERR
;
212 vsyslog(syslog_priority
, message
, ap
);
215 * Thanks, IBM, for not providing vsyslog() in AIX!
217 * They also warn that the syslog functions shouldn't
218 * be used in multithreaded programs, but the only thing
219 * obvious that seems to make the syslog_r functions
220 * better is that they have an additional argument
221 * that points to the information that's static to
222 * the syslog code in non-thread-safe versions. Most
223 * of that data is set by openlog(); since we already
224 * do an openlog before doing logging, and don't
225 * change that data afterwards, I suspect that, in
226 * practice, the regular syslog routines are OK for
227 * us (especially given that we'd end up having one
228 * static struct syslog_data anyway, which means we'd
229 * just be like the non-thread-safe version).
233 vsnprintf(logbuf
, sizeof logbuf
, message
, ap
);
234 syslog(syslog_priority
, "%s", logbuf
);
239 void rpcapd_log_set(int log_to_systemlog_arg
, int log_debug_messages_arg
)
241 log_debug_messages
= log_debug_messages_arg
;
242 log_to_systemlog
= log_to_systemlog_arg
;
245 void rpcapd_log(log_priority priority
, const char *message
, ...)
249 if (priority
!= LOGPRIO_DEBUG
|| log_debug_messages
) {
250 va_start(ap
, message
);
251 if (log_to_systemlog
)
253 rpcapd_vlog_systemlog(priority
, message
, ap
);
257 rpcapd_vlog_stderr(priority
, message
, ap
);