mirror of
https://github.com/Laupetin/OpenAssetTools.git
synced 2026-01-13 20:21:48 +00:00
fix: only show console color when supported
This commit is contained in:
@@ -3,10 +3,72 @@
|
||||
#include <format>
|
||||
#include <iostream>
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <Windows.h>
|
||||
#endif
|
||||
|
||||
namespace
|
||||
{
|
||||
bool logLevelSet = false;
|
||||
|
||||
bool globalUseColor = true;
|
||||
bool colorSet = false;
|
||||
|
||||
bool CanUseColor()
|
||||
{
|
||||
#ifdef _WIN32
|
||||
DWORD dwMode = 0;
|
||||
const auto stdoutHandle = GetStdHandle(STD_OUTPUT_HANDLE);
|
||||
GetConsoleMode(stdoutHandle, &dwMode);
|
||||
if (!(dwMode & ENABLE_VIRTUAL_TERMINAL_PROCESSING))
|
||||
{
|
||||
SetConsoleMode(stdoutHandle, dwMode | ENABLE_VIRTUAL_TERMINAL_PROCESSING);
|
||||
dwMode = 0;
|
||||
GetConsoleMode(stdoutHandle, &dwMode);
|
||||
if (!(dwMode & ENABLE_VIRTUAL_TERMINAL_PROCESSING))
|
||||
return false;
|
||||
}
|
||||
|
||||
const auto stderrHandle = GetStdHandle(STD_ERROR_HANDLE);
|
||||
GetConsoleMode(stderrHandle, &dwMode);
|
||||
if (!(dwMode & ENABLE_VIRTUAL_TERMINAL_PROCESSING))
|
||||
{
|
||||
SetConsoleMode(stderrHandle, dwMode | ENABLE_VIRTUAL_TERMINAL_PROCESSING);
|
||||
dwMode = 0;
|
||||
GetConsoleMode(stderrHandle, &dwMode);
|
||||
if (!(dwMode & ENABLE_VIRTUAL_TERMINAL_PROCESSING))
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
|
||||
return true;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
namespace con
|
||||
{
|
||||
LogLevel globalLogLevel = LogLevel::INFO;
|
||||
bool globalUseColor = true;
|
||||
LogLevel _globalLogLevel = LogLevel::INFO;
|
||||
|
||||
void init()
|
||||
{
|
||||
if (!logLevelSet)
|
||||
set_log_level(LogLevel::INFO);
|
||||
|
||||
if (!colorSet)
|
||||
set_use_color(true);
|
||||
}
|
||||
|
||||
void set_log_level(const LogLevel value)
|
||||
{
|
||||
logLevelSet = true;
|
||||
_globalLogLevel = value;
|
||||
}
|
||||
|
||||
void set_use_color(const bool value)
|
||||
{
|
||||
colorSet = true;
|
||||
globalUseColor = value && CanUseColor();
|
||||
}
|
||||
|
||||
void _debug_internal(const std::string& str)
|
||||
{
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <format>
|
||||
#include <string>
|
||||
|
||||
namespace con
|
||||
{
|
||||
enum class LogLevel
|
||||
enum class LogLevel : std::uint8_t
|
||||
{
|
||||
DEBUG = 0,
|
||||
INFO,
|
||||
@@ -13,8 +14,11 @@ namespace con
|
||||
ERROR
|
||||
};
|
||||
|
||||
extern LogLevel globalLogLevel;
|
||||
extern bool globalUseColor;
|
||||
extern LogLevel _globalLogLevel;
|
||||
|
||||
void init();
|
||||
void set_log_level(LogLevel value);
|
||||
void set_use_color(bool value);
|
||||
|
||||
void _debug_internal(const std::string& str);
|
||||
void _info_internal(const std::string& str);
|
||||
@@ -23,42 +27,42 @@ namespace con
|
||||
|
||||
inline void debug(const std::string& str)
|
||||
{
|
||||
if (static_cast<unsigned>(globalLogLevel) > static_cast<unsigned>(LogLevel::DEBUG))
|
||||
if (static_cast<unsigned>(_globalLogLevel) > static_cast<unsigned>(LogLevel::DEBUG))
|
||||
return;
|
||||
_debug_internal(str);
|
||||
}
|
||||
|
||||
template<class Arg0, class... OtherArgs> void debug(std::format_string<Arg0, OtherArgs...> fmt, Arg0&& arg0, OtherArgs&&... otherArgs)
|
||||
{
|
||||
if (static_cast<unsigned>(globalLogLevel) > static_cast<unsigned>(LogLevel::DEBUG))
|
||||
if (static_cast<unsigned>(_globalLogLevel) > static_cast<unsigned>(LogLevel::DEBUG))
|
||||
return;
|
||||
_debug_internal(std::vformat(fmt.get(), std::make_format_args(arg0, otherArgs...)));
|
||||
}
|
||||
|
||||
inline void info(const std::string& str)
|
||||
{
|
||||
if (static_cast<unsigned>(globalLogLevel) > static_cast<unsigned>(LogLevel::INFO))
|
||||
if (static_cast<unsigned>(_globalLogLevel) > static_cast<unsigned>(LogLevel::INFO))
|
||||
return;
|
||||
_info_internal(str);
|
||||
}
|
||||
|
||||
template<class Arg0, class... OtherArgs> void info(std::format_string<Arg0, OtherArgs...> fmt, Arg0&& arg0, OtherArgs&&... otherArgs)
|
||||
{
|
||||
if (static_cast<unsigned>(globalLogLevel) > static_cast<unsigned>(LogLevel::INFO))
|
||||
if (static_cast<unsigned>(_globalLogLevel) > static_cast<unsigned>(LogLevel::INFO))
|
||||
return;
|
||||
_info_internal(std::vformat(fmt.get(), std::make_format_args(arg0, otherArgs...)));
|
||||
}
|
||||
|
||||
inline void warn(const std::string& str)
|
||||
{
|
||||
if (static_cast<unsigned>(globalLogLevel) > static_cast<unsigned>(LogLevel::WARN))
|
||||
if (static_cast<unsigned>(_globalLogLevel) > static_cast<unsigned>(LogLevel::WARN))
|
||||
return;
|
||||
_warn_internal(str);
|
||||
}
|
||||
|
||||
template<class Arg0, class... OtherArgs> void warn(std::format_string<Arg0, OtherArgs...> fmt, Arg0&& arg0, OtherArgs&&... otherArgs)
|
||||
{
|
||||
if (static_cast<unsigned>(globalLogLevel) > static_cast<unsigned>(LogLevel::WARN))
|
||||
if (static_cast<unsigned>(_globalLogLevel) > static_cast<unsigned>(LogLevel::WARN))
|
||||
return;
|
||||
_warn_internal(std::vformat(fmt.get(), std::make_format_args(arg0, otherArgs...)));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user