I am using spdlog in a C ++ application, where I have created a class that builds a log pool, with variable content in the pool, which is not always the same, since it is done inside a function that is passed a parameter indicating if you want to have a log only in the console, only in one file or both simultaneously.
In other words, the pool can include a logger to the console plus a logger to a file or only one of the two.
OPTION 1: I have tried to get this functionality with this code, but it doesn't compile and it shows an error stating that it doesn't support push_back function:
std::shared_ptr<spdlog::logger> MyClass::loggerPull;
spdlog::sinks_init_list loggerList;
if (logToUse == USE_CONSOLE)
loggerList.push_back(myConsoleLog);
else if (logToUse == USE_FILE)
loggerList.push_back(myFileLog);
else
{
loggerList.push_back(myConsoleLog);
loggerList.push_back(myFileLog);
}
loggerPull = std::make_shared<spdlog::async_logger>("mylogger", loggerList.begin(), loggerList.end(), spdlog::thread_pool(), spdlog::async_overflow_policy::block);
OPTION 2: This code compiles and works:
std::shared_ptr<spdlog::logger> MyClass::loggerPull;
if (logToUse == LOG_TO_CONSOLE)
{
spdlog::sinks_init_list loggerList = { myConsoleLog };
loggerPull = std::make_shared<spdlog::async_logger>("mylogger", loggerList.begin(), loggerList.end(), spdlog::thread_pool(), spdlog::async_overflow_policy::block);
}
else if (logToUse == LOG_TO_FILE)
{
spdlog::sinks_init_list loggerList = { myFileLog };
loggerPull = std::make_shared<spdlog::async_logger>("mylogger", loggerList.begin(), loggerList.end(), spdlog::thread_pool(), spdlog::async_overflow_policy::block);
}
else
{
spdlog::sinks_init_list loggerList = { myConsoleLog, myFileLog };
loggerPull = std::make_shared<spdlog::async_logger>("mylogger", loggerList.begin(), loggerList.end(), spdlog::thread_pool(), spdlog::async_overflow_policy::block);
}
However, I would like to define the loggerList outside of the "if ... else if ..." and use some function similar to push_back or instert in order to add the necessary loggers to the list, similarly as done in OPTION 1.
Is there a function in spdlog that allows adding loggers to an empty loggerList list instead of creating and filling the list in the declaration of loggerList ?