diff options
author | Mikhail Redko <ovitnez@gmail.com> | 2021-04-11 20:41:44 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-04-11 19:41:44 +0200 |
commit | 1539bed3db86f2153f2d0d5fbf24bf9ee4af1d92 (patch) | |
tree | ed25236f21145d23d6e3a3fc9d7ff62675a13ba9 /src/common | |
parent | 6bdfa91fa795b24bfba6a4497784b693f6216638 (diff) |
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
Diffstat (limited to 'src/common')
-rw-r--r-- | src/common/Logging/AppenderConsole.cpp | 13 | ||||
-rw-r--r-- | src/common/Logging/AppenderConsole.h | 1 | ||||
-rw-r--r-- | src/common/Utilities/Util.cpp | 34 | ||||
-rw-r--r-- | src/common/Utilities/Util.h | 5 |
4 files changed, 51 insertions, 2 deletions
diff --git a/src/common/Logging/AppenderConsole.cpp b/src/common/Logging/AppenderConsole.cpp index af2117c6d0c..eb02f5f3ccf 100644 --- a/src/common/Logging/AppenderConsole.cpp +++ b/src/common/Logging/AppenderConsole.cpp @@ -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); } diff --git a/src/common/Logging/AppenderConsole.h b/src/common/Logging/AppenderConsole.h index 6c6be4b6c59..19c5c211028 100644 --- a/src/common/Logging/AppenderConsole.h +++ b/src/common/Logging/AppenderConsole.h @@ -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]; diff --git a/src/common/Utilities/Util.cpp b/src/common/Utilities/Util.cpp index b1cb2824305..bf724629676 100644 --- a/src/common/Utilities/Util.cpp +++ b/src/common/Utilities/Util.cpp @@ -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; diff --git a/src/common/Utilities/Util.h b/src/common/Utilities/Util.h index cf4c6cff23f..5b90f0cf86e 100644 --- a/src/common/Utilities/Util.h +++ b/src/common/Utilities/Util.h @@ -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); |