From 39f47a4862bef68f72e7ef64ad818192ac6aa189 Mon Sep 17 00:00:00 2001 From: Drath Date: Sat, 27 Dec 2025 14:56:28 +0100 Subject: [PATCH] logger updates --- include/log.hpp | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/include/log.hpp b/include/log.hpp index 2fc2a15..de6db0f 100644 --- a/include/log.hpp +++ b/include/log.hpp @@ -1,7 +1,9 @@ #ifndef LOG_HPP #define LOG_HPP -#define log(level, fmt, ...) _log(level, std::source_location::current(), fmt, ##__VA_ARGS__) // comment to disable logging +// comment out to disable logging in general +#define log(level, fmt, ...) _log(level, std::source_location::current(), fmt, ##__VA_ARGS__) +// comment out to disable logging to file // #define LOG_FILE_LOCATION "linuxSmartie.log.gz" #ifdef log @@ -17,12 +19,10 @@ inline std::mutex fmtx; // file logging mutex #endif -// #define FUNC_NAME pFname(std::source_location::current().function_name()) - inline std::mutex cmtx; // console logging mutex enum class LOG { INF, WRN, ERR, DBG, START }; -inline std::string_view pFname(std::string_view full) { +constexpr std::string_view pFname(std::string_view full) { if (auto pos = full.find(' '); pos != std::string_view::npos) full.remove_prefix(pos + 1); @@ -36,7 +36,6 @@ template inline void _log(LOG level, const std::source_location& loc, std::format_string str, Args&&... args) { constexpr std::string_view reset = "\033[0m"; constexpr std::string_view pattern = "[{}{}{}] "; - std::string_view tag = pFname(loc.function_name()); std::string_view color = ""; switch (level) { @@ -59,7 +58,7 @@ inline void _log(LOG level, const std::source_location& loc, std::format_string< /* log to console */ { std::scoped_lock lock(cmtx); - std::print(pattern, color, tag, reset); + std::print(pattern, color, pFname(loc.function_name()), reset); std::println(str, std::forward(args)...); } @@ -69,7 +68,7 @@ inline void _log(LOG level, const std::source_location& loc, std::format_string< auto now = std::chrono::system_clock::now(); auto local = std::chrono::current_zone()->to_local(now); std::string msg = std::format("{:%x %H:%M:%S }", floor(local)); // timestamp - std::format_to(std::back_inserter(msg), pattern, color, tag, reset); // tag + std::format_to(std::back_inserter(msg), pattern, color, pFname(loc.function_name()), reset); // tag std::format_to(std::back_inserter(msg), str, std::forward(args)...); // actual log message std::scoped_lock lock(fmtx);