c++ 简单的日志

minilogger日志

#ifndef LOGGER_H
#define LOGGER_H
#include <string>
#include <fstream>

// ============================================================
//  get system time 
// ============================================================
#include <time.h>


// ============================================================
// time in 24 hours hh:mm:ss format
// ============================================================
static std::string TimeStamp()
{
	char str[9];

	// get the time, and convert it to struct tm format
	time_t a = time(0);
	struct tm* b = localtime(&a);

	// print the time to the string
	strftime(str, 9, "%H:%M:%S", b);

	return str;
}

// ============================================================
// date YYYY:MM:DD format
// ============================================================
static std::string DateStamp()
{
	char str[11];

	// get the time, and convert it to struct tm format
	time_t a = time(0);
	struct tm* b = localtime(&a);

	// print the time to the string
	strftime(str, 11, "%Y.%m.%d", b);

	return str;
}


// ============================================================
// Here is an example of a simple log decorator, you can define your own decorator
// ============================================================
class TextDecorator
{
public:
	static std::string FileHeader(const std::string& p_title)
	{
		return "==================================================\n" +
			p_title + "\n" +
			"==================================================\n\n";
	}

	static std::string SessionOpen()
	{
		return "\n";
	}

	static std::string SessionClose()
	{
		return "\n";
	}

	static std::string Decorate(const std::string& p_string)
	{
		return p_string + "\n";
	}
};


// ============================================================
// New Logger with a new log file and new log title
// ============================================================
template<class decorator>
class Logger
{
public:
	Logger(const std::string& p_filename,
		const std::string& p_logtitle,
		bool p_timestamp = false,
		bool p_datestamp = false);

	~Logger();
	void Log(const std::string& p_entry);

protected:
	std::fstream m_logfile;
	bool m_timestamp;
	bool m_datestamp;

};


typedef Logger<TextDecorator> TextLog;



template<class decorator>
Logger<decorator>::Logger(const std::string& p_filename,
	const std::string& p_logtitle,
	bool p_timestamp,
	bool p_datestamp)
{
	// now the tricky part... testing to see if a file is open or not.
    // stupid C++. You need to open a file in read mode, and if it doesn't
    // open correctly, you know that it doesn't exist.
	// a file is open or not.
	std::fstream filetester(p_filename.c_str(), std::ios::in);

	if (filetester.is_open())
	{
		// the file exists, so just close the test file
		filetester.close();

		// open the real file and set app mode
		m_logfile.open(p_filename.c_str(), std::ios::out | std::ios::app);
	}
	else
	{
		// file doesn't exist.
		m_logfile.open(p_filename.c_str(), std::ios::out);

		// print out a file header to the file
		m_logfile << decorator::FileHeader(p_logtitle);
	}

	// print out an opening statement. Make sure it is time-and-date-stamped
	m_timestamp = true;
	m_datestamp = true;
	m_logfile << decorator::SessionOpen();
	Log("Session opened.");
	m_timestamp = p_timestamp;
	m_datestamp = p_datestamp;

}

template< class decorator >
Logger< decorator >::~Logger()
{
	m_timestamp = true;
	m_datestamp = true;
	Log("Session closed.");
	m_logfile << decorator::SessionClose();

}


template< class decorator >
void Logger< decorator >::Log(const std::string& p_entry)
{
	std::string message;

	if (m_datestamp)
	{
		message += "[" + DateStamp() + "] ";
	}
	if (m_timestamp)
	{
		message += "[" + TimeStamp() + "] ";
	}

	message += p_entry;
	m_logfile << decorator::Decorate(message);
	m_logfile.flush();
}





#endif

使用细节

#include "minilogger.h"
#include <iostream>
#include <string>
using namespace std;



#define LOG(mylog,fmt,args...)	do{\
			char _buf[1024] = {0};\
			snprintf(_buf, sizeof(_buf), "[%s:%s:%d][LOG_NORMAL]"#fmt"\n",__FILE__,__FUNCTION__,__LINE__, ##args);\
			mylog.Log(string(_buf));\
		}while(false)



Logger<TextDecorator> mylog("./mylogfile.txt","this is title!",true,true);

void myfunction()
{
	int a=1;
	int b=2;
	LOG(mylog,"this is a log in function a+b=%d",a+b);
}


int main()
{
	myfunction();
	return 0;
	
}

参考:
https://github.com/ysbbswork/Minilogger

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值