欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 汽车 > 维修 > C++【日志模块中的writer类】前文中 循环队列用法

C++【日志模块中的writer类】前文中 循环队列用法

2025/6/14 12:41:42 来源:https://blog.csdn.net/weixin_45397344/article/details/144032596  浏览:    关键词:C++【日志模块中的writer类】前文中 循环队列用法

用到前文中的循环队列模板

/*
**  File name:   LogWriter.h
**  Author:      
**  Date:        2024-11-4
**  Brief:       日志写入类
**  Note:        日志写入类,负责将日志写入文件和连接客户端。
**  Copyright (C) 1392019713@qq.com All rights reserved.
*/
#pragma once#include "../../Algorithm/Include/LoopQueue.h"
#include "../../System/Include/Singleton.h"
#include "LogStream.h"
#include <string>
#include <thread>class CByteArray
{
public:CByteArray();~CByteArray();public:std::string m_strMsg;
};class CLogWriter
{DECLARE_UNMANGED_SINGLETON_X(CLogWriter)
private:CLogWriter();public:~CLogWriter();bool WriteLog(const CByteArray& rByteArray);void SetRunning(bool bIsRunning);bool IsRunning() const;void StartWriteFileThread();void StartWriteRemoteThread();static void WriteFileThreadFunc(CLogWriter* pLogWriter);static void WriteRemoteThreadFunc(CLogWriter* pLogWriter);private:std::unique_ptr<std::thread> m_pWriteFileThread;CLoopQueue<CByteArray> m_writeFileloopQueue;std::unique_ptr<std::thread> m_pWriteRemoteThread;CLoopQueue<CByteArray> m_writeRemoteloopQueue;bool m_bIsRunning;
};
#include "../Include/LogWriter.h"
#include <fstream>IMPLEMENT_UNMANED_SINGLETON_X(CLogWriter)CByteArray::CByteArray()
{}CByteArray::~CByteArray()
{}/// /CLogWriter::CLogWriter()
{m_bIsRunning = true;m_writeFileloopQueue.Create(100);
}CLogWriter::~CLogWriter()
{m_bIsRunning = false;m_pWriteFileThread->join();//m_pWriteRemoteThread->join();
}bool CLogWriter::WriteLog(const CByteArray& rByteArray)
{bool bRet = m_writeFileloopQueue.EnQueue(rByteArray);if (!bRet){return false;}bRet = m_writeRemoteloopQueue.EnQueue(rByteArray);if (!bRet){return false;}return true;
}void CLogWriter::SetRunning(bool bIsRunning)
{m_bIsRunning = bIsRunning;
}bool CLogWriter::IsRunning() const
{return m_bIsRunning;
}void CLogWriter::StartWriteFileThread()
{m_pWriteFileThread = std::make_unique<std::thread>(&CLogWriter::WriteFileThreadFunc, this);
}void CLogWriter::StartWriteRemoteThread()
{}void CLogWriter::WriteFileThreadFunc(CLogWriter* pLogWriter)
{while (pLogWriter->IsRunning()){if(pLogWriter->m_writeFileloopQueue.GetSize() <= 0){std::this_thread::sleep_for(std::chrono::milliseconds(10));continue;}CByteArray byteArray;if(!pLogWriter->m_writeFileloopQueue.DeQueue(byteArray)){continue;}if (!CLogSystem::Instance().IsSaveToFile()){continue;}//if (std::filesystem::exists(logfilepath))//{//	auto fileSize = GetFileSize(logfilepath);//	if (fileSize > static_cast<size_t>(5 * 1024) * 1024)//	{//		strftime(tdatetime, sizeof(tdatetime), "%Y%m%d%H%M%S", &now_tm);//		auto newlogfilepath = "test.log" + std::string(tdatetime)).u8string();//		std::filesystem::rename(logfilepath, newlogfilepath);//	}//}std::ofstream file(CLogSystem::Instance().GetFileName(), std::ios::app);if (!file.is_open()){pLogWriter->SetRunning(false);break;}file << byteArray.m_strMsg;}
}void CLogWriter::WriteRemoteThreadFunc(CLogWriter* pLogWriter)
{while (pLogWriter->IsRunning()){}
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词