turn debug() functions into macros

this makes calling them with more expensive arguments efficient without
wrapping them into additional conditionals.
This commit is contained in:
Oswald Buddenhagen 2021-12-21 18:44:39 +01:00
parent 1a0255c566
commit 6f023376a1
6 changed files with 57 additions and 84 deletions

View File

@ -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 *, ... );

View File

@ -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 */

View File

@ -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 */

View File

@ -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 )

View File

@ -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 )
{

View File

@ -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 );
}
void
vdebug( int cat, const char *msg, va_list va )
{
if (DFlags & cat) {
vprintf( msg, va );
fflush( stdout );
need_nl = 0;
}
}
void
vdebugn( int cat, const char *msg, va_list va )
print( const char *msg, ... )
{
if (DFlags & cat) {
vprintf( msg, va );
fflush( stdout );
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
printn( const char *msg, ... )
{
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;
}
}