Linux下实现跟踪程序执行信息记录日志文件

//debug.h

/*************************************************************        FileName : debug.h    FileFunc : 头文件    Version  : V0.1        Author   : Sunrier        Date     : 2012-06-09    Descp    : Linux下实现跟踪程序执行信息记录日志文件   *************************************************************/#ifndef   _DEBUG_H_    #define   _DEBUG_H_ #ifdef __cplusplusextern "C" {#endif#include <stdio.h>#include <stdlib.h> #include <string.h>  #include <time.h> #include <dirent.h>#include <stdarg.h> #include <sys/stat.h>#define ERROR        __FILE__,__LINE__,0x01#define DEBUG        __FILE__,__LINE__,0x02void GetCurrentTime(char *pTime);void SetLogName(char *pLogPath,char *pProcName,char *pLogName);void TRACE_LOG(char *pFileName,int iLine,int iLogType,char *pLogName,const char *fmt, ...);#ifdef __cplusplus}#endif#endif 

//debug.c

/*************************************************************        FileName : debug.c    FileFunc : 实现文件    Version  : V0.1        Author   : Sunrier        Date     : 2012-06-09    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/%s%.6s.log",pLogPath,pProcName,&szTime[2]);}void  TRACE_LOG(char *pFileName,int iLine,int iLogType,char *pLogName,const char *fmt, ...){FILE *fp;va_list vap;char szTime[15];char szLogType[10];char szTemp[512];int iLen=0;memset(szTime,0,sizeof(szTime));memset(szLogType,0,sizeof(szLogType));memset(szTemp,0,sizeof(szTemp));GetCurrentTime(szTime);va_start(vap,fmt);if(iLogType==0x01){strcpy(szLogType,"ERROR");}else if(iLogType==0x02){strcpy(szLogType,"DEBUG");}sprintf(szTemp,"[%14s][P%d][%10s][L%04d][%.6s]:\n",szTime,getpid(),pFileName,iLine,szLogType);//iLen=strlen(pLogName)-10;//memcpy(pLogName+iLen,&szTime[2],6);fp=fopen(pLogName,"a+");fprintf(fp,"%s",szTemp);vfprintf(fp,fmt,vap);fprintf(fp,"\n");fflush(fp);fclose(fp);fprintf(stderr,"[%s][P%d][%s][%d]:",szLogType,getpid(),pFileName,iLine);memset(szTemp,0,sizeof(szTemp));vsprintf(szTemp,fmt,vap);va_end(vap);fprintf(stderr,"%s\n",szTemp);//打印到屏幕fflush(stderr);}

//demo.c

/*************************************************************        FileName : demo.c    FileFunc : 跟踪程序执行信息并把相应执行结果记录到日志文件    Version  : V0.1        Author   : Sunrier        Date     : 2012-06-09    Descp    : Linux下实现跟踪程序执行信息记录日志文件  *************************************************************/#include <stdio.h>#include <string.h>#include "debug.h"int func( int iFlag ){if( iFlag ){return 0;}else{return 1;} }int main(int argc,char *argv[]){char szLogPath[100],szProcName[100],szLogName[100];char *p = NULL;int iRetCode = 0;int iFlag = 0;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);iFlag = 0;iRetCode = func( iFlag );if( iRetCode ){TRACE_LOG(ERROR,szLogName,"func error,iRetCode=[%d]\n",iRetCode);return 1;}TRACE_LOG(DEBUG,szLogName,"Success =[%d]\n",iRetCode);//TRACE_LOG(DEBUG,"Success !");return 0;}

//makefile

#makefiledemo:demo.c debug.c debug.h@gcc -o demo demo.c debug.cclean:@cd /mnt/hgfs/Sunrier/code/ && \ls | grep -v ^makefile$$ | grep -v [.]c$$ | grep -v [.]h$$ | grep -v [.]sql$$ | grep -v [.]sh$$ | xargs rm -rf#makefile

问:一只小狗在沙漠中旅行,结果死了,问他是怎么死的?

Linux下实现跟踪程序执行信息记录日志文件

相关文章:

你感兴趣的文章:

标签云: