【C++ spdlog】C++ 日志库 spdlog 使用

1、在 https://github.com/gabime/spdlog 处下载源文件;

2、下载后解压,将得到以下文件,其中include文件夹里是所需的头文件及源码;

【C++ spdlog】C++ 日志库 spdlog 使用_第1张图片 【C++ spdlog】C++ 日志库 spdlog 使用_第2张图片

3、新建一个C++控制台应用程序项目spdlog-test,在项目属性页VC++目录-包含目录中添加上述include路径

【C++ spdlog】C++ 日志库 spdlog 使用_第3张图片

4、添加源文件 源.cpp,输入以下代码

#include 
#include 
#include "spdlog/spdlog.h"
#include "spdlog/sinks/rotating_file_sink.h"

using namespace std;
using namespace spdlog;

auto rotating_logger = rotating_logger_mt("mylog", "logs/rotating.txt", 1048576 * 5, 3);

int main(int, char *[])
{
	int a, b;
	a = 5;
	b = 3;
	float c = 0.1245;
	string s = "hello";
	cout << "a=" << a << " b=" << b << endl;
	
	rotating_logger->error("error!!!");
	rotating_logger->info("a = {},b={},a/b={},a%b={}", a, b, a/b,a%b);
	rotating_logger->info("c = {},s = {} ", c, s);
rotating_logger->flush();

	system("pause");

	return 1;
}

5、在源.cpp同级目录下创建logs文件夹

【C++ spdlog】C++ 日志库 spdlog 使用_第4张图片

6、运行程序,将在logs文件夹下生成rotating.txt日志文件,其内容为

【C++ spdlog】C++ 日志库 spdlog 使用_第5张图片

7、上述日志文件,仅在程序退出时才保存日志,如果要想在程序运行时也能够实时保存日志,可以在程序中添加以下语句

rotating_logger->flush();

参考:
https://github.com/gabime/spdlog
https://www.cnblogs.com/oucsheep/p/8426548.html
https://blog.csdn.net/yanxiaobugyunsan/article/details/79088533

你可能感兴趣的:(日志库)