Skip to content

Commit

Permalink
feat: ✨ reimplement log event handler; add logging customization via …
Browse files Browse the repository at this point in the history
…environment variable
  • Loading branch information
Xminent committed Dec 27, 2023
1 parent a2c018e commit eafc744
Showing 1 changed file with 42 additions and 2 deletions.
44 changes: 42 additions & 2 deletions src/saber.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,48 @@ Saber::Saber(std::string_view token)
shard{ekizu::ShardId::ONE, token, ekizu::Intents::AllIntents} {
logger = spdlog::stdout_color_mt("logger");
spdlog::set_pattern("[%Y-%m-%d %H:%M:%S.%e] [%^%l%$] %v");
spdlog::set_level(spdlog::level::debug);

#ifdef _DEBUG
auto level = spdlog::level::debug;
#else
auto level = spdlog::level::info;
#endif

if (const auto *log_level_e = std::getenv("SABER_LOG_LEVEL");
log_level_e != nullptr) {
std::string_view log_level{log_level_e};

if (log_level == "info") {
level = spdlog::level::info;
} else if (log_level == "warn") {
level = spdlog::level::warn;
} else if (log_level == "error") {
level = spdlog::level::err;
} else if (log_level == "debug") {
level = spdlog::level::debug;
} else if (log_level == "trace") {
level = spdlog::level::trace;
} else if (log_level == "critical") {
level = spdlog::level::critical;
} else if (log_level == "off") {
level = spdlog::level::off;
}
}

spdlog::set_level(level);

shard.attach_logger([this](const ekizu::Log &log) {
switch (log.level) {
case ekizu::LogLevel::Info: logger->info(log.message); break;
case ekizu::LogLevel::Warn: logger->warn(log.message); break;
case ekizu::LogLevel::Error: logger->error(log.message); break;
case ekizu::LogLevel::Debug: logger->debug(log.message); break;
case ekizu::LogLevel::Trace: logger->trace(log.message); break;
case ekizu::LogLevel::Critical:
logger->critical(log.message);
break;
}
});
}

void Saber::run(const boost::asio::yield_context &yield) {
Expand Down Expand Up @@ -59,7 +100,6 @@ void Saber::handle_event(ekizu::Event ev,
users_cache.put(m.message.author.id, m.message.author);
commands.process_commands(m.message, yield);
},
[this](const ekizu::Log &l) { logger->debug(l.message); },
[this](ekizu::Resumed) { logger->info("Resumed"); },
[this](const auto &e) {
logger->warn("Unhandled event: {}", typeid(e).name());
Expand Down

0 comments on commit eafc744

Please sign in to comment.