]> The Tcpdump Group git mirrors - libpcap/commitdiff
Defer initialization of a log system to the first attempt to log to it.
authorGuy Harris <[email protected]>
Mon, 7 Jan 2019 22:57:23 +0000 (14:57 -0800)
committerGuy Harris <[email protected]>
Mon, 7 Jan 2019 22:57:23 +0000 (14:57 -0800)
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.

rpcapd/log.c

index 414e95425bc7b9cca4cf00d332660ee467f36964..0de23318f946830261f5295bfacddf5aaf260185 100644 (file)
@@ -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)