中国开发网: 论坛: 程序员情感CBD: 贴子 66197
haitao
刚才贴过,现在再贴一个完整的吧
#include "CommLog.h"
#include <string.h>
#include <time.h>
#include <sys/timeb.h>
#include <stdio.h>
#include <stdlib.h>
//#include <unistd.h>
#include <stdarg.h>

#include "../mp/Exception.h"

class ClogException: public CException
{
public:
ClogException(int nCode, const char* szMsg);
};
ClogException::ClogException(int nCode, const char* szMsg)
: CException(nCode, szMsg)
{
}


char logfilename[256]="\0";
char curwithdate=1; //0:mm:ss 1:hh:mm:ss 2:dd hh:mm:ss 3:mm-dd hh:mm:ss 4:yyyy-mm-dd hh:mm:ss
int lasty=0,lastd=0,lasth=0;

int inlog=0;

char* itoa10(int i,char* s)
{ if (i==0)
{ s[0]='0';
s[1]=0;
return s;
}

char *p=s, *b=s;
if (i<0)
{ *p='-'; b=s+1; i=-i;
p++;
}

for ( ; i>0; p++, i/=10)
*p='0'+i%10;
*p=0;
char t;
for (p--; p>b; p--,b++)
{ t=*b;
*b=*p;
*p=t;
}
return s;
}

int commlogInit(const char* exefilename)
{
if (logfilename[0]!='\0')
return -1;
strcpy(logfilename,exefilename);
strcat(logfilename,".s.log");


#ifdef WIN32
commlogStr("\n----<<< AppStart[@win32]...");
#else
commlogStr("\n----<<< AppStart[@linux]...");
#endif
return 0;
}

int commlogHead(FILE** f,char withdate)
{
*f=fopen(logfilename,"a+t");
if (*f==NULL)
return -1;

struct tm *t;
struct timeb mtp;
ftime(&mtp);
t = localtime(&mtp.time);

/*
struct tm *t;
long ti;
time(&ti);
t=localtime(&ti);
*/

if (withdate<0)
withdate=curwithdate;

if (withdate!=4 && (lasty!=t->tm_year || lastd!=t->tm_yday || lasth!=t->tm_hour))
withdate=4;
lasty=t->tm_year;
lastd=t->tm_yday;
lasth=t->tm_hour;

if (withdate==0)
fprintf(*f,"%.2d:%.2d.%.3d|",t->tm_min,t->tm_sec, mtp.millitm);
else if (withdate==1)
fprintf(*f,"%.2d:%.2d:%.2d.%.3d|",t->tm_hour,t->tm_min,t->tm_sec, mtp.millitm);
else if (withdate==2)
fprintf(*f,"%.2d %.2d:%.2d:%.2d.%.3d|",t->tm_mday,t->tm_hour,t->tm_min,t->tm_sec, mtp.millitm);
else if (withdate==3)
fprintf(*f,"%.2d-%.2d %.2d:%.2d:%.2d.%.3d|",t->tm_mon+1,t->tm_mday,t->tm_hour,t->tm_min,t->tm_sec, mtp.millitm);
else
fprintf(*f,"%.4d-%.2d-%.2d %.2d:%.2d:%.2d.%.3d|",t->tm_year+1900,t->tm_mon+1,t->tm_mday,t->tm_hour,t->tm_min,t->tm_sec, mtp.millitm);

return 0;
}

#define dohead \
/*try{*/\
if (logfilename[0]=='\0') \
return -1; \
\
/*if (inlog)*/ \
/* return -3;*/ \
inlog=1; \
\
FILE* f=NULL; \
/*try{*/\
if (commlogHead(&f,withdate)!=0) \
{ inlog=0; \
return -2; \
} \
/*}catch(ClogException ){ return(-4); }*/

#define doend \
fputs("\n",f); \
/*ffrush(f);*/ \
fclose(f); \
\
inlog=0; \
return 0; \
/*}catch(ClogException ){ return(-5); }*/



int commlogStr(const char* str,char withdate)
{
dohead

fputs(str,f);
fputs("\n",f);

doend
}

int commlogInt(const char* str,const int d,char withdate)
{
dohead

fputs(str,f);
char s[30];
itoa10(d,s);
fputs(s,f);

doend
}

int commlogStr2(const char* str,const char* str2,char withdate)
{
dohead

fputs(str,f);
fputs(str2,f);

doend
}

int commlogStrs(char withdate,...)
{

dohead

char* pa;
va_list marker;

va_start( marker, withdate ); /* Initialize variable arguments. */
while( 1 )
{
pa = va_arg( marker, char*);
if (pa==NULL)
break;
fputs(pa,f);
}
va_end( marker ); /* Reset variable arguments. */

doend

}
我的blog:http://szhaitao.blog.hexun.com & http://www.hoolee.com/user/haitao
--以上均为泛泛之谈--
不尽牛人滚滚来,无边硬伤纷纷现 人在江湖(出来的),哪能不挨刀(总归是要的)
网络对话,歧义纷生;你以为明白了对方的话,其实呢?

您所在的IP暂时不能使用低版本的QQ,请到:http://im.qq.com/下载安装最新版的QQ,感谢您对QQ的支持和使用

相关信息:


欢迎光临本社区,您还没有登录,不能发贴子。请在 这里登录