struct { char name[32]; char address[64]; char userid[9]; char uclass[31]; int age; double income; double deposit; } table; /* ** 聲明一個結構類型數據,準備用來測試 HexLog 效果。 */ strcpy(table.name , "測試人員1"); strcpy(table.address, "東城大街32號"); strcpy(table.userid , "99876"); strcpy(table.uclass , "中文專業"); table.age = 32; table.income = 3618.90; table.deposit = 10239.51; set_logRunLevel(LOGDEBUG); set_logStyle(0); wLog(LOGDEBUG, "====== 結構體 table 數據打印 ============"); wLog(LOGDEBUG, "table.name = [%s]", table.name); wLog(LOGDEBUG, "table.address= [%s]", table.address); wLog(LOGDEBUG, "table.userid = [%s]", table.userid); wLog(LOGDEBUG, "table.uclass = [%s]", table.uclass); wLog(LOGDEBUG, "table.age = [%d]", table.age ); wLog(LOGDEBUG, "table.income = [%.2f]", table.income); wLog(LOGDEBUG, "table.deposit= [%.2f]", table.deposit); wLog(LOGWARN, "====== 結構體 table 數據打印結束 =========="); wHex(LOGWARN, &table, sizeof(table), "十六進制結構體 %s 測試", "table"); restore_logStyle();
這是一段打印 結構數據的日誌,如下爲 相應日誌打印效果測試
[WARN ][十六進制] 測試結束 style[20] [DEBUG]====== 結構體 table 數據打印 ============ [DEBUG]table.name = [測試人員1] [DEBUG]table.address= [東城大街32號] [DEBUG]table.userid = [99876] [DEBUG]table.uclass = [中文專業] [DEBUG]table.age = [32] [DEBUG]table.income = [3618.90] [DEBUG]table.deposit= [10239.51] [WARN ]====== 結構體 table 數據打印結束 ========== ########[testlog.c][43] [FUNC]-=-=-=-=- 緩衝區(十六進制結構體 table 測試) 長度(156) ######## + 01-02-03-04-05-06-07-08-09-10-11-12-13-14-15-16-17-18-19-20 + ====== ASCII ====== + 000001: b2 e2 ca d4 c8 cb d4 b1 31 00 00 00 20 00 00 00 e0 9a fb 4c | 測試人員1... ...鄽? | 000021: 00 00 00 00 f4 9f fb 4c 03 00 00 00 b6 ab b3 c7 b4 f3 bd d6 | ....魺鸏....東城大街 | 000041: 33 32 ba c5 00 f2 04 08 e0 e5 04 08 00 00 00 00 28 c1 ee bf | 32號.?.噱......(令?| 000061: 03 00 00 00 07 00 00 00 03 00 00 00 b3 a4 04 08 03 00 00 00 | ............長...... | 000081: c0 d5 04 08 25 00 00 00 10 e9 04 08 d0 e8 04 08 39 39 38 37 | 勒..%....?.需..998. | 000101: 36 00 00 00 00 d6 d0 ce c4 d7 a8 d2 b5 00 ff ff 00 00 00 00 | .....中文專業..... | 000121: e4 ca 50 54 9f 88 09 00 18 00 00 00 09 00 00 00 20 00 00 00 | 涫PT焾.......... ... | 000141: cd cc cc cc cc 45 ac 40 7b 14 ae 47 c1 ff c3 40 | 吞燙蘀珸{.瓽?聾 | + ----------------------------------------------------------- + -------------------- +
上述日誌數據中,每行展現了 20 個字符,方便用戶作數據定位,同時右邊部分 ASCII 方式展現,比較好的處理了漢字問題,多數漢字均可以看到。rest
如下爲具體程序實現日誌
#define HEXLEN 20 /* 十六進制日誌字符數 */ struct HEXBUF { int point; /* 數據顯示位置 */ int chn ; /* 漢字標識 */ char scop[ 6 + 1 ]; /* 位置偏移數 */ char sstr[ 20 + 1 ]; /* ASCII 打印串 */ char shex[ 61 + 1 ]; /* 十六進制打印串 */ } ; /* ** 十六進制打印緩衝區初始化 */ void HexBuf_init(struct HEXBUF *p ) { p->point = 0; memset(p->scop, 0x0 , sizeof(p->scop)); memset(p->sstr, 0x20, HEXLEN ); memset(p->shex, 0x20, HEXLEN*3 ); return ; } /* ** 定長數據包 十六進制打印程序 */ int printHexLog( void *PInfo, char *Title, int mLen) { int cnt; // 當前數據處理位置 int point ; char temp[5]; unsigned char *ptr = (unsigned char *) PInfo ; struct HEXBUF hex; memset(&hex, 0x0, sizeof(hex)); printf( "%25s------- %s (%d) ------- \n", "", Title, mLen); HexBuf_init(&hex); for( cnt = 0; cnt < mLen ; cnt ++, ptr ++ ) { if(hex.point && (cnt % HEXLEN) == 0 ) { hex.chn = hex.chn & 0x01; if( hex.chn ) hex.sstr[HEXLEN-1]='.'; printf("%s: %s | %s |\n", hex.scop, hex.shex, hex.sstr); HexBuf_init( &hex ); } if(hex.point == 0 ) sprintf(hex.scop, "%04d", cnt ); point = hex.point; if( (*ptr) < 0x20 ) hex.sstr[point] = '.'; else { if(point == 0 && hex.chn ) { hex.chn = 0; hex.sstr[0] = '.'; } else { hex.sstr[point] = (*ptr); if( (*ptr & 0x80) == 0x80 ) hex.chn ++ ; } } point *= 3; sprintf(temp, "%02x ", (*ptr & 0xff ) ); memcpy(hex.shex + point , temp, 3); hex.point ++; } if( hex.point ) printf("%s: %s | %s |\n", hex.scop, hex.shex, hex.sstr); printf("%25s------- End ------- \n", ""); return 0 ; }