您的位置:首页 > 运维架构 > Linux

linux 下 打印日志函数

2010-12-30 15:24 549 查看
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 ;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: