Skip to content

Instantly share code, notes, and snippets.

@yohhoy
Last active July 15, 2021 07:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yohhoy/72834acbd2be2bd9df6758cd3b3ce6a6 to your computer and use it in GitHub Desktop.
Save yohhoy/72834acbd2be2bd9df6758cd3b3ce6a6 to your computer and use it in GitHub Desktop.
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(int argc, char* argv[])
{
#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));
#if 0
// https://github.com/gabime/spdlog/blob/v2.x/tests/test_cfg.cpp
//#include "spdlog/cfg/argv.h"
spdlog::cfg::load_argv_levels(argc, argv);
#endif
// 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