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

Merge pull request #597 from Laupetin/fix/color-only-when-supported

fix: only show console color when supported
This commit is contained in:
Jan
2025-11-30 18:15:06 +01:00
committed by GitHub
14 changed files with 108 additions and 29 deletions

View File

@@ -35,6 +35,8 @@ namespace image_converter
bool Start(const int argc, const char** argv) override bool Start(const int argc, const char** argv) override
{ {
con::init();
auto shouldContinue = true; auto shouldContinue = true;
if (!m_args.ParseArgs(argc, argv, shouldContinue)) if (!m_args.ParseArgs(argc, argv, shouldContinue))
return false; return false;

View File

@@ -146,12 +146,12 @@ bool ImageConverterArgs::ParseArgs(const int argc, const char** argv, bool& shou
// -v; --verbose // -v; --verbose
if (m_argument_parser.IsOptionSpecified(OPTION_VERBOSE)) if (m_argument_parser.IsOptionSpecified(OPTION_VERBOSE))
con::globalLogLevel = con::LogLevel::DEBUG; con::set_log_level(con::LogLevel::DEBUG);
else else
con::globalLogLevel = con::LogLevel::INFO; con::set_log_level(con::LogLevel::INFO);
// --no-color // --no-color
con::globalUseColor = !m_argument_parser.IsOptionSpecified(OPTION_NO_COLOR); con::set_use_color(!m_argument_parser.IsOptionSpecified(OPTION_NO_COLOR));
return true; return true;
} }

View File

@@ -432,6 +432,8 @@ class LinkerImpl final : public Linker
public: public:
bool Start(const int argc, const char** argv) override bool Start(const int argc, const char** argv) override
{ {
con::init();
auto shouldContinue = true; auto shouldContinue = true;
if (!m_args.ParseArgs(argc, argv, shouldContinue)) if (!m_args.ParseArgs(argc, argv, shouldContinue))
return false; return false;

View File

@@ -203,12 +203,12 @@ bool LinkerArgs::ParseArgs(const int argc, const char** argv, bool& shouldContin
// -v; --verbose // -v; --verbose
if (m_argument_parser.IsOptionSpecified(OPTION_VERBOSE)) if (m_argument_parser.IsOptionSpecified(OPTION_VERBOSE))
con::globalLogLevel = con::LogLevel::DEBUG; con::set_log_level(con::LogLevel::DEBUG);
else else
con::globalLogLevel = con::LogLevel::INFO; con::set_log_level(con::LogLevel::INFO);
// --no-color // --no-color
con::globalUseColor = !m_argument_parser.IsOptionSpecified(OPTION_NO_COLOR); con::set_use_color(!m_argument_parser.IsOptionSpecified(OPTION_NO_COLOR));
// b; --base-folder // b; --base-folder
if (m_argument_parser.IsOptionSpecified(OPTION_BASE_FOLDER)) if (m_argument_parser.IsOptionSpecified(OPTION_BASE_FOLDER))

View File

@@ -94,12 +94,12 @@ bool ModManArgs::ParseArgs(const int argc, const char** argv, bool& shouldContin
// -v; --verbose // -v; --verbose
if (m_argument_parser.IsOptionSpecified(OPTION_VERBOSE)) if (m_argument_parser.IsOptionSpecified(OPTION_VERBOSE))
con::globalLogLevel = con::LogLevel::DEBUG; con::set_log_level(con::LogLevel::DEBUG);
else else
con::globalLogLevel = con::LogLevel::INFO; con::set_log_level(con::LogLevel::INFO);
// --no-color // --no-color
con::globalUseColor = !m_argument_parser.IsOptionSpecified(OPTION_NO_COLOR); con::set_use_color(!m_argument_parser.IsOptionSpecified(OPTION_NO_COLOR));
return true; return true;
} }

View File

@@ -129,6 +129,8 @@ int main(int argc, const char** argv)
g_set_application_name("OpenAssetTools ModMan"); g_set_application_name("OpenAssetTools ModMan");
#endif #endif
con::init();
ModManArgs args; ModManArgs args;
auto shouldContinue = true; auto shouldContinue = true;
if (!args.ParseArgs(MODMAN_ARGC, MODMAN_ARGV, shouldContinue)) if (!args.ParseArgs(MODMAN_ARGC, MODMAN_ARGV, shouldContinue))

View File

@@ -46,6 +46,8 @@ public:
int Run(const int argc, const char** argv) int Run(const int argc, const char** argv)
{ {
con::init();
auto shouldContinue = true; auto shouldContinue = true;
if (!m_args.ParseArgs(argc, argv, shouldContinue)) if (!m_args.ParseArgs(argc, argv, shouldContinue))
return 1; return 1;

View File

@@ -127,12 +127,12 @@ bool RawTemplaterArguments::ParseArgs(const int argc, const char** argv, bool& s
// -v; --verbose // -v; --verbose
if (m_argument_parser.IsOptionSpecified(OPTION_VERBOSE)) if (m_argument_parser.IsOptionSpecified(OPTION_VERBOSE))
con::globalLogLevel = con::LogLevel::DEBUG; con::set_log_level(con::LogLevel::DEBUG);
else else
con::globalLogLevel = con::LogLevel::INFO; con::set_log_level(con::LogLevel::INFO);
// --no-color // --no-color
con::globalUseColor = !m_argument_parser.IsOptionSpecified(OPTION_NO_COLOR); con::set_use_color(!m_argument_parser.IsOptionSpecified(OPTION_NO_COLOR));
// -o; --output // -o; --output
if (m_argument_parser.IsOptionSpecified(OPTION_OUTPUT_FOLDER)) if (m_argument_parser.IsOptionSpecified(OPTION_OUTPUT_FOLDER))

View File

@@ -30,6 +30,8 @@ public:
*/ */
bool Start(const int argc, const char** argv) bool Start(const int argc, const char** argv)
{ {
con::init();
auto shouldContinue = true; auto shouldContinue = true;
if (!m_args.ParseArgs(argc, argv, shouldContinue)) if (!m_args.ParseArgs(argc, argv, shouldContinue))
return false; return false;

View File

@@ -303,12 +303,12 @@ bool UnlinkerArgs::ParseArgs(const int argc, const char** argv, bool& shouldCont
// -v; --verbose // -v; --verbose
if (m_argument_parser.IsOptionSpecified(OPTION_VERBOSE)) if (m_argument_parser.IsOptionSpecified(OPTION_VERBOSE))
con::globalLogLevel = con::LogLevel::DEBUG; con::set_log_level(con::LogLevel::DEBUG);
else else
con::globalLogLevel = con::LogLevel::INFO; con::set_log_level(con::LogLevel::INFO);
// --no-color // --no-color
con::globalUseColor = !m_argument_parser.IsOptionSpecified(OPTION_NO_COLOR); con::set_use_color(!m_argument_parser.IsOptionSpecified(OPTION_NO_COLOR));
// -min; --minimal-zone // -min; --minimal-zone
m_minimal_zone_def = m_argument_parser.IsOptionSpecified(OPTION_MINIMAL_ZONE_FILE); m_minimal_zone_def = m_argument_parser.IsOptionSpecified(OPTION_MINIMAL_ZONE_FILE);

View File

@@ -3,10 +3,72 @@
#include <format> #include <format>
#include <iostream> #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 namespace con
{ {
LogLevel globalLogLevel = LogLevel::INFO; LogLevel _globalLogLevel = LogLevel::INFO;
bool globalUseColor = true;
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) void _debug_internal(const std::string& str)
{ {

View File

@@ -1,11 +1,12 @@
#pragma once #pragma once
#include <cstdint>
#include <format> #include <format>
#include <string> #include <string>
namespace con namespace con
{ {
enum class LogLevel enum class LogLevel : std::uint8_t
{ {
DEBUG = 0, DEBUG = 0,
INFO, INFO,
@@ -13,8 +14,11 @@ namespace con
ERROR ERROR
}; };
extern LogLevel globalLogLevel; extern LogLevel _globalLogLevel;
extern bool globalUseColor;
void init();
void set_log_level(LogLevel value);
void set_use_color(bool value);
void _debug_internal(const std::string& str); void _debug_internal(const std::string& str);
void _info_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) 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; return;
_debug_internal(str); _debug_internal(str);
} }
template<class Arg0, class... OtherArgs> void debug(std::format_string<Arg0, OtherArgs...> fmt, Arg0&& arg0, OtherArgs&&... otherArgs) 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; return;
_debug_internal(std::vformat(fmt.get(), std::make_format_args(arg0, otherArgs...))); _debug_internal(std::vformat(fmt.get(), std::make_format_args(arg0, otherArgs...)));
} }
inline void info(const std::string& str) 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; return;
_info_internal(str); _info_internal(str);
} }
template<class Arg0, class... OtherArgs> void info(std::format_string<Arg0, OtherArgs...> fmt, Arg0&& arg0, OtherArgs&&... otherArgs) 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; return;
_info_internal(std::vformat(fmt.get(), std::make_format_args(arg0, otherArgs...))); _info_internal(std::vformat(fmt.get(), std::make_format_args(arg0, otherArgs...)));
} }
inline void warn(const std::string& str) 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; return;
_warn_internal(str); _warn_internal(str);
} }
template<class Arg0, class... OtherArgs> void warn(std::format_string<Arg0, OtherArgs...> fmt, Arg0&& arg0, OtherArgs&&... otherArgs) 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; return;
_warn_internal(std::vformat(fmt.get(), std::make_format_args(arg0, otherArgs...))); _warn_internal(std::vformat(fmt.get(), std::make_format_args(arg0, otherArgs...)));
} }

View File

@@ -6,6 +6,7 @@
#include "Persistence/IDataRepository.h" #include "Persistence/IDataRepository.h"
#include "Persistence/InMemory/InMemoryRepository.h" #include "Persistence/InMemory/InMemoryRepository.h"
#include "Printing/PrettyPrinter.h" #include "Printing/PrettyPrinter.h"
#include "Utils/Logging/Log.h"
#include "ZoneCodeGeneratorArguments.h" #include "ZoneCodeGeneratorArguments.h"
#include <cstdio> #include <cstdio>
@@ -23,6 +24,8 @@ public:
int Run(const int argc, const char** argv) override int Run(const int argc, const char** argv) override
{ {
con::init();
auto shouldContinue = true; auto shouldContinue = true;
if (!m_args.ParseArgs(argc, argv, shouldContinue)) if (!m_args.ParseArgs(argc, argv, shouldContinue))
return 1; return 1;

View File

@@ -173,12 +173,12 @@ bool ZoneCodeGeneratorArguments::ParseArgs(const int argc, const char** argv, bo
// -v; --verbose // -v; --verbose
if (m_argument_parser.IsOptionSpecified(OPTION_VERBOSE)) if (m_argument_parser.IsOptionSpecified(OPTION_VERBOSE))
con::globalLogLevel = con::LogLevel::DEBUG; con::set_log_level(con::LogLevel::DEBUG);
else else
con::globalLogLevel = con::LogLevel::INFO; con::set_log_level(con::LogLevel::INFO);
// --no-color // --no-color
con::globalUseColor = !m_argument_parser.IsOptionSpecified(OPTION_NO_COLOR); con::set_use_color(!m_argument_parser.IsOptionSpecified(OPTION_NO_COLOR));
// -p; --print // -p; --print
if (m_argument_parser.IsOptionSpecified(OPTION_PRINT)) if (m_argument_parser.IsOptionSpecified(OPTION_PRINT))