Skip to content

Instantly share code, notes, and snippets.

@yohhoy

yohhoy/spdlog-templ.cpp

Last active Oct 28, 2019
Embed
What would you like to do?
template code for spdlog library
#include <memory>
#include <vector>
#define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_DEBUG
#include "spdlog/spdlog.h"
#include "spdlog/sinks/stdout_color_sinks.h"
#include "spdlog/sinks/daily_file_sink.h"
#include "spdlog/sinks/rotating_file_sink.h"
int main()
{
#if 1
// https://github.com/gabime/spdlog/wiki/4.-Sinks
std::vector<spdlog::sink_ptr> sinks = {
std::make_shared<spdlog::sinks::stdout_color_sink_mt>(),
std::make_shared<spdlog::sinks::daily_file_sink_mt>("daily.log", 23, 59), // 23:59
std::make_shared<spdlog::sinks::rotating_file_sink_mt>("rotate.log", 10*1024*1024, 5) // 10MB x 5files
};
spdlog::set_default_logger(std::make_shared<spdlog::logger>("multi_sink", begin(sinks), end(sinks)));
#else
// default logger = spdlog::sinks::stdout_color_sink_mt
// suffix "_mt" = multi-thread / "_st" = single-thread support.
#endif
// set loglevel at runtime
const auto loglevel = "info";
spdlog::set_level(spdlog::level::from_str(loglevel));
// https://github.com/gabime/spdlog/wiki/3.-Custom-formatting
spdlog::set_pattern("%+"); // spdlog default
spdlog::set_pattern("[%D %T] %^[%l]%$ %v");
SPDLOG_TRACE("trace val={}", 42); // disabled at compile time (see SPDLOG_ACTIVE_LEVEL)
SPDLOG_DEBUG("debug"); // disabled at runtime (see set_level)
SPDLOG_INFO("info");
SPDLOG_WARN("warn");
SPDLOG_ERROR("error");
SPDLOG_CRITICAL("critical");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.