linux 下 打印日誌函數

int milltime(char *str,char *fmt1);
static  errlog (char *File, char * Info);
void Elog (char *FILE,int Line,int iGroup,char *fmt,... );
#define log  __FILE__, __LINE_

 

 

/********************************************************************
function: 添加打印日誌函數
author:  吳家應
date:  20101224
使用方式:  Elog(log,iGroup,"%s",a)
********************************************************************/

int milltime ( str, fmt1 )
char    * str, * fmt1;
{
        struct  timeb   tp;
        struct  tm  * tm;
        char        fmt [ 20 ];
        char        date [ 10+1 ];

        ftime ( &tp );
        tm = localtime ( & ( tp.time )  );
        sprintf(date,"%d-%d-%d ",tm->tm_year+1900,tm->tm_mon,tm->tm_mday);
        sprintf ( fmt, "%s %%02d%s%%02d%s%%02d%s%%03d", date,fmt1, fmt1, fmt1 );
        sprintf ( str, fmt, ( tm -> tm_hour ) , ( tm -> tm_min  ) ,( tm -> tm_sec  ),  ( tp.millitm ) );
        return 0 ;

};
static  errlog ( File, Info )
char    * File, * Info;
{
        int Len;
        char    Path [ 80 ];
        FILE    * fp;
        Len = strlen ( Info );
        sprintf ( Path, "../log/%s", File );
        fp = fopen ( Path, "a" );
        if ( fp == NULL )
        {
                fprintf ( stderr, "%s(%d): fopen(%s) error", __FILE__, __LINE__, Path );
                return ( -1 );
        }
        fwrite  ( Info, 1, Len, fp );
        fclose ( fp );
            return ( 0 );
};
void Elog (char *FILE,int Line,int iGroup,char *fmt,... )
{
        int Len;
        char Info[ 1100 ];
        char str[1100];
        char T_File [ 80 ];
        char MTime[ 23 ];
        va_list args;

 memset(Info,'/0',sizeof(Info));
 memset(str,'/0',sizeof(str));
 memset(T_File,'/0',sizeof(T_File));
 memset(MTime,'/0',sizeof(MTime));

        va_start(args,fmt);
 vsprintf(str,fmt,args);

        milltime ( MTime, ":" );
 sprintf(T_File,"%s_%d_E_log",FILE,iGroup);

        sprintf ( Info, "%s %s(%d) : ",MTime,FILE, Line );
        strcat ( Info, str );
        strcat ( Info, "/n" );
        if ( errlog ( T_File, Info ) < 0 )
        {
                Len = strlen ( Info );
                fprintf ( stderr, "%s %s(%d): errlog error", Info, __FILE__, __LINE__ );
        }
        va_end ( args );
        return ;

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章