【发布时间】:2020-10-14 16:44:07
【问题描述】:
我想通过循环创建日志。这是一个简单的例子:
#include <iostream>
#include "logging.hpp"
int main(int argc,char* argv[]){
for(int i=1; i<argc; i++)
Logger l(argv[i]);
return 0;
}
这是logging.hpp文件内容:
#include "spdlog/spdlog.h"
#include "spdlog/sinks/stdout_color_sinks.h"
#include <iostream>
class Logger{
public:
Logger(std::string msg){
auto console = spdlog::stdout_color_mt("console");
console->info(msg);
}
};
它编译没有问题,但是当我运行带有多个参数的程序时,它会发生错误:
[2020-10-14 20:12:30.067] [console] [info] .<first-argument>
terminate called after throwing an instance of 'spdlog::spdlog_ex'
what(): logger with name 'console' already exists
Aborted (core dumped)
【问题讨论】:
-
我认为错误信息很明确:您只能创建一个实例。例如,您可以创建
console记录器static。这将确保整个程序中只有一个名为“console”的实例 -
@Botje 你知道解决方案是什么吗?
-
您是在尝试将多条消息记录到
ONE日志,还是创建多个日志并为每个日志写入一条消息(就像您现在所做的那样)?