diff --git a/include/log.hpp b/include/log.hpp index 6955cdf..f06264e 100644 --- a/include/log.hpp +++ b/include/log.hpp @@ -1,25 +1,26 @@ #ifndef LOG_HPP #define LOG_HPP -#define log _log // comment to disable logging +#define log _log // comment to disable logging +// #define LOG_FILE_LOCATION "linuxSmartie.log.gz" #ifdef log -#include - -#define LOG_FILE_LOCATION "linuxSmartie.log.gz" #include #include #include #include -inline std::mutex cmtx; // console logging mutex +#ifdef LOG_FILE_LOCATION +#include inline std::mutex fmtx; // file logging mutex +#endif + +inline std::mutex cmtx; // console logging mutex enum class LOG { INF, WRN, ERR, DBG, START }; template inline void _log(LOG level, std::string_view tag, std::format_string str, Args&&... args) { - constexpr bool logToFile = true; constexpr std::string_view reset = "\033[0m"; constexpr std::string_view pattern = "[{}{}{}] "; std::string_view color = ""; @@ -48,21 +49,23 @@ inline void _log(LOG level, std::string_view tag, std::format_string st std::println(str, std::forward(args)...); } +#ifdef LOG_FILE_LOCATION /* log to file */ - if (logToFile) { + { 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), str, std::forward(args)...); // actual log message - - std::scoped_lock lock (fmtx); + 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), str, std::forward(args)...); // actual log message + + std::scoped_lock lock(fmtx); gzFile file = gzopen(LOG_FILE_LOCATION, "ab"); if (file) { gzprintf(file, "%s\n", msg.c_str()); gzclose(file); } } +#endif } #else