From cfdcbd734b4ede4933c87cbe4c44f8aa855b910d Mon Sep 17 00:00:00 2001
From: guowenxue <guowenxue@gmail.com>
Date: Mon, 27 May 2024 17:29:46 +0800
Subject: [PATCH] add test example code in booster

---
 project/booster/logger.c |   83 +++++++++++++----------------------------
 1 files changed, 27 insertions(+), 56 deletions(-)

diff --git a/project/booster/logger.c b/project/booster/logger.c
index 217eb02..b973d06 100644
--- a/project/booster/logger.c
+++ b/project/booster/logger.c
@@ -201,79 +201,50 @@
         L.lockfn(L.udata, 0);
 }
 
-#define LINELEN 81
-#define CHARS_PER_LINE 16
-static char *print_char =
-"                "
-"                "
-" !\"#$%&'()*+,-./"
-"0123456789:;<=>?"
-"@ABCDEFGHIJKLMNO"
-"PQRSTUVWXYZ[\\]^_"
-"`abcdefghijklmno"
-"pqrstuvwxyz{|}~ "
-"                "
-"                "
-" ???????????????"
-"????????????????"
-"????????????????"
-"????????????????"
-"????????????????"
-"????????????????";
 
 void log_dump(int level, const char *prompt, char *buf, size_t len)
 {
-    int rc;
-    int idx;
-    char prn[LINELEN];
-    char lit[CHARS_PER_LINE + 2];
-    char hc[4];
-    short line_done = 1;
+    int                 i, j, ofset;
+	char                line[256];
+    unsigned char       c;
+	unsigned char      *buffer = (unsigned char *)buf;
 
     if (!L.fp || level>L.level)
         return;
 
     if( prompt )
-        _log_write(level, __FILE__, __LINE__, "%s", prompt);
+        _log_write(level, __FILE__, __LINE__, "%s\r\n", prompt);
 
-    rc = len;
-    idx = 0;
-    lit[CHARS_PER_LINE] = '\0';
-
-    while (rc > 0)
+    for(i=0; i<len; i+=16)
     {
-        if (line_done)
-            snprintf(prn, LINELEN, "%08X: ", idx);
+        ofset = snprintf(line, sizeof(line), "%04x: ", i);
 
-        do
+        /* print hex representation, and print spaces if end of buffer */
+        for(j=0; j<16; j++)
         {
-            unsigned char c = buf[idx];
-            snprintf(hc, 4, "%02X ", c);
-            strncat(prn, hc, LINELEN);
-
-            lit[idx % CHARS_PER_LINE] = print_char[c];
+            if(i+j < len)
+                ofset += snprintf(line+ofset, sizeof(line)-ofset, "%02x ", buffer[i+j]);
+            else
+                ofset += snprintf(line+ofset, sizeof(line)-ofset, "   ");
         }
-        while (--rc > 0 && (++idx % CHARS_PER_LINE != 0));
+		ofset += snprintf(line+ofset, sizeof(line)-ofset, " ");
 
-        line_done = (idx % CHARS_PER_LINE) == 0;
-        if (line_done)
+        /* print ASCII representation */
+        for(j=0; j<16; j++)
         {
-            if (L.fp)
-                fprintf(L.fp, "%s  %s\n", prn, lit);
+            if (i+j < len)
+            {
+                c = buffer[i+j];
+                ofset += snprintf(line+ofset, sizeof(line)-ofset, "%c", (c>=32 && c<=126) ? c : '.');
+            }
+            else
+            {
+                ofset += snprintf(line+ofset, sizeof(line)-ofset, " ");
+            }
         }
-    }
-
-    if (!line_done)
-    {
-        int ldx = idx % CHARS_PER_LINE;
-        lit[ldx++] = print_char[(int)buf[idx]];
-        lit[ldx] = '\0';
-
-        while ((++idx % CHARS_PER_LINE) != 0)
-            strncat(prn, "   ", sizeof(prn)-strlen(prn));
 
         if (L.fp)
-            fprintf(L.fp, "%s  %s\n", prn, lit);
-
+            fprintf(L.fp, "%s\r\n", line);
     }
 }
+

--
Gitblit v1.9.1