commit 6f023376a1dae14a643cfaf833ec003756070243
Author: Oswald Buddenhagen <o...@users.sf.net>
Date:   Tue Dec 21 18:44:39 2021 +0100

    turn debug() functions into macros
    
    this makes calling them with more expensive arguments efficient without
    wrapping them into additional conditionals.

 src/common.h      | 17 ++++++++++++--
 src/drv_maildir.c | 12 ++--------
 src/drv_proxy.c   | 22 ++-----------------
 src/main.c        | 12 ++--------
 src/sync.c        | 22 ++-----------------
 src/util.c        | 56 ++++++++++++++++++++++++++++-------------------
 6 files changed, 57 insertions(+), 84 deletions(-)

diff --git a/src/common.h b/src/common.h
index bb6bd396..7b27eaab 100644
--- a/src/common.h
+++ b/src/common.h
@@ -116,8 +116,21 @@ void stats( void );
 
 /* util.c */
 
-void ATTR_PRINTFLIKE(2, 0) vdebug( int, const char *, va_list va );
-void ATTR_PRINTFLIKE(2, 0) vdebugn( int, const char *, va_list va );
+#ifdef DEBUG_FLAG
+#  define debug(...) \
+       do { \
+               if (DFlags & DEBUG_FLAG) \
+                       print( __VA_ARGS__ ); \
+       } while (0)
+#  define debugn(...) \
+       do { \
+               if (DFlags & DEBUG_FLAG) \
+                       printn( __VA_ARGS__ ); \
+       } while (0)
+#endif
+
+void ATTR_PRINTFLIKE(1, 2) print( const char *, ... );
+void ATTR_PRINTFLIKE(1, 2) printn( const char *, ... );
 void ATTR_PRINTFLIKE(1, 2) info( const char *, ... );
 void ATTR_PRINTFLIKE(1, 2) infon( const char *, ... );
 void ATTR_PRINTFLIKE(1, 2) progress( const char *, ... );
diff --git a/src/drv_maildir.c b/src/drv_maildir.c
index 6a2b93b3..d74a4f7b 100644
--- a/src/drv_maildir.c
+++ b/src/drv_maildir.c
@@ -6,6 +6,8 @@
  * mbsync - mailbox synchronizer
  */
 
+#define DEBUG_FLAG DEBUG_MAILDIR
+
 #include "driver.h"
 
 #include <ctype.h>
@@ -85,16 +87,6 @@ static struct flock lck;
 
 static int MaildirCount;
 
-static void ATTR_PRINTFLIKE(1, 2)
-debug( const char *msg, ... )
-{
-       va_list va;
-
-       va_start( va, msg );
-       vdebug( DEBUG_MAILDIR, msg, va );
-       va_end( va );
-}
-
 /* Keep the mailbox driver flag definitions in sync: */
 /* grep for MAILBOX_DRIVER_FLAG */
 /* The order is according to alphabetical maildir flag sort */
diff --git a/src/drv_proxy.c b/src/drv_proxy.c
index 34b2db39..54902ff9 100644
--- a/src/drv_proxy.c
+++ b/src/drv_proxy.c
@@ -4,6 +4,8 @@
  * mbsync - mailbox synchronizer
  */
 
+#define DEBUG_FLAG DEBUG_DRV
+
 #include "driver.h"
 
 typedef struct gen_cmd gen_cmd_t;
@@ -25,26 +27,6 @@ typedef union proxy_store {
        };
 } proxy_store_t;
 
-static void ATTR_PRINTFLIKE(1, 2)
-debug( const char *msg, ... )
-{
-       va_list va;
-
-       va_start( va, msg );
-       vdebug( DEBUG_DRV, msg, va );
-       va_end( va );
-}
-
-static void ATTR_PRINTFLIKE(1, 2)
-debugn( const char *msg, ... )
-{
-       va_list va;
-
-       va_start( va, msg );
-       vdebugn( DEBUG_DRV, msg, va );
-       va_end( va );
-}
-
 /* Keep the mailbox driver flag definitions in sync: */
 /* grep for MAILBOX_DRIVER_FLAG */
 /* The order is according to alphabetical maildir flag sort */
diff --git a/src/main.c b/src/main.c
index d04752b6..e0936104 100644
--- a/src/main.c
+++ b/src/main.c
@@ -5,6 +5,8 @@
  * mbsync - mailbox synchronizer
  */
 
