齐齐哈尔建设局网站为什么网站浏览不是做的那样

张小明 2026/1/10 7:28:27
齐齐哈尔建设局网站,为什么网站浏览不是做的那样,建电子商务网站注意事项,营销页面设计综合我之前学过的异步日志库#xff0c;流的缓冲区以及TensorRT里sample的日志设计。总结出了一套流式异步日志。 参考文章#xff1a; C笔记#xff1a;实现小型日志系统-CSDN博客 TensorRT笔记#xff08;2#xff09;#xff1a;解析样例中Logger日志类的设计-CSDN…综合我之前学过的异步日志库流的缓冲区以及TensorRT里sample的日志设计。总结出了一套流式异步日志。参考文章C笔记实现小型日志系统-CSDN博客TensorRT笔记2解析样例中Logger日志类的设计-CSDN博客C笔记std::stringbuf_修改std::string的缓存区-CSDN博客异步日志这部分和小型日志系统那块基本一样//MyLogger.h #pragma once #includethread #include mutex #include condition_variable #include atomic #include queue #includeiostream #includefstream #includesstream #includechrono #include map enum class LogLevel { INFO, DEBUG, WARN, ERR, }; class LogQueue { public: void push(const std::string msg); bool pop(std::string msg); void shutdown();//关闭 private: std::queuestd::string queue; std::mutex mtx; std::condition_variable cond_var; std::atomicbool is_shutdown false; }; class LogSystem { public: static LogSystem GetInstance() { static LogSystem instance; return instance; } ~LogSystem(); void log(const LogLevel level, const std::string msg) { //加入时间 auto now std::chrono::system_clock::to_time_t(std::chrono::system_clock::now()); std::tm tm; localtime_s(tm, now); std::ostringstream ts; ts std::put_time(tm, %Y-%m-%d %H:%M:%S); //根据格式化构造写入的字符串 std::string result [ ts.str() ][ level_string[level] ] msg; log_queue.push(result); } private: LogSystem(); LogSystem(const LogSystem) delete; LogSystem operator(const LogSystem) delete; std::mapLogLevel, std::string level_string; LogQueue log_queue; std::thread work_thread; std::ofstream log_file; std::atomicbool exit_flagfalse; inline static const char* filename Log.txt; };//MyLogger.cpp #include MyLogger.h void LogQueue::push(const std::string msg) { { std::lock_guardstd::mutex lock(mtx); if (is_shutdown) { throw std::runtime_error(LogQueue has been shut down!); } queue.push(msg); } cond_var.notify_one(); } bool LogQueue::pop(std::string msg) { { std::unique_lockstd::mutex lock(mtx); cond_var.wait(lock, [this]() { return !queue.empty() || is_shutdown; }); if (is_shutdown queue.empty()) return false; msg queue.front(); queue.pop(); } return true; } void LogQueue::shutdown() { is_shutdown true; cond_var.notify_all(); } LogSystem::~LogSystem() { exit_flag true; log_queue.shutdown(); if (work_thread.joinable()) { work_thread.join(); } if (log_file.is_open()) { log_file.close(); } } LogSystem::LogSystem() :log_file(filename, std::ios::out | std::ios::app) { if (!log_file.is_open()) { throw std::runtime_error(Failed to open log file); } level_string { {LogLevel::INFO,info}, {LogLevel::DEBUG,DEBUG}, {LogLevel::WARN,WARN}, {LogLevel::ERR,ERROR} }; work_thread std::thread([this]() { std::string msg; while (log_queue.pop(msg)) { //这里就不打换行了默认流里面会有 log_file msg std::flush; } }); }流式设计这一块种缓冲区和stringbuf里的示例也基本一样流的设计参考TensorRT的设计class LogBuffer :public std::stringbuf { public: explicit LogBuffer(LogLevel level):mLevel(level) { } ~LogBuffer() { if (pbase() ! pptr()) { putOutput(); } } int sync()override { putOutput(); return 0; } private: LogBuffer(const LogBuffer) delete; LogBuffer operator(const LogBuffer) delete; void putOutput() { //关键这里丢给异步日志 LogSystem::GetInstance().log(mLevel, this-str()); this-str(); } LogLevel mLevel; }; class LogStream :public std::ostream { public: explicit LogStream(LogLevel level) : std::ostream(nullptr), buf(level) { rdbuf(buf); } private: LogBuffer buf; }; //最后定义了这四个日志等级的宏 #define LOG_INFO LogStream(LogLevel::INFO) #define LOG_DEBUG LogStream(LogLevel::DEBUG) #define LOG_WARN LogStream(LogLevel::WARN) #define LOG_ERR LogStream(LogLevel::ERR)rdbuf// 获取当前 streambuf std::streambuf* rdbuf() const; // 设置新的 streambuf返回旧的 std::streambuf* rdbuf(std::streambuf* sb);使用测试int main() { auto f []() { LOG_INFO hello 1 std::endl; }; std::thread t1(f), t2(f); t1.join(); t2.join(); }[2025-12-14 16:57:09][info] hello1 [2025-12-14 16:57:09][info] hello1设计思想为什么要做成流式这个其实没有为什么用C笔记实现小型日志系统-CSDN博客里面的函数接口依然能做到异步日志。做成流只是为了好看。当然实际上还是方便了一点不用每次都选择日志等级而是直接使用对应的宏即可。并且如果要获取__FILE____LINE__等信息宏因为不会设计函数调用过程也更精准。宏的设计这里比较关键我们写的这几个宏对应的日志流对象都是临时的。作用域结束后/或者接收了std::endl等就会把内容丢到日志队列里去。为什么不做成全局的日志对象这是最关键的问题。答案是不能这样做。如果做成像std::cout那样的全局对象。那么多个线程同时用流的方式往里写就会出问题。因为都是对同一个流对象写那就会出现混乱对操作加锁就像TensorRT那样。答案也是不行。因为加锁只能保证一次的原子性。不能保证多线程的顺序性多线程之间的数据还是会混杂。如果要对整个流对象加锁那反而丢失了性能完全不如临时流对象的效果。临时流对象之间是没有任何约束的只有在push到日志队列的时候会竞争一下队列的锁。
版权声明:本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!

音乐网站开发模板有关应用网站

构建可信AI系统:Kotaemon的答案溯源机制详解 在金融、医疗和法律等高风险领域,一个AI助手随口说出的“年化收益率为5%”可能带来百万级的投资决策偏差。当企业开始将大模型引入核心业务流程时,人们不再满足于“回答得快”,而是迫切…

张小明 2026/1/4 12:02:07 网站建设

慈溪专业做网站公司软件著作权交易平台

Kubernetes Pod 垂直扩缩容实战指南:从重启到无重启 在 Kubernetes 中,Pod 的 CPU 和内存资源(resources.requests 与 resources.limits)通常在创建时就确定,后续调整往往意味着 Pod 重启,这在对高可用、低中断的业务场景中并不理想。 从 Kubernetes 1.27 开始,官方引…

张小明 2026/1/6 10:41:51 网站建设

免费网站空间 评测网站上的淘客组件是怎样做的

在移动办公成为常态的今天,如何快速将Android手机的移动网络共享给Mac电脑是许多用户的迫切需求。HoRNDIS驱动程序正是为此而生的专业解决方案,它能通过USB数据线将Android设备变身为网络适配器,为macOS用户提供稳定可靠的网络连接。 【免费下…

张小明 2026/1/8 8:05:56 网站建设

网络推广策划案范文5篇seo和网络推广有什么区别

Fritzing:零基础也能玩转电子设计的革命性工具 【免费下载链接】fritzing-app Fritzing desktop application 项目地址: https://gitcode.com/gh_mirrors/fr/fritzing-app 还在为复杂的电路图头疼不已吗?想要像搭积木一样轻松设计电路吗&#xff…

张小明 2026/1/9 5:45:04 网站建设