Skip to content

Commit

Permalink
[Update] Windows fast Console output
Browse files Browse the repository at this point in the history
Directly call the windows console API for ConsoleLogStream to significantly speed up output
  • Loading branch information
mrouffet committed Mar 31, 2024
1 parent ee6e60d commit f485ab6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Source/SA/Logger/Streams/Console/ConsoleColor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
18 changes: 17 additions & 1 deletion Source/SA/Logger/Streams/Console/ConsoleLogStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,34 @@

#include <Streams/Console/ConsoleLogStream.hpp>

#include <SA/Support/API/Windows.hpp>

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<DWORD>(msg.size()), 0, NULL);
#else
std::wcout << ToWString(_log) << L'\n';
#endif

SA::SetConsoleColor(CslColor::Reset);
}
Expand Down

0 comments on commit f485ab6

Please sign in to comment.