Skip to content

Commit

Permalink
Log: Switch to fmt::text_style for coloring (#365)
Browse files Browse the repository at this point in the history
  • Loading branch information
GranMinigun committed Feb 29, 2024
1 parent bc42666 commit 2ad2fa8
Showing 1 changed file with 27 additions and 7 deletions.
34 changes: 27 additions & 7 deletions src/nba/include/nba/log.hpp
Expand Up @@ -8,6 +8,7 @@
#pragma once

#include <array>
#include <fmt/color.h>
#include <fmt/format.h>
#include <string_view>
#include <utility>
Expand Down Expand Up @@ -38,16 +39,35 @@ namespace detail {
template<Level level, typename... Args>
inline void Log(std::string_view format, Args&&... args) {
if constexpr((detail::kLogMask & level) != 0) {
fmt::text_style style = {};
char const* prefix = "[?]";

if constexpr(level == Trace) prefix = "\e[36m[T]";
if constexpr(level == Debug) prefix = "\e[34m[D]";
if constexpr(level == Info) prefix = "\e[37m[I]";
if constexpr(level == Warn) prefix = "\e[33m[W]";
if constexpr(level == Error) prefix = "\e[35m[E]";
if constexpr(level == Fatal) prefix = "\e[31m[F]";
if constexpr(level == Trace) {
style = fmt::fg(fmt::terminal_color::cyan);
prefix = "[T]";
}
if constexpr(level == Debug) {
style = fmt::fg(fmt::terminal_color::blue);
prefix = "[D]";
}
if constexpr(level == Info) {
prefix = "[I]";
}
if constexpr(level == Warn) {
style = fmt::fg(fmt::terminal_color::yellow);
prefix = "[W]";
}
if constexpr(level == Error) {
style = fmt::fg(fmt::terminal_color::magenta);
prefix = "[E]";
}
if constexpr(level == Fatal) {
style = fmt::fg(fmt::terminal_color::red);
prefix = "[F]";
}

fmt::print("{} {}\n", prefix, fmt::format(format, std::forward<Args>(args)...));
const auto& style_ref = style;
fmt::print(style_ref, "{} {}\n", prefix, fmt::format(format, std::forward<Args>(args)...));
}
}

Expand Down

0 comments on commit 2ad2fa8

Please sign in to comment.