C++开发基础之自定义异步日志库实现及性能测试

2024-09-07 08:36

本文主要是介绍C++开发基础之自定义异步日志库实现及性能测试,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

在这里插入图片描述

1. 前言

在软件开发中,日志记录是一个必不可少的部分。通过日志,我们可以记录系统的运行状态、错误信息以及调试数据。然而,当系统的日志量很大时,日志写入操作可能会影响系统的性能,尤其是在 I/O 操作较为频繁的情况下。因此,构建一个异步日志系统成为提升性能的重要手段。

在这篇博客中,我们将实现一个 C++ 异步日志库,支持日志级别分类和自定义文件路径、文件名等功能。同时,我们还会进行性能测试,评估异步日志系统的写入效率。


2. 异步日志系统的基本功能设计

1. 日志级别与日志结构

首先,我们需要定义日志的几种级别,并将其与日志消息、时间戳等信息封装在一起:

// 日志级别
enum class LogLevel {INFO,WARNING,ERROR,EXCEPTION
};// 日志项
struct LogEntry {std::string message;LogLevel level;std::string timestamp;
};

我们定义了四种常见的日志级别:INFO(信息)、WARNING(警告)、 ERROR(错误)和EXCEPTION(异常)。每条日志都包含一条消息、日志级别和时间戳。

2. 获取当前时间和日期

为了记录日志的时间,我们需要获取当前系统时间并将其转换为标准的可读格式:

#include <chrono>
#include <ctime>
#include <sstream>
#include <iomanip>// 获取当前时间的时间戳
std::string GetCurrentTimestamp() {auto now = std::chrono::system_clock::now();std::time_t now_time = std::chrono::system_clock::to_time_t(now);std::tm tm;localtime_s(&tm, &now_time);  std::stringstream ss;ss << std::put_time(&tm, "%Y-%m-%d %H:%M:%S");return ss.str();
}// 获取当前日期,用于日志文件命名
std::string GetCurrentDate() {auto now = std::chrono::system_clock::now();std::time_t now_time = std::chrono::system_clock::to_time_t(now);std::tm tm;localtime_s(&tm, &now_time);std::stringstream ss;ss << std::put_time(&tm, "%Y-%m-%d");return ss.str();
}

GetCurrentTimestamp() 函数用于获取精确到秒的时间戳,而 GetCurrentDate() 用于生成日志文件的日期,以便我们根据日期创建日志文件。

3. 异步写入日志实现

接下来,我们构建一个 MyLogger 类,负责处理日志的异步写入。为了避免阻塞主线程,我们将日志写入操作放在单独的线程中处理,并使用 std::queue 存储待写入的日志。

#include <iostream>
#include <fstream>
#include <string>
#include <thread>
#include <mutex>
#include <queue>
#include <condition_variable>class MyLogger {
public:MyLogger(const std::string& output_dir): output_dir_(output_dir), stop_flag_(false) {StartLoggingThread();}~MyLogger() {StopLoggingThread();}// 记录日志void Log(const std::string& message, LogLevel level) {std::lock_guard<std::mutex> lock(queue_mutex_);log_queue_.emplace(LogEntry{ message, level, GetCurrentTimestamp() });condition_.notify_one();  // 通知日志线程有新日志}private:std::string output_dir_;std::queue<LogEntry> log_queue_;std::mutex queue_mutex_;std::condition_variable condition_;bool stop_flag_;std::thread logging_thread_;// 启动日志线程void StartLoggingThread() {logging_thread_ = std::thread([this]() {while (!stop_flag_ || !log_queue_.empty()) {std::unique_lock<std::mutex> lock(queue_mutex_);condition_.wait(lock, [this]() {return !log_queue_.empty() || stop_flag_;});while (!log_queue_.empty()) {LogEntry entry = log_queue_.front();log_queue_.pop();lock.unlock();WriteLogToFile(entry);lock.lock();}}});}// 停止日志线程void StopLoggingThread() {{std::lock_guard<std::mutex> lock(queue_mutex_);stop_flag_ = true;}condition_.notify_one();if (logging_thread_.joinable()) {logging_thread_.join();}}// 写入日志到文件void WriteLogToFile(const LogEntry& entry) {std::string filename = output_dir_ + "/log_" + GetCurrentDate() + ".txt";std::ofstream log_file(filename, std::ios_base::app);if (log_file.is_open()) {log_file << "[" << entry.timestamp << "] "<< LogLevelToString(entry.level) << ": "<< entry.message << std::endl;}}
};

4. 调用示例

我们可以创建一个 MyLogger 实例,并随时向其中添加日志:

// MyLogApp.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//#include <iostream>
#include "MyLogger.h"
#include <string>
#include <thread>
#include <filesystem>namespace fs = std::filesystem;int main()
{// 指定日志文件的输出路径std::string log_output_dir = "./logs";if (!fs::exists(log_output_dir)) {fs::create_directory(log_output_dir);}// 创建Logger实例MyLogger logger(log_output_dir);// 记录不同级别的日志logger.Log("This is an INFO message", LogLevel::INFO);logger.Log("This is a WARNING message", LogLevel::WARNING);logger.Log("This is an ERROR message", LogLevel::ERROR);logger.Log("This is an EXCEPTION message", LogLevel::EXCEPTION);// 等待一会,保证日志异步写入std::this_thread::sleep_for(std::chrono::seconds(1));
}

在这个示例中,日志会被写入当前日期命名的文件中,例如 log_2024-09-06.txt。异步线程将自动处理日志写入,主线程不会因 I/O 操作而被阻塞。

[2024-09-06 15:07:02] INFO: This is an INFO message
[2024-09-06 15:07:02] WARNING: This is a WARNING message
[2024-09-06 15:07:02] ERROR: This is an ERROR message
[2024-09-06 15:07:02] EXCEPTION: This is an EXCEPTION message

3、日志库的性能测试

为了评估日志系统的性能,我们设计了一个简单的 Benchmark 测试。测试内容包括单次日志写入和批量日志写入的耗时。

1. 测试代码

我们使用 std::chrono 进行时间测量,并定义 LoggerBenchmark 类来测试性能:

#pragma once
#include "MyLogger.h"class MyLoggerBenchmark 
{
public:MyLoggerBenchmark(MyLogger& logger) : logger_(logger) {}// 单次日志写入测试void SingleLogTest();// 批量日志写入测试void BulkLogTest(int num_logs);
private:MyLogger& logger_;
};
#pragma once#include "MyLoggerBenchmark.h"// 单次日志写入测试
void MyLoggerBenchmark::SingleLogTest() {auto start = std::chrono::high_resolution_clock::now();logger_.Log("Single log test message", LogLevel::INFO);auto end = std::chrono::high_resolution_clock::now();auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count();std::cout << "Single log write took " << duration << " microseconds." << std::endl;
}// 批量日志写入测试
void MyLoggerBenchmark::BulkLogTest(int num_logs) {std::vector<std::string> messages;for (int i = 0; i < num_logs; ++i) {messages.push_back("Bulk log test message #" + std::to_string(i + 1));}auto start = std::chrono::high_resolution_clock::now();for (const auto& msg : messages) {logger_.Log(msg, LogLevel::INFO);}auto end = std::chrono::high_resolution_clock::now();auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();std::cout << "Bulk log write of " << num_logs << " logs took " << duration << " milliseconds." << std::endl;std::cout << "Average log write time: " << duration * 1000.0 / num_logs << " microseconds." << std::endl;
}

2. Benchmark 调用示例

// MyLogApp.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//#include <filesystem>
#include "MyLoggerBenchmark.h"using namespace std;
namespace fs = std::filesystem;int main()
{// 指定日志文件的输出路径std::string log_output_dir = "./logs";if (!fs::exists(log_output_dir)) {fs::create_directory(log_output_dir);}// 创建Logger实例MyLogger logger(log_output_dir);MyLoggerBenchmark benchmark(logger);// 单条日志写入测试benchmark.SingleLogTest();// 批量日志写入测试benchmark.BulkLogTest(100000);  // 写入 100000 条日志// 等待日志线程完成写入std::this_thread::sleep_for(std::chrono::seconds(2));
}

3. Benchmark 结果

我们通过批量写入 100000 条日志来测量日志系统的性能。输出如下:
日志文件记录

[2024-09-06 15:35:37] INFO: Single log test message
[2024-09-06 15:35:37] INFO: Bulk log test message #1
[2024-09-06 15:35:37] INFO: Bulk log test message #2
[2024-09-06 15:35:37] INFO: Bulk log test message #3
[2024-09-06 15:35:37] INFO: Bulk log test message #4
[2024-09-06 15:35:37] INFO: Bulk log test message #5
[2024-09-06 15:35:37] INFO: Bulk log test message #6
[2024-09-06 15:35:37] INFO: Bulk log test message #7
[2024-09-06 15:35:37] INFO: Bulk log test message #8
[2024-09-06 15:35:37] INFO: Bulk log test message #9
[2024-09-06 15:35:37] INFO: Bulk log test message #10
...
[2024-09-06 15:35:37] INFO: Bulk log test message #100000

控制台输出

Single log write took 2895 microseconds.
Bulk log write of 100000 logs took 1817 milliseconds.
Average log write time: 18.17 microseconds.

从结果中可以看出,异步日志系统在批量写入时效率较高,每条日志的平均写入时间大约为 18.17 微秒。


4. 总结

在本文中,我们实现了一个简单的 C++ 异步日志库,支持自定义日志文件命名、日志级别分类以及异步日志写入操作。通过测试,我们验证了异步日志系统在大量日志写入时的性能优势。

在这里插入图片描述

这篇关于C++开发基础之自定义异步日志库实现及性能测试的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1144589

相关文章

SpringBoot集成redisson实现延时队列教程

《SpringBoot集成redisson实现延时队列教程》文章介绍了使用Redisson实现延迟队列的完整步骤,包括依赖导入、Redis配置、工具类封装、业务枚举定义、执行器实现、Bean创建、消费... 目录1、先给项目导入Redisson依赖2、配置redis3、创建 RedissonConfig 配

Python的Darts库实现时间序列预测

《Python的Darts库实现时间序列预测》Darts一个集统计、机器学习与深度学习模型于一体的Python时间序列预测库,本文主要介绍了Python的Darts库实现时间序列预测,感兴趣的可以了解... 目录目录一、什么是 Darts?二、安装与基本配置安装 Darts导入基础模块三、时间序列数据结构与

基于 Cursor 开发 Spring Boot 项目详细攻略

《基于Cursor开发SpringBoot项目详细攻略》Cursor是集成GPT4、Claude3.5等LLM的VSCode类AI编程工具,支持SpringBoot项目开发全流程,涵盖环境配... 目录cursor是什么?基于 Cursor 开发 Spring Boot 项目完整指南1. 环境准备2. 创建

C++右移运算符的一个小坑及解决

《C++右移运算符的一个小坑及解决》文章指出右移运算符处理负数时左侧补1导致死循环,与除法行为不同,强调需注意补码机制以正确统计二进制1的个数... 目录我遇到了这么一个www.chinasem.cn函数由此可以看到也很好理解总结我遇到了这么一个函数template<typename T>unsigned

Python使用FastAPI实现大文件分片上传与断点续传功能

《Python使用FastAPI实现大文件分片上传与断点续传功能》大文件直传常遇到超时、网络抖动失败、失败后只能重传的问题,分片上传+断点续传可以把大文件拆成若干小块逐个上传,并在中断后从已完成分片继... 目录一、接口设计二、服务端实现(FastAPI)2.1 运行环境2.2 目录结构建议2.3 serv

C#实现千万数据秒级导入的代码

《C#实现千万数据秒级导入的代码》在实际开发中excel导入很常见,现代社会中很容易遇到大数据处理业务,所以本文我就给大家分享一下千万数据秒级导入怎么实现,文中有详细的代码示例供大家参考,需要的朋友可... 目录前言一、数据存储二、处理逻辑优化前代码处理逻辑优化后的代码总结前言在实际开发中excel导入很

SpringBoot+RustFS 实现文件切片极速上传的实例代码

《SpringBoot+RustFS实现文件切片极速上传的实例代码》本文介绍利用SpringBoot和RustFS构建高性能文件切片上传系统,实现大文件秒传、断点续传和分片上传等功能,具有一定的参考... 目录一、为什么选择 RustFS + SpringBoot?二、环境准备与部署2.1 安装 RustF

Nginx部署HTTP/3的实现步骤

《Nginx部署HTTP/3的实现步骤》本文介绍了在Nginx中部署HTTP/3的详细步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学... 目录前提条件第一步:安装必要的依赖库第二步:获取并构建 BoringSSL第三步:获取 Nginx

MyBatis Plus实现时间字段自动填充的完整方案

《MyBatisPlus实现时间字段自动填充的完整方案》在日常开发中,我们经常需要记录数据的创建时间和更新时间,传统的做法是在每次插入或更新操作时手动设置这些时间字段,这种方式不仅繁琐,还容易遗漏,... 目录前言解决目标技术栈实现步骤1. 实体类注解配置2. 创建元数据处理器3. 服务层代码优化填充机制详

Python实现Excel批量样式修改器(附完整代码)

《Python实现Excel批量样式修改器(附完整代码)》这篇文章主要为大家详细介绍了如何使用Python实现一个Excel批量样式修改器,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一... 目录前言功能特性核心功能界面特性系统要求安装说明使用指南基本操作流程高级功能技术实现核心技术栈关键函