Reputation: 393
I have a batch script that starts a server. This server then prints out its logs to the STDOUT and STDERR in case of errors.
I want to redirect both STDOUT and STDERR to a tee command to split the output.
Using the tee
command from UnxUtils this worked just fine.
However in order to make my application portable I cannot depend on UnxUtils so I need to write my own tee command.
I wrote a batch script that reads out the STDOUT like suggested in this answer (2. codeblock).
This works, but only for the redirected STDOUT and not for the STDERR.
Note: Output from STDERR is missing in both the file and the command line.
How can I make it read both streams?
How I redirect the output (%1 is the server, %2 is the file I want the logs to go to)?
%1 2>&1 | %~dp0tee.bat -a %2
This works fine (as it calls UnxUtils, not my batch script):
%1 2>&1 | tee -a %2
How I read the input in tee.bat
:
for /F "tokens=*" %%a in ('findstr /n $') do (
set "line=%%a"
setlocal EnableDelayedExpansion
set "line=!line:*:=!"
echo(!line!
if %append%==true (
echo(!line! >> %output%
) else (
echo(!line! > %output%
)
endlocal
)
Upvotes: 0
Views: 1453
Reputation: 16236
PowerShell has a Tee-Object
cmdlet. The $Input
variable receives stdin.
echo asf 2>&1 | powershell -NoLogo -NoProfile -Command "$Input | Tee-Object -FilePath './tee.txt'"
See also: https://stackoverflow.com/a/38668360/447901
Upvotes: 1