]> granicus.if.org Git - rtmpdump/commitdiff
Make LogHexString() more usable, add DEBUG2 level
authorhyc <hyc@400ebc74-4327-4243-bc38-086b20814532>
Tue, 29 Dec 2009 22:23:05 +0000 (22:23 +0000)
committerhyc <hyc@400ebc74-4327-4243-bc38-086b20814532>
Tue, 29 Dec 2009 22:23:05 +0000 (22:23 +0000)
git-svn-id: svn://svn.mplayerhq.hu/rtmpdump/trunk@131 400ebc74-4327-4243-bc38-086b20814532

log.c
log.h

diff --git a/log.c b/log.c
index b6be0f41104ab24d590ff92187f9ba2374f27ccd..165f6027b2c01fec48140e2af36f2164f11352ba 100644 (file)
--- a/log.c
+++ b/log.c
@@ -22,6 +22,8 @@
 #include <stdio.h>
 #include <stdarg.h>
 #include <string.h>
+#include <assert.h>
+#include <ctype.h>
 
 #include "log.h"
 
@@ -121,18 +123,53 @@ void LogHex(int level, const char *data, unsigned long len)
        LogPrintf("\n");
 }
 
-void LogHexString(const char *data, unsigned long len)
+void LogHexString(int level, const char *data, unsigned long len)
 {
+       static const char hexdig[] = "0123456789abcdef";
+#define BP_OFFSET 9
+#define BP_GRAPH 60
+#define BP_LEN 80
+       char    line[BP_LEN];
        unsigned long i;
-       if ( debuglevel==LOGCRIT )
+
+       if ( !data || level > debuglevel )
                return;
-       for(i=0; i<len; i++) {
-               LogPrintf("%02X ", (unsigned char)data[i]);
-       }
-       LogPrintf("\n");
 
-       for(i=0; i<len; i++) {
-               LogPrintf("%c", (unsigned char)data[i]);
+       /* in case len is zero */
+       line[0] = '\n';
+       line[1] = '\0';
+
+       for ( i = 0 ; i < len ; i++ ) {
+               int n = i % 16;
+               unsigned off;
+
+               if( !n ) {
+                       if( i ) LogPrintf( "%s", line );
+                       memset( line, ' ', sizeof(line)-2 );
+                       line[sizeof(line)-2] = '\n';
+                       line[sizeof(line)-1] = '\0';
+
+                       off = i % 0x0ffffU;
+
+                       line[2] = hexdig[0x0f & (off >> 12)];
+                       line[3] = hexdig[0x0f & (off >>  8)];
+                       line[4] = hexdig[0x0f & (off >>  4)];
+                       line[5] = hexdig[0x0f & off];
+                       line[6] = ':';
+               }
+
+               off = BP_OFFSET + n*3 + ((n >= 8)?1:0);
+               line[off] = hexdig[0x0f & ( data[i] >> 4 )];
+               line[off+1] = hexdig[0x0f & data[i]];
+
+               off = BP_GRAPH + n + ((n >= 8)?1:0);
+
+               if ( isprint( (unsigned char) data[i] )) {
+                       line[BP_GRAPH + n] = data[i];
+               } else {
+                       line[BP_GRAPH + n] = '.';
+               }
        }
-       LogPrintf("\n");
+
+       LogPrintf( "%s", line );
 }
diff --git a/log.h b/log.h
index fcb035ad59c452a2c3185f163e846cbccf5b6cc9..b16bb0c784c6b37cbc63d341eec58c75fcd66977 100644 (file)
--- a/log.h
+++ b/log.h
@@ -39,8 +39,9 @@ extern "C" {
 #define LOGERROR        1
 #define LOGWARNING     2
 #define LOGINFO                3
-#define LOGDEBUG               4
-#define LOGALL         5
+#define LOGDEBUG       4
+#define LOGDEBUG2      5
+#define LOGALL         6
 
 #define Log    AMF_Log
 #define LogHex AMF_LogHex
@@ -57,7 +58,7 @@ void LogPrintf(const char *format, ...);
 void LogStatus(const char *format, ...);
 void Log(int level, const char *format, ...);
 void LogHex(int level, const char *data, unsigned long len);
-void LogHexString(const char *data, unsigned long len);
+void LogHexString(int level, const char *data, unsigned long len);
 
 #ifdef __cplusplus
 }