diff options
| author | hachem <im@hachem.wtf> | 2026-08-28 21:33:19 +0200 |
|---|---|---|
| committer | hachem <im@hachem.wtf> | 2026-08-28 21:33:19 +0200 |
| commit | ab021cb74a2b3f0dab3258bf50929912100d1974 (patch) | |
| tree | 55fe6e611798a72083cf19485f90f06a25e2fde7 /src/core | |
| parent | 86afee9b5c4954fda84d500b85328cca18ee3de4 (diff) | |
fix: cross platform build
Diffstat (limited to 'src/core')
| -rw-r--r-- | src/core/log.cpp | 59 | ||||
| -rw-r--r-- | src/core/log.h | 23 |
2 files changed, 62 insertions, 20 deletions
diff --git a/src/core/log.cpp b/src/core/log.cpp new file mode 100644 index 0000000..f7285f3 --- /dev/null +++ b/src/core/log.cpp @@ -0,0 +1,59 @@ +#include "core/log.h" + +// there is no standard way to ask whether a stream is a terminal +#ifdef HSDBG_WINDOWS + #define WIN32_LEAN_AND_MEAN + #define NOMINMAX + #include <windows.h> + + #include <io.h> +#else + #include <unistd.h> +#endif + +namespace Hsdbg::Log::Detail +{ + namespace + { + constexpr int STDOUT_DESCRIPTOR = 1; + constexpr int STDERR_DESCRIPTOR = 2; + +#ifdef HSDBG_WINDOWS + // a windows console prints escapes literally until it is asked to + // interpret them, so a console that refuses gets the plain format + auto probe(bool to_stderr) -> bool + { + if (_isatty(to_stderr ? STDERR_DESCRIPTOR : STDOUT_DESCRIPTOR) == 0) + return false; + + const HANDLE console = GetStdHandle(to_stderr ? STD_ERROR_HANDLE : STD_OUTPUT_HANDLE); + + if (console == nullptr || console == INVALID_HANDLE_VALUE) + return false; + + DWORD mode = 0; + + if (GetConsoleMode(console, &mode) == 0) + return false; + + if ((mode & ENABLE_VIRTUAL_TERMINAL_PROCESSING) != 0) + return true; + + return SetConsoleMode(console, mode | ENABLE_VIRTUAL_TERMINAL_PROCESSING) != 0; + } +#else + auto probe(bool to_stderr) -> bool + { + return isatty(to_stderr ? STDERR_DESCRIPTOR : STDOUT_DESCRIPTOR) != 0; + } +#endif + } + + auto is_terminal(bool to_stderr) -> bool + { + static const bool out = probe(false); + static const bool err = probe(true); + + return to_stderr ? err : out; + } +} diff --git a/src/core/log.h b/src/core/log.h index a66a61e..7caf93f 100644 --- a/src/core/log.h +++ b/src/core/log.h @@ -8,13 +8,6 @@ #include <string_view> #include <utility> -// there is no standard way to ask whether a stream is a terminal -#ifdef HSDBG_WINDOWS - #include <io.h> -#else - #include <unistd.h> -#endif - namespace Hsdbg::Log { enum class Level : uint8_t @@ -66,19 +59,9 @@ namespace Hsdbg::Log return ""; } - inline auto is_terminal(bool to_stderr) -> bool - { - constexpr int STDOUT_DESCRIPTOR = 1; - constexpr int STDERR_DESCRIPTOR = 2; - - const int descriptor = to_stderr ? STDERR_DESCRIPTOR : STDOUT_DESCRIPTOR; - -#ifdef HSDBG_WINDOWS - return _isatty(descriptor) != 0; -#else - return isatty(descriptor) != 0; -#endif - } + // true only when the stream is a terminal that understands ansi escapes, + // which needs platform calls, so it lives in log.cpp + auto is_terminal(bool to_stderr) -> bool; inline auto write(Level level, std::string_view message) -> void { |
