#ifndef RUNLOG_H_
#define RUNLOG_H_
#include <time.h>
#include <sys/stat.h>
#include <io.h>
enum TLOGLEVEL
{
TLOGLEVEL_SEVERE = 0,
TLOGLEVEL_STEP =3,
TLOGLEVEL_ERROR = 5,
TLOGLEVEL_INFO = 7,
TLOGLEVEL_DEBUG = 8
};
enum TERRORCODE
{
Ret_SUCCEED = 0,
Ret_LOADFIAL = 100,
Ret_CREATEVLANFIAL = 101,
Ret_INSTALLOSFIAL = 102,
Ret_STARTHOSTFIAL = 103,
Ret_STOPDHCPFAIL = 104,
Ret_STARTDHCPFAIL = 105,
Ret_EXECICEFAIL = 106,
Ret_SETROUTTINGFAIL = 107,
Ret_INPUTPARAMERR = 9997,
Ret_OUTPUTPARAMERR = 9998,
Ret_FAIL = 9999
};
const int LOG_MAX_PATH = 256;
const int MaxLogFileLength = 10*1024*1024;
//#define log runLog::kllog
#define RUNLOG runLog::kllog
#define ANSI /* Comment out for UNIX version */
#ifdef ANSI /* ANSI compatible version */
#include <stdarg.h>
int average( int first, ... );
#else /* UNIX compatible version */
#include <varargs.h>
int average( va_list );
#endif
#include <stdio.h>
#include <string>
using namespace std;
class runLog
{
public:
runLog();
~runLog();
void init();
static void checkFile();
void setLevel(TLOGLEVEL level);
static void kllog(TLOGLEVEL level,char* format,...);
static void newDayStart();
static void delOutTimeFile();
protected:
private:
static FILE* m_pfile;
static string m_initFileName;
static string m_filename;
static TLOGLEVEL m_logLevel;
static int m_iCurrentDay;
static bool m_bneedExecDel;
};
#endif
/
//#include "stdafx.h"
#include "runLog.h"
string runLog::m_initFileName="runlog";
string runLog::m_filename="";
FILE* runLog::m_pfile = NULL;
TLOGLEVEL runLog::m_logLevel = TLOGLEVEL_SEVERE;
int runLog::m_iCurrentDay = 0;
bool runLog::m_bneedExecDel = true;
runLog::runLog()
{
}
runLog::~runLog()
{
if (m_logLevel>TLOGLEVEL_ERROR)
{
fprintf(m_pfile,"****************************************************/n");
fprintf(m_pfile,"-----------------stop DBQueryAgent!--------------------/n");
fprintf(m_pfile,"****************************************************/n");
}
fflush(m_pfile);
if(NULL!=m_pfile)
fclose(m_pfile);
m_filename = "";
m_logLevel= TLOGLEVEL_SEVERE;
m_pfile = NULL;
}
void runLog::init()
{
time_t timep;
struct tm *p;
time(&timep);
p=localtime(&timep);
m_iCurrentDay = p->tm_mday;
char tmp[LOG_MAX_PATH] = "";
strftime(tmp,256,"%Y%m%d",p);
m_filename=m_initFileName + tmp ;
m_filename += ".log";
}
void runLog::kllog(TLOGLEVEL level,char* format,...)
{
//如果运行时日志级别大于当天系统日志级别则不打印
if(level>m_logLevel)
{
return ;
}
time_t timep;
struct tm *p;
time(&timep);
p=localtime(&timep);
//检测是否是新的一天
if (m_iCurrentDay!=p->tm_mday)
{
newDayStart();
}
//1点的时候执行删除旧文件的任务
if (m_bneedExecDel&&1==p->tm_hour)
{
delOutTimeFile();
m_bneedExecDel = false;
}
if(1!=p->tm_hour)
{
m_bneedExecDel = true;
}
static int stepNum = 0;
checkFile();
char tmp[1024]="";
char destText[2048]="";
strftime(tmp,1024,"%Y%m%d%H%M%S",p);
//拼装日志头,格式为[日志级别: 日期]: 信息
if (level == TLOGLEVEL_DEBUG)
{
sprintf(destText,"[DEBUG:%s]: ",tmp);
}
else if(level == TLOGLEVEL_INFO)
{
sprintf(destText,"[INFO:%s]: ",tmp);
}
else if(level == TLOGLEVEL_STEP)
{
stepNum++;
sprintf(destText,"[step %s]: ",tmp);
}
else
{
sprintf(destText,"[ERROR:%s]: ",tmp);
}
va_list ap ;
va_start(ap,format);
vsprintf(tmp,format,ap);
strcat(destText,tmp);
int len = strlen(destText);
//如果日志没有换行符的话,则添加换行符。
if (destText[len-1] != '/n')
{
destText[len] ='/n';
destText[len+1]='/0';
}
fprintf(m_pfile,destText);
fflush(m_pfile);
va_end(ap);
}
void runLog::setLevel(TLOGLEVEL level)
{
if(level<TLOGLEVEL_SEVERE||level>TLOGLEVEL_DEBUG)
{
fprintf(m_pfile,"ERROR: loglevel iserror!");
}
m_logLevel = level;
}
void runLog::checkFile()
{
struct stat buf;
memset((void*)&buf, 0, sizeof(struct stat));
if (-1 == stat(m_filename.c_str(), &buf)) //Not Exist
{
if (NULL != m_pfile)
{
fclose(m_pfile);
m_pfile = NULL;
}
}
if (buf.st_size > MaxLogFileLength)
{
string OldFileName ;
time_t timep;
struct tm *p;
time(&timep);
p=localtime(&timep);
char tmp[LOG_MAX_PATH] = "";
//sprintf(tmp,"%4d%2d%2d%2d%2d%2d",(1900+p->tm_year),
// (1+p->tm_mon),p->tm_mday, p->tm_hour, p->tm_min, p->tm_sec);
strftime(tmp,256,"%Y%m%d%H%M%S",p);
OldFileName =m_initFileName + tmp ;
OldFileName += ".log";
//当前日志文件名改非当前文件名
fclose(m_pfile);
m_pfile=NULL;
rename(m_filename.c_str(),OldFileName.c_str());
}
if (NULL == m_pfile)
{
m_pfile = fopen(m_filename.c_str(),"a+");
if (m_logLevel>TLOGLEVEL_ERROR)
{
fprintf(m_pfile,"****************************************************/n");
fprintf(m_pfile,"---------- start DBQueryAgent!--------------------/n");
fprintf(m_pfile,"****************************************************/n");
}
}
}
void runLog::newDayStart()
{
if (NULL != m_pfile)
{
if (m_logLevel>TLOGLEVEL_ERROR)
{
fprintf(m_pfile,"****************************************************/n");
fprintf(m_pfile,"---------- current day is over!--------------------/n");
fprintf(m_pfile,"****************************************************/n");
}
fclose(m_pfile);
m_pfile = NULL;
m_filename.clear();
time_t timep;
struct tm *p;
time(&timep);
p=localtime(&timep);
m_iCurrentDay = p->tm_mday;
char tmp[LOG_MAX_PATH] = "";
strftime(tmp,256,"%Y%m%d",p);
m_filename=m_initFileName + tmp ;
m_filename += ".log";
m_pfile = fopen(m_filename.c_str(),"a+");
if (m_logLevel>TLOGLEVEL_ERROR)
{
fprintf(m_pfile,"****************************************************/n");
fprintf(m_pfile,"---------- new day is start !--------------------/n");
fprintf(m_pfile,"****************************************************/n");
}
}
}
void runLog::delOutTimeFile()
{
int curday=atoi(m_filename.substr(6,8).c_str());
char dayTime[9];
struct _finddata_t c_file;
intptr_t hFile;
if( (hFile = _findfirst( "*.log", &c_file )) == -1L )
{
//printf( "No *.c files in current directory!/n" );
return ;
}
else
{
do {
if( c_file.attrib & _A_SUBDIR)
{
//如果是目录的话跳过
continue;
}
memset(dayTime,0,9);
strncpy(dayTime,c_file.name+6,8);
int daytmp=atoi(dayTime);
if (-1 == daytmp)
{
continue;
}
//删除3天以前的数据
if (curday-daytmp >2)
{
remove(c_file.name);
}
} while( _findnext( hFile, &c_file ) == 0 );
_findclose( hFile );
}
}