Skip to content

Commit

Permalink
Added file sink
Browse files Browse the repository at this point in the history
  • Loading branch information
Fixstars-iizuka committed Nov 3, 2023
1 parent de468ca commit 9a82623
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 26 deletions.
25 changes: 23 additions & 2 deletions src/log.cc
Original file line number Diff line number Diff line change
@@ -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<spdlog::sinks::stderr_color_sink_mt>();
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<spdlog::sinks::basic_file_sink_mt>("logs/ion.log", false);
file_sink->set_level(spdlog::level::trace);

spdlog::register_logger(std::make_shared<spdlog::logger>("ion", spdlog::sinks_init_list{console_sink, file_sink}));
}
} logger;

}
} // anonymous
32 changes: 8 additions & 24 deletions src/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<class... Types> static void critical(Types... args) { spdlog::get("ion")->critical(args...); }
template<class... Types> static void error (Types... args) { spdlog::get("ion")->error (args...); }
template<class... Types> static void warn (Types... args) { spdlog::get("ion")->warn (args...); }
template<class... Types> static void info (Types... args) { spdlog::get("ion")->info (args...); }
template<class... Types> static void debug (Types... args) { spdlog::get("ion")->debug (args...); }

};

namespace log {
template<class... Types> static void critical(Types... args) { spdlog::get("ion")->critical(args...); }
template<class... Types> static void error (Types... args) { spdlog::get("ion")->error (args...); }
template<class... Types> static void warn (Types... args) { spdlog::get("ion")->warn (args...); }
template<class... Types> static void info (Types... args) { spdlog::get("ion")->info (args...); }
template<class... Types> static void debug (Types... args) { spdlog::get("ion")->debug (args...); }
template<class... Types> static void trace (Types... args) { spdlog::get("ion")->trace (args...); }
} // log
} // ion

#endif

0 comments on commit 9a82623

Please sign in to comment.