From 9a8262370aa8e5d9f29e7d0afb868afa69321769 Mon Sep 17 00:00:00 2001 From: Takuro Iizuka Date: Thu, 2 Nov 2023 17:09:33 -0700 Subject: [PATCH] Added file sink --- src/log.cc | 25 +++++++++++++++++++++++-- src/log.h | 32 ++++++++------------------------ 2 files changed, 31 insertions(+), 26 deletions(-) diff --git a/src/log.cc b/src/log.cc index 209f1689..d981178b 100644 --- a/src/log.cc +++ b/src/log.cc @@ -1,7 +1,28 @@ +#include "spdlog/cfg/helpers.h" +#include "spdlog/details/os.h" +#include "spdlog/sinks/stdout_color_sinks.h" +#include "spdlog/sinks/basic_file_sink.h" + #include "log.h" namespace { -auto log_ = ion::log(); +struct Logger { + Logger() + { + auto console_sink = std::make_shared(); + auto env_val = spdlog::details::os::getenv("ION_LOG_LEVEL"); + if (env_val.empty()) { + console_sink->set_level(spdlog::level::critical); + } else { + console_sink->set_level(spdlog::level::from_str(env_val)); + } + + auto file_sink = std::make_shared("logs/ion.log", false); + file_sink->set_level(spdlog::level::trace); + + spdlog::register_logger(std::make_shared("ion", spdlog::sinks_init_list{console_sink, file_sink})); + } +} logger; -} +} // anonymous diff --git a/src/log.h b/src/log.h index dda6e2d9..24ec49af 100644 --- a/src/log.h +++ b/src/log.h @@ -2,32 +2,16 @@ #define ION_LOG_H #include "spdlog/spdlog.h" -#include "spdlog/cfg/helpers.h" -#include "spdlog/details/os.h" -#include "spdlog/sinks/stdout_color_sinks.h" namespace ion { - -struct log { - - log() { - static auto logger = spdlog::stderr_color_mt("ion"); - auto env_val = spdlog::details::os::getenv("ION_LOG_LEVEL"); - if (env_val.empty()) { - spdlog::set_level(spdlog::level::critical); - } else { - spdlog::cfg::helpers::load_levels(env_val); - } - } - - template static void critical(Types... args) { spdlog::get("ion")->critical(args...); } - template static void error (Types... args) { spdlog::get("ion")->error (args...); } - template static void warn (Types... args) { spdlog::get("ion")->warn (args...); } - template static void info (Types... args) { spdlog::get("ion")->info (args...); } - template static void debug (Types... args) { spdlog::get("ion")->debug (args...); } - -}; - +namespace log { +template static void critical(Types... args) { spdlog::get("ion")->critical(args...); } +template static void error (Types... args) { spdlog::get("ion")->error (args...); } +template static void warn (Types... args) { spdlog::get("ion")->warn (args...); } +template static void info (Types... args) { spdlog::get("ion")->info (args...); } +template static void debug (Types... args) { spdlog::get("ion")->debug (args...); } +template static void trace (Types... args) { spdlog::get("ion")->trace (args...); } +} // log } // ion #endif