0

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?

1
  • 1
    I have no knowledge about Phyton, but two ideas: a) logging.warning might write to CON: directly (not to STDOUT, so nothing to capture). b) logging.warning might write to STDERR (so nothing on STDOUT to capture) (I guess, it's easier for you to veryfy/falsify those ideas than it's for me)
    – Stephan
    Commented Nov 29 at 14:56

1 Answer 1

0

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.