summaryrefslogtreecommitdiff
path: root/rpcapd
diff options
context:
space:
mode:
authorGuy Harris <guy@alum.mit.edu>2019-01-07 14:57:23 -0800
committerGuy Harris <guy@alum.mit.edu>2019-01-07 14:57:23 -0800
commita5f191481140b173ababa9f7dc425ef65bdab739 (patch)
tree0a4b57761d31e86e160a2605ed3de89b426c0c31 /rpcapd
parentf3a833ea74fdc05a5445424db306949979b5f30c (diff)
downloadlibpcap-a5f191481140b173ababa9f7dc425ef65bdab739.tar.gz
Defer initialization of a log system to the first attempt to log to it.
That: 1) avoids doing any initialization if we end up logging to the standard error and 2) means that the first time we log to the "system log" we'll attempt to initialize it, even if we've logged to the standard error earlier.
Diffstat (limited to 'rpcapd')
-rw-r--r--rpcapd/log.c62
1 files changed, 24 insertions, 38 deletions
diff --git a/rpcapd/log.c b/rpcapd/log.c
index 414e9542..0de23318 100644
--- a/rpcapd/log.c
+++ b/rpcapd/log.c
@@ -61,17 +61,21 @@ static void rpcapd_vlog_systemlog(log_priority,
PCAP_FORMAT_STRING(const char *), va_list) PCAP_PRINTFLIKE(2, 0);
#ifdef _WIN32
-static HANDLE log_handle = INVALID_HANDLE;
-
#define MESSAGE_SUBKEY \
"SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\rpcapd"
-static void rpcapd_log_init(void)
+static void rpcapd_vlog_systemlog(log_priority priority, const char *message,
+ va_list ap)
{
- if (log_to_systemlog)
- {
- HKEY hey_handle;
+ static int initialized = 0;
+ HKEY hey_handle;
+ static HANDLE log_handle;
+ WORD eventlog_type;
+ DWORD event_id;
+ char msgbuf[1024];
+ char *strings[1];
+ if (!initialized) {
/*
* Register our message stuff in the Registry.
*
@@ -82,27 +86,17 @@ static void rpcapd_log_init(void)
if (RegCreateKey(HKEY_LOCAL_MACHINE, MESSAGE_SUBKEY,
&key_handle) != ERROR_SUCCESS) {
/*
- * Failed - give up and just log to the
+ * Failed - give up and just log this message,
+ * and all subsequent messages, to the
* standard error.
*/
log_to_systemlog = 0;
+ initialized = 1;
+ rpcapd_vlog_stderr(priority, message, ap);
return;
}
log_handle = RegisterEventSource(NULL, "rpcapd");
- }
-}
-
-static void rpcapd_vlog_systemlog(log_priority priority, const char *message,
- va_list ap)
-{
- WORD eventlog_type;
- DWORD event_id;
- char msgbuf[1024];
- char *strings[1];
-
- if (log_handle == INVALID_HANDLE) {
- /* Failed to initialize, or haven't tried */
- return;
+ initialized = 1;
}
switch (priority) {
@@ -145,19 +139,20 @@ static void rpcapd_vlog_systemlog(log_priority priority, const char *message,
strings, NULL);
}
#else
-static void rpcapd_log_init(void)
-{
- if (log_to_systemlog)
- {
- openlog("rpcapd", LOG_PID, LOG_DAEMON);
- }
-}
-
static void rpcapd_vlog_systemlog(log_priority priority, const char *message,
va_list ap)
{
+ static int initialized = 0;
int syslog_priority;
+ if (!initialized) {
+ //
+ // Open the log.
+ //
+ openlog("rpcapd", LOG_PID, LOG_DAEMON);
+ initialized = 1;
+ }
+
switch (priority) {
case LOGPRIO_DEBUG:
@@ -193,17 +188,8 @@ void rpcapd_log_set(int log_to_systemlog_arg, int log_debug_messages_arg)
void rpcapd_log(log_priority priority, const char *message, ...)
{
- static int initialized = 0;
va_list ap;
- if (!initialized) {
- //
- // Initialize the logging system.
- //
- rpcapd_log_init();
- initialized = 1;
- }
-
if (priority != LOGPRIO_DEBUG || log_debug_messages) {
va_start(ap, message);
if (log_to_systemlog)