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 方式展示,比较好的处理了汉字问题,多数汉字都可以看到。
以下为具体程序实现
#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 ;
}