C++写日志Log

GitHub上面下载的代码,挺好用,权当学习

#ifndef YLOG_YLOG_H_
#define YLOG_YLOG_H_

#include <string>
#include <fstream>
#include <cassert>
#include <ctime>

class YLog{
private:
	std::ofstream of_;
	int minlevel_;
public:
	enum Type 
	{
		ADD = 0,
		OVER
	};
	enum Level 
	{
		INFO = 0,
		ERR
	};
	YLog(const int level, const std::string &logfile, const int type = YLog::OVER) : minlevel_(level) 
	{
		assert((this->ERR == level || this->INFO == level) && "Logfile create failed, please check the level(YLog::ERR or YLog::INFO.");
		if (type == this->ADD) 
		{
			this->of_.open(logfile.c_str(), std::ios_base::out | std::ios_base::app);
		}
		else if (type == this->OVER)
		{
			this->of_.open(logfile.c_str(), std::ios_base::out | std::ios_base::trunc);
		}
		else 
		{
			assert(0 && "Logfile create failed, please check the type(YLog::OVER or YLog::ADD).");
		}
		assert(this->of_.is_open() && "Logfile create failed, please check the logfile's name and path.");
		return;
	}
	~YLog(){
		if (this->of_.is_open()) 
		{
			this->of_.close();
		}
		return;
	}
	template<typename T> void W(const std::string &codefile, const std::string &functionName, const int codeline, const int level, const std::string &info, const T &value) 
	{
		assert(this->of_.is_open() && "Logfile write failed.");
		if (this->ERR == level) 
		{
			this->of_ << "[ERROR] ";
		}
		else if (this->INFO == level) 
		{
			if (this->INFO == this->minlevel_) 
			{
				this->of_ << "[INFO] ";
			}
			else 
			{
				return;
			}
		}
		else 
		{
			assert(0 && "Log write failed, please check the level(YLog::ERR or YLog::INFO.");
		}
		time_t sectime = time(NULL);
		tm tmtime;
#ifdef _WIN32
#if _MSC_VER<1600
		tmtime = *localtime(&sectime);
#else
		localtime_s(&tmtime, &sectime);
#endif
#else
		localtime_r(&sectime, &tmtime);
#endif
		this->of_ << tmtime.tm_year + 1900 << '-' << tmtime.tm_mon + 1 << '-' << tmtime.tm_mday <<
			' ' << tmtime.tm_hour << ':' << tmtime.tm_min << ':' << tmtime.tm_sec <<
			' ' << codefile << "(L:" << codeline<<')'<<"-<" <<functionName << "> " << info << ":" << value << std::endl;
		return;
	}


	template<typename T> void W(const std::string &codefile, const int codeline, const int level, const std::string &info, const T &value) 
	{
		assert(this->of_.is_open() && "Logfile write failed.");
		if (this->ERR == level) 
		{
			this->of_ << "[ERROR] ";
		}
		else if (this->INFO == level)
		{
			if (this->INFO == this->minlevel_) 
			{
				this->of_ << "[INFO] ";
			}
			else 
			{
				return;
			}
		}
		else 
		{
			assert(0 && "Log write failed, please check the level(YLog::ERR or YLog::INFO.");
		}
		time_t sectime = time(NULL);
		tm tmtime;
#ifdef _WIN32
#if _MSC_VER<1600
		tmtime = *localtime(&sectime);
#else
		localtime_s(&tmtime, &sectime);
#endif
#else
		localtime_r(&sectime, &tmtime);
#endif
		this->of_ << tmtime.tm_year + 1900 << '-' << tmtime.tm_mon + 1 << '-' << tmtime.tm_mday <<
			' ' << tmtime.tm_hour << ':' << tmtime.tm_min << ':' << tmtime.tm_sec <<
			' ' << codefile << '(' << codeline << ") " << info << ":\n" << value << std::endl;
		return;
	}
};
#endif // YLOG_YLOG_H_ 
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

WJsuperrunner

你的鼓励是我最大的动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值