天天看点

C++最简单的日志库

如何使用Demo示例1

simpleLogClass gLog;
...
gLog.fileMaxSize = 64 * 1024;
//gLog.outputLevel = simpleLog::LevelType::_TRACE;//默认TRACE级别输出
gLog.fileName = ws2s(GetLocalAppDataPath(L"SmartCloud")) + "\\WeesooUpdate.log";
gLog.fileOldName = ws2s(GetLocalAppDataPath(L"SmartCloud")) + "\\WeesooUpdate.old.log";
...
gLog.error("读取配置文件失败!");
...
gLog.trace("开启下载!");
...
           

如何使用Demo示例2

// KagulaLogger.cpp : Implementation of CKagulaLogger
#include "stdafx.h"
#include "KagulaLogger.h"

#include <comutil.h>
#pragma comment(lib, "comsuppw.lib")
// CKagulaLogger

/*
参考资料
 [1]《BSTR、char*和CString转换》
 http://blog.csdn.net/zeuskaaba/article/details/4082826
*/
STDMETHODIMP CKagulaLogger::Init(BSTR LogFileName, BSTR backupFileName, LONG fileSizeLimit)
{
	char *plogFileName = _com_util::ConvertBSTRToString(LogFileName);
	char *pbackupFileName = _com_util::ConvertBSTRToString(backupFileName);

	//simpleLogClass _logger;
	_logger.fileMaxSize = fileSizeLimit;
	_logger.fileName = plogFileName;
	_logger.fileOldName = pbackupFileName;

	delete plogFileName;
	delete pbackupFileName;

	return S_OK;
}


STDMETHODIMP CKagulaLogger::Trace(BSTR msg)
{
	char *p = _com_util::ConvertBSTRToString(msg);
	std::string strMsg = p;
	delete p;

	_logger.trace(strMsg);

	return S_OK;
}


STDMETHODIMP CKagulaLogger::Info(BSTR msg)
{
	char *p = _com_util::ConvertBSTRToString(msg);
	std::string strMsg = p;
	delete p;

	_logger.info(strMsg);

	return S_OK;
}


STDMETHODIMP CKagulaLogger::Error(BSTR msg)
{
	char *p = _com_util::ConvertBSTRToString(msg);
	std::string strMsg = p;
	delete p;

	_logger.error(strMsg);

	return S_OK;
}


STDMETHODIMP CKagulaLogger::SetOutputLevel(BSTR level)
{
	//使用_com_util::ConvertBSTRToString转数据类型,需要把临时指针delete掉,否则会内存泄露。
	char *p =_com_util::ConvertBSTRToString(level);
	string sLevel  = p;
	delete p;

	if (sLevel=="trace")
	{
		_logger.outputLevel = simpleLog::LevelType::_TRACE;
	} else if (sLevel=="info")
	{
		_logger.outputLevel = simpleLog::LevelType::INFO;
	} else if (sLevel=="error")
	{
		_logger.outputLevel = simpleLog::LevelType::_ERROR;
	} else if (sLevel=="naught")
	{
		_logger.outputLevel = simpleLog::LevelType::NAUGHT;
	}

	return S_OK;
}
           

头文件清单

#ifndef _SIMPLELOG_H_
#define _SIMPLELOG_H_

/*
Title:C++ simple log
LastUpdate:[email protected]
Revision:3
Environment:VS2010SP1
Note:using under emergency status!
History
[3]2016-01-29 [1]GCC 4.4.7 Support.
[2]2015-10-10 [1]support multi instance in the same process.
[1]2015-04-10 [1]limit log file size[2]add output level function![3]add test case
*/
#include <string>
#include <fstream>

using namespace std;

namespace simpleLog
{
	namespace LevelType{//less is more important.
		extern const int _TRACE;
		extern const int INFO;
		extern const int _ERROR;
		extern const int NAUGHT; //no output
	}
}

class simpleLogClass
{
public:
	simpleLogClass();

	void trace(std::string strTrace);
	void info(std::string strInfo);
	void error(std::string strInfo);

	string fileName;
	string fileOldName;
	long   fileMaxSize;
	int    outputLevel;
private:
	void outputDebugInfo(int _outputLevel, std::string strInfo);
	void limitFileSize();
	long getFileSize();
	void writeTime(ofstream &file);
};


#endif
           

实现文件清单

#include "simpleLog.h"

#include <stdio.h>  

#include <boost/thread/thread.hpp>

using namespace std;

namespace simpleLog
{
	namespace LevelType{//less is more important.
		const int _TRACE = 90;
		const int INFO = 50;
		const int _ERROR = 30;
		const int NAUGHT = 0; //no output
	}
}

simpleLogClass::simpleLogClass()
{
	fileName = "simpleLog.log";
	fileOldName = "simpleLog.old.log";
	fileMaxSize = 64 * 1024;//default is 64KB size.
	outputLevel = simpleLog::LevelType::_TRACE;
}

long simpleLogClass::getFileSize() {
	FILE * pFile;
	long size;

	pFile = fopen(fileName.c_str(), "rb");
	if (pFile == NULL)
		return 0l;

	fseek(pFile, 0, SEEK_END);
	size = ftell(pFile);
	fclose(pFile);
	return size;
}

void simpleLogClass::limitFileSize()
{
	if (getFileSize() > fileMaxSize)
	{
		//if old name file exist, remove it.
		remove(fileOldName.c_str());

		//rename the current file to old name file.
		rename(fileName.c_str(), fileOldName.c_str());
	}
}

void simpleLogClass::writeTime(ofstream &file)
{
	time_t t;
	tm *lt;
	t = time(NULL);
	lt = localtime(&t);

	file << (lt->tm_year + 1900) << "-" << (lt->tm_mon + 1) << "-" << lt->tm_mday << " ";
	file << lt->tm_hour << ":" << lt->tm_min << ":" << lt->tm_sec << "  ";
}

void simpleLogClass::outputDebugInfo(int _outputLevel, std::string strInfo)
{
	static boost::mutex mInfo;
	boost::lock_guard<boost::mutex> lock(mInfo);

	limitFileSize();

	std::string strLevel;
	switch (_outputLevel)
	{
	case simpleLog::LevelType::_TRACE:
		strLevel = "trace";
		break;
	case simpleLog::LevelType::INFO:
		strLevel = "info";
		break;
	case simpleLog::LevelType::_ERROR:
		strLevel = "error";
		break;
	}

	std::ofstream file(fileName.c_str(), ios::app | ios::out);
	writeTime(file);
	file << strLevel.c_str() << " " << strInfo.c_str() << endl;
	file.close();
}

void simpleLogClass::trace(std::string strTrace)
{
	if (outputLevel >= simpleLog::LevelType::_TRACE)
	{
		outputDebugInfo(simpleLog::LevelType::_TRACE, strTrace);
	}
}

void simpleLogClass::info(std::string strInfo)
{
	if (outputLevel >= simpleLog::LevelType::INFO)
	{
		outputDebugInfo(simpleLog::LevelType::INFO, strInfo);
	}
}

void simpleLogClass::error(std::string strInfo)
{
	if (outputLevel >= simpleLog::LevelType::_ERROR)
	{
		outputDebugInfo(simpleLog::LevelType::_ERROR, strInfo);
	}
}
           

继续阅读