Handle char encoding on logs more correctly

This commit is contained in:
Yukino Song
2025-04-01 21:27:30 +08:00
parent a31828dd9c
commit e271592408
5 changed files with 75 additions and 6 deletions

View File

@@ -1,5 +1,7 @@
#include "utils.h"
#include <unordered_map>
#include <SetupApi.h>
#include "src/utility.h"
@@ -67,6 +69,46 @@ std::string acpToUtf8(const std::string& origStr) {
return utf8Str;
}
std::string currentCodePageToCharset() {
// Map of Windows code pages to their corresponding charset strings
static const std::unordered_map<int, std::string> codePageCharsetMap = {
{65001, "UTF-8"},
{1200, "UTF-16LE"},
{1201, "UTF-16BE"},
{1250, "windows-1250"},
{1251, "windows-1251"},
{1252, "windows-1252"},
{1253, "windows-1253"},
{1254, "windows-1254"},
{1255, "windows-1255"},
{1256, "windows-1256"},
{1257, "windows-1257"},
{1258, "windows-1258"},
{936, "GBK"}, // Simplified Chinese
{949, "EUC-KR"}, // Korean
{950, "Big5"}, // Traditional Chinese
{932, "Shift_JIS"} // Japanese
};
static std::string charsetStr;
if (charsetStr.empty()) {
// Get the active Windows code page
int codePage = GetACP();
// Find the charset in the map
auto it = codePageCharsetMap.find(codePage);
if (it != codePageCharsetMap.end()) {
charsetStr = it->second;
} else {
// Fallback charset if code page is not found in the map
charsetStr = "ISO-8859-1";
}
}
return charsetStr;
}
// Modified from https://github.com/FrogTheFrog/Sunshine/blob/b6f8573d35eff7c55da6965dfa317dc9722bd4ef/src/platform/windows/display_device/windows_utils.cpp
std::string

View File

@@ -10,6 +10,7 @@ std::wstring acpToUtf16(const std::string& origStr);
std::string utf16ToAcp(const std::wstring& utf16Str);
std::string utf8ToAcp(const std::string& utf8Str);
std::string acpToUtf8(const std::string& currentStr);
std::string currentCodePageToCharset();
std::string get_error_string(LONG error_code);