From 904e2b698e5470eca2ff2737d11ec1f070463ac0 Mon Sep 17 00:00:00 2001 From: Amneesh Singh Date: Wed, 13 Sep 2023 03:43:24 +0530 Subject: [PATCH] add libfmt to replace current logging mechanism Signed-off-by: Amneesh Singh --- flake.nix | 10 ++++-- src/util/log.cc | 49 -------------------------- src/util/log.hh | 83 ++++++++++++++++++++++++++------------------ src/util/meson.build | 1 - 4 files changed, 57 insertions(+), 86 deletions(-) delete mode 100644 src/util/log.cc diff --git a/flake.nix b/flake.nix index fcbbc10..88745c5 100644 --- a/flake.nix +++ b/flake.nix @@ -19,7 +19,14 @@ llvm = pkgs.llvmPackages_16; stdenv = llvm.libcxxStdenv; - nativeBuildInputs = with pkgs; [ meson ninja ]; + nativeBuildInputs = with pkgs; [ + meson + ninja + + # libraries + pkg-config + fmt.dev + ]; in rec { packages = rec { @@ -49,7 +56,6 @@ # other tools valgrind - llvm.lldb ]); }; diff --git a/src/util/log.cc b/src/util/log.cc deleted file mode 100644 index bafd2c6..0000000 --- a/src/util/log.cc +++ /dev/null @@ -1,49 +0,0 @@ -#include "log.hh" - -namespace logging { - -namespace ansi { -static const char* RED = "\033[31m"; -static const char* YELLOW = "\033[33m"; -static const char* MAGENTA = "\033[35m"; -static const char* WHITE = "\033[37m"; -static const char* BOLD = "\033[1m"; -static const char* RESET = "\033[0m"; -} - -Logger::Logger(std::ostream& os) - : os(os){}; - -void -Logger::set_level(Level level) { - switch (level) { - case Level::Debug: - os << ansi::MAGENTA << ansi::BOLD << "[DEBUG] "; - break; - - case Level::Info: - os << ansi::WHITE << "[INFO] "; - break; - - case Level::Warn: - os << ansi::YELLOW << "[WARN] "; - break; - - case Level::Error: - os << ansi::RED << ansi::BOLD << "[ERROR] "; - break; - - // unreachable - default: { - } - } -} - -void -Logger::reset_level() { - os << ansi::RESET; -} -} - -// Global logger -logging::Logger logger; diff --git a/src/util/log.hh b/src/util/log.hh index 4ae43ec..3e7e270 100644 --- a/src/util/log.hh +++ b/src/util/log.hh @@ -1,42 +1,57 @@ #pragma once -#include +#include #include -#include -#include -namespace logging { -enum class Level { - Debug, - Info, - Warn, - Error, -}; +namespace logger { +inline std::ostream& stream = std::clog; -class Logger { - public: - Logger(std::ostream& os = std::clog); - - template - void print(Level level, Args&&... args) { - set_level(level); - (os << ... << args); - reset_level(); - os << std::endl; - } - - private: - std::ostream& os; - void set_level(Level level); - void reset_level(); -}; +namespace ansi { +static constexpr char RED[] = "\033[31m"; +static constexpr char YELLOW[] = "\033[33m"; +static constexpr char MAGENTA[] = "\033[35m"; +static constexpr char WHITE[] = "\033[37m"; +static constexpr char BOLD[] = "\033[1m"; +static constexpr char RESET[] = "\033[0m"; } -extern logging::Logger logger; +template +inline void +log_raw(const fmt::format_string& fmt, Args&&... args) { + fmt::println(stream, fmt, std::forward(args)...); +} -#define log_debug(...) logger.print(logging::Level::Debug, __VA_ARGS__) -#define log_info(...) logger.print(logging::Level::Info, __VA_ARGS__) -#define log_warn(...) logger.print(logging::Level::Warn, __VA_ARGS__) -#define log_error(...) logger.print(logging::Level::Error, __VA_ARGS__) -#define log(...) log_info(__VA_ARGS__) -#define debug(value) log_debug(#value, " = ", value) +template +inline void +log_debug(const fmt::format_string& fmt, Args&&... args) { + fmt::print(stream, "{}{}[DEBUG] ", ansi::MAGENTA, ansi::BOLD); + log_raw(fmt, std::forward(args)...); + fmt::print(stream, ansi::RESET); +} + +template +inline void +log_info(const fmt::format_string& fmt, Args&&... args) { + fmt::print(stream, "{}[INFO] ", ansi::WHITE); + log_raw(fmt, std::forward(args)...); + fmt::print(stream, ansi::RESET); +} + +template +inline void +log_warn(const fmt::format_string& fmt, Args&&... args) { + fmt::print(stream, "{}[WARN] ", ansi::YELLOW); + log_raw(fmt, std::forward(args)...); + fmt::print(stream, ansi::RESET); +} + +template +inline void +log_error(const fmt::format_string& fmt, Args&&... args) { + fmt::print(stream, "{}{}[ERROR] ", ansi::RED, ansi::BOLD); + log_raw(fmt, std::forward(args)...); + fmt::print(stream, ansi::RESET); +} +} + +#define debug(value) logger::log_debug("{} = {}", #value, value) diff --git a/src/util/meson.build b/src/util/meson.build index 2a1fcbe..41c0f08 100644 --- a/src/util/meson.build +++ b/src/util/meson.build @@ -1,4 +1,3 @@ lib_sources += files( - 'log.cc', 'utils.cc' ) \ No newline at end of file