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

View File

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