diff options
Diffstat (limited to 'src/core/log.cpp')
| -rw-r--r-- | src/core/log.cpp | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/src/core/log.cpp b/src/core/log.cpp new file mode 100644 index 0000000..22bba7a --- /dev/null +++ b/src/core/log.cpp @@ -0,0 +1,56 @@ +#include "core/log.h" + +#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 + 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; + } +} |
