From 3ad30c0c7a22fc16d40f187c69ef729ad3f8112b Mon Sep 17 00:00:00 2001 From: James R Date: Wed, 24 May 2023 18:31:38 -0700 Subject: [PATCH] debugfile: replace mixed string/control character notation with hexdump Example of old notation: [10]O[0,0]jartha[0,254,142,131]c[133]hL^{[170][[251,199]+E[181]U+t[247,183,129,165,152]#$[164,22,10,169]r[150,212,3,232,3,0,0,2,0,0,0,8,0]0[8,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,6,9,2,0,0,1,0,217,161,192] D[0,0,0,0,0,0,0] Example of new notation: 0: 01 0e 6a 61 72 74 68 61 00 57 00 09 5a 00 42 00 ..jartha.W..Z.B. 16: 02 01 00 b7 6a d4 20 6f 2b 45 b5 ....j. o+E. --- src/d_net.c | 51 +++++++++++++++++++++++++++++---------------------- 1 file changed, 29 insertions(+), 22 deletions(-) diff --git a/src/d_net.c b/src/d_net.c index c04ec400b..7a759819e 100644 --- a/src/d_net.c +++ b/src/d_net.c @@ -731,39 +731,46 @@ static UINT32 NetbufferChecksum(void) #ifdef DEBUGFILE -static void fprintfstring(char *s, size_t len) +static void fprintfline(char *s, size_t len) { - INT32 mode = 0; size_t i; - for (i = 0; i < len; i++) - if (s[i] < 32) + for (i = 0; i < 16; ++i) + { + if (i < len) { - if (!mode) - { - fprintf(debugfile, "[%d", (UINT8)s[i]); - mode = 1; - } - else - fprintf(debugfile, ",%d", (UINT8)s[i]); + fprintf(debugfile, "%02x%s", + ((unsigned char*)s)[i], + i % 4 == 3 ? " " : " "); } else { - if (mode) - { - fprintf(debugfile, "]"); - mode = 0; - } - fprintf(debugfile, "%c", s[i]); + fprintf(debugfile, "%*s", i % 4 == 3 ? 4 : 3, ""); } - if (mode) - fprintf(debugfile, "]"); + } + + for (i = 0; i < len; ++i) + { + fprintf(debugfile, "%c", isprint(s[i]) ? s[i] : '.'); + } + + fprintf(debugfile, "\n"); +} + +static void fprintfstring(char *s, size_t len) +{ + size_t i; + + for (i = 0; i < len; i += 16) + { + fprintf(debugfile, "%10s: ", sizeu1(i)); + fprintfline(&s[i], min(len - i, 16)); + } } static void fprintfstringnewline(char *s, size_t len) { fprintfstring(s, len); - fprintf(debugfile, "\n"); } /// \warning Keep this up-to-date if you add/remove/rename packet types @@ -847,7 +854,7 @@ static void DebugPrintpacket(const char *header) UINT8 *cmd = (UINT8 *)(&serverpak->cmds[serverpak->numslots * serverpak->numtics]); size_t ntxtcmd = &((UINT8 *)netbuffer)[doomcom->datalength] - cmd; - fprintf(debugfile, " firsttic %u ply %d tics %d ntxtcmd %s\n ", + fprintf(debugfile, " firsttic %u ply %d tics %d ntxtcmd %s\n", (UINT32)serverpak->starttic, serverpak->numslots, serverpak->numtics, sizeu1(ntxtcmd)); /// \todo Display more readable information about net commands fprintfstringnewline((char *)cmd, ntxtcmd); @@ -881,7 +888,7 @@ static void DebugPrintpacket(const char *header) case PT_TEXTCMD2: case PT_TEXTCMD3: case PT_TEXTCMD4: - fprintf(debugfile, " length %d\n ", netbuffer->u.textcmd[0]); + fprintf(debugfile, " length %d\n", netbuffer->u.textcmd[0]); fprintf(debugfile, "[%s]", netxcmdnames[netbuffer->u.textcmd[1] - 1]); fprintfstringnewline((char *)netbuffer->u.textcmd + 2, netbuffer->u.textcmd[0] - 1); break;