I want to create log over a loop. Here is a simple exampe:
#include <iostream>
#include "logging.hpp"
int main(int argc,char* argv[]){
for(int i=1; i<argc; i++)
Logger l(argv[i]);
return 0;
}
And this is logging.hpp
file content :
#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);
}
};
it compiles with no problem but when I run program with more than one arguments it occurs error:
[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)
The very quick and dirty fix is to do:
This will ensure there is only ever one instance, as the initializer is executed exactly once.