From d79af47a2852e27614c7087abadd13e6c5655637 Mon Sep 17 00:00:00 2001 From: davide Date: Mon, 12 Sep 2016 22:28:37 +0200 Subject: [PATCH] added example --- .gitignore | 1 + example/multisink.cpp | 47 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 example/multisink.cpp diff --git a/.gitignore b/.gitignore index 39b75483..5524d609 100644 --- a/.gitignore +++ b/.gitignore @@ -45,6 +45,7 @@ example/* !example/example.sln !example/example.vcxproj !example/CMakeLists.txt +!example/multisink.cpp # generated files generated diff --git a/example/multisink.cpp b/example/multisink.cpp new file mode 100644 index 00000000..fe6539b5 --- /dev/null +++ b/example/multisink.cpp @@ -0,0 +1,47 @@ +#include "spdlog/spdlog.h" + +#include +#include + +namespace spd = spdlog; +int main(int, char*[]) +{ + 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 sinks; + sinks.push_back( std::make_shared() ); + sinks.push_back( std::make_shared("./log_regular_file.txt") ); + sinks.push_back( std::make_shared("./log_debug_file.txt") ); + + spdlog::logger console_multisink("multisink", sinks.begin(), sinks.end() ); + console_multisink.set_level( spdlog::level::warn); + + 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. + + console_multisink.warn("warn: will print only on console and regular file"); + + if( enable_debug ) + { + 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 + } + 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 spd::spdlog_ex& ex) + { + std::cout << "Log init failed: " << ex.what() << std::endl; + return 1; + } +} +