/**************************************
Logger.h -- the class logger
version 1.0.8, Apr 8th, 2016
Copyright (C) 2015-2016 Acheld CHEN
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
*************************************************/
//head file Logger.h
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#ifdef WIN32
#include <windows.h>
#else
#include <unistd.h> // linux下头文件
#endif
#define FILE_MAX_SIZE (1024*2)
class CLogger
{
public:
CLogger(void);
~CLogger(void);
public:
int m_nIndex;
/*
获得当前时间字符串
@param buffer [out]: 时间字符串
@return 空
*/
void get_local_time(char* buffer);
/*
获得文件大小
@param filename [in]: 文件名
@return 文件大小
*/
long get_file_size(char* filename);
/*
写入日志文件
@param filename [in]: 日志文件名
@param max_size [in]: 日志文件大小限制
@param buffer [in]: 日志内容
@param buf_size [in]: 日志内容大小
@return 空
*/
void write_log_file(char* filename, long max_size, char* buffer, unsigned buf_size);
int TestLogger();
};
extern CLogger* theLogger;
//cpp file ,Logger.cpp
#include "StdAfx.h"
#include "Logger.h"
CLogger::CLogger(void)
{
m_nIndex=0;
}
CLogger::~CLogger(void)
{
}
int CLogger::TestLogger()
{
int i;
for (i=0; i<500; ++i)
{
char buffer[32];
memset(buffer, 0, sizeof(buffer));
sprintf(buffer, "====> %d\n", i);
write_log_file("D:\\Log\\log.txt", FILE_MAX_SIZE, buffer, strlen(buffer));
#ifdef WIN32
Sleep(10); // 毫秒
#else
sleep(1); // 秒
#endif
}
// system("pause");
return 0;
}
void CLogger::write_log_file(char* filename, long max_size, char* buffer, unsigned buf_size)
{
if (filename != NULL && buffer != NULL)
{
// 文件超过最大限制, 删除
long length = get_file_size(filename);
if (length > max_size)
{
#define _DELETE_MODEL_
#ifdef _DELETE_MODEL_
unlink(filename); // 删除文件
#endif
//#define _RENAME_MODEL
#ifdef _RENAME_MODEL
m_nIndex++;
char lpName[256] = "\0";
char lpIndex[32] = "\0";
char lpSuffix[32] = "\0";
sprintf(lpIndex,"_%d",m_nIndex);
char fileTemp[256] = "\0";
strcpy(fileTemp,filename);
char* dir = strrchr(fileTemp,'\\');
char* pSuffix = strrchr(fileTemp,'.');
strcpy(lpSuffix,pSuffix);
if (pSuffix){
strcpy(pSuffix,"\0");
}
strcpy(lpName,fileTemp);
strcat(lpName,lpIndex);
strcat(lpName,lpSuffix);
rename(filename,lpName);
#endif
}
// 写日志
{
FILE *fp;
fp = fopen(filename, "at+");
if (fp != NULL)
{
char now[32];
memset(now, 0, sizeof(now));
get_local_time(now);
fwrite(now, strlen(now)+1, 1, fp);
fwrite(buffer, buf_size, 1, fp);
fclose(fp);
fp = NULL;
}
}
}
}
long CLogger::get_file_size(char* filename)
{
long length = 0;
FILE *fp = NULL;
fp = fopen(filename, "rb");
if (fp != NULL)
{
fseek(fp, 0, SEEK_END);
length = ftell(fp);
}
if (fp != NULL)
{
fclose(fp);
fp = NULL;
}
return length;
}
void CLogger::get_local_time(char* buffer)
{
time_t rawtime;
struct tm* timeinfo;
time(&rawtime);
timeinfo = localtime(&rawtime);
sprintf(buffer, "%04d-%02d-%02d %02d:%02d:%02d",
(timeinfo->tm_year+1900), timeinfo->tm_mon, timeinfo->tm_mday,
timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
}
写固定文件大小的日志文件
最新推荐文章于 2020-11-29 16:34:59 发布