Reduce #ifdef ENABLE_DEBUG

Hrvoje Niksic <[email protected]> Wed, 22 Jun 2005 03:25:35 +0200
Newsgroups gmane.comp.web.wget.patches
Message-ID <[email protected]>
Several places in the code that need to print complex debugging output
do things like:

#ifdef ENABLE_DEBUG
if (opt.debug)
  {
    ... printing goes here ...
  }
#endif

These can be compacted into a single IF_DEBUG statement, expanded into
if (opt.debug) or if (0), depending on whether ENABLE_DEBUG is
defined.  Since this reduces the total number of #ifdefs in the code,
it is almost by definition a Good Thing.


2005-06-22  Hrvoje Niksic  <[email protected]>

	* openssl.c, connect.c, host.c: Replace instances of #ifdef
	ENABLE_DEBUG if (opt.debug) {...} #endif with IF_DEBUG {...}.

	* main.c: Rename the IF_DEBUG defined here to WHEN_DEBUG.

	* wget.h (IF_DEBUG): New macro.
	(DEBUGP): Define in terms of IF_DEBUG.

Index: src/cookies.c
===================================================================
--- src/cookies.c	(revision 1737)
+++ src/cookies.c	(working copy)
@@ -258,8 +258,7 @@
   hash_table_put (jar->chains, chain_key, cookie);
   ++jar->cookie_count;
 
-#ifdef ENABLE_DEBUG
-  if (opt.debug)
+  IF_DEBUG
     {
       time_t exptime = cookie->expiry_time;
       DEBUGP (("\nStored cookie %s %d%s %s <%s> <%s> [expiry %s] %s %s\n",
@@ -271,7 +270,6 @@
 	       cookie->expiry_time ? datetime_str (&exptime) : "none",
 	       cookie->attr, cookie->value));
     }
-#endif
 }
 
 /* Discard a cookie matching COOKIE's domain, port, path, and
Index: src/host.c
===================================================================
--- src/host.c	(revision 1737)
+++ src/host.c	(working copy)
@@ -622,8 +622,7 @@
   ++al->refcount;
   hash_table_put (host_name_addresses_map, xstrdup_lower (host), al);
 
-#ifdef ENABLE_DEBUG
-  if (opt.debug)
+  IF_DEBUG
     {
       int i;
       debug_logprintf ("Caching %s =>", host);
@@ -631,7 +630,6 @@
 	debug_logprintf (" %s", pretty_print_address (al->addresses + i));
       debug_logprintf ("\n");
     }
-#endif
 }
 
 /* Remove HOST from the DNS cache.  Does nothing is HOST is not in
Index: src/connect.c
===================================================================
--- src/connect.c	(revision 1737)
+++ src/connect.c	(working copy)
@@ -278,10 +278,9 @@
     int on = 1;
     /* In case of error, we will go on anyway... */
     int err = setsockopt (sock, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof (on));
-#ifdef ENABLE_DEBUG
-    if (err < 0) 
-      DEBUGP (("Failed setting IPV6_V6ONLY: %s", strerror (errno)));
-#endif
+    IF_DEBUG
+      if (err < 0) 
+	DEBUGP (("Failed setting IPV6_V6ONLY: %s", strerror (errno)));
   }
 #endif
 
Index: src/wget.h
===================================================================
--- src/wget.h	(revision 1737)
+++ src/wget.h	(working copy)
@@ -98,14 +98,17 @@
 # define UNLIKELY(exp) (exp)
 #endif
 
-/* Print X if debugging is enabled; a no-op otherwise.  */
+/* Execute the following statement if debugging is both enabled at
+   compile-time and requested at run-time; a no-op otherwise.  */
 
 #ifdef ENABLE_DEBUG
-# define DEBUGP(x) do if (UNLIKELY (opt.debug)) {debug_logprintf x;} while (0)
-#else  /* not ENABLE_DEBUG */
-# define DEBUGP(x) do {} while (0)
-#endif /* not ENABLE_DEBUG */
+# define IF_DEBUG if (UNLIKELY (opt.debug))
+#else
+# define IF_DEBUG if (0)
+#endif
 
+#define DEBUGP(x) do { IF_DEBUG { debug_logprintf x; } } while (0)
+
 /* Define an integer type that works for file sizes, content lengths,
    and such.  Normally we could just use off_t, but off_t is always
    32-bit on Windows.  */
Index: src/openssl.c
===================================================================
--- src/openssl.c	(revision 1737)
+++ src/openssl.c	(working copy)
@@ -422,8 +422,7 @@
       goto no_cert;		/* must bail out since CERT is NULL */
     }
 
-#ifdef ENABLE_DEBUG
-  if (opt.debug)
+  IF_DEBUG
     {
       char *subject = X509_NAME_oneline (X509_get_subject_name (cert), 0, 0);
       char *issuer = X509_NAME_oneline (X509_get_issuer_name (cert), 0, 0);
@@ -432,7 +431,6 @@
       OPENSSL_free (subject);
       OPENSSL_free (issuer);
     }
-#endif
 
   vresult = SSL_get_verify_result (ssl);
   if (vresult != X509_V_OK)
Index: src/main.c
===================================================================
--- src/main.c	(revision 1737)
+++ src/main.c	(working copy)
@@ -112,9 +112,9 @@
 #endif
 
 #ifdef ENABLE_DEBUG
-# define IF_DEBUG(x) x
+# define WHEN_DEBUG(x) x
 #else
-# define IF_DEBUG(x) NULL
+# define WHEN_DEBUG(x) NULL
 #endif
 
 struct cmdline_option {
@@ -158,7 +158,7 @@
     { "convert-links", 'k', OPT_BOOLEAN, "convertlinks", -1 },
     { "cookies", 0, OPT_BOOLEAN, "cookies", -1 },
     { "cut-dirs", 0, OPT_VALUE, "cutdirs", -1 },
-    { IF_DEBUG ("debug"), 'd', OPT_BOOLEAN, "debug", -1 },
+    { WHEN_DEBUG ("debug"), 'd', OPT_BOOLEAN, "debug", -1 },
     { "delete-after", 0, OPT_BOOLEAN, "deleteafter", -1 },
     { "directories", 0, OPT_BOOLEAN, "dirstruct", -1 },
     { "directory-prefix", 'P', OPT_VALUE, "dirprefix", -1 },
@@ -254,7 +254,7 @@
     { "waitretry", 0, OPT_VALUE, "waitretry", -1 },
   };
 
-#undef IF_DEBUG
+#undef WHEN_DEBUG
 #undef IF_SSL
 
 /* Return a string that contains S with "no-" prepended.  The string