logging lib

This commit is contained in:
Drath
2025-10-03 16:57:38 +02:00
parent 31f2c8bf7c
commit 7e7d8d2aa1
12 changed files with 80 additions and 48 deletions

39
include/log.hpp Normal file
View File

@@ -0,0 +1,39 @@
#ifndef LOG_HPP
#define LOG_HPP
// #define PATTERN "[{}{}{}] "
#include <mutex>
#include <print>
#include <string_view>
enum class LOG { INF, WRN, ERR, DBG };
inline std::mutex mtx;
template <typename... Args>
inline void log(LOG level, std::string_view tag, std::format_string<Args...> str, Args&&... args) {
constexpr std::string_view reset = "\033[0m";
constexpr std::string_view green = "\033[32m";
constexpr std::string_view yellow = "\033[33m";
constexpr std::string_view red = "\033[31m";
constexpr std::string_view pattern = "[{}{}{}] ";
std::scoped_lock lock(mtx);
switch (level) {
case LOG::INF:
std::print(pattern, green, tag, reset);
break;
case LOG::WRN:
std::print(pattern, yellow, tag, reset);
break;
case LOG::ERR:
std::print(pattern, red, tag, reset);
break;
default:
std::print(pattern, "", tag, "");
}
std::println(str, std::forward<Args>(args)...);
}
#endif