From b4da565534b3826037576a6c44e40716b024f89a Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Mon, 9 Jul 2007 19:25:42 +0000 Subject: The new logHexDump function seems to work just fine. --- src/logger.cpp | 44 ++++++++++++++++++++++++++++++++++++++++++++ src/logger.h | 5 +++++ src/tests/logger.cpp | 2 ++ 3 files changed, 51 insertions(+) (limited to 'src') diff --git a/src/logger.cpp b/src/logger.cpp index 34b5d78..1e0313b 100644 --- a/src/logger.cpp +++ b/src/logger.cpp @@ -129,3 +129,47 @@ void Bu::Logger::setLevel( int n ) } } +void Bu::Logger::hexDump( int nLevel, const char *sFile, const char *sFunction, + int nLine, const void *pDataV, long nDataLen, + const char *lpName ) +{ + log( nLevel, sFile, sFunction, nLine, "Displaying %ld bytes of %s.\n", nDataLen, lpName ); + const unsigned char *pData = (const unsigned char *)pDataV; + int j = 0; + Bu::FString sBorder; + for( int l = 0; l < 8*3+2*8+2; l++ ) sBorder += ((l!=8*3)?("-"):("+")); + sBorder += '\n'; + log( nLevel, sFile, sFunction, nLine, sBorder.getStr() ); + Bu::FString sLine; + for(;;) + { + int kmax = 8; + if( nDataLen-j < 8 ) kmax = nDataLen-j; + for(int k = 0; k < 8; k++ ) + { + if( k < kmax ) + { + char buf[4]; + sprintf( buf, "%02X ", (int)((unsigned char)pData[j+k]) ); + sLine += buf; + } + else + { + sLine += "-- "; + } + } + sLine += "| "; + for(int k = 0; k < kmax; k++ ) + { + char buf[3]; + sprintf( buf, "%c ", (pData[j+k]>32 && pData[j+k]<=128)?(pData[j+k]):('.') ); + sLine += buf; + } + sLine += '\n'; + log( nLevel, sFile, sFunction, nLine, sLine.getStr() ); + sLine = ""; + j += kmax; + if( j >= nDataLen ) break; + } + log( nLevel, sFile, sFunction, nLine, sBorder.getStr() ); +} diff --git a/src/logger.h b/src/logger.h index f8e1692..f561e88 100644 --- a/src/logger.h +++ b/src/logger.h @@ -75,6 +75,8 @@ namespace Bu void setMask( int n ); void setLevel( int n ); + void hexDump( int nLevel, const char *sFile, const char *sFunction, int nLine, const void *pData, long nDataLen, const char *lpName ); + private: Bu::FString sLogFormat; int nLevelMask; @@ -89,6 +91,9 @@ namespace Bu #define lineLog( nLevel, sFrmt, ...) \ Bu::Logger::getInstance().log( nLevel, __FILE__, __PRETTY_FUNCTION__, __LINE__, sFrmt, ##__VA_ARGS__ ) +#define logHexDump( nLevel, pData, iSize, sName ) \ + Bu::Logger::getInstance().hexDump( nLevel, __FILE__, __PRETTY_FUNCTION__, __LINE__, pData, iSize, sName ) + /** * Set the Bu::Logger logging mask directly. See Bu::Logger::setMask for * details. diff --git a/src/tests/logger.cpp b/src/tests/logger.cpp index 290f479..59be9d4 100644 --- a/src/tests/logger.cpp +++ b/src/tests/logger.cpp @@ -22,6 +22,8 @@ int main() setLogFormat("%L: %y-%02m-%02d %h:%02M:%02s %f:%l:%F: %t"); lineLog( 5, "Hey, error: %s", strerror( errno ) ); + logHexDump( 5, "This is a test of the hex-dump facility", 16, "Random stuff"); + Thing gh; gh.go( 6); } -- cgit v1.2.3