From 7b55c92f8d1401a93c8fd8e342da271dce742000 Mon Sep 17 00:00:00 2001
From: guowenxue <guowenxue@gmail.com>
Date: Sat, 15 Nov 2025 00:15:04 +0800
Subject: [PATCH] update logger source code
---
project/4.mqttd/booster/logger.c | 89 +++++++++++++++-----------------------------
1 files changed, 30 insertions(+), 59 deletions(-)
diff --git a/project/4.mqttd/booster/logger.c b/project/4.mqttd/booster/logger.c
index ea93dd7..b973d06 100644
--- a/project/4.mqttd/booster/logger.c
+++ b/project/4.mqttd/booster/logger.c
@@ -4,11 +4,11 @@
*
* Filename: logger.c
* Description: This file is common logger API functions
- *
+ *
* Version: 1.0.0(11/08/23)
* Author: Guo Wenxue <guowenxue@gmail.com>
* ChangeLog: 1, Release initial version on "11/08/23 16:18:43"
- *
+ *
********************************************************************************/
#include <stdio.h>
@@ -79,7 +79,7 @@
}
else
{
- if ( (err = pthread_mutex_unlock(l)) != 0 )
+ if ( (err = pthread_mutex_unlock(l) != 0) )
log_error("Unable to unlock log lock: %s", strerror(err));
}
}
@@ -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