-
Notifications
You must be signed in to change notification settings - Fork 5k
Fix extra carriage returns in piped ANSI output on Windows #3442
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: v1.x
Are you sure you want to change the base?
Conversation
Now lets test on windows
Added default value to argument
Changed improper misplaced includes.
|
@gabime any updates / recommendations on this ? |
|
@gabime Any progress here ? |
|
The ansicolor sink was never designed for windows. Why not use the wincolor sink |
|
New terminal on windows can be set to understand ANSI escape code. https://learn.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences |
| # Static/Shared library | ||
| # --------------------------------------------------------------------------------------- | ||
| set(SPDLOG_SRCS src/spdlog.cpp src/stdout_sinks.cpp src/color_sinks.cpp src/file_sinks.cpp src/async.cpp src/cfg.cpp) | ||
| list(APPEND SPDLOG_SRCS include/spdlog/sinks/ansicolor_sink-inl.h) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be included in https://github.com/gabime/spdlog/blob/v1.x/src/stdout_sinks.cpp instead
| #ifdef _WIN32 | ||
| DWORD bytes_written = 0; | ||
| HANDLE h = reinterpret_cast<HANDLE>(_get_osfhandle(_fileno(target_file_))); | ||
| WriteFile(h, formatted.data() + start, static_cast<DWORD>(end - start), &bytes_written, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
_get_osfhandle shoild be called only once at construction time and store the result in a class member
| #include <spdlog/details/os.h> | ||
| #include <spdlog/pattern_formatter.h> | ||
|
|
||
| #if defined(_WIN32) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please use #ifdef _WIN32
Description
This PR fixes incorrect line endings in ANSI-colored output when redirected or piped on Windows terminals. A possible fix for #3138.
Problem
When using
fwrite()on Windows, the CRT applies text-mode translation, converting\nto\r\n. This results in an extra carriage return (\r) being injected, especially noticeable when ANSI-colored output is redirected or piped.Solution
On Windows, replace
fwrite()with a direct call toWriteFile()using the native file handle obtained from_get_osfhandle(_fileno(...)). This bypasses the CRT entirely, ensuring output is written exactly as intended, without automatic newline translation.Code Change
CMake
Also included ansicolor_sink-inl.h explicitly in the build to avoid linking issues (when linking to static library).