diff --git a/src/common/base.cpp b/src/common/base.cpp index 3876caa34f25b5434b30def7a628716e6b8a5bcd..c91bef84a52550f277fa833456e2670b2828f9eb 100644 --- a/src/common/base.cpp +++ b/src/common/base.cpp @@ -45,6 +45,9 @@ using namespace std::chrono; namespace Hdc { namespace Base { +#ifdef _WIN32 + UINT g_oldConsoleOutputCP = 0; +#endif bool g_isBackgroundServer = false; string g_tempDir = ""; uint16_t g_logFileCount = MAX_LOG_FILE_COUNT; @@ -2628,5 +2631,15 @@ void CloseOpenFd(void) fileStream.close(); return ret; } + +#ifdef _WIN32 + void RestoreConsoleOutputCP(UINT outputCP) + { + if (outputCP == 0) { + return; + } + SetConsoleOutputCP(outputCP); + } +#endif } // namespace Base } // namespace Hdc \ No newline at end of file diff --git a/src/common/base.h b/src/common/base.h index 2a5c4ac65339edc83b056fa55fc9aae65fc9c182..4acee06f5e7a2d3b4d6f9c0a9f42b72a337ff3cf 100644 --- a/src/common/base.h +++ b/src/common/base.h @@ -38,6 +38,9 @@ namespace Hdc { namespace Base { +#ifdef _WIN32 + extern UINT g_oldConsoleOutputCP; +#endif uint8_t GetLogLevel(); extern bool g_isBackgroundServer; extern uint8_t g_logLevel; @@ -293,6 +296,9 @@ namespace Base { void UpdateHeartbeatSwitchCache(); bool GetheartbeatSwitch(); bool WriteToFile(const std::string& fileName, const std::string &content, std::ios_base::openmode mode); +#ifdef _WIN32 + void RestoreConsoleOutputCP(UINT outputCP); +#endif } // namespace base } // namespace Hdc diff --git a/src/host/client.cpp b/src/host/client.cpp index 5340a8a3b3e14015fb62836868a85ca2501252b8..61fa8fdf3e4919d216cb6070c550636125a9fe5a 100755 --- a/src/host/client.cpp +++ b/src/host/client.cpp @@ -834,6 +834,9 @@ bool HdcClient::WaitFor(const string &str) std::this_thread::sleep_for(std::chrono::seconds(timeout)); wait = true; } else { +#ifdef _WIN32 + Base::RestoreConsoleOutputCP(Base::g_oldConsoleOutputCP); +#endif _exit(0); } } diff --git a/src/host/main.cpp b/src/host/main.cpp index 308c3177a70dc2ae76f1ad80d3d488d12adcc52b..c281840339a6806920ae963427195eace505f47b 100644 --- a/src/host/main.cpp +++ b/src/host/main.cpp @@ -438,23 +438,13 @@ void RunExternalClient(string &str, string &connectKey, string &containerInOut) #ifndef UNIT_TEST -#ifdef _WIN32 -static void RestoreConsoleOutputCP(UINT outputCP) -{ - if (outputCP == 0) { - return; - } - SetConsoleOutputCP(outputCP); -} -#endif - // hdc -l4 -m -s ip:port|hdc -l4 -m // hdc -l4 - s ip:port list targets int main(int argc, const char *argv[]) { Base::UpdateEnvCache(); #ifdef _WIN32 - UINT oldOutputCP = GetConsoleOutputCP(); + Base::g_oldConsoleOutputCP = GetConsoleOutputCP(); SetConsoleOutputCP(CP_UTF8); #endif string options; @@ -470,7 +460,7 @@ int main(int argc, const char *argv[]) delete[](reinterpret_cast(optArgv)); if (cmdOptionResult) { #ifdef _WIN32 - RestoreConsoleOutputCP(oldOutputCP); + Base::RestoreConsoleOutputCP(Base::g_oldConsoleOutputCP); #endif return 0; } @@ -492,7 +482,7 @@ int main(int argc, const char *argv[]) Hdc::RunClientMode(commands, g_serverListenString, g_connectKey, g_isPullServer); Hdc::Base::RemoveLogCache(); #ifdef _WIN32 - RestoreConsoleOutputCP(oldOutputCP); + Base::RestoreConsoleOutputCP(Base::g_oldConsoleOutputCP); #endif _exit(0); } @@ -542,7 +532,7 @@ int main(int argc, const char *argv[]) WRITE_LOG(LOG_DEBUG, "!!!!!!!!!Main finish main"); Hdc::Base::RemoveLogCache(); #ifdef _WIN32 - RestoreConsoleOutputCP(oldOutputCP); + Base::RestoreConsoleOutputCP(Base::g_oldConsoleOutputCP); #endif return 0; }