00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "assa/Logger.h"
00020 #include "assa/MemDump.h"
00021
00022 using namespace ASSA;
00023
00024 const char MemDump::m_empty_str[] = "Null";
00025
00026 MemDump::
00027 MemDump(const char* msg_, int len_) : m_dump (NULL)
00028 {
00029 register int i;
00030 register int j;
00031 register int k;
00032
00033 const char *p;
00034 char *hex;
00035 char *ascii;
00036
00037 long final_len;
00038
00039
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
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
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_;
00072 hex = m_dump;
00073 ascii = m_dump + 41;
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 }
00108
00109 void
00110 MemDump::
00111 dump_to_log (unsigned long mask_, const char* info_, const char* msg_, int len_)
00112 {
00113
00114
00115
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 }
00125