Reputation: 131
I tried to run the following command in Windows command prompt.
abc.exe >log.txt 2>&1
I'm expecting all output from abc.exe
to be directed to log.txt
, but it doesn't work, as the log.txt
is empty.
However, if I just execute abc.exe
, the output is showing up in Windows command prompt.
I'm not sure what is the output handler used by this application (STDOUT or STDERR), but I'm wondering is there a way to capture all messages regardless of the handler.
Upvotes: 9
Views: 4769
Reputation: 36348
Addendum: as of Windows 10 v1809, Windows finally supports pseudoconsoles. If available, this offers a better solution than using the legacy console API.
If you really need to capture that message, use the console API.
CreateConsoleScreenBuffer
and SetConsoleActiveScreenBuffer
allow you to switch to a dedicated screen buffer to avoid interfering with the existing one.
SetConsoleScreenBufferSize
can make the buffer wide enough to avoid line rollover.
SetConsoleCursorPosition
can set the cursor position as required.
After you've run the program, ReadConsoleOutput
allows you to read what it wrote to the console.
You can then use GetStdHandle(STD_OUTPUT_HANDLE)
and SetConsoleActiveScreenBuffer
to return the console to the original buffer, and CloseHandle
to close your extra buffer.
Upvotes: 1
Reputation: 11317
The symptom that console output is not visible when redirected to a file can be due to a missing flush()
in the program that writes to the standard output. However, the output should be visible when the program exits (gracefully) or when the respective buffer fills up and is flushed automatically.
Upvotes: 0