[英]unable to redirect output message in windows command prompt (cmd.exe)
I tried to run the following command in Windows command prompt. 我试图在Windows命令提示符下运行以下命令。
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. 我希望
abc.exe
所有输出都定向到log.txt
,但由于log.txt
为空,因此它不起作用。
However, if I just execute abc.exe
, the output is showing up in Windows command prompt. 但是,如果我只执行
abc.exe
,则输出将显示在Windows命令提示符下。
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. 我不确定此应用程序使用的输出处理程序是什么(STDOUT或STDERR),但是我想知道是否有一种方法可以捕获所有消息,而与处理程序无关。
Addendum: as of Windows 10 v1809, Windows finally supports pseudoconsoles . 附录:从Windows 10 v1809开始,Windows 最终支持伪控制台。 If available, this offers a better solution than using the legacy console API.
如果有的话,这提供了比使用旧版控制台API更好的解决方案。
If you really need to capture that message, use the console API . 如果您确实需要捕获该消息,请使用控制台API 。
CreateConsoleScreenBuffer
and SetConsoleActiveScreenBuffer
allow you to switch to a dedicated screen buffer to avoid interfering with the existing one. CreateConsoleScreenBuffer
和SetConsoleActiveScreenBuffer
允许您切换到专用的屏幕缓冲区,以避免干扰现有的屏幕缓冲区。
SetConsoleScreenBufferSize
can make the buffer wide enough to avoid line rollover. SetConsoleScreenBufferSize
可以使缓冲区足够宽,以避免行翻转。
SetConsoleCursorPosition
can set the cursor position as required. SetConsoleCursorPosition
可以根据需要设置光标位置。
After you've run the program, ReadConsoleOutput
allows you to read what it wrote to the console. 运行该程序后,
ReadConsoleOutput
允许您读取它写入控制台的内容。
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. 然后,您可以使用
GetStdHandle(STD_OUTPUT_HANDLE)
和SetConsoleActiveScreenBuffer
将控制台返回到原始缓冲区,并使用CloseHandle
关闭额外的缓冲区。
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. 控制台输出重定向到文件时不可见的症状可能是由于写入标准输出的程序中缺少
flush()
造成的。 However, the output should be visible when the program exits (gracefully) or when the respective buffer fills up and is flushed automatically. 但是,当程序退出(正常)或相应的缓冲区填满并自动刷新时,输出应该可见。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.