c++日志单例实现

为了使项目的所有日志都打印到同一个日志中,必须使得所有类使用同一个日志,因此将日志类实现为单例。

.h文件

cpp 复制代码
#pragma once

#include<fstream>

class LogHablee
{
private:
	LogHablee(std::string& dbg_dir);
	LogHablee(const LogHablee&) = delete;
	LogHablee& operator=(const LogHablee&) = delete;


	static LogHablee* _ins;	

public:
	void getNowTimePrefix(std::string& now_time_prefix);


public:
	static LogHablee* getInstance(std::string& dbg_dir)
	{
		if (_ins == nullptr)
		{
			_ins = new LogHablee(dbg_dir);
		}
		return _ins;
	}

	std::ofstream log;
};

.cpp文件

cpp 复制代码
#include "LogHablee.h"
#include<string>


LogHablee* LogHablee::_ins = nullptr;

LogHablee::LogHablee(std::string& dbg_dir)
{
	std::string nowTimePrefix;
	getNowTimePrefix(nowTimePrefix);

	std::string logFilePath(dbg_dir + "/" + nowTimePrefix + "_log.txt");
	this->log.open(logFilePath, std::ios::trunc);
}

void LogHablee::getNowTimePrefix(std::string& now_time_prefix)
{
	std::time_t now_time;
	struct tm* p = new tm;
	std::time(&now_time);
	localtime_s(p, &now_time);
	int year = p->tm_year + 1900;
	int month = p->tm_mon + 1;
	int day = p->tm_mday;
	int hour = p->tm_hour;
	int minute = p->tm_min;
	int second = p->tm_sec;
	delete p;
	// 20221130_134024: 2022年11月30日13点40分24秒
	now_time_prefix = std::to_string(year)
		+ std::string(2 - std::to_string(month).length(), '0') + std::to_string(month)
		+ std::string(2 - std::to_string(day).length(), '0') + std::to_string(day)
		+ "_"
		+ std::string(2 - std::to_string(hour).length(), '0') + std::to_string(hour)
		+ std::string(2 - std::to_string(minute).length(), '0') + std::to_string(minute)
		+ std::string(2 - std::to_string(second).length(), '0') + std::to_string(second);
}

另一个使用到log的类的.h文件

cpp 复制代码
#pragma once
#include<string>


class AClass
{
public:
	AClass(std::string& dbg_dir):_dbgDir(dbg_dir) {}

	void test();

private:
	std::string _dbgDir;
};

# 另一个使用到log的类的.cpp文件

cpp 复制代码
#include "AClass.h"
#include"LogHablee.h"

void AClass::test()
{
	std::string nowTimePrefix;

	LogHablee* pHablee = LogHablee::getInstance(this->_dbgDir);

	pHablee->getNowTimePrefix(nowTimePrefix);
	pHablee->log << nowTimePrefix
		<< ": in AClass::test function"
		<< std::endl;
}

main函数

cpp 复制代码
#include"LogHablee.h"
#include<iostream>
#include"AClass.h"

int main()
{
	std::string nowTimePrefix;

	std::string dbgDir("./");
	LogHablee* pHablee = LogHablee::getInstance(dbgDir);


	pHablee->getNowTimePrefix(nowTimePrefix);
	pHablee->log << nowTimePrefix
		<< ": PROGRAM START, version 1.0.0.0"
		<< std::endl;


	AClass a(dbgDir);
	a.test(); // a.test()里面的日志内容也会写入到一开始创建的日志中

	return 0;
}
相关推荐
luj_17682 小时前
星火科技助力边远地区防病攻坚
c语言·开发语言·c++·经验分享·算法
always_TT2 小时前
【Python 日志记录:logging 模块入门】
开发语言·python·php
xcLeigh2 小时前
Go入门:变量声明的五种方式详解
java·开发语言·golang
zmzb01033 小时前
C++课后习题训练记录Day175
开发语言·c++
啦啦啦啦啦zzzz3 小时前
工具:动态类工厂和用配置文件存储属性
c++·设计模式·工具·动态工厂
脱胎换骨-军哥3 小时前
C++ 代码规范与格式化指南
开发语言·c++·代码规范
枕星而眠4 小时前
C++ STL Map容器完全指南:从有序红黑树到无序哈希表
java·开发语言
爱吃牛肉的大老虎5 小时前
Rust对象之结构体,枚举,特性
开发语言·后端·rust
bbq粉刷匠5 小时前
HashMap 底层原理深度拆解(二):putVal 完整链路解析(懒加载 · 链表遍历 · 尾插法)
java·开发语言·哈希算法
秋雨梧桐叶落莳5 小时前
计算器仿写总结
学习·macos·ios·objective-c·cocoa