Something similar to the linux tee
command which logs to file while it prints STDOUT.
Redirecting STDOUT makes receiving party (the script) responsible for display and logging. As per documentation:
StdoutRead()
does not block, it will return immediately. In order to get all data, it must be called in a loop.
Example:
#AutoIt3Wrapper_Change2CUI=Y
#include <Constants.au3>
#include <MsgBoxConstants.au3>
Global Enum $EXIT_OK, _
$EXIT_NOCOMPILE, _
$EXIT_ABORT
Global Const $g_sPromptError = 'Compile this script and run resulting executable instead.', _
$g_sPromptInput = 'Enter a command:', _
$g_sInputDefault = 'ping localhost -n 10'
Global $g_sCMD = '', _
$g_sSTD = ''
Main()
Func Main()
If Not @Compiled Then
MsgBox($MB_OK + $MB_ICONERROR, @ScriptName, $g_sPromptError)
Exit $EXIT_NOCOMPILE
EndIf
$g_sCMD = InputBox(@ScriptName, $g_sPromptInput, $g_sInputDefault)
If @error Then Exit $EXIT_ABORT
$g_sSTD = _getCmdStd($g_sCMD)
MsgBox($MB_OK + $MB_ICONINFORMATION, $g_sCMD, $g_sSTD)
Exit $EXIT_OK
EndFunc
Func _getCmdStd(Const $sCMD, Const $sDir = '', Const $iType = $STDERR_MERGED, Const $bShow = False, Const $iDelay = 100)
Local $sTMP = ''
Local $sSTD = ''
Local $sCOM = @ComSpec & ' /c ' & $sCMD
Local Const $iWin = $bShow ? @SW_SHOW : @SW_HIDE
Local Const $iPID = Run($sCOM, $sDir, $iWin, $iType)
While True
$sTMP = StdoutRead($iPID, False, False)
If @error Then
ExitLoop 1
ElseIf $sTMP Then
$sTMP = StringReplace($sTMP, @CR & @CR, '')
$sSTD &= $sTMP
ConsoleWrite($sTMP)
EndIf
Sleep($iDelay)
WEnd
Return SetError(@error, @extended, $sSTD)
EndFunc
Returns STDOUT (and STDERR) after execution completes, while writing to console during execution. Replace MsgBox()
as required (logging function).