#include <MemDump.h>
Public Member Functions | |
MemDump (const char *msg_, int len_) | |
Constructor converts original binary image to hex and ascii representation, and stores resultant image in internal buffer for later retrieval. | |
~MemDump () | |
Destructor releases image memory. | |
const char * | getMemDump () const |
Obtain a pointer to the dump image (null-terminated char string). | |
Static Public Member Functions | |
static void | dump_to_log (unsigned long mask_, const char *info_, const char *msg_, int len_) |
Write hex/ascii dump of a memory region to log file. | |
Private Attributes | |
char * | m_dump |
pointer to converted image | |
Static Private Attributes | |
static const char | m_empty_str [] = "Null" |
static Null string |
Definition at line 40 of file MemDump.h.
|
Constructor converts original binary image to hex and ascii representation, and stores resultant image in internal buffer for later retrieval. MemDump owns the image and will free the memory once it is out of scope
Definition at line 27 of file MemDump.cpp. References ASSA::ASSAERR, DL, and m_dump. 00027 : m_dump (NULL) 00028 { 00029 register int i; // ptr into source buffer 00030 register int j; // pair elements counter 00031 register int k; // pairs counter [0;16[ 00032 00033 const char *p; // ptr into source buffer 00034 char *hex; // hex ptr into destination buffer 00035 char *ascii; // ascii ptr into destination buffer 00036 00037 long final_len; 00038 00039 /*--- Precondition --- */ 00040 if (len_ <= 0 || msg_ == (char*) NULL) { 00041 DL((ASSAERR,"No data to process.\n")); 00042 DL((ASSAERR,"Data length requested: %d <= 0!\n", len_)); 00043 return; 00044 } 00045 j = k = 1; 00046 00047 /*--- 00048 Each row holds 16 bytes of data. It requres 74 characters maximum. 00049 Here's some examples: 00050 00051 0 1 2 3 4 5 6 7 00052 0123456789012345678901234567890123456789012345678901234567890123456789012 00053 ------------------------------------------------------------------------- 00054 3132 3037 3039 3039 3031 3130 3839 3033 1207090901108903 00055 3038 3132 3030 3331 3030 0d0a 3839 3033 0812003100\r\n8903 00056 0d0a 0d0a 0d0a 0d0a 0d0a 0d0a 0d0a 0d0a \r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n 00057 00058 If all 16 bytes are control characters, the ASCII representation 00059 will extend line to 72 characters plus cartrige return and line 00060 feed at the end of the line. 00061 00062 If len_ is not multiple of 16, we add one more row and another row 00063 just to be on a safe side. 00064 ---*/ 00065 00066 final_len = (int (len_/16) + 1 + (len_ % 16 ? 1:0)) * 74; 00067 00068 m_dump = new char[final_len]; 00069 memset (m_dump, ' ', final_len); 00070 00071 p = msg_; // ptr to original image 00072 hex = m_dump; // current ptr to hex image 00073 ascii = m_dump + 41; // current ptr to ascii image 00074 00075 for (i = 0; i < len_; i++) 00076 { 00077 sprintf(hex,"%01x%01x", p[i] >> 4 & 0x0f, p[i] & 0x0f); 00078 hex+=2; 00079 00080 if (p[i] == '\n') { sprintf(ascii,"\\n"); ascii+=2; } 00081 else if (p[i] == '\t') { sprintf(ascii,"\\t"); ascii+=2; } 00082 else if (p[i] == '\v') { sprintf(ascii,"\\v"); ascii+=2; } 00083 else if (p[i] == '\b') { sprintf(ascii,"\\b"); ascii+=2; } 00084 else if (p[i] == '\r') { sprintf(ascii,"\\r"); ascii+=2; } 00085 else if (p[i] == '\f') { sprintf(ascii,"\\f"); ascii+=2; } 00086 else if (p[i] == '\a') { sprintf(ascii,"\\a"); ascii+=2; } 00087 else if (p[i] == '\0') { sprintf(ascii,"\\0"); ascii+=2; } 00088 else { 00089 sprintf (ascii++,"%c", ((p[i] < ' ' || p [i] > '~') ? '.' : p [i])); 00090 } 00091 00092 if (!(j++ % 2)) { 00093 sprintf (hex++," "); 00094 } 00095 00096 k %= 16; 00097 00098 if (!(k++)) { 00099 *hex = ' '; 00100 sprintf (ascii++,"\n"); 00101 hex = ascii; 00102 ascii += 41; 00103 } 00104 } 00105 *hex = ' '; 00106 m_dump [final_len-1] = '\0'; 00107 }
|
|
Destructor releases image memory.
Definition at line 84 of file MemDump.h. References m_dump, and m_empty_str. 00085 { 00086 if (m_dump && m_dump != m_empty_str) { 00087 delete [] m_dump; 00088 } 00089 m_dump = NULL; 00090 }
|
|
Write hex/ascii dump of a memory region to log file.
Definition at line 111 of file MemDump.cpp. Referenced by ASSA::CharInBuffer::dump(), and ASSA::Socketbuf::underflow(). 00112 { 00113 /* A very important shortcut (performance-wise) 00114 * It saves on constructing unnecessary MemDump object when 00115 * message logging for that particular group is disabled. 00116 */ 00117 00118 if (LOGGER->group_enabled (static_cast<Group> (mask_)) && len_ > 0) 00119 { 00120 MemDump temp (msg_, len_); 00121 DL((mask_, "(%d bytes) %s\n", len_, info_)); 00122 DL((mask_, "\n\n%s\n\n", temp.getMemDump ())); 00123 } 00124 }
|
|
Obtain a pointer to the dump image (null-terminated char string).
Definition at line 94 of file MemDump.h. References m_dump, and m_empty_str. Referenced by ASSA::xdrIOBuffer::dump(), and ASSA::io_ptrs::dump(). 00095 { 00096 return (m_dump ? (const char*) m_dump : m_empty_str); 00097 }
|
|
pointer to converted image
Definition at line 44 of file MemDump.h. Referenced by getMemDump(), MemDump(), and ~MemDump(). |
|
static Null string
Definition at line 47 of file MemDump.h. Referenced by getMemDump(), and ~MemDump(). |