From 0e0551db6b3650a7334dc23829b65ec63b5bc485 Mon Sep 17 00:00:00 2001 From: Simon Rozman Date: Tue, 20 Jan 2026 08:48:00 +0100 Subject: [PATCH] debug: convert UTF-8 to ACP on Windows Our source code assumes all-UTF-8 strings, on Windows the 8-bit active code page is admin-adjustable. Hence we need to convert at runtime. Signed-off-by: Simon Rozman --- include/stdex/debug.hpp | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/include/stdex/debug.hpp b/include/stdex/debug.hpp index c0d715d6e..a3f94e8e6 100644 --- a/include/stdex/debug.hpp +++ b/include/stdex/debug.hpp @@ -7,6 +7,7 @@ #include "compat.hpp" #include "string.hpp" +#include "unicode.hpp" #include #include #include @@ -22,8 +23,15 @@ namespace stdex _Unreferenced_(format); _Unreferenced_(arg); #elif defined(_WIN32) - auto tmp = stdex::vsprintf(format, stdex::locale_default, arg); - OutputDebugStringA(tmp.c_str()); + auto tmp = stdex::vsprintf(format, stdex::locale_utf8, arg); + auto cp = static_cast(GetACP()); + if (cp != stdex::charset_id::utf8) { + auto tmpW = stdex::str2wstr(tmp, stdex::charset_id::utf8); + auto tmpA = stdex::wstr2str(tmpW, cp); + OutputDebugStringA(tmpA.c_str()); + } + else + OutputDebugStringA(tmp.c_str()); #else vfprintf(stdout, format, arg); #endif @@ -35,7 +43,7 @@ namespace stdex _Unreferenced_(format); _Unreferenced_(arg); #elif defined(_WIN32) - auto tmp = stdex::vsprintf(format, stdex::locale_default, arg); + auto tmp = stdex::vsprintf(format, stdex::locale_utf8, arg); OutputDebugStringW(tmp.c_str()); #else vfwprintf(stdout, format, arg); @@ -74,8 +82,15 @@ namespace stdex _Unreferenced_(format); _Unreferenced_(arg); #elif defined(_WIN32) - auto tmp = stdex::vsprintf(format, stdex::locale_default, arg); - OutputDebugStringA(tmp.c_str()); + auto tmp = stdex::vsprintf(format, stdex::locale_utf8, arg); + auto cp = static_cast(GetACP()); + if (cp != stdex::charset_id::utf8) { + auto tmpW = stdex::str2wstr(tmp, stdex::charset_id::utf8); + auto tmpA = stdex::wstr2str(tmpW, cp); + OutputDebugStringA(tmpA.c_str()); + } + else + OutputDebugStringA(tmp.c_str()); #else vfprintf(stderr, format, arg); #endif @@ -87,7 +102,7 @@ namespace stdex _Unreferenced_(format); _Unreferenced_(arg); #elif defined(_WIN32) - auto tmp = stdex::vsprintf(format, stdex::locale_default, arg); + auto tmp = stdex::vsprintf(format, stdex::locale_utf8, arg); OutputDebugStringW(tmp.c_str()); #else vfwprintf(stderr, format, arg);