mirror of
https://github.com/TrinityCore/TrinityCore.git
synced 2026-01-15 23:20:36 +01:00
Core/Misc: Fixed utf8 encoding in console input/output. (#26352)
* Core/Misc: Fixed utf8 encoding in console input/output. * Fix gcc build * Fixed that weird 'a' with circle above it and other similar letters. Also fixed encoding in AppenderConsole which sometimes did not work as it should * Fix build on Linux * Probably better to do it like this
This commit is contained in:
@@ -163,6 +163,15 @@ void AppenderConsole::ResetColor(bool stdout_stream)
|
||||
#endif
|
||||
}
|
||||
|
||||
void AppenderConsole::Print(std::string const& str, bool error)
|
||||
{
|
||||
#if TRINITY_PLATFORM == TRINITY_PLATFORM_WINDOWS
|
||||
WriteWinConsole(str + "\n", error);
|
||||
#else
|
||||
utf8printf(error ? stderr : stdout, "%s\n", str.c_str());
|
||||
#endif
|
||||
}
|
||||
|
||||
void AppenderConsole::_write(LogMessage const* message)
|
||||
{
|
||||
bool stdout_stream = !(message->level == LOG_LEVEL_ERROR || message->level == LOG_LEVEL_FATAL);
|
||||
@@ -195,9 +204,9 @@ void AppenderConsole::_write(LogMessage const* message)
|
||||
}
|
||||
|
||||
SetColor(stdout_stream, _colors[index]);
|
||||
utf8printf(stdout_stream ? stdout : stderr, "%s%s\n", message->prefix.c_str(), message->text.c_str());
|
||||
Print(message->prefix + message->text, !stdout_stream);
|
||||
ResetColor(stdout_stream);
|
||||
}
|
||||
else
|
||||
utf8printf(stdout_stream ? stdout : stderr, "%s%s\n", message->prefix.c_str(), message->text.c_str());
|
||||
Print(message->prefix + message->text, !stdout_stream);
|
||||
}
|
||||
|
||||
@@ -53,6 +53,7 @@ class TC_COMMON_API AppenderConsole : public Appender
|
||||
private:
|
||||
void SetColor(bool stdout_stream, ColorTypes color);
|
||||
void ResetColor(bool stdout_stream);
|
||||
void Print(std::string const& str, bool error);
|
||||
void _write(LogMessage const* message) override;
|
||||
bool _colored;
|
||||
ColorTypes _colors[NUM_ENABLED_LOG_LEVELS];
|
||||
|
||||
@@ -600,6 +600,40 @@ bool Utf8ToUpperOnlyLatin(std::string& utf8String)
|
||||
return WStrToUtf8(wstr, utf8String);
|
||||
}
|
||||
|
||||
#if TRINITY_PLATFORM == TRINITY_PLATFORM_WINDOWS
|
||||
bool ReadWinConsole(std::string& str, size_t size /*= 256*/)
|
||||
{
|
||||
wchar_t* commandbuf = new wchar_t[size + 1];
|
||||
HANDLE hConsole = GetStdHandle(STD_INPUT_HANDLE);
|
||||
DWORD read;
|
||||
|
||||
if (!ReadConsoleW(hConsole, commandbuf, size, &read, NULL))
|
||||
{
|
||||
delete[] commandbuf;
|
||||
return false;
|
||||
}
|
||||
|
||||
commandbuf[read] = 0;
|
||||
|
||||
bool ok = WStrToUtf8(commandbuf, wcslen(commandbuf), str);
|
||||
delete[] commandbuf;
|
||||
return ok;
|
||||
}
|
||||
|
||||
bool WriteWinConsole(std::string_view str, bool error /*= false*/)
|
||||
{
|
||||
std::wstring wstr;
|
||||
if (!Utf8toWStr(str, wstr))
|
||||
return false;
|
||||
|
||||
HANDLE hConsole = GetStdHandle(error ? STD_ERROR_HANDLE : STD_OUTPUT_HANDLE);
|
||||
DWORD toWrite = wstr.size();
|
||||
DWORD write;
|
||||
|
||||
return WriteConsoleW(hConsole, wstr.c_str(), wstr.size(), &write, NULL);
|
||||
}
|
||||
#endif
|
||||
|
||||
std::string Trinity::Impl::ByteArrayToHexStr(uint8 const* bytes, size_t arrayLen, bool reverse /* = false */)
|
||||
{
|
||||
int32 init = 0;
|
||||
|
||||
@@ -292,6 +292,11 @@ TC_COMMON_API void utf8printf(FILE* out, const char *str, ...);
|
||||
TC_COMMON_API void vutf8printf(FILE* out, const char *str, va_list* ap);
|
||||
TC_COMMON_API bool Utf8ToUpperOnlyLatin(std::string& utf8String);
|
||||
|
||||
#if TRINITY_PLATFORM == TRINITY_PLATFORM_WINDOWS
|
||||
TC_COMMON_API bool ReadWinConsole(std::string& str, size_t size = 256);
|
||||
TC_COMMON_API bool WriteWinConsole(std::string_view str, bool error = false);
|
||||
#endif
|
||||
|
||||
TC_COMMON_API bool IsIPAddress(char const* ipaddress);
|
||||
|
||||
TC_COMMON_API uint32 CreatePIDFile(std::string const& filename);
|
||||
|
||||
@@ -78,11 +78,7 @@ namespace Trinity::Impl::Readline
|
||||
void utf8print(void* /*arg*/, std::string_view str)
|
||||
{
|
||||
#if TRINITY_PLATFORM == TRINITY_PLATFORM_WINDOWS
|
||||
std::wstring wbuf;
|
||||
if (!Utf8toWStr(str, wbuf))
|
||||
return;
|
||||
|
||||
wprintf(L"%s", wbuf.c_str());
|
||||
WriteWinConsole(str);
|
||||
#else
|
||||
{
|
||||
printf(STRING_VIEW_FMT, STRING_VIEW_FMT_ARG(str));
|
||||
@@ -139,14 +135,10 @@ void CliThread()
|
||||
std::string command;
|
||||
|
||||
#if TRINITY_PLATFORM == TRINITY_PLATFORM_WINDOWS
|
||||
wchar_t commandbuf[256];
|
||||
if (fgetws(commandbuf, sizeof(commandbuf), stdin))
|
||||
if (!ReadWinConsole(command))
|
||||
{
|
||||
if (!WStrToUtf8(commandbuf, wcslen(commandbuf), command))
|
||||
{
|
||||
PrintCliPrefix();
|
||||
continue;
|
||||
}
|
||||
PrintCliPrefix();
|
||||
continue;
|
||||
}
|
||||
#else
|
||||
char* command_str = readline(CLI_PREFIX);
|
||||
|
||||
Reference in New Issue
Block a user