+#define DEBUG_FLAG DEBUG_MAIN
+
 #include "sync.h"
 
 #include <fcntl.h>
@@ -87,16 +89,6 @@ PACKAGE " " VERSION " - mailbox synchronizer\n"
        exit( code );
 }
 
-static void ATTR_PRINTFLIKE(1, 2)
-debug( const char *msg, ... )
-{
-       va_list va;
-
-       va_start( va, msg );
-       vdebug( DEBUG_MAIN, msg, va );
-       va_end( va );
-}
-
 #ifdef __linux__
 static void ATTR_NORETURN
 crashHandler( int n )
diff --git a/src/sync.c b/src/sync.c
index 968d563a..767f502c 100644
--- a/src/sync.c
+++ b/src/sync.c
@@ -5,6 +5,8 @@
  * mbsync - mailbox synchronizer
  */
 
+#define DEBUG_FLAG DEBUG_SYNC
+
 #include "sync.h"
 
 #include <fcntl.h>
@@ -30,26 +32,6 @@ int trash_total[2], trash_done[2];
 
 const char *str_fn[] = { "far side", "near side" }, *str_hl[] = { "push", 
"pull" };
 
-static void ATTR_PRINTFLIKE(1, 2)
-debug( const char *msg, ... )
-{
-       va_list va;
-
-       va_start( va, msg );
-       vdebug( DEBUG_SYNC, msg, va );
-       va_end( va );
-}
-
-static void ATTR_PRINTFLIKE(1, 2)
-debugn( const char *msg, ... )
-{
-       va_list va;
-
-       va_start( va, msg );
-       vdebugn( DEBUG_SYNC, msg, va );
-       va_end( va );
-}
-
 static void
 Fclose( FILE *f, int safe )
 {
diff --git a/src/util.c b/src/util.c
index b5251282..d5edd956 100644
--- a/src/util.c
+++ b/src/util.c
@@ -33,34 +33,38 @@ flushn( void )
 }
 
 static void ATTR_PRINTFLIKE(1, 0)
-printn( const char *msg, va_list va )
+vprint( const char *msg, va_list va )
 {
-       if (*msg == '\v')
-               msg++;
-       else
-               flushn();
        vprintf( msg, va );
        fflush( stdout );
+       need_nl = 0;
 }
 
 void
-vdebug( int cat, const char *msg, va_list va )
+print( const char *msg, ... )
 {
-       if (DFlags & cat) {
-               vprintf( msg, va );
-               fflush( stdout );
-               need_nl = 0;
-       }
+       va_list va;
+
+       va_start( va, msg );
+       vprint( msg, va );
+       va_end( va );
+}
+
+static void ATTR_PRINTFLIKE(1, 0)
+vprintn( const char *msg, va_list va )
+{
+       vprint( msg, va );
+       need_nl = 1;
 }
 
 void
-vdebugn( int cat, const char *msg, va_list va )
+printn( const char *msg, ... )
 {
-       if (DFlags & cat) {
-               vprintf( msg, va );
-               fflush( stdout );
-               need_nl = 1;
-       }
+       va_list va;
+
+       va_start( va, msg );
+       vprintn( msg, va );
+       va_end( va );
 }
 
 void
@@ -75,6 +79,16 @@ progress( const char *msg, ... )
        need_nl = 1;
 }
 
+static void ATTR_PRINTFLIKE(1, 0)
+nvprint( const char *msg, va_list va )
+{
+       if (*msg == '\v')
+               msg++;
+       else
+               flushn();
+       vprint( msg, va );
+}
+
 void
 info( const char *msg, ... )
 {
@@ -82,9 +96,8 @@ info( const char *msg, ... )
 
        if (DFlags & VERBOSE) {
                va_start( va, msg );
-               printn( msg, va );
+               nvprint( msg, va );
                va_end( va );
-               need_nl = 0;
        }
 }
 
@@ -95,7 +108,7 @@ infon( const char *msg, ... )
 
        if (DFlags & VERBOSE) {
                va_start( va, msg );
-               printn( msg, va );
+               nvprint( msg, va );
                va_end( va );
                need_nl = 1;
        }
@@ -108,9 +121,8 @@ notice( const char *msg, ... )
 
        if (!(DFlags & QUIET)) {
                va_start( va, msg );
-               printn( msg, va );
+               nvprint( msg, va );
                va_end( va );
-               need_nl = 0;
        }
 }
 


_______________________________________________
isync-devel mailing list
isync-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/isync-devel

Reply via email to