c++ 日志类

时间更改内容备注
2023.12.27初始化
2023.1.23增加使用example、完善构造函数
#pragma once
// .h
/**
 * @brief 日志类
*/

#include <string>
#include <memory>

class Logger
{
public:

	enum LogLevel
	{
		LL_DEBUG, // 优先级 低
		LL_INFO,
		LL_WARNING,
		LL_ERROR, // 优先级 高
		LL_NONE,
	};

	enum LogTarget
	{
		LT_FILE, // 文件
		LT_TERMINAL, // 终端
		LT_FILEANDTERMINAL // both
	};

public:
	Logger();
	Logger(LogTarget tar, const std::string& path, LogLevel level, const std::string& perfix);
	Logger(LogTarget tar, LogLevel level, const std::string& perfix);
	~Logger();

public:
	static Logger* getInstance();

public:
	void Debug(const std::string& text);
	void Info(const std::string& text);
	void Wraning(const std::string& text);
	void Error(const std::string& text);

private:
	class Impl;
	std::unique_ptr<Impl> impl_;
};

#include "Logger.h"

#include <fstream>
#include <iostream>
#include <chrono>

// .cpp
/**
 * @brief 日志类
*/
class Logger::Impl
{
public:
	Impl(Logger::LogTarget, const std::string&, Logger::LogLevel);
	Impl(Logger::LogTarget, const std::string&, Logger::LogLevel, const std::string&);
	~Impl();

public:
	void Export(LogLevel level, const std::string& info);

private:
	std::string currentTime();

public:
	LogTarget target_;
	std::string path_;
	LogLevel level_;
	std::string perfix_;

private:
	std::ofstream m_file_;
	std::string m_content_;
};

Logger::Impl::Impl(Logger::LogTarget target, const std::string& path, Logger::LogLevel level)
	: target_(target)
	, path_(path)
	, level_(level)
{
	m_content_ = "************************Start***********************\n";

	if (target_ != LogTarget::LT_FILE)
	{
		std::cout << m_content_;
	}
	if (target_ != LogTarget::LT_TERMINAL)
	{
		m_file_.open(path, std::ios::out | std::ios::app);
		m_file_ << m_content_;
	}
}

Logger::Impl::Impl(Logger::LogTarget target, const std::string& path, Logger::LogLevel level, const std::string& perfix)
	: target_(target)
	, path_(path)
	, level_(level)
	, perfix_(perfix)
{
	m_content_ = "************************Start***********************\n";

	if (target_ != LogTarget::LT_FILE)
	{
		std::cout << m_content_;
	}
	if (target_ != LogTarget::LT_TERMINAL)
	{
		m_file_.open(path, std::ios::out | std::ios::app);
		m_file_ << m_content_;
	}
}

Logger::Impl::~Impl()
{
	m_content_ = "************************End***********************\n";

	if (target_ != LogTarget::LT_FILE)
	{
		std::cout << m_content_;
	}
	if (target_ != LogTarget::LT_TERMINAL)
	{
		m_file_ << m_content_;
		m_file_.close();
	}
}

void Logger::Impl::Export(LogLevel level, const std::string& info)
{
	m_content_ = perfix_.empty() ? "" : "["+ perfix_ +"]";

	if (level_ > level)
		return;

	switch (level)
	{
	case Logger::LL_DEBUG:
		m_content_.append("[DEBUG] ");
		break;
	case Logger::LL_INFO:
		m_content_.append("[INFO] ");
		break;
	case Logger::LL_WARNING:
		m_content_.append("[WARNING] ");
		break;
	case Logger::LL_ERROR:
		m_content_.append("[ERROR] ");
		break;
	default:
		break;
	}

	m_content_.append(currentTime());

	m_content_.append(info);

	m_content_.append("\n");

	if (target_ != LogTarget::LT_FILE)
	{
		std::cout << m_content_;
	}
	if (target_ != LogTarget::LT_TERMINAL)
	{
		m_file_ << m_content_;
	}
}

std::string Logger::Impl::currentTime()
{
	auto tNow = std::chrono::system_clock::now();
	auto tRawTime_ = std::chrono::system_clock::to_time_t(tNow);

	char buffer[_MAX_PATH];
	// %Y-%m-%d %H:%M:%S
	strftime(buffer, _MAX_PATH, "%H:%M:%S", localtime(&tRawTime_));

	auto tSec = std::chrono::duration_cast<std::chrono::seconds>(tNow.time_since_epoch());
	auto tMilli = std::chrono::duration_cast<std::chrono::milliseconds>(tNow.time_since_epoch());
	auto ms = std::to_string((tMilli - tSec).count());

	ms.insert(0, ".000", 4 - ms.length());

	return buffer + ms + " ";
}

Logger::Logger()
{
	impl_ = std::make_unique<Impl>(Logger::LogTarget::LT_FILE, "calcSpeedJPEG.log", Logger::LogLevel::LL_DEBUG);
}

Logger::Logger(LogTarget tar, const std::string& path, LogLevel level, const std::string& perfix)
{
	impl_ = std::make_unique<Impl>(tar, path, level);
}

Logger::Logger(LogTarget tar, LogLevel level, const std::string& perfix)
{
	impl_ = std::make_unique<Impl>(tar, "", level, perfix);
}

Logger::~Logger()
{
}

Logger* Logger::getInstance()
{
	static Logger ins;
	return &ins;
}

void Logger::Debug(const std::string& text)
{
	impl_->Export(LogLevel::LL_DEBUG, text);
}

void Logger::Info(const std::string& text)
{
	impl_->Export(LogLevel::LL_INFO, text);
}

void Logger::Wraning(const std::string& text)
{
	impl_->Export(LogLevel::LL_WARNING, text);
}

void Logger::Error(const std::string& text)
{
	impl_->Export(LogLevel::LL_ERROR, text);
}
使用方法
std::unique_ptr<Logger> loger_ = std::make_unique<Logger>(Logger::LT_TERMINAL, Logger::LL_DEBUG, "PanelWidget");

loger_->Debug("message");
  • 10
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值