2
0
mirror of https://github.com/Laupetin/OpenAssetTools.git synced 2025-09-12 19:47:27 +00:00

feat: add centralized logging component

This commit is contained in:
Jan Laupetin
2025-09-10 19:46:16 +02:00
parent 9285293650
commit 1bf4033f41
2 changed files with 90 additions and 0 deletions

View File

@@ -0,0 +1,42 @@
#include "Log.h"
#include <format>
#include <iostream>
namespace con
{
LogLevel globalLogLevel = LogLevel::INFO;
bool globalUseColor = true;
void _debug_internal(const std::string& str)
{
if (globalUseColor)
std::cout << std::format("\x1B[90m{}\x1B[0m\n", str);
else
std::cout << std::format("{}\n", str);
}
void _info_internal(const std::string& str)
{
if (globalUseColor)
std::cout << std::format("\x1B[37m{}\x1B[0m\n", str);
else
std::cout << std::format("{}\n", str);
}
void _warn_internal(const std::string& str)
{
if (globalUseColor)
std::cout << std::format("\x1B[33m{}\x1B[0m\n", str);
else
std::cout << std::format("{}\n", str);
}
void _error_internal(const std::string& str)
{
if (globalUseColor)
std::cerr << std::format("\x1B[31m{}\x1B[0m\n", str);
else
std::cerr << std::format("{}\n", str);
}
} // namespace con

View File

@@ -0,0 +1,48 @@
#pragma once
#include <format>
namespace con
{
enum class LogLevel
{
DEBUG = 0,
INFO,
WARN,
ERROR
};
extern LogLevel globalLogLevel;
extern bool globalUseColor;
void _debug_internal(const std::string& str);
void _info_internal(const std::string& str);
void _warn_internal(const std::string& str);
void _error_internal(const std::string& str);
template<class... Args> void debug(std::format_string<Args...> fmt, Args&&... args)
{
if (static_cast<unsigned>(globalLogLevel) > static_cast<unsigned>(LogLevel::DEBUG))
return;
_debug_internal(std::vformat(fmt.get(), std::make_format_args(args...)));
}
template<class... Args> void info(std::format_string<Args...> fmt, Args&&... args)
{
if (static_cast<unsigned>(globalLogLevel) > static_cast<unsigned>(LogLevel::INFO))
return;
_info_internal(std::vformat(fmt.get(), std::make_format_args(args...)));
}
template<class... Args> void warn(std::format_string<Args...> fmt, Args&&... args)
{
if (static_cast<unsigned>(globalLogLevel) > static_cast<unsigned>(LogLevel::WARN))
return;
_warn_internal(std::vformat(fmt.get(), std::make_format_args(args...)));
}
template<class... Args> void error(std::format_string<Args...> fmt, Args&&... args)
{
_error_internal(std::vformat(fmt.get(), std::make_format_args(args...)));
}
} // namespace con