[deploy] fix Logger output stream redirection

This commit is contained in:
Nomango 2020-04-01 15:19:19 +08:00
parent a1ed803bc1
commit d459db8173
2 changed files with 17 additions and 19 deletions

View File

@ -178,7 +178,7 @@ Logger::Logger()
, output_stream_(std::cout.rdbuf()) , output_stream_(std::cout.rdbuf())
, error_stream_(std::cerr.rdbuf()) , error_stream_(std::cerr.rdbuf())
{ {
ResetOutputStream(); ResetStreamToStdStream();
} }
Logger::~Logger() Logger::~Logger()
@ -277,7 +277,7 @@ void Logger::Output(Level level, StringStream& sstream)
} }
} }
void Logger::ResetOutputStream() void Logger::ResetStreamToStdStream()
{ {
bool has_console = ::GetConsoleWindow() != nullptr; bool has_console = ::GetConsoleWindow() != nullptr;
if (has_console) if (has_console)
@ -302,17 +302,17 @@ void Logger::ResetOutputStream()
(void)std::cout.imbue(std::locale()); (void)std::cout.imbue(std::locale());
(void)std::cerr.imbue(std::locale()); (void)std::cerr.imbue(std::locale());
RedirectOutputStreamBuffer(std::cout.rdbuf()); RedirectOutputStream(std::cout.rdbuf());
RedirectErrorStreamBuffer(std::cerr.rdbuf()); RedirectErrorStream(std::cerr.rdbuf());
} }
} }
std::streambuf* Logger::RedirectOutputStreamBuffer(std::streambuf* buf) std::streambuf* Logger::RedirectOutputStream(std::streambuf* buf)
{ {
return output_stream_.rdbuf(buf); return output_stream_.rdbuf(buf);
} }
std::streambuf* Logger::RedirectErrorStreamBuffer(std::streambuf* buf) std::streambuf* Logger::RedirectErrorStream(std::streambuf* buf)
{ {
return error_stream_.rdbuf(buf); return error_stream_.rdbuf(buf);
} }
@ -335,6 +335,8 @@ void Logger::ShowConsole(bool show)
} }
else else
{ {
ResetStreamToStdStream();
// disable the close button of console // disable the close button of console
HMENU hmenu = ::GetSystemMenu(console, FALSE); HMENU hmenu = ::GetSystemMenu(console, FALSE);
::RemoveMenu(hmenu, SC_CLOSE, MF_BYCOMMAND); ::RemoveMenu(hmenu, SC_CLOSE, MF_BYCOMMAND);
@ -355,8 +357,6 @@ void Logger::ShowConsole(bool show)
} }
} }
} }
ResetOutputStream();
} }
} // namespace kiwano } // namespace kiwano

View File

@ -90,11 +90,6 @@ public:
template <typename... _Args> template <typename... _Args>
void Println(Level level, _Args&&... args); void Println(Level level, _Args&&... args);
/// \~chinese
/// @brief 显示或关闭控制台
/// @note 此操作会重置输出流
void ShowConsole(bool show);
/// \~chinese /// \~chinese
/// @brief 启用日志 /// @brief 启用日志
void Enable(); void Enable();
@ -108,20 +103,21 @@ public:
OutputStream& GetOutputStream(); OutputStream& GetOutputStream();
/// \~chinese /// \~chinese
/// @brief 获取错误 /// @brief 获取错误输出
OutputStream& GetErrorStream(); OutputStream& GetErrorStream();
/// \~chinese /// \~chinese
/// @brief 重定向输出流 /// @brief 重定向输出流
std::streambuf* RedirectOutputStreamBuffer(std::streambuf* buf); std::streambuf* RedirectOutputStream(std::streambuf* buf);
/// \~chinese /// \~chinese
/// @brief 重定向错误 /// @brief 重定向错误输出
std::streambuf* RedirectErrorStreamBuffer(std::streambuf* buf); std::streambuf* RedirectErrorStream(std::streambuf* buf);
/// \~chinese /// \~chinese
/// @brief 重置输出流 /// @brief 显示或关闭控制台
void ResetOutputStream(); /// @note 此操作会重定向输出流到标准输出流
void ShowConsole(bool show);
private: private:
Logger(); Logger();
@ -132,6 +128,8 @@ private:
void Output(Level level, StringStream& sstream); void Output(Level level, StringStream& sstream);
void ResetStreamToStdStream();
void ResetConsoleColor() const; void ResetConsoleColor() const;
OutputStream& DefaultOutputColor(OutputStream& out); OutputStream& DefaultOutputColor(OutputStream& out);