Linux下将数据以十六进制的形式记录到日志文件
2012-06-13 22:37
615 查看
//debug.h
//debug.c
//demo.c
//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
/***************************************************************** 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 ->
相关文章推荐
- Linux中误删除数据文件和归档日志的恢复方法
- Linux下实现跟踪程序执行信息记录日志文件
- linux删除指定日期前的日志文件中的记录
- linux的Nginx访问日志、Nginx日志切割、静态文件不记录日志和过期时间介绍
- linux编程--将十六进制数据输出到指定文件里
- 使用hive重定向导出的数据文件中包含WARN日志记录问题
- linux下c/c++实例之二日志记录及文件读取
- VB.net类库中应用log4net实现日志记录(文件形式)
- Linux下日志文件监控系统Logwatch的使用记录
- linux的访问日志不记录静态文件、切割、静态元素过期时间配置
- VB源码--TXT文件尾部追加资料,可用于记录日志及数据添加等用途
- linux二进制/十六进制日志文件如何查看和编辑
- linux系统中c++写日志文件功能分享
- 使用log4j把日志记录到不同文件
- [强烈推荐]Oracle:只有rman备份(数据,参数,日志,控制文件全丢失)的恢复
- Linux(9)后台运行python程序并输出到日志文件
- Linux日志文件总管logrotate
- 如果你的数据库文件只剩下数据文件没有日志文件时,如何附加
- Linux下 数据文件 效验问题
- 【转】搜索引擎会通过日志文件把用户每次检索使用的所有检索串都记录下来,每个查询串的长度为1-255字节