C++的一个日志类,和log4cplus很像
2015-01-08 15:47
465 查看
filelogs.h
#ifndef H_FILELOGS_H
#define H_FILELOGS_H
#include "stdlib.h"
#include "stdio.h"
#include <string.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#define MAX_FILE_LEN 5242880 //5M
#define MAX_PATH_LEN 300
#define RTN_FAIL -1
#define RTN_SUCCESS 0
#define WriteLog(Msg) WriteMsgLog(__FILE__,__LINE__,Msg)
class CFileLogs
{
public:
CFileLogs();
virtual ~CFileLogs();
bool IsOpen();
int init(int iLogLevel,const char *pcDebugLogFileName);
bool OpenFile(char *);
bool OpenNewOutputFile(char *);
bool GetLock();
int WriteMsgLog(const char *,int,const char *);
int WriteOutputMsg(const char *);
public:
char m_FileName[MAX_PATH_LEN + FILENAME_MAX + 1];
int m_LogLevel;
private:
bool m_lock;
FILE *m_file;
private:
bool CheckFile();
void GetTimeStr(char *);
};
#endif
filelogs.cpp
// filelogs.cpp: implementation of the CFileLogs class.
//
//////////////////////////////////////////////////////////////////////
#include "filelogs.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CFileLogs::CFileLogs()
{
m_lock = false;
m_file = NULL;
}
CFileLogs::~CFileLogs()
{
if(m_file != NULL)
fclose(m_file);
}
//////////////////////////////////////////////////////////////////////
// public method
//////////////////////////////////////////////////////////////////////
bool CFileLogs::GetLock()
{
if(m_lock == true)
return false;
m_lock = true;
return true;
}
bool CFileLogs::IsOpen()
{
if(m_file != NULL)
return true;
else
return false;
}
int CFileLogs::init(int iLogLevel,const char *pcDebugLogFileName)
{
this->m_LogLevel = iLogLevel;
//if log level less than 1 , then don't open log file
if (this->m_LogLevel > 0) {
if (pcDebugLogFileName == NULL) {
return RTN_FAIL;
}else {
strcpy(this->m_FileName, pcDebugLogFileName);
//this->m_DebugLogFileName = strDuplicate(pcDebugLogFileName);
}
}
FILE *logFile=NULL;
if (this->m_LogLevel >= 3) {
logFile = fopen(this->m_FileName,(char *)"a+");
if (logFile == NULL) {
printf("Cannot open file %s to write!\n", this->m_FileName);
return RTN_FAIL;
}
fclose(logFile);
}
return RTN_SUCCESS;
}
bool CFileLogs::OpenFile(char *filePathAndName)
{
if(filePathAndName == NULL)
return false;
sprintf(m_FileName, filePathAndName);
if((m_file = fopen(m_FileName, "a")) == NULL)
return false;
return true;
}
bool CFileLogs::OpenNewOutputFile(char *filePathAndName)
{
if(filePathAndName == NULL)
return false;
if( IsOpen() )
fclose(m_file);
sprintf(m_FileName, filePathAndName);
if((m_file = fopen(m_FileName, "a")) == NULL)
return false;
return true;
}
int CFileLogs::WriteMsgLog(const char *pcsrcfile,int line,const char *strMsg)
{
char buf[100];
CheckFile();
if(m_file == NULL)
m_file = fopen(m_FileName, "a");
if(m_file != NULL && strMsg != NULL) {
GetTimeStr(buf);
fprintf(m_file, "[%s]line[%d][%s] %s\n",pcsrcfile,line,buf,strMsg);//pcsrcfile
fflush( m_file );
}
else {
return 0;
}
m_lock = false;
return 1;
}
int CFileLogs::WriteOutputMsg(const char *strMsg)
{
if (m_file == NULL)
m_file = fopen(m_FileName, "a");
if (m_file != NULL && strMsg != NULL) {
fprintf(m_file, "%s\n",strMsg);
fflush( m_file );
}
else {
return 0;
}
m_lock = false;
return 1;
}
bool CFileLogs::CheckFile()
{
struct stat statBuf;
int nRet;
char strNewName[500];
struct tm * pTime;
if(m_file == NULL)
return false;
nRet = fstat(
#ifdef LINUX
fileno(m_file)
#else
#ifdef HPUX
fileno(m_file)
#else
m_file->_file
#endif
#endif
, &statBuf );
if( nRet != 0 ) {
printf( "CFileLogs:Bad file handle!\n" );
return false;
} else {
if(statBuf.st_size > MAX_FILE_LEN) {
pTime = localtime(&statBuf.st_mtime);
sprintf( strNewName, "%sY%dM%dD%dH%dM%dS%d", m_FileName, pTime->tm_year + 1900, pTime->tm_mon+1, pTime->tm_mday, pTime->tm_hour, pTime->tm_min, pTime->tm_sec);
fclose(m_file);
m_file = NULL;
rename(m_FileName, strNewName);
}
}
return true;
}
void CFileLogs::GetTimeStr(char *pStr)
{
time_t ltime;
struct tm *pNow,now;
time(<ime);
#ifdef WIN32
pNow = localtime(<ime);
#else
pNow = localtime_r(<ime, &now);
#endif
sprintf(pStr, (char *)"%2d-%02d-%02d %02d:%02d:%02d",
pNow->tm_year + 1900, pNow->tm_mon+1, pNow->tm_mday,
pNow->tm_hour, pNow->tm_min, pNow->tm_sec);
return;
}
#ifndef H_FILELOGS_H
#define H_FILELOGS_H
#include "stdlib.h"
#include "stdio.h"
#include <string.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#define MAX_FILE_LEN 5242880 //5M
#define MAX_PATH_LEN 300
#define RTN_FAIL -1
#define RTN_SUCCESS 0
#define WriteLog(Msg) WriteMsgLog(__FILE__,__LINE__,Msg)
class CFileLogs
{
public:
CFileLogs();
virtual ~CFileLogs();
bool IsOpen();
int init(int iLogLevel,const char *pcDebugLogFileName);
bool OpenFile(char *);
bool OpenNewOutputFile(char *);
bool GetLock();
int WriteMsgLog(const char *,int,const char *);
int WriteOutputMsg(const char *);
public:
char m_FileName[MAX_PATH_LEN + FILENAME_MAX + 1];
int m_LogLevel;
private:
bool m_lock;
FILE *m_file;
private:
bool CheckFile();
void GetTimeStr(char *);
};
#endif
filelogs.cpp
// filelogs.cpp: implementation of the CFileLogs class.
//
//////////////////////////////////////////////////////////////////////
#include "filelogs.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CFileLogs::CFileLogs()
{
m_lock = false;
m_file = NULL;
}
CFileLogs::~CFileLogs()
{
if(m_file != NULL)
fclose(m_file);
}
//////////////////////////////////////////////////////////////////////
// public method
//////////////////////////////////////////////////////////////////////
bool CFileLogs::GetLock()
{
if(m_lock == true)
return false;
m_lock = true;
return true;
}
bool CFileLogs::IsOpen()
{
if(m_file != NULL)
return true;
else
return false;
}
int CFileLogs::init(int iLogLevel,const char *pcDebugLogFileName)
{
this->m_LogLevel = iLogLevel;
//if log level less than 1 , then don't open log file
if (this->m_LogLevel > 0) {
if (pcDebugLogFileName == NULL) {
return RTN_FAIL;
}else {
strcpy(this->m_FileName, pcDebugLogFileName);
//this->m_DebugLogFileName = strDuplicate(pcDebugLogFileName);
}
}
FILE *logFile=NULL;
if (this->m_LogLevel >= 3) {
logFile = fopen(this->m_FileName,(char *)"a+");
if (logFile == NULL) {
printf("Cannot open file %s to write!\n", this->m_FileName);
return RTN_FAIL;
}
fclose(logFile);
}
return RTN_SUCCESS;
}
bool CFileLogs::OpenFile(char *filePathAndName)
{
if(filePathAndName == NULL)
return false;
sprintf(m_FileName, filePathAndName);
if((m_file = fopen(m_FileName, "a")) == NULL)
return false;
return true;
}
bool CFileLogs::OpenNewOutputFile(char *filePathAndName)
{
if(filePathAndName == NULL)
return false;
if( IsOpen() )
fclose(m_file);
sprintf(m_FileName, filePathAndName);
if((m_file = fopen(m_FileName, "a")) == NULL)
return false;
return true;
}
int CFileLogs::WriteMsgLog(const char *pcsrcfile,int line,const char *strMsg)
{
char buf[100];
CheckFile();
if(m_file == NULL)
m_file = fopen(m_FileName, "a");
if(m_file != NULL && strMsg != NULL) {
GetTimeStr(buf);
fprintf(m_file, "[%s]line[%d][%s] %s\n",pcsrcfile,line,buf,strMsg);//pcsrcfile
fflush( m_file );
}
else {
return 0;
}
m_lock = false;
return 1;
}
int CFileLogs::WriteOutputMsg(const char *strMsg)
{
if (m_file == NULL)
m_file = fopen(m_FileName, "a");
if (m_file != NULL && strMsg != NULL) {
fprintf(m_file, "%s\n",strMsg);
fflush( m_file );
}
else {
return 0;
}
m_lock = false;
return 1;
}
bool CFileLogs::CheckFile()
{
struct stat statBuf;
int nRet;
char strNewName[500];
struct tm * pTime;
if(m_file == NULL)
return false;
nRet = fstat(
#ifdef LINUX
fileno(m_file)
#else
#ifdef HPUX
fileno(m_file)
#else
m_file->_file
#endif
#endif
, &statBuf );
if( nRet != 0 ) {
printf( "CFileLogs:Bad file handle!\n" );
return false;
} else {
if(statBuf.st_size > MAX_FILE_LEN) {
pTime = localtime(&statBuf.st_mtime);
sprintf( strNewName, "%sY%dM%dD%dH%dM%dS%d", m_FileName, pTime->tm_year + 1900, pTime->tm_mon+1, pTime->tm_mday, pTime->tm_hour, pTime->tm_min, pTime->tm_sec);
fclose(m_file);
m_file = NULL;
rename(m_FileName, strNewName);
}
}
return true;
}
void CFileLogs::GetTimeStr(char *pStr)
{
time_t ltime;
struct tm *pNow,now;
time(<ime);
#ifdef WIN32
pNow = localtime(<ime);
#else
pNow = localtime_r(<ime, &now);
#endif
sprintf(pStr, (char *)"%2d-%02d-%02d %02d:%02d:%02d",
pNow->tm_year + 1900, pNow->tm_mon+1, pNow->tm_mday,
pNow->tm_hour, pNow->tm_min, pNow->tm_sec);
return;
}
相关文章推荐
- 让Log4CPLUS每个CPP记一个日志文件
- 一个基于log4cplus的简单日志类
- 让Log4CPLUS每个CPP记一个日志文件
- C++开源日志库log4cplus
- c++日志工具log4cplus的使用
- 贴一下Log4cplus每天记录一个日志文件的配置。
- C++的一个记日志的代码
- 一个简易的C++日志记录类
- c++日志工具之——log4cplus
- VC-Logger 是一个简单易用的 C++ 程序通用日志组件
- 一个迷你的日志类(c++版)
- 一个跨平台的c++日志模块实现
- c++ 多线程写日志的一个很实用的日志类源码(支持 c++ builder)
- c++开源日志库log4cplus
- 一个简单的c++写日志类
- C++日志库log4cplus使用手册
- 一个基于Loki::SingletonHolder的Windows线程安全的C++日志类实现
- 转C++日志库log4cplus使用手册
- log4cplus是C++编写的开源的日志系统
- C++实现一个简单的异常日志记录类