pythonstdoutcommand-promptautoitscite

AutoIt StdoutRead does not capture python logging module to stdout


I am trying to get the full stdout output of my python script execution available in my AutoIt calling script, I just noticed that the standard logging call is not captured by StdoutRead function in autoit.

#include "Constants.au3"
#pragma compile(Console, true)
#AutoIt3Wrapper_Change2CUI=Y



Func ExecTestScript($command, $useEnv="")
    Local $line
    Local $pid = Run("cmd.exe", @SystemDir, @SW_HIDE, $STDIN_CHILD + $STDOUT_CHILD)
    StdinWrite($pid, 'cd "' & @ScriptDir & '"' & @CRLF)
    If StringLen($useEnv) > 0 Then
        StdinWrite($pid, "conda activate " & $useEnv & @CRLF)
    EndIf
    Local $cmd = $command & @CRLF
    ConsoleWrite($cmd & @CRLF)
    StdinWrite($pid, $cmd)
    StdinWrite($pid, "exit" & @CRLF)
    Local $nChars
    While 1
        $nChars = StdoutRead($pid)
        If @error Then ExitLoop
        If @extended > 0 Then
            ConsoleWrite("++ " & $nChars & @CRLF)
            $line &= $nChars
        EndIf
    WEnd
    ProcessClose($pid)
    return $line
EndFunc

Local $res = ExecTestScript('python myTestScript.py', 'py38')

My simple python script

import logging

logging.warning("Warning this could HURT!")

print(f'Hello world!')

Here is the debug output of my autoit script.

AutoIt Output

As you can see the logging instruction appears in SciTe Output but not by means of my script which is logging in green. The SciTe Output also captures the microsoft message when DOS command prompt is openned.

Does someone has a solution or idea of the issue?


Solution

  • Thanks to @Stephan to point out the root cause of this. I have modified my code to include stderr. The python script also need to explicitly set logging level to Debug. Here is the autoit script.

    #include "Constants.au3"
    #pragma compile(Console, true)
    #AutoIt3Wrapper_Change2CUI=Y
    
    
    
    Func ExecTestScript($command, $useEnv="")
        Local $line
        Local $pid = Run("cmd.exe", @SystemDir, @SW_HIDE, $STDIN_CHILD + $STDOUT_CHILD + $STDERR_CHILD)
        StdinWrite($pid, 'cd "' & @ScriptDir & '"' & @CRLF)
        If StringLen($useEnv) > 0 Then
            StdinWrite($pid, "conda activate " & $useEnv & @CRLF)
        EndIf
        Local $cmd = $command & @CRLF
        ConsoleWrite($cmd & @CRLF)
        StdinWrite($pid, $cmd)
        StdinWrite($pid, "exit" & @CRLF)
        Local $nChars
        While 1
            $nChars = StdoutRead($pid)
    
            If @error Then ExitLoop
            If @extended > 0 Then
                ConsoleWrite("+ " & $nChars & @CRLF)
                $line &= $nChars
            EndIf
            $nChars = StderrRead($pid)
            If @error Then ExitLoop
            If @extended > 0 Then
                ConsoleWrite("> " & $nChars & @CRLF)
                $line &= $nChars
            EndIf
        WEnd
        ProcessClose($pid)
        return $line
    EndFunc
    
    Local $res = ExecTestScript('python myTestScript.py', 'py38')
    

    The python script.

    import logging
    import sys
    import time
    
    #~ logging.basicConfig(stream=sys.stderr, level=logging.DEBUG)
    logging.getLogger().setLevel(logging.DEBUG)
    
    logging.debug("Harmless debug Message")
    time.sleep(0.1)
    logging.info("Just an information")
    time.sleep(0.1)
    logging.warning("Warning this could HURT!")
    time.sleep(0.1)
    logging.error("Did you try to divide by zero")
    time.sleep(0.1)
    logging.critical("Internet is down")
    time.sleep(0.1)
    
    print(f'Hello world!')
    

    enter image description here