c++高效、快速的日志类Spdlog

spdlog是一个非常快的日志类
*/

#include "spdlog/spdlog.h"
#include 
   
   
    
    
#include 
    
    
     
     

using std::cout;
using std::cin;
using std::endl;


void async_example();

namespace spd = spdlog;


int main(int argc,char * argv [ ])
{
	try{
		//创建标准输出日志
		auto console = spd::stdout_logger_mt("console");
		console->info("Welcome to spdlog!");
		console->error("Some error message with arg{}..", 1);

		//格式化输出的例子
		console->warn("Easy padding in numbers like{:08d}", 12);
		console->critical("Support for int: {0:d}; hex: {0:x}; oct:{0:o}; bin: {0:b}", 42);
		console->info("Support for floats {:03.2f}", 1.23456);
		console->info("Positinoal args are {1}{0}..", "too", "Supported");
		console->info("{:<30}", "left aligned");

		//可以用get从全局注册的地方获取想要的日志
		spd::get("console")->info("loggers can be retrieved from a global registry using the spdlog::get(logger_name) function");

		//创建日志文件
		auto rotating_logger = spd::rotating_logger_mt("some_logger_name","logs/mylogfile",1048576 *5,3);
		for (int i=0;i<10;i++){
			rotating_logger->info("{} * {} equals {:>10}", i, i, i*i);
		}

		//定时创建日志文件
		auto daily_logger = spd::daily_logger_mt("daily_logger","logs/daily",19,00);
		daily_logger->info(123.44);

		//自定义消息输出格式
		spd::set_pattern("----[%Y-%m-%d %H:%M:%S %l] [thread %t] %v ----");
		rotating_logger->info("This is another message with custom format");

		//运行日志级别
		spd::set_level(spd::level::info);
		console->debug("This message should not be diaplayed!");
		console->set_level(spd::level::debug);
		console->debug("This message should be displayed..");

		//异步模式非常快,只需调用spdlog::set_async_mode(q_size),然后创建的日志都将是异步的
		async_example();

		
	}
	catch (const spd::spdlog_ex& ex){
		cout << "Log init failed:" << ex.what() << endl;
		return 1;
	}

	return 0;
}

void async_example(){
	size_t q_size = 4096;//队列的大小必须是2的幂
	spd::set_async_mode(q_size);
	auto async_file = spd::daily_logger_st("async_file_logger", "logs/async_log.txt");
	for (int i=0;i<100;i++){
		async_file->info("Async message #{}", i);
	}
}

    
    
   
   
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

书灯

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值