Boost之log日志使用
不讲理论,直接上在程序中可用代码:
一、引入Boost模块
开发环境:Visual Studio 2017
Boost库版本:1.68.0
安装方式:Nuget
安装命令:
#只安装下面几个即可
Install-package boost -version 1.68.0
Install-package boost_filesystem-vc141 -version 1.68.0
Install-package boost_log_setup-vc141 -version 1.68.0
Install-package boost_log-vc141 -version 1.68.0#这里是其他模块,可不安装
Install-package boost_atomic-vc141 -version 1.68.0
Install-package boost_chrono-vc141 -version 1.68.0
Install-package boost_date_time-vc141 -version 1.68.0
Install-package boost_system-vc141 -version 1.68.0
Install-package boost_thread-vc141 -version 1.68.0
Install-package boost_locale-vc141 -version 1.68.0
调用Nuget控制台:
二、引入下面两个hpp文件
boost_logger.hpp
#pragma once#include <string>
#include <fstream>
#include <iostream>
#include <boost/filesystem.hpp>
#include <boost/smart_ptr/shared_ptr.hpp>
#include <boost/core/null_deleter.hpp>
#include <boost/log/core.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/sinks/async_frontend.hpp>
#include <boost/log/sinks/text_ostream_backend.hpp>
#include <boost/log/sources/record_ostream.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/support/date_time.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include <boost/log/sources/severity_logger.hpp>
#include <boost/log/attributes/current_thread_id.hpp>
#include <boost/log/attributes/current_process_name.hpp>
#include <boost/log/attributes/attribute.hpp>
#include <boost/log/attributes/attribute_cast.hpp>
#include <boost/log/attributes/attribute_value.hpp>
#include <boost/log/sinks/async_frontend.hpp>// Related headersQDebug
#include <boost/log/sinks/unbounded_fifo_queue.hpp>
#include <boost/log/sinks/unbounded_ordering_queue.hpp>
#include <boost/log/sinks/bounded_fifo_queue.hpp>
#include <boost/log/sinks/bounded_ordering_queue.hpp>
#include <boost/log/sinks/drop_on_overflow.hpp>
#include <boost/log/sinks/block_on_overflow.hpp>//这里是logger的头文件,后面根据实际路径引入
#include "logger.hpp"//引入各种命名空间
namespace logging = boost::log;
namespace src = boost::log::sources;
namespace expr = boost::log::expressions;
namespace sinks = boost::log::sinks;
namespace keywords = boost::log::keywords;
namespace attrs = boost::log::attributes;
//建立日志源,支持严重属性
thread_local static boost::log::sources::severity_logger<log_level> lg;#define BOOST_LOG_Q_SIZE 1000//创建输出槽:synchronous_sink是同步前端,允许多个线程同时写日志,后端无需考虑多线程问题
typedef sinks::asynchronous_sink<sinks::text_file_backend, sinks::bounded_fifo_queue<BOOST_LOG_Q_SIZE, sinks::block_on_overflow>> sink_t;
static std::ostream &operator<<(std::ostream &strm, log_level level)
{static const char *strings[] ={"debug","info","warn","error","critical"};if (static_cast<std::size_t>(level) < sizeof(strings) / sizeof(*strings))strm << strings[level];elsestrm << static_cast<int>(level);return strm;
}
class boost_logger : public logger_iface
{
public:boost_logger(const std::string& dir) : m_level(log_level::error_level), dir(dir){}~boost_logger(){}/*** 日志初始化*/void init() override{//判断日志文件所在路径是否存在if (boost::filesystem::exists(dir) == false){boost::filesystem::create_directories(dir);}//添加公共属性logging::add_common_attributes();//获取日志库核心core = logging::core::get();//创建后端,并设值日志文件相关控制属性boost::shared_ptr<sinks::text_file_backend> backend = boost::make_shared<sinks::text_file_backend>(keywords::open_mode = std::ios::app, // 采用追加模式keywords::file_name = dir + "/%Y%m%d_%N.log", //归档日志文件名keywords::rotation_size = 10 * 1024 * 1024, //超过此大小自动建立新文件keywords::time_based_rotation = sinks::file::rotation_at_time_point(0, 0, 0), //每隔指定时间重建新文件keywords::min_free_space = 100 * 1024 * 1024 //最低磁盘空间限制);if (!_sink){_sink.reset(new sink_t(backend));//向日志源添加槽core->add_sink(_sink);}//添加线程ID公共属性core->add_global_attribute("ThreadID", attrs::current_thread_id());//添加进程公共属性core->add_global_attribute("Process", attrs::current_process_name());//设置过滤器_sink->set_filter(expr::attr<log_level>("Severity") >= m_level);// 如果不写这个,它不会实时的把日志写下去,而是等待缓冲区满了,或者程序正常退出时写下// 这样做的好处是减少IO操作,提高效率_sink->locked_backend()->auto_flush(true); // 使日志实时更新//这些都可在配置文件中配置_sink->set_formatter(expr::stream<< "["<< expr::attr<std::string>("Process") << ":" << expr::attr<attrs::current_thread_id::value_type>("ThreadID") << ":"<< expr::attr<unsigned int>("LineID") << "]["<< expr::format_date_time<boost::posix_time::ptime>("TimeStamp", "%Y-%m-%d %H:%M:%S.%f") << "]["<< expr::attr<log_level>("Severity") << "] "<< expr::smessage);}/*** 停止记录日志*/void stop() override{warn_log("boost logger stopping");_sink->flush();_sink->stop();core->remove_sink(_sink);}/*** 设置日志级别*/void set_log_level(log_level level) override{m_level = level;if (_sink){_sink->set_filter(expr::attr<log_level>("Severity") >= m_level);}}log_level get_log_level() override{return m_level;}void debug_log(const std::string &msg) override{BOOST_LOG_SEV(lg, debug_level) << msg << std::endl;}void info_log(const std::string &msg) override{BOOST_LOG_SEV(lg, info_level) << blue << msg << normal << std::endl;}void warn_log(const std::string &msg) override{BOOST_LOG_SEV(lg, warn_level) << yellow << msg << normal << std::endl;}void error_log(const std::string &msg) override{BOOST_LOG_SEV(lg, error_level) << red << msg << normal << std::endl;}void critical_log(const std::string &msg) override{BOOST_LOG_SEV(lg, critical_level) << red << msg << normal << std::endl;}private:log_level m_level;boost::shared_ptr<logging::core> core;boost::shared_ptr<sink_t> _sink;//日志文件路径const std::string& dir;
};
logger.hpp
#pragma once
#define BOOST_ALL_DYN_LINK#include <string> // std::string
#include <iostream> // std::cout
#include <fstream>
#include <sstream> // std::ostringstream
#include <memory>typedef std::basic_ostringstream<char> tostringstream;
static const char black[] = {0x1b, '[', '1', ';', '3', '0', 'm', 0};
static const char red[] = {0x1b, '[', '1', ';', '3', '1', 'm', 0};
static const char yellow[] = {0x1b, '[', '1', ';', '3', '3', 'm', 0};
static const char blue[] = {0x1b, '[', '1', ';', '3', '4', 'm', 0};
static const char normal[] = {0x1b, '[', '0', ';', '3', '9', 'm', 0};
#define ACTIVE_LOGGER_INSTANCE (*activeLogger::getLoggerAddr())
// note: this will replace the logger instace. If this is not the first time to set the logger instance.
// Please make sure to delete/free the old instance.
#define INIT_LOGGER(loggerImpPtr) \{ \ACTIVE_LOGGER_INSTANCE = loggerImpPtr; \ACTIVE_LOGGER_INSTANCE->init(); \}
#define CHECK_LOG_LEVEL(logLevel) (ACTIVE_LOGGER_INSTANCE ? ((ACTIVE_LOGGER_INSTANCE->get_log_level() <= log_level::logLevel##_level) ? true : false) : false)
#define SET_LOG_LEVEL(logLevel) \{ \if (ACTIVE_LOGGER_INSTANCE) \(ACTIVE_LOGGER_INSTANCE->set_log_level(log_level::logLevel##_level)); \}
#define DESTROY_LOGGER \{ \if (ACTIVE_LOGGER_INSTANCE) \{ \ACTIVE_LOGGER_INSTANCE->stop(); \delete ACTIVE_LOGGER_INSTANCE; \} \}enum log_level
{debug_level = 0,info_level,warn_level,error_level,critical_level
};class logger_iface
{
public:logger_iface(void) = default;virtual ~logger_iface(void) = default;logger_iface(const logger_iface &) = default;logger_iface &operator=(const logger_iface &) = default;public:virtual void init() = 0;virtual void stop() = 0;virtual void set_log_level(log_level level) = 0;virtual log_level get_log_level() = 0;virtual void debug_log(const std::string &msg) = 0;virtual void info_log(const std::string &msg) = 0;virtual void warn_log(const std::string &msg) = 0;virtual void error_log(const std::string &msg) = 0;virtual void critical_log(const std::string &msg) = 0;
};class activeLogger
{
public:static logger_iface **getLoggerAddr(){static logger_iface *activeLogger;return &activeLogger;}
};#define __LOGGING_ENABLED#ifdef __LOGGING_ENABLED
#define __LOG(level, msg) \\{ \tostringstream var; \var << "[" << __FILE__ << ":" << __LINE__ << ":" << __func__ << "] \n" \<< msg; \if (ACTIVE_LOGGER_INSTANCE) \ACTIVE_LOGGER_INSTANCE->level##_log(var.str()); \}
#else
#define __LOG(level, msg)
#endif /* __LOGGING_ENABLED */
三、使用样例
#include "logger/boost_logger.hpp"
#include "logger/simpleLogger.hpp"void testCustomLogger() {//初始化日志对象:日志路径后期从配置文件读取const std::string logDir = "E:\\log";INIT_LOGGER(new boost_logger(logDir));//设置过滤级别(可以读取配置文件)SET_LOG_LEVEL(debug);//输出各级别日志,(void *)ACTIVE_LOGGER_INSTANCE:代表激活的日志实例(可不写)__LOG(critical, "hello logger!"<< "this is critical log" << (void *)ACTIVE_LOGGER_INSTANCE);__LOG(debug, "hello logger!!!!!!!!!!!!!!!"<< "this is debug log");
}
相关文章:

Boost之log日志使用
不讲理论,直接上在程序中可用代码: 一、引入Boost模块 开发环境:Visual Studio 2017 Boost库版本:1.68.0 安装方式:Nuget 安装命令: #只安装下面几个即可 Install-package boost -version 1.68.0 Install…...

多功能jquery图片预览放大镜插件
xZoom是一款多功能的jquery图片预览放大镜插件。它支持多种图片放大模式,可以和Fancy Box或Magnific Pop-up等插件结合使用,功能非常强大。 在线预览 下载 使用方法 在页面中引入jquery和xzoom.css以及xzoom.js文件。 <link rel"stylesheet&…...
CSS系列(39)-- Shapes详解
前端技术探索系列:CSS Shapes详解 ✨ 致读者:探索形状布局的艺术 👋 前端开发者们, 今天我们将深入探讨 CSS Shapes,这个强大的形状布局特性。 基础形状 🚀 圆形与椭圆 /* 基础圆形 */ .circle {widt…...
AI 神经网络在智能家居场景中的应用
在科技持续进步的当下,智能家居领域正经历着深刻变革,AI 神经网络技术的融入成为推动这一变革的关键力量,为家居生活带来了诸多显著变化与提升,本文将几种常见的AI算法应用做了一下总结,希望对物联网从业者有所帮助。 …...
Rocky DEM tutorial7_Conical Dryer_锥形干燥器
tutorial 7_Conical Dryer_锥形干燥器 文章目录 tutorial 7_Conical Dryer_锥形干燥器0. 目的1. 模型介绍2. 模型设置2.1设置physics2.2 导入几何2.3 设置motion2.4 Boundary边界设置2.5 设置材料2.6设置材料间相互作用2.7 创建粒子2.8 设置颗粒进口2.9 求解器设置3. 后处理Enj…...
CSS(二):美化网页元素
目录 字体样式 文本样式 列表样式 背景图片 字体样式 字体相关的 CSS 属性: font-family:设置字体font-size:设置字体大小font-weight:设置字体的粗细(如 normal, bold, lighter 等)color:…...

平方根无迹卡尔曼滤波(SR-UKF)算法,用于处理三维非线性状态估计问题
本MATLAB 代码实现了平方根无迹卡尔曼滤波(SR-UKF)算法,用于处理三维非线性状态估计问题 文章目录 运行结果代码概述代码 运行结果 三轴状态曲线对比: 三轴误差曲线对比: 误差统计特性输出(命令行截图&…...

【论文笔记】Visual Alignment Pre-training for Sign Language Translation
🍎个人主页:小嗷犬的个人主页 🍊个人网站:小嗷犬的技术小站 🥭个人信条:为天地立心,为生民立命,为往圣继绝学,为万世开太平。 基本信息 标题: Visual Alignment Pre-tra…...
NLP基础知识 - 向量化
NLP基础知识 - 向量化 目录 NLP基础知识 - 向量化 NLP基础知识 - 向量化目录什么是向量化?为什么需要向量化?常见的向量化方法1. 词袋模型(Bag of Words, BoW)2. TF-IDF(词频-逆文档频率)3. 词嵌入&#x…...
JAVA学习笔记_MySQL进阶
文章目录 存储引擎InnoDB引擎MyISAM引擎Memory存储引擎的选择 索引索引数据结构Btree(多路平衡查找树)BTreeHash索引为什么InnoDQB存储引擎采用Btree索引结构 索引分类思考题 索引语法索引性能分析慢查询日志show profiesexplain 索引的使用规则最左前缀法则索引失效SQL提示覆盖…...

ffmpeg: stream_loop报错 Error while filtering: Operation not permitted
问题描述 执行ffmpeg命令的时候,报错:Error while filtering: Operation not permitted 我得命令如下 ffmpeg -framerate 25 -y -i /data/workerspace/mtk/work_home/mtk_202406111543-l9CSU91H1f1b3/tmp/%08d.png -stream_loop -1 -i /data/workerspa…...
Vue.use()和Vue.component()
当很多页面用到同一个组件,又不想每次都在局部注册时,可以在main.js 中全局注册 Vue.component()一次只能注册一个组件 import CcInput from /components/cc-input.vue Vue.component(CcInput);Vue.use()一次可以注册多个组件 对于自定义的组件&#…...

javaweb 04 springmvc
0.1 在上一次的课程中,我们开发了springbootweb的入门程序。 基于SpringBoot的方式开发一个web应用,浏览器发起请求 /hello 后 ,给浏览器返回字符串 “Hello World ~”。 其实呢,是我们在浏览器发起请求,请求了我们…...

[Visual studio] 性能探测器
最近发现VS的profile还是很好用的, 可以找到项目代码的瓶颈,比如发现CPU的每一个函数的时间占比,分析代码耗时分布,和火焰图一样的效果 如何使用 1. 打开你的项目,调整成release状态 2. 点击调试->性能探测器 3…...
【漫话机器学习系列】017.大O算法(Big-O Notation)
大 O 表示法(Big-O Notation) 大 O 表示法是一种用于描述算法复杂性的数学符号,主要用于衡量算法的效率,特别是随着输入规模增大时算法的运行时间或占用空间的增长趋势。 基本概念 时间复杂度 描述算法所需的运行时间如何随输入数…...

IntelliJ IDEA中设置激活的profile
在IntelliJ IDEA中设置激活的profile,可以通过以下步骤进行: 通过Run/Debug Configurations设置 打开Run/Debug Configurations对话框: 在IDEA的顶部菜单栏中,选择“Run”菜单,然后点击“Edit Configurations...”或者…...
Qt 的信号槽机制详解:之信号槽引发的 Segmentation Fault 问题拆析(上)
Qt 的信号槽机制详解:之因信号槽误用引发的 Segmentation Fault 问题拆析(上) 前言一. 信号与槽的基本概念信号(Signal)槽(Slot)连接信号与槽 二. 信号槽机制的实现原理元对象系统(M…...
uniapp中实现APP调用本地通知栏通知、震动、本地提示音或者mp3提醒
要在uniapp中实现APP调用本地通知栏通知、震动和本地提示音或者mp3提醒,你可以使用uni-app提供的原生API和插件来实现。 通知栏通知: 你可以使用uni-app的原生API uni.showToast() 或者 uni.showModal() 来实现通知栏通知的功能。可以在需要发送通知的地…...
ADB 上传文件并使用脚本监控上传百分比
有个需求,需要测试 emmc的外部连续写入性能,使用 ADB 上传一个巨大的文件。并且在上传到一定值时进行干预。 因此但是 adb push 命令本身会 block 运行并且不返回进度,因此需要一个额外的监控脚本。 上传脚本: echo off setloc…...
【数据库】PostgreSQL(持续更新)
目录 K8S 部署基本使用高级特性 K8S 部署 # pg.yaml --- apiVersion: v1 kind: PersistentVolume metadata:name: tv-postgres-pvnamespace: locallabels:storage: tv-postgres-pv spec:accessModes:- ReadWriteOncecapacity:storage: 50Gi # 按需修改,需要保持与…...
KubeSphere 容器平台高可用:环境搭建与可视化操作指南
Linux_k8s篇 欢迎来到Linux的世界,看笔记好好学多敲多打,每个人都是大神! 题目:KubeSphere 容器平台高可用:环境搭建与可视化操作指南 版本号: 1.0,0 作者: 老王要学习 日期: 2025.06.05 适用环境: Ubuntu22 文档说…...
React 第五十五节 Router 中 useAsyncError的使用详解
前言 useAsyncError 是 React Router v6.4 引入的一个钩子,用于处理异步操作(如数据加载)中的错误。下面我将详细解释其用途并提供代码示例。 一、useAsyncError 用途 处理异步错误:捕获在 loader 或 action 中发生的异步错误替…...

Spark 之 入门讲解详细版(1)
1、简介 1.1 Spark简介 Spark是加州大学伯克利分校AMP实验室(Algorithms, Machines, and People Lab)开发通用内存并行计算框架。Spark在2013年6月进入Apache成为孵化项目,8个月后成为Apache顶级项目,速度之快足见过人之处&…...

Cilium动手实验室: 精通之旅---20.Isovalent Enterprise for Cilium: Zero Trust Visibility
Cilium动手实验室: 精通之旅---20.Isovalent Enterprise for Cilium: Zero Trust Visibility 1. 实验室环境1.1 实验室环境1.2 小测试 2. The Endor System2.1 部署应用2.2 检查现有策略 3. Cilium 策略实体3.1 创建 allow-all 网络策略3.2 在 Hubble CLI 中验证网络策略源3.3 …...

Springcloud:Eureka 高可用集群搭建实战(服务注册与发现的底层原理与避坑指南)
引言:为什么 Eureka 依然是存量系统的核心? 尽管 Nacos 等新注册中心崛起,但金融、电力等保守行业仍有大量系统运行在 Eureka 上。理解其高可用设计与自我保护机制,是保障分布式系统稳定的必修课。本文将手把手带你搭建生产级 Eur…...

安宝特方案丨船舶智造的“AR+AI+作业标准化管理解决方案”(装配)
船舶制造装配管理现状:装配工作依赖人工经验,装配工人凭借长期实践积累的操作技巧完成零部件组装。企业通常制定了装配作业指导书,但在实际执行中,工人对指导书的理解和遵循程度参差不齐。 船舶装配过程中的挑战与需求 挑战 (1…...
智能AI电话机器人系统的识别能力现状与发展水平
一、引言 随着人工智能技术的飞速发展,AI电话机器人系统已经从简单的自动应答工具演变为具备复杂交互能力的智能助手。这类系统结合了语音识别、自然语言处理、情感计算和机器学习等多项前沿技术,在客户服务、营销推广、信息查询等领域发挥着越来越重要…...

基于Java+MySQL实现(GUI)客户管理系统
客户资料管理系统的设计与实现 第一章 需求分析 1.1 需求总体介绍 本项目为了方便维护客户信息为了方便维护客户信息,对客户进行统一管理,可以把所有客户信息录入系统,进行维护和统计功能。可通过文件的方式保存相关录入数据,对…...

【Linux手册】探秘系统世界:从用户交互到硬件底层的全链路工作之旅
目录 前言 操作系统与驱动程序 是什么,为什么 怎么做 system call 用户操作接口 总结 前言 日常生活中,我们在使用电子设备时,我们所输入执行的每一条指令最终大多都会作用到硬件上,比如下载一款软件最终会下载到硬盘上&am…...

五子棋测试用例
一.项目背景 1.1 项目简介 传统棋类文化的推广 五子棋是一种古老的棋类游戏,有着深厚的文化底蕴。通过将五子棋制作成网页游戏,可以让更多的人了解和接触到这一传统棋类文化。无论是国内还是国外的玩家,都可以通过网页五子棋感受到东方棋类…...