lsfg-vk/include/utils/log.hpp
PancakeTAS f306c48e6d
cleanup headers
supersedes #32 fixes #42
2025-07-10 17:01:17 +02:00

74 lines
2.2 KiB
C++

#pragma once
#include <format>
#include <fstream>
#include <iostream>
#include <mutex>
#include <set>
#include <string>
#include <string_view>
namespace Log {
namespace Internal {
extern bool isSetup;
extern std::set<std::string> debugModules;
extern bool debugAllModules;
extern std::ofstream logFile;
extern std::mutex logMutex;
void setup();
}
template<typename... Args>
void log(std::string_view color, std::string_view module,
std::format_string<Args...> fmt, Args&&... args) {
Internal::setup();
const std::string prefix = std::format("lsfg-vk({}): ", module);
const std::string message = std::format(fmt, std::forward<Args>(args)...);
const std::lock_guard<std::mutex> lock(Internal::logMutex);
std::cerr << color << prefix << message << "\033[0m" << '\n';
if (Internal::logFile.is_open()) {
Internal::logFile << prefix << message << '\n';
Internal::logFile.flush();
}
}
const std::string_view WHITE = "\033[1;37m";
const std::string_view YELLOW = "\033[1;33m";
const std::string_view RED = "\033[1;31m";
template<typename... Args>
void info(std::string_view module, std::format_string<Args...> fmt, Args&&... args) {
log(WHITE, module, fmt, std::forward<Args>(args)...);
}
template<typename... Args>
void warn(std::string_view module, std::format_string<Args...> fmt, Args&&... args) {
log(YELLOW, module, fmt, std::forward<Args>(args)...);
}
template<typename... Args>
void error(std::string_view module, std::format_string<Args...> fmt, Args&&... args) {
log(RED, module, fmt, std::forward<Args>(args)...);
}
const std::string_view GRAY = "\033[1;90m";
#ifdef LSFG_NO_DEBUG
template<typename... Args>
void debug(std::string_view, std::format_string<Args...>, Args&&...) {}
#else
template<typename... Args>
void debug(std::string_view module, std::format_string<Args...> fmt, Args&&... args) {
Internal::setup();
if (Internal::debugAllModules || Internal::debugModules.contains(std::string(module)))
log(GRAY, module, fmt, std::forward<Args>(args)...);
}
#endif
}