From c4d46b886a302defa7c99608c4761eb0fe7de340 Mon Sep 17 00:00:00 2001 From: Russell King Date: Thu, 18 Oct 2012 15:04:26 +0100 Subject: Cleanup debug output function --- vmeta_lib.c | 57 +++++++++++++++++++++++++-------------------------------- 1 file changed, 25 insertions(+), 32 deletions(-) diff --git a/vmeta_lib.c b/vmeta_lib.c index dea64c4..de811ad 100644 --- a/vmeta_lib.c +++ b/vmeta_lib.c @@ -549,45 +549,38 @@ SIGN32 vdec_os_driver_clean(void) } /* display debug message */ -static int dbg_printf(UNSG32 dbglevel, const char* format, ...) { - char dbgBuf[256] = {'\0'}; - va_list var; -#if VMETA_LOG_ON - FILE *fp_log = fopen(VMETA_LOG_FILE,"a+"); - if(fp_log == NULL) { - return -1; - } -#endif +static int dbg_printf(UNSG32 dbglevel, const char* format, ...) +{ + /* Fast exit path */ + if (globalDbgLevel == VDEC_DEBUG_NONE) + return 0; + + if (globalDbgLevel & (dbglevel | VDEC_DEBUG_ALL)) { + FILE *f; + char buf[256]; + va_list ap; + int ret; + + va_start(ap, format); + ret = vsnprintf(buf, sizeof(buf), format, ap); + va_end(ap); - if(VDEC_DEBUG_NONE == globalDbgLevel) - goto DBG_EXIT; - else { - va_start(var, format); - vsprintf(dbgBuf, format, var); - va_end(var); - - if(VDEC_DEBUG_ALL & globalDbgLevel) - goto DBG_PRINT; - else if((VDEC_DEBUG_MEM & globalDbgLevel) && (dbglevel == VDEC_DEBUG_MEM)) - goto DBG_PRINT; - else if((VDEC_DEBUG_LOCK & globalDbgLevel) && (dbglevel == VDEC_DEBUG_LOCK)) - goto DBG_PRINT; - else if((VDEC_DEBUG_VER & globalDbgLevel) && (dbglevel == VDEC_DEBUG_VER)) - goto DBG_PRINT; - else - goto DBG_EXIT; - } -DBG_PRINT: #if VMETA_LOG_ON - fprintf(fp_log,dbgBuf); + f = fopen(VMETA_LOG_FILE, "a+"); + if (f == NULL) + return -1; #else - printf("%s", dbgBuf); + f = stderr; #endif + fputs(buf, f); -DBG_EXIT: + if (ret >= sizeof(buf)) + fputs("... (output truncated)\n", f); #if VMETA_LOG_ON - fclose(fp_log); + fclose(f); #endif + } + return 0; } -- cgit