From f485ab6ea6bfe7270683e4c50d659f865ee70d8d Mon Sep 17 00:00:00 2001 From: Maxime ROUFFET Date: Sun, 31 Mar 2024 10:17:37 +0900 Subject: [PATCH] [Update] Windows fast Console output Directly call the windows console API for ConsoleLogStream to significantly speed up output --- .../SA/Logger/Streams/Console/ConsoleColor.cpp | 2 +- .../Streams/Console/ConsoleLogStream.cpp | 18 +++++++++++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/Source/SA/Logger/Streams/Console/ConsoleColor.cpp b/Source/SA/Logger/Streams/Console/ConsoleColor.cpp index 1087fc6..bba061d 100644 --- a/Source/SA/Logger/Streams/Console/ConsoleColor.cpp +++ b/Source/SA/Logger/Streams/Console/ConsoleColor.cpp @@ -11,7 +11,7 @@ namespace SA #if SA_WIN /// Windows console handle. - HANDLE cslHandle = GetStdHandle(STD_OUTPUT_HANDLE); + static HANDLE cslHandle = GetStdHandle(STD_OUTPUT_HANDLE); void SetConsoleColor(ConsoleColor _cslColor) { diff --git a/Source/SA/Logger/Streams/Console/ConsoleLogStream.cpp b/Source/SA/Logger/Streams/Console/ConsoleLogStream.cpp index 7f15442..ff4b152 100644 --- a/Source/SA/Logger/Streams/Console/ConsoleLogStream.cpp +++ b/Source/SA/Logger/Streams/Console/ConsoleLogStream.cpp @@ -4,18 +4,34 @@ #include +#include + namespace SA { +#if SA_WIN + /// Windows console handle. + static HANDLE cslHandle = GetStdHandle(STD_OUTPUT_HANDLE); +#endif + void ConsoleLogStream::Flush() { +#if SA_WIN + FlushConsoleInputBuffer(cslHandle); +#else std::wcout.flush(); +#endif } void ConsoleLogStream::Output(const Log& _log) { theme.SetConsoleColorFromLevel(_log.level); - std::wcout << ToWString(_log) << std::endl; +#if SA_WIN + std::wstring msg = ToWString(_log) + L'\n'; + WriteConsoleW(cslHandle, msg.data(), static_cast(msg.size()), 0, NULL); +#else + std::wcout << ToWString(_log) << L'\n'; +#endif SA::SetConsoleColor(CslColor::Reset); }