#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
}