基于C++ spdlog日志库的完善封装

2023-05-16

spdlog是一个C++编写的极速日志打印库,支持异步写日志以及多种模式和格式化选项。以下基于spdlog库封装了一个简单易用的功能类,采用的是header-only方式,便于项目集成。

代码

#pragma once
#include <iostream>
#include <string>
#include <memory>
#include <time.h>
#include <chrono>
#include "spdlog/spdlog.h"
#include "spdlog/async.h"
#include "spdlog/sinks/stdout_color_sinks.h" // or "../stdout_sinks.h" if no color needed
#include "spdlog/sinks/basic_file_sink.h"
#include "spdlog/sinks/rotating_file_sink.h"

static inline int NowDateToInt()
{
	time_t now;
	time(&now);

	// choose thread save version in each platform
	tm p;
#ifdef _WIN32
	localtime_s(&p, &now);
#else
	localtime_r(&now, &p);
#endif // _WIN32
	int now_date = (1900 + p.tm_year) * 10000 + (p.tm_mon + 1) * 100 + p.tm_mday;
	return now_date;
}

static inline int NowTimeToInt()
{
	time_t now;
	time(&now);
	// choose thread save version in each platform
	tm p;
#ifdef _WIN32
	localtime_s(&p, &now);
#else
	localtime_r(&now, &p);
#endif // _WIN32

	int now_int = p.tm_hour * 10000 + p.tm_min * 100 + p.tm_sec;
	return now_int;
}

class XLogger
{
public:
	static XLogger* getInstance()
	{
		static XLogger xlogger;
		return &xlogger;
	}

	std::shared_ptr<spdlog::logger> getLogger()
	{
		return m_logger;
	}

private:
	// make constructor private to avoid outside instance
	XLogger()
	{
		// hardcode log path
		const std::string log_dir = "./log"; // should create the folder if not exist
		const std::string logger_name_prefix = "test_";

		// decide print to console or log file
		bool console = false;

		// decide the log level
		std::string level = "debug";

		try
		{
			// logger name with timestamp
			int date = NowDateToInt();
			int time = NowTimeToInt();
			const std::string logger_name = logger_name_prefix + std::to_string(date) + "_" + std::to_string(time);

			if (console)
				m_logger = spdlog::stdout_color_st(logger_name); // single thread console output faster
			else
				//m_logger = spdlog::create_async<spdlog::sinks::basic_file_sink_mt>(logger_name, log_dir + "/" + logger_name + ".log"); // only one log file
				m_logger = spdlog::create_async<spdlog::sinks::rotating_file_sink_mt>(logger_name, log_dir + "/" + logger_name + ".log", 500 * 1024 * 1024, 1000); // multi part log files, with every part 500M, max 1000 files

			// custom format
			m_logger->set_pattern("%Y-%m-%d %H:%M:%S.%f <thread %t> [%l] [%@] %v"); // with timestamp, thread_id, filename and line number

            if (level == "trace")
			{
				m_logger->set_level(spdlog::level::trace);
				m_logger->flush_on(spdlog::level::trace);
			}
			else if (level == "debug")
			{
				m_logger->set_level(spdlog::level::debug);
				m_logger->flush_on(spdlog::level::debug);
			}
			else if (level == "info")
			{
				m_logger->set_level(spdlog::level::info);
				m_logger->flush_on(spdlog::level::info);
			}
			else if (level == "warn")
			{
				m_logger->set_level(spdlog::level::warn);
				m_logger->flush_on(spdlog::level::warn);
			}
			else if (level == "error")
			{
				m_logger->set_level(spdlog::level::err);
				m_logger->flush_on(spdlog::level::err);
			}
		}
		catch (const spdlog::spdlog_ex& ex)
		{
			std::cout << "Log initialization failed: " << ex.what() << std::endl;
		}
	}

	~XLogger()
	{
		spdlog::drop_all(); // must do this
	}

	void* operator new(size_t size)
	{}

	XLogger(const XLogger&) = delete;
	XLogger& operator=(const XLogger&) = delete;

private:
	std::shared_ptr<spdlog::logger> m_logger;
};

// use embedded macro to support file and line number
#define XLOG_TRACE(...) SPDLOG_LOGGER_CALL(XLogger::getInstance()->getLogger().get(), spdlog::level::trace, __VA_ARGS__)
#define XLOG_DEBUG(...) SPDLOG_LOGGER_CALL(XLogger::getInstance()->getLogger().get(), spdlog::level::debug, __VA_ARGS__)
#define XLOG_INFO(...) SPDLOG_LOGGER_CALL(XLogger::getInstance()->getLogger().get(), spdlog::level::info, __VA_ARGS__)
#define XLOG_WARN(...) SPDLOG_LOGGER_CALL(XLogger::getInstance()->getLogger().get(), spdlog::level::warn, __VA_ARGS__)
#define XLOG_ERROR(...) SPDLOG_LOGGER_CALL(XLogger::getInstance()->getLogger().get(), spdlog::level::err, __VA_ARGS__)

int main()
{
	// print log test, you can transfer any param to do format
	int param = 1;

	XLOG_TRACE("this is trace log record, param: {}", ++param); // int type param is ok
	XLOG_DEBUG("this is debug log record, param: {}", ++param);
	XLOG_INFO("this is info log record, param: {}", ++param);
	XLOG_WARN("this is warn log record, param: {}", double(++param)); // double type param is ok
	XLOG_ERROR("this is error log record, param: {}", std::to_string(++param)); // string type param is ok

	return 0;
}
  • 支持打印到控制台和文件两种方式
  • 采用单例模式,多种日志级别的宏调用封装
  • 格式化打印,尽可能的打印调试所需信息
  • 可以选择将每一行都立即刷新到文件,但要注意级别,尽量不要在info或者debug级别上开启,这个选项会导致日志刷新时间戳延迟

输出

例如,日志文件:test_20201230_154656.log

2020-12-30 15:46:56.681969 <thread 91920> [debug] [main.cpp:135] this is debug log record, param: 2
2020-12-30 15:46:56.681969 <thread 91920> [info] [main.cpp:136] this is info log record, param: 3
2020-12-30 15:46:56.682457 <thread 91920> [warning] [main.cpp:137] this is warn log record, param: 4
2020-12-30 15:46:56.682457 <thread 91920> [error] [main.cpp:138] this is error log record, param: 5

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

基于C++ spdlog日志库的完善封装 的相关文章

  • 双目立体视觉 I:标定和校正

    点击上方 AI公园 xff0c 关注公众号 xff0c 选择加 星标 或 置顶 作者 xff1a Ali Yasin Eser 编译 xff1a ronghuaiyang 导读 双目立体校正和标定 大家好 xff01 今天我们将讨论什么是立
  • 4_竞赛无人机基本自动飞行支持函数与导航控制函数解析——零基础学习竞赛无人机搭积木式编程

    竞赛无人机基本自动飞行支持函数与导航控制函数解析 基本自动飞行支持函数 void basic auto flight support void 根据前面几讲的介绍 xff0c 要想实现无人机的自动飞行 xff0c 单依靠姿态自稳 高度控制远
  • Ubuntu20.04/Ubuntu22.04 配置VScode+Opencv+cmake(C++)

    下面介绍Ubuntu20 04下安装opencv xff0c 当然Ubuntu22 04也适用 xff0c 然后将opencv链接到VsCode 先主体按照 gt 点我 xff1a 链接1 lt 的第一点进行安装 xff0c 但是特别注意
  • makefile和cmake

    目录 作用优点cmake 作用 makefile关系到了整个工程的编译规则 一个工程中的源文件不计其数 xff0c 其按类型 功能 模块分别放在若干个目录中 xff0c makefile定义了一系列的规则来指定 xff0c 哪些文件需要先编
  • Linux查看线程的堆栈信息

    1 使用top命令 xff0c 查找pid 2 显示线程 xff0c 查找线程tid ps mp pid o THREAD tid time sort rn 3 将线程id转化为16进制0xtid printf 34 x n 34 tid
  • MPU6050 简介

    目录 关于MPU6050芯片 关于小板 关于厂家和DATASHEET 关于漂移 关于角加速度还是角速度 关于精度和量程 xff08 可调 xff0c 可选 xff09 关于功耗 xff0c 陀螺仪 43 加速器工作电流 xff1a 3 8m
  • 银河麒麟V10操作系统安装putty和cutecom和网络调试助手(mNetAssist)

    银河麒麟V10操作系统安装putty和cutecom和网络调试助手 xff08 mNetAssist xff09 安装Putty 需要连接网络 sudo apt get install putty 安装Cutecom 需要连接网络 sudo
  • STM32串口中断接收和中断发送

    STM32串口USART1中断接收和中断发送 先贴出中断函数 void USART1 IRQHandler void if USART GetITStatus USART1 USART IT RXNE 61 RESET USART Clea
  • ICP(Iterative Closest Point迭代最近点)算法学习笔记

    背景 xff1a 博主从百度百科开始学习icp算法 xff0c 主要是后期加得学习笔记 xff08 红色部分 xff09 ICP算法 xff1a 以点集对点集 xff08 PSTPS xff09 配准方法为基础 xff0c 他们阐述了一种曲
  • linux设备上的Onvif 实现21:解决大华摄像头无法使用问题

    好长时间没有再写该系列文章了 xff0c 最近刚好摸索着解决了大华摄像头无法使用问题 xff0c 记录下来 xff0c 应该对其他博友有所帮助 之前虽然写了一大堆文章说明了如何使用gsoap连接摄像头 xff0c 但这是针对一台海康的摄像头
  • NRF24L01工作原理(发送接收通道地址)解读 图示

    NRF24L01工作原理 xff08 发送接收通道地址 xff09 解读 图示 NRF24l01工作原理 xff08 发送接收通道地址 xff09 网上说明不清晰 xff0c 特制作本说明 xff0c xff08 制作 xff1a 流浪的蛙
  • Realsense D435i 使用

    工作之后才发现问题不是单线程地来找你 xff0c 而是多线程并发地涌向你 D435i是一款良心传感器 xff0c 美中不足的是你拿不到广角图像 虽然现在不负责传感器测试了 xff08 老大布置什么 xff0c 打工的就去做什么就好了 xff
  • Kalibr标定d435i

    figure it out 最近准备使用realsense d435i xff0c 先对其进行标定 整体环境是基于ROS的 xff0c 因为Kalibr是在ROS环境下 大致过程如下 xff1a imu标定 gt 双目标定 gt 双目 43
  • cout函数未定义问题和函数隐形类型转换

    cout函数未定义的一些问题 xff1a 看上面的图片 xff0c 有个朋友说 xff0c 按照上面using和修改了iostream为什么还是显示未定呢 xff1f 到底怎么办呢 xff1f 解决 xff1a include lt ios

随机推荐

  • C++ primer plus

    https github com lilinxiong cppPrimerPlus six https github com lilinxiong cppPrimerPlus six 最近在看C 43 43 primer plus这本书 x
  • ROS编译过程中的错误

    一 catkin make编译错误 xff08 1 xff09 Could not find a package configuration file provided by 34 gazebo ros control 34 with an
  • 算法导论学习笔记-2

    第二章 xff1a 函数的增长 2 1渐近记号 记号 定义 xff1a g n 61 f n 存在正常数c1 c2和n0 xff0c 使对所有n n0 xff0c 有0 c1 g n f n c2 g n xff1b 满足该定义 xff0c
  • C++理论复习之运算符重载

    C 43 43 运算符重载 重载操作符的一般格式 xff1a Type operator 43 Type A Type B 可重载与不可重载的操作符列表 操作符重载的注意事项 重载操作符必须有一个类类型的操作数 xff0c 不能重载内置类型
  • 计算机网络概述

    OSI参考模型 国际标准化组织 I S O 开发了开放式系统互联 O S I 参考模型 xff0c 以促进计算机系统的开放互联 开放式互联就是可在多个厂家的环境中支持互联 很少有产品是完全的O S I 模式 xff1b 相反 xff0c 其
  • PCL:如何自定义一个点云PointT类型

    1 xff0c 使用基础点云类型 include 34 pcl point types h 34 include 34 pcl impl instantiate hpp 34 include 34 foo h 34 include 34 i
  • IP地址

    ip地址的分类 A类地址 设计IPv4 A类地址的目的是支持巨型网络 xff0c 因为对规模巨大网络的需求很小 xff0c 因此开发了这种结构使主机地址数很大 xff0c 而严格限制可被定义为A类网络的数量 一个A类I P地址仅使用第一个8
  • 数据存储大端小端模式的理解

    Big Endian和Little Endian的定义如下 xff1a 1 Little Endian就是低位字节排放在内存的低地址端 xff0c 高位字节排放在内存的高地址端 2 Big Endian 就是高位字节排放在内存的低地址端 x
  • cocos2dx实例开发之经典坦克

    小时候红白机上玩的的经典90坦克 xff0c 看起来简单 xff0c 做起来其实有点复杂 xff0c 这里用原版素材还原了一个简版 预览 工程结构 游戏架构 包括场景 xff1a 欢迎界面 xff0c 主菜单游戏场景 步骤 菜单场景 对于图
  • 数值分析C++:统计(均值/方差/偏度/峰度),积分,微分,蒙特卡罗

    用C 43 43 实现几个简单的数值分析计算 xff0c 以便深入理解计算机在求解代数问题的过程 原理 以下主要针对普通实数 xff0c 以及一元代数 统计 算数平均值 几何平均值 方差 偏度 峰度 积分 求解定积分的一般数学描述式 但是由
  • python脚本编程:监控指定进程的cpu和内存使用率

    为了测试某个服务的稳定性 xff0c 通常需要在服务长时间运行的情况下 xff0c 监控其资源消耗情况 xff0c 比如cpu和内存使用 这里借助python的psutil这个包可以很方便的监控指定进程号 xff08 PID xff09 的
  • python脚本编程:实时监控日志文件

    用python可以很小巧轻便的实时监控日志文件增量刷新 xff0c 根据某些关键字进行匹配 xff0c 方便做运维异常告警 代码 span class token keyword import span time span class to
  • C++获取机器启动至今的时长和机器启动的时间戳

    根据当前时间戳与机器启动至今的时间长度相减 xff0c 可以精确计算出机器启动时刻的时间戳epochtime 代码 span class token macro property span class token directive key
  • pytorch基于RNN实现文本情感分析并用C++加载模型预测

    文本情感分析是机器学习自然语言处理NLP中常见的应用场景 xff0c 给定一段文本 xff0c 识别其中的情绪或态度 xff0c 对其进行分类并标签化 这个手段可以应用于书籍电影评价 用户对产品满意度调查 人机对话感情色彩提取和金融研报分析
  • pytorch使用matplotlib和tensorboard实现模型和训练的可视化

    pytorch构建和训练深度学习模型的过程中 xff0c 往往需要能够直观的观测到可视化的过程 xff0c 比如画出训练曲线等 对于简单的曲线绘制可以使用matplotlib库做出基本的图 xff0c 如果需要更加高级的可视化过程 xff0
  • Eigen库:常见错误(最坑的库,没有之一)

    1 3rdparty eigen eigen3 eigen src core assignevaluator h 833 error C2338 YOU MIXED MATRICES OF DIFFERENT SIZES Eigen Mat
  • win10 资源管理器打开FTP站点跳到IE

    原文链接 xff1a https zhidao baidu com question 549827901 html 我也遇到一样的问题 xff0c 参考很多网页 xff0c 最好终于解决了 xff0c 我不能保证一定解决你的问题 xff0c
  • C++11写的一个简洁的单例类型模版包装器

    单例是经常用到的设计模式实践 xff0c 对于全局使用的唯一资源事例一般都把类型封装成单例 xff0c 但是有时候觉得对于每个class都要改造一遍单例会觉得比较繁琐 xff0c 尤其是在预先不知道哪些类型会使用为单例的时候 为了应对这种情
  • C++获取对应进程的cpu和内存使用情况(支持linux和windows)

    运维监控程序中经常需要根据一个进程号pid去监控实时的cpu和内存占用 xff0c 以下整理了一个C 43 43 实现的简单例子 xff0c 并封装为方便跨平台调用的函数 代码 span class token macro property
  • 基于C++ spdlog日志库的完善封装

    spdlog是一个C 43 43 编写的极速日志打印库 xff0c 支持异步写日志以及多种模式和格式化选项 以下基于spdlog库封装了一个简单易用的功能类 xff0c 采用的是header only方式 xff0c 便于项目集成 代码 p