概要
C++ 日志系统,通过策略模式支持日志输出到终端或文件,自动附带时间戳、日志等级、进程号、源文件名和行号,并且线程安全,用户只需用 LOG(level) << "消息" 就能方便地记录日志,适用于程序调试、运行监控和错误追踪。
建立打印策略
在日志系统里,如果你想支持 不同的输出方式(比如输出到终端、输出到文件,甚至以后扩展到网络、数据库),通常会用 继承 + 多态 来设计:
定义一个 抽象基类 LogStrategy,里面声明一个纯虚函数,比如 void log(const std::string &msg) = 0;。
class LogStrategy
{
public:
virtual void SyncLog(const std::string &message) = 0;
};
再派生出两个子类:
向终端打印日志
ConsoleLogStrategy(向终端打印日志)
class ConsoleLogStrategy : public LogStrategy
{
public:
ConsoleLogStrategy(){}
void SyncLog(const string &message) override
{
pthread_mutex_lock(&mutex);
cout << message << endl;
pthread_mutex_unlock(&mutex);
}
~ConsoleLogStrategy(){}
};
这里我们public继承了LogStrategy,将SynLog进行重写,能够打印字符串数据到终端
向文件写日志
FileLogStrategy(向文件写日志)
将日志打印到文件当中,我们需要定义一下打印到那个目录的哪个文件当中
const string defaultpath = "./log";
const string defaultfile = "my.log";
构建一个继承LogStrategy的类,里面先包含最基本的函数:SyncLog用于打印日志信息
const string defaultpath = "./log";
const string defaultfile = "my.log";
class FileLogStrategy : public LogStrategy
{
public:
FileLogStrategy(const string &path = defaultpath, const string &file = defaultfile)
: _path(path),
_file(file){}
void SyncLog(const string &message) override{}
~FileLogStrategy(){}
private:
string _path; // 日志文件所在路径
string _file; // 日志文件本身
};
这里我们又定义了两个变量,分别是_path(路径)和_file(文件名),并用默认构造函数将他们初始化我们设置为的默认值
为了后续通过ofstream打开文件并输入,在我们添加完默认值之后,我们可能不知道系统中是否已经存在了这个目录,所以我们需要判断,如果不存在需要我们自己创建。
FileLogStrategy(const string &path=defaultpath, const string &file=defaultfile) //构造函数
: _path(path),
_file(file) {
pthread_mutex_lock(&mutex);
if (filesystem::exists(_path)) {
return;
}
try {
filesystem: :create_directories(_path);
}
catch (const filesystem::filesystem_error &e) {
cerr << e.what() << endl;
}
pthread_mutex_unlock(&mutex);
}
这里的filesystem::exists(_path)检查目录是否存在,不存在则用filesystem: :create_directories(_path);创建,创建失败则捕获异常信息
当我们确定文件和目录存在后,开始编写SyncLog向文件中输入日志信息,这时我们类中有两个变量,分别是_file和_path通过这两个变量来确定文件的相对路径。
string filename = _path + (_path.back() == '/' ? "" : "/") + _file;
确定了相对路径之后便可以打开文件输入日志
ofstream out(filename, ios::app);
这里的ios:app的含义是向文件末尾追加信息。
完整代码:
const string defaultpath = "./log";
const string defaultfile = "my.log";
class FileLogStrategy: public LogStrategy {
public: FileLogStrategy(const string & path = defaultpath, const string & file = defaultfile):
_path(path),
_file(file) {
pthread_mutex_lock( & mutex);
if (filesystem: :exists(_path)) {
return;
}
try {
filesystem: :create_directories(_path);
} catch(const filesystem: :filesystem_error & e) {
cerr << e.what() << endl;
}
pthread_mutex_unlock( & mutex);
}
void SyncLog(const string & message) override {
pthread_mutex_lock( & mutex);
string filename = _path + (_path.back() == '/' ? "": "/") + _file; // "./log/" + "my.log"
ofstream out(filename, ios: :app); // 追加写入的 方式打开
if (!out.is_open()) {
return;
}
out << message << endl;
out.close();
pthread_mutex_unlock( & mutex);
}~FileLogStrategy() {}
private: string _path; // 日志文件所在路径
string _file; // 日志文件本身
};
形成日志类型
在我们传日志信息的时候我们需要固定日志的类型,所以我们使用enum枚举来固定日志的类型,规定用户只能使用我们规定的类型
enum class LogLevel {
DEBUG,
INFO,
WARNING,
ERROR,
FATAL
};
这里我们规定了5个类型,因为,到后面我们需要将日志类型通过字符串打印到日志文件当中,所以我们需要一个函数来进行转换。
std: :string Level2Str(LogLevel level) {
switch(level) {
case LogLevel:: DEBUG: return "DEBUG";
case LogLevel:: INFO: return "INFO";
case LogLevel:: WARNING: return "WARNING";
case LogLevel:: ERROR: return "ERROR";
case LogLevel:: FATAL: return "FATAL";
default: return "UNKNOWN";
}
}
后面我们通过Level2Str来将我们输入的LogLevel转化为字符串,打印到问价当中
获取日志时间
获取了类型之后我们还要获取日志的打印时间
获取当前时间戳
time_t curr = time(nullptr);
将日志转化成本地时间
struct tm curr_tm;
localtime_r(&curr, &curr_tm);
将时间结构体转化成字符串表示
char timebuffer[128];
snprintf(timebuffer, sizeof(timebuffer),"%4d-%02d-%02d %02d:%02d:%02d",
curr_tm.tm_year+1900,
curr_tm.tm_mon+1,
curr_tm.tm_mday,
curr_tm.tm_hour,
curr_tm.tm_min,
curr_tm.tm_sec
);
整体代码
std::string GetTimeStamp()
{
time_t curr = time(nullptr);
struct tm curr_tm;
localtime_r(&curr, &curr_tm);
char timebuffer[128];
snprintf(timebuffer, sizeof(timebuffer),"%4d-%02d-%02d %02d:%02d:%02d",
curr_tm.tm_year+1900,
curr_tm.tm_mon+1,
curr_tm.tm_mday,
curr_tm.tm_hour,
curr_tm.tm_min,
curr_tm.tm_sec
);
return timebuffer;
}
封装日志
上面我们提到了不同的打印日志的策略,所以我们需要使用类指针来确定后面需要用到的打印策略
class Logger
{
public:
Logger()
{
EnableConsoleLogStrategy();
//EnableFileLogStrategy();
}
void EnableFileLogStrategy()
{
_fflush_strategy = make_unique<FileLogStrategy>();
}
void EnableConsoleLogStrategy()
{
_fflush_strategy = make_unique<ConsoleLogStrategy>();
}
LogMessage operator()(LogLevel level, std::string name, int line)
{
return LogMessage(level, name, line, *this);
}
~Logger()
{
}
unique_ptr<LogStrategy> _fflush_strategy;
};
在Logger类里面确定了打印的方式和日志类型和文件名和行号,如果不调用函数则默认使用
文件打印日志
获取,整合,输出日志信息
那么这个LogMessage是什么呢?
我们这个类中是没有输出日志信息的,只能用来定义日志的打印方式,那我们需要在类中定义一个类,来整合信息,并且将信息打印到文件中,Logger负责传递给LogMessage打印方式,所以我们传递了一个*this
class LogMessage
{
public:
LogMessage(LogLevel &level, std::string &src_name, int line_number, Logger &logger)
: _curr_time(GetTimeStamp()),
_level(level),
_pid(getpid()),
_src_name(src_name),
_line_number(line_number),
_logger(logger) {
// 日志的左边部分,合并起来
stringstream ss;
ss << "[" << _curr_time << "] "
<< "[" << Level2Str(_level) << "] "
<< "[" << _pid << "] "
<< "[" << _src_name << "] "
<< "[" << _line_number << "] "
<< "- ";
_loginfo = ss.str();
}
private:
string _curr_time;
LogLevel _level;
pid_t _pid;
string _src_name;
int _line_number;
string _loginfo;
// 合并之后,一条完整的信息
Logger &_logger;
};
这里我们将所有信息定义在LogMessage中,再通过构造函数将它们包装再_loginfo中
不仅仅只是这些固定信息,我们打印日志的时候也需要我们自己输入一些信息,为了方便使用我们直接重载一下<<,将我们自己的信息输入进_loginfo中
template <typename T>
LogMessage &operator<<(const T &info) {
// a = b = c =d;
// 日志的右半部分,可变的
std::stringstream ss;
ss << info;
_loginfo += ss.str();
return *this;
}
这里的template <typename T>,表示我们info可以是任意类型的数据,后面都会转成字符串。
比如:
LogMessage() << "hell world" << "XXXX" << 3.14 << 1234
当LogMessage的对象结束时,我们在它的析构函数中调用父类的打印方法
~LogMessage() {
if (_logger._fflush_strategy) {
_logger._fflush_strategy->SyncLog(_loginfo);
}
}
整体代码:
class Logger
{
public:
Logger() {
EnableConsoleLogStrategy();
}
void EnableFileLogStrategy() {
_fflush_strategy = std::make_unique<FileLogStrategy>();
}
void EnableConsoleLogStrategy() {
_fflush_strategy = std::make_unique<ConsoleLogStrategy>();
}
// 表示的是未来的一条日志
class LogMessage
{
public:
LogMessage(LogLevel &level, std::string &src_name, int line_number, Logger &logger)
: _curr_time(GetTimeStamp()),
_level(level),
_pid(getpid()),
_src_name(src_name),
_line_number(line_number),
_logger(logger) {
// 日志的左边部分,合并起来
std::stringstream ss;
ss << "[" << _curr_time << "] "
<< "[" << Level2Str(_level) << "] "
<< "[" << _pid << "] "
<< "[" << _src_name << "] "
<< "[" << _line_number << "] "
<< "- ";
_loginfo = ss.str();
}
// LogMessage() << "hell world" << "XXXX" << 3.14 << 1234
template <typename T>
LogMessage &operator<<(const T &info) {
// a = b = c =d;
// 日志的右半部分,可变的
std::stringstream ss;
ss << info;
_loginfo += ss.str();
return *this;
}
~LogMessage() {
if (_logger._fflush_strategy) {
_logger._fflush_strategy->SyncLog(_loginfo);
}
}
private:
std::string _curr_time;
LogLevel _level;
pid_t _pid;
std::string _src_name;
int _line_number;
std::string _loginfo;
// 合并之后,一条完整的信息
Logger &_logger;
};
// 这里故意写成返回临时对象
LogMessage operator()(LogLevel level, std::string name, int line) {
return LogMessage(level, name, line, *this);
}
~Logger() {
}
private:
std::unique_ptr<LogStrategy> _fflush_strategy;
};
这里我们直接使用只能指针,在日志系统中使用 unique_ptr 的好处是自动管理策略对象内存、支持安全的策略切换、明确所有权、避免内存泄漏,使代码更安全、简洁.
宏定义
为了简化使用很多时候我们在使用operator()的时候不需要自己输入文件名和行号,所以我们定义一个宏
Logger logger;
#define LOG(level) logger(level, __FILE__, __LINE__)
#define Enable_Console_Log_Strategy() logger.EnableConsoleLogStrategy()
#define Enable_File_Log_Strategy() logger.EnableFileLogStrategy()
日志整体代码
#ifndef __LOG_HPP__
#define __LOG_HPP__
#include <iostream>
#include <cstdio>
#include <string>
#include <filesystem> //C++17
#include <sstream>
#include <fstream>
#include <memory>
#include <ctime>
#include <unistd.h>
#include "mutex.hpp"
namespace LogModule
{
using namespace MutexModule;
const std::string gsep = "\r\n";
// 策略模式,C++多态特性
// 2. 刷新策略 a: 显示器打印 b:向指定的文件写入
// 刷新策略基类
class LogStrategy
{
public:
~LogStrategy() = default;
virtual void SyncLog(const std::string &message) = 0;
};
// 显示器打印日志的策略 : 子类
class ConsoleLogStrategy : public LogStrategy
{
public:
ConsoleLogStrategy()
{
}
void SyncLog(const std::string &message) override
{
LockGuard lockguard(_mutex);
std::cout << message << gsep;
}
~ConsoleLogStrategy()
{
}
private:
Mutex _mutex;
};
// 文件打印日志的策略 : 子类
const std::string defaultpath = "./log";
const std::string defaultfile = "my.log";
class FileLogStrategy : public LogStrategy
{
public:
FileLogStrategy(const std::string &path = defaultpath, const std::string &file = defaultfile)
: _path(path),
_file(file)
{
LockGuard lockguard(_mutex);
if (std::filesystem::exists(_path))
{
return;
}
try
{
std::filesystem::create_directories(_path);
}
catch (const std::filesystem::filesystem_error &e)
{
std::cerr << e.what() << '\n';
}
}
void SyncLog(const std::string &message) override
{
LockGuard lockguard(_mutex);
std::string filename = _path + (_path.back() == '/' ? "" : "/") + _file; // "./log/" + "my.log"
std::ofstream out(filename, std::ios::app); // 追加写入的 方式打开
if (!out.is_open())
{
return;
}
out << message << gsep;
out.close();
}
~FileLogStrategy()
{
}
private:
std::string _path; // 日志文件所在路径
std::string _file; // 日志文件本身
Mutex _mutex;
};
// 形成一条完整的日志&&根据上面的策略,选择不同的刷新方式
// 1. 形成日志等级
enum class LogLevel
{
DEBUG,
INFO,
WARNING,
ERROR,
FATAL
};
std::string Level2Str(LogLevel level)
{
switch (level)
{
case LogLevel::DEBUG:
return "DEBUG";
case LogLevel::INFO:
return "INFO";
case LogLevel::WARNING:
return "WARNING";
case LogLevel::ERROR:
return "ERROR";
case LogLevel::FATAL:
return "FATAL";
default:
return "UNKNOWN";
}
}
std::string GetTimeStamp()
{
time_t curr = time(nullptr);
struct tm curr_tm;
localtime_r(&curr, &curr_tm);
char timebuffer[128];
snprintf(timebuffer, sizeof(timebuffer),"%4d-%02d-%02d %02d:%02d:%02d",
curr_tm.tm_year+1900,
curr_tm.tm_mon+1,
curr_tm.tm_mday,
curr_tm.tm_hour,
curr_tm.tm_min,
curr_tm.tm_sec
);
return timebuffer;
}
// 1. 形成日志 && 2. 根据不同的策略,完成刷新
class Logger
{
public:
Logger()
{
EnableConsoleLogStrategy();
}
void EnableFileLogStrategy()
{
_fflush_strategy = std::make_unique<FileLogStrategy>();
}
void EnableConsoleLogStrategy()
{
_fflush_strategy = std::make_unique<ConsoleLogStrategy>();
}
// 表示的是未来的一条日志
class LogMessage
{
public:
LogMessage(LogLevel &level, std::string &src_name, int line_number, Logger &logger)
: _curr_time(GetTimeStamp()),
_level(level),
_pid(getpid()),
_src_name(src_name),
_line_number(line_number),
_logger(logger)
{
// 日志的左边部分,合并起来
std::stringstream ss;
ss << "[" << _curr_time << "] "
<< "[" << Level2Str(_level) << "] "
<< "[" << _pid << "] "
<< "[" << _src_name << "] "
<< "[" << _line_number << "] "
<< "- ";
_loginfo = ss.str();
}
// LogMessage() << "hell world" << "XXXX" << 3.14 << 1234
template <typename T>
LogMessage &operator<<(const T &info)
{
// a = b = c =d;
// 日志的右半部分,可变的
std::stringstream ss;
ss << info;
_loginfo += ss.str();
return *this;
}
~LogMessage()
{
if (_logger._fflush_strategy)
{
_logger._fflush_strategy->SyncLog(_loginfo);
}
}
private:
std::string _curr_time;
LogLevel _level;
pid_t _pid;
std::string _src_name;
int _line_number;
std::string _loginfo; // 合并之后,一条完整的信息
Logger &_logger;
};
// 这里故意写成返回临时对象
LogMessage operator()(LogLevel level, std::string name, int line)
{
return LogMessage(level, name, line, *this);
}
~Logger()
{
}
private:
std::unique_ptr<LogStrategy> _fflush_strategy;
};
// 全局日志对象
Logger logger;
// 使用宏,简化用户操作,获取文件名和行号
#define LOG(level) logger(level, __FILE__, __LINE__)
#define Enable_Console_Log_Strategy() logger.EnableConsoleLogStrategy()
#define Enable_File_Log_Strategy() logger.EnableFileLogStrategy()
}
#endif
使用日志
演示代码:
#include<iostream>
#include"log.hpp"
using namespace std;
using namespace LogModule;
int main()
{
Enable_Console_Log_Strategy();
LOG(LogLevel::DEBUG) << "hello world" << 3.14 << 1234;
LOG(LogLevel::INFO) << "hello world" << 3.14 << 1234;
LOG(LogLevel::WARNING) << "hello world" << 3.14 << 1234;
}
演示结果:
root@hcss-ecs-f59a:/gch/code/HaoHao/learn3/day2# g++ -o exe main.cc -std=c++17
root@hcss-ecs-f59a:/gch/code/HaoHao/learn3/day2# ./exe
[2025-09-13 11:25:36] [DEBUG] [5528] [main.cc] [8] - hello world3.141234
[2025-09-13 11:25:36] [INFO] [5528] [main.cc] [9] - hello world3.141234
[2025-09-13 11:25:36] [WARNING] [5528] [main.cc] [10] - hello world3.141234
Linux日志系统实现与应用

被折叠的 条评论
为什么被折叠?



