95年五粮液52度价格:我的应用程序的log文件变得越来越大,请问如何使用循环logging?

来源:百度文库 编辑:中科新闻网 时间:2024/05/03 07:47:09
能否给出一段C/C++的代码?万分感谢!

以下这段程序是我自己 编写并测试过了的测试过了的.....
最大定义的默认的可循环logfile size是500M
不过,在使用的时候,用logfilesize 来替代
除非logfilesize=0才使用

本程序的使用方法:

if(traceEnabled) {
trace("....");
}

#define MAXFILESIZE (500*1024*1024) /*Maximum file size in Kbytes:500M*/

static CRITICAL_SECTION traceMutex ;
//static CRITICAL_SECTION performanceTraceMutex ;
static char fname[256]=LOGDIR; /*set a default logdir*/
static int initialized=0; /*trace() should call traceInit if this value is not set*/
extern int removeTraceFiles; /*t==1, the log file will be deleted at anytime*/
extern int maxfilesize; /*filesize Kbytes */
extern long position;
void
traceInit(char *p_logDir) {

strcpy(fname, p_logDir) ;

InitializeCriticalSection(&traceMutex) ;
//InitializeCriticalSection(&performanceTraceMutex) ;

if (removeTraceFiles) {
DeleteFile(fname);
removeTraceFiles=0; /* don't delete logfile repeatedly*/
position=0;
}

if ((maxfilesize<=0)||(maxfilesize>=MAXFILESIZE)) {
maxfilesize=MAXFILESIZE;
/*just in case if maxfilesize is wrongly defined, won't cause error*/
}

initialized = 1;

}

void traceWrapup()
{
if ( initialized )
DeleteCriticalSection( &traceMutex );
}

void
trace(char *msg) {
FILE *file ;
struct tm *t ;
struct timeb tp;
char new_msg[26];
char tmpString[200];
int filesize,iNewFile,iFileSize,newMsgSize=0;

if (!initialized) {
/* the log file has not yet been initialised*/
traceInit(fname);
}

EnterCriticalSection(&traceMutex) ;

ftime( &tp );
t = localtime(&(tp.time)) ;

if (removeTraceFiles) {
DeleteFile(fname);
removeTraceFiles=0; /* don't delete logfile repeatedly*/
position=0;
}
newMsgSize=sprintf(new_msg, "[%02d/%02d %02d:%02d:%02d.%03d] ",
t->tm_mon+1, t->tm_mday,
t->tm_hour, t->tm_min, t->tm_sec,
tp.millitm);

if (!access(fname,0)) {
file = fopen(fname, "r+"); /*open file for read and write*/
iNewFile=0;
}
else {
file = fopen(fname, "a+") ;/* */
if (file!=NULL) {
PrintToFile(file,new_msg);
position=ftell(file);

}
iNewFile=1;
}

if ((!iNewFile) && (file!=NULL)) {

/*Check the file size, if the limitation is exceeded
then try to rewrite the file from beginning
otherwise do appendant*/
fseek(file,0L,SEEK_END); /*to the end of file*/
filesize=ftell(file); /*calculate the file size*/

iFileSize=filesize+newMsgSize+strlen(msg);

if (iFileSize>= maxfilesize) {
/*Appendant to file is not allowed, so goto position*/

if ((position>=filesize)||(position<0)) {
position=0;
}
fseek(file, position, SEEK_SET);

if (!position) {
sprintf(tmpString, "[%02d/%02d %02d:%02d:%02d.%03d] Warning:Log file is FULL and existing logs are being re-written!\n",
t->tm_mon+1, t->tm_mday,
t->tm_hour, t->tm_min, t->tm_sec,
tp.millitm);
PrintToFile(file, tmpString);
}
}
PrintToFile(file, new_msg);
PrintToFile(file, msg);
PrintToFile(file,"\n");
position=ftell(file);
position=(position>=maxfilesize)?0:position;
}

if (file!=NULL) {
fflush(file) ;
fclose(file) ;
}
LeaveCriticalSection(&traceMutex) ;
}

void PrintToFile(FILE *file, char *msg)
{
while (*msg) if (!ferror(file)) fputc(*msg++,file);
}

貌似效率很低的样子……建议投机取巧,定期建立新log,删除旧log

以上。
偷懒的老狼