changeset 529:c44ebf5b08fd

Improve printf debugging in tests.
author Matti Hamalainen <ccr@tnsp.org>
date Sat, 28 Dec 2019 09:55:37 +0200
parents 337118002dd6
children 94d4130cc29c
files tests.c th_string.c th_string.h
diffstat 3 files changed, 37 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/tests.c	Sat Dec 28 09:54:57 2019 +0200
+++ b/tests.c	Sat Dec 28 09:55:37 2019 +0200
@@ -25,7 +25,7 @@
 typedef struct
 {
     char *header;
-    BOOL shown;
+    BOOL shown, failed;
 } test_ctx;
 
 
@@ -183,6 +183,8 @@
             tprint(0, "\n");
         }
         tests_failed++;
+
+        ctx->failed = TRUE;
     }
 }
 
@@ -210,6 +212,10 @@
     va_list tmp;
     test_ctx ctx;
 
+    // Setup printf debug value
+    th_printf_debug = th_verbosity >= 3;
+    th_printf_debug_prefix = "  - ";
+
     // Test basic *printf() functionality
     test_start(&ctx, "th_vsnprintf(%" PRIu_SIZE_T ", \"%s\", %s)", len, fmt, msg);
 
@@ -228,6 +234,13 @@
     test_result_msg(&ctx, (unsigned char) buf2[len] == SET_SENTINEL_BYTE, "buffer #2 overflow, sentinel 0x%02x", buf2[len]);
     }
 
+    if (ctx.failed && !th_printf_debug && th_verbosity >= 1)
+    {
+        th_printf_debug = TRUE;
+        va_copy(tmp, ap); ret1 = th_vsnprintf(buf1, len, fmt, tmp);
+        va_copy(tmp, ap); ret2 = vsnprintf(buf2, len, fmt, tmp);
+    }
+
     test_end(&ctx);
 }
 
@@ -563,8 +576,6 @@
 
     tprint(1, "Enabled test types are 0x%04x.\n", optFlags);
 
-    // Setup printf debug value
-    th_printf_debug = th_verbosity >= 3;
 
     //
     // Test series for printf()
--- a/th_string.c	Sat Dec 28 09:54:57 2019 +0200
+++ b/th_string.c	Sat Dec 28 09:55:37 2019 +0200
@@ -192,6 +192,8 @@
 
 #ifdef TH_PRINTF_DEBUG
 BOOL th_printf_debug = FALSE;
+char *th_printf_debug_prefix = NULL;
+
 
 static void pflag(char *buf, const char *str, const int sep, const int flags, const int flg)
 {
@@ -217,8 +219,24 @@
     return buf;
 }
 
-#define PP_PRINTF(...) do { if (th_printf_debug) fprintf(stdout, __VA_ARGS__); } while (0)
+#define PP_LINE(...) \
+    do { \
+        if (th_printf_debug) { \
+            if (th_printf_debug_prefix != NULL) \
+                fputs(th_printf_debug_prefix, stdout); \
+            fprintf(stdout, __VA_ARGS__); \
+        } \
+    } while (0)
+
+#define PP_PRINTF(...) \
+    do { \
+        if (th_printf_debug) { \
+            fprintf(stdout, __VA_ARGS__); \
+        } \
+    } while (0)
+
 #else
+#define PP_LINE(...) /* stub */
 #define PP_PRINTF(...) /* stub */
 #endif
 
@@ -288,7 +306,7 @@
 
     if (f_flags & TH_PF_POINTER && vret == 0)
     {
-        PP_PRINTF("^");
+        PP_LINE("^");
         qlen = f_len + nlen;
         nwidth = f_width > qlen ? f_width - qlen : 0;
         nprec = 0;
@@ -296,13 +314,13 @@
     else
     if ((f_flags & TH_PF_ZERO) && f_prec < 0 && f_width > 0)
     {
-        PP_PRINTF("#");
+        PP_LINE("#");
         nprec = f_width - qlen;
         nwidth = 0;
     }
     else
     {
-        PP_PRINTF("$");
+        PP_LINE("$");
         nprec = (f_prec >= 0) ? f_prec - f_len : 0;
         nwidth = (f_width >= 0) ? f_width - qlen : 0;
     }
--- a/th_string.h	Sat Dec 28 09:54:57 2019 +0200
+++ b/th_string.h	Sat Dec 28 09:55:37 2019 +0200
@@ -151,6 +151,7 @@
 
 #ifdef TH_PRINTF_DEBUG
 extern BOOL th_printf_debug;
+extern char *th_printf_debug_prefix;
 #endif