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 ;
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 ;
相关文章推荐
- linux c 定义打印时间/文件名/行/函数日志的可变参数宏定义,如果需要的话可以参考
- linux C/C++ 日志打印函数
- Linux下syslog日志函数使用
- linux系统实时打印tomcat运行日志
- 打印日志时同时打印出文件名,行数,函数名
- linux打印当前函数调用栈backtrace
- linux下的access()函数判断文件是否存在、打印时间
- 调试信息的宏定义:能够打印出文件名、函数、行数、日志,方便调试
- 日志 - 打印函数的调用过程
- linux系统实时打印tomcat运行日志
- 守护进程使用syslog函数打印信息(基于ARM-Linux)
- 在Linux中打印函数调用堆栈(一)
- iOS中打印日志显示系统详细时间,类名,行号,函数名,线程,及打印值
- linux环境下部署tomcat工程,日志不走log4j配置而打印到catalina.out可能的原因
- Linux日志文件与syslog函数介绍
- Linux 内核堆栈打印函数
- Linux驱动调试手段:打印信息日志
- Linux 下的纯 C 日志函数库: zlog
- Linux配置Netconsole远程打印日志信息
- linux系统实时打印tomcat运行日志