spdlog/example/multisink.cpp

47 lines
1.9 KiB
C++
Raw Normal View History

#include "spdlog/sinks/basic_file_sink.h"
2018-04-20 07:03:36 -04:00
#include "spdlog/sinks/stdout_sinks.h"
2018-04-28 18:36:45 -04:00
#include "spdlog/spdlog.h"
2016-09-12 16:28:37 -04:00
#include <iostream>
#include <memory>
2018-03-09 08:26:33 -05:00
int main(int, char *[])
2016-09-12 16:28:37 -04:00
{
bool enable_debug = true;
try
{
// This other example use a single logger with multiple sinks.
// This means that the same log_msg is forwarded to multiple sinks;
// Each sink can have it's own log level and a message will be logged.
std::vector<spdlog::sink_ptr> sinks;
2018-03-09 08:26:33 -05:00
sinks.push_back(std::make_shared<spdlog::sinks::stdout_sink_mt>());
sinks.push_back(std::make_shared<spdlog::sinks::basic_file_sink_mt>("./log_regular_file.txt"));
sinks.push_back(std::make_shared<spdlog::sinks::basic_file_sink_mt>("./log_debug_file.txt"));
2016-09-12 16:28:37 -04:00
2018-03-09 08:26:33 -05:00
spdlog::logger console_multisink("multisink", sinks.begin(), sinks.end());
console_multisink.set_level(spdlog::level::warn);
2016-09-12 16:28:37 -04:00
2018-03-09 08:26:33 -05:00
sinks[0]->set_level(spdlog::level::trace); // console. Allow everything. Default value
sinks[1]->set_level(spdlog::level::trace); // regular file. Allow everything. Default value
sinks[2]->set_level(spdlog::level::off); // regular file. Ignore everything.
2016-09-12 16:28:37 -04:00
console_multisink.warn("warn: will print only on console and regular file");
2018-03-09 08:26:33 -05:00
if (enable_debug)
2016-09-12 16:28:37 -04:00
{
2018-03-09 08:26:33 -05:00
console_multisink.set_level(spdlog::level::debug); // level of the logger
sinks[1]->set_level(spdlog::level::debug); // regular file
sinks[2]->set_level(spdlog::level::debug); // debug file
2016-09-12 16:28:37 -04:00
}
console_multisink.debug("Debug: you should see this on console and both files");
// Release and close all loggers
spdlog::drop_all();
}
// Exceptions will only be thrown upon failed logger or sink construction (not during logging)
catch (const spdlog::spdlog_ex &ex)
2016-09-12 16:28:37 -04:00
{
std::cout << "Log init failed: " << ex.what() << std::endl;
return 1;
}
}