Files
kaizen/src/utils/log.hpp
2025-12-15 15:25:45 +01:00

39 lines
1.4 KiB
C++

#pragma once
#include <common.hpp>
#include <print>
#include <string>
#if !defined(NDEBUG) && !defined(_WIN32)
#include <dlfcn.h>
#endif
namespace Util {
enum LogLevel : u8 { Trace, Debug, Warn, Info, Error, Always };
#ifndef NDEBUG
static constexpr auto globalLogLevel = Debug;
#else
static constexpr auto globalLogLevel = Info;
#endif
template <LogLevel messageType = Info, class... Args>
void print(const std::format_string<Args...> fmt, Args... args) {
if (messageType >= globalLogLevel) {
if (messageType <= Debug) {
#ifndef NDEBUG
std::println(fmt, std::forward<Args>(args)...);
#endif
} else {
std::println(fmt, std::forward<Args>(args)...);
}
}
}
#define panic(fmt, ...) do { Util::print<Util::Error>("[FATAL] " fmt __VA_OPT__(,) __VA_ARGS__); exit(-1); } while(0)
#define error(fmt, ...) do { Util::print<Util::Error>("[ERROR] " fmt __VA_OPT__(,) __VA_ARGS__); } while(0)
#define warn(fmt, ...) do { Util::print<Util::Warn>("[WARN] " fmt __VA_OPT__(,) __VA_ARGS__); } while(0)
#define info(fmt, ...) do { Util::print<Util::Info>("[INFO] " fmt __VA_OPT__(,) __VA_ARGS__); } while(0)
#define debug(fmt, ...) do { Util::print<Util::Debug>("[DEBUG] " fmt __VA_OPT__(,) __VA_ARGS__); } while(0)
#define trace(fmt, ...) do { Util::print<Util::Trace>("[TRACE] " fmt __VA_OPT__(,) __VA_ARGS__); } while(0)
#define always(fmt, ...) do { Util::print<Util::Always>(fmt __VA_OPT__(,) __VA_ARGS__); } while(0)
} // namespace Util