Linux下将数据以十六进制的形式记录到日志文件

48 篇文章 0 订阅

 

//debug.h

/*****************************************************************    
    FileName : debug.h
    FileFunc : 头文件
    Version  : V0.1    
    Author   : Sunrier    
    Date     : 2012-06-13
    Descp    : Linux下将数据以十六进制的形式记录到日志文件   
*****************************************************************/
#ifndef   _DEBUG_H_    
#define   _DEBUG_H_ 

#ifdef __cplusplus
extern "C" {
#endif

#include <stdio.h>
#include <stdlib.h> 
#include <string.h>  
#include <time.h> 
#include <dirent.h>
#include <stdarg.h> 
#include <sys/stat.h>

void GetCurrentTime(char *pTime);
void SetLogName(char *pLogPath,char *pProcName,char *pLogName);
void PrintHexMsgLog(char *pTitle,unsigned char *pucMsg,unsigned int uiLength,char *pLogName);

#ifdef __cplusplus
}
#endif

#endif 


 

 

//debug.c

/*****************************************************************    
    FileName : debug.c
    FileFunc : 实现文件
    Version  : V0.1    
    Author   : Sunrier    
    Date     : 2012-06-13
    Descp    : Linux下将数据以十六进制的形式记录到日志文件   
*****************************************************************/
#include "debug.h"

void GetCurrentTime(char *pTime)  
{  
   time_t t;  
   struct tm tm1;  
  
   t=time(NULL);  
   memcpy(&tm1,localtime(&t),sizeof(struct tm));  
   sprintf(pTime,"%04d%02d%02d%02d%02d%02d",tm1.tm_year+1900,tm1.tm_mon+1,tm1.tm_mday,tm1.tm_hour,tm1.tm_min,tm1.tm_sec);  
}  

void SetLogName(char *pLogPath,char *pProcName,char *pLogName)  
{  
  char szTime[15];  
  DIR *pDir = NULL;  
  
    memset(szTime,0,sizeof(szTime));  
    GetCurrentTime(szTime);  
  
    pDir = opendir(pLogPath);  
    if( pDir==NULL )  
    {     
        mkdir(pLogPath,S_IREAD|S_IWRITE|S_IEXEC);  
    }     
    else  
    {     
        closedir(pDir);  
    }  
       
    sprintf(pLogName,"%s/%sdata_%.6s.log",pLogPath,pProcName,&szTime[2]);  
      
}  

void PrintHexMsgLog(char *pTitle,unsigned char *pucMsg,unsigned int uiLength,char *pLogName)
{
	FILE *fp;
	unsigned int i,j,k;
	char szTime[15];
	
	memset(szTime,0,sizeof(szTime));
	GetCurrentTime(szTime);
	fp=fopen(pLogName,"a+");
	fprintf(fp,"%s",pTitle);
	fprintf(fp,"%.4s-%.2s-%.2s %.2s:%.2s:%.2s\n",szTime,&szTime[4],&szTime[6],&szTime[8],&szTime[10],&szTime[12]);
	for(i=0; i<uiLength; i++,pucMsg++)
	{
		fprintf(fp," %02X",(*pucMsg)&0xff);
		if((i+1)%5==0)
			fprintf(fp," ");
		if((i+1)%15==0)
		{
			pucMsg-=14;
			fprintf(fp," -> ");
			for(j=0;j<15;j++,pucMsg++)
			{
				if((*pucMsg)<32 || (*pucMsg)>127)
					fprintf(fp,"*");
				else
					fprintf(fp,"%c",*pucMsg);
				if((j+1)%5==0)
					fprintf(fp," ");
			}
			pucMsg--;
			fprintf(fp,"\n");
		}
	}
	k=uiLength%15;
	pucMsg-=k;
	if(i%15!=0)
	{
		do
		{
			fprintf(fp,"   ");
			if((i+1)%5 == 0)
				fprintf(fp," ");
			i++;
		}while(i%15!=0);
		fprintf(fp," -> ");
		for(j=0;j<k;j++,pucMsg++)
		{
			fprintf(fp,"%c",*pucMsg);
			if((j+1)%5==0)
				fprintf(fp," ");
		}

	}
	fprintf(fp,"\n");
	fflush(fp);
	fclose(fp);
}



 

//demo.c

/*************************************************************    
    FileName : demo.c
    FileFunc : Linux下跟踪程序中的数据
    Version  : V0.1    
    Author   : Sunrier    
    Date     : 2012-06-13
    Descp    : Linux下将数据以十六进制的形式记录到日志文件 
*************************************************************/
#include <stdio.h>
#include <string.h>
#include "debug.h"

int main(int argc,char *argv[])
{
	char szLogPath[100],szProcName[100],szLogName[100];
	char *p = NULL;
	int iRetCode = 0;
	char szTitle[20];
	unsigned int uiLength;
	unsigned char ucI,ucData[20];
	
	memset(szLogPath,0,sizeof(szLogPath));
	memset(szProcName,0,sizeof(szProcName));
	memset(szLogName,0,sizeof(szLogName));
	p = strrchr(argv[0],'/');
	sprintf(szProcName,"%s_",p);
	sprintf(szLogPath,"%s/log",getenv("HOME"));
	
	SetLogName(szLogPath,szProcName,szLogName);
	
	memset(ucData,0,sizeof(ucData));
	for(ucI=0; ucI<20; ucI++)
	{
		ucData[ucI] = ucI;
	}
	
	memset(szTitle,0,sizeof(szTitle));
	memcpy(szTitle,"ucData : ",9);
	uiLength = 20;	
	PrintHexMsgLog(szTitle,ucData,uiLength,szLogName);
	
	return 0;
	
}


 

//makefile

#makefile开始
demo:demo.c debug.c debug.h
	@gcc -o $@ $?
#demo:demo.c debug.c debug.h
#	@gcc -o demo demo.c debug.c
clean	:
	@ls | grep -v ^makefile$$ | grep -v [.]c$$ | grep -v [.]h$$ | grep -v [.]sql$$ |xargs rm -rf 
#makefile结束

 

 

[Sunrier@localhost homework]$ make
[Sunrier@localhost homework]$ ls
debug.c  debug.h  demo  demo.c  makefile
[Sunrier@localhost homework]$ ./demo
[Sunrier@localhost homework]$

 

demo_data_120613.log

ucData : 2012-06-13 22:34:07
 00 01 02 03 04  05 06 07 08 09  0A 0B 0C 0D 0E  -> ***** ***** ***** 
 0F 10 11 12 13                                  ->  


 


 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值