Search code examples
pythonloggingsubprocesswxpython

Print output produced by logging.info in a wxpython text control


Basically what I want to do is get the output produced by a command line executable such as coursera-dl into a text control. But it(command line executable) prints it output using logging.info and it seems that subprocess is not able to read the logging.info print out but when the logging.info function is changed to print, wxpython is able to read the output from cmd into a textctrl. I use python27. My code from bunch of codes put together from the net:

self.courses_list = ""
def execute(self,command,textctrl):
        #clear the textctrl
        #try:
        textctrl.SetValue("")
        si=subprocess.STARTUPINFO()
        si.dwFlags |= subprocess.STARTF_USESHOWWINDOW
        process = subprocess.Popen(command,stdout=subprocess.PIPE,**self.subprocess_args(False))
        output = ''
        self.out=[]
   # Poll process for new output until finished
    for line in iter(process.stdout.readline, ""):
        textctrl.AppendText(line)
        output += line#.strip().decode('utf-8')
        self.courses_list+=line
        print(line)
        self.out.append(line)

    process.wait()
    exitCode = process.returncode

    if (exitCode == 0):
        return output
    else:
        raise Exception(command, exitCode, output)

def subprocess_args(self,include_stdout=True):
    if hasattr(subprocess, 'STARTUPINFO'):
        si=subprocess.STARTUPINFO()
        si.dwFlags |= subprocess.STARTF_USESHOWWINDOW
        env = os.environ
    else:
        si=None
        env=None

    if include_stdout:
        ret={'stdout:':subprocess.PIPE}
    else:
        ret={}

    ret.update({'stdin':subprocess.PIPE,
                'stderr':subprocess.PIPE,
                'startupinfo':si,
                'env':env})
    return ret

Solution

  • It appears it has something to do with reading of the output from stderr. So changing 'stderr':subprocess.PIPE to 'stderr':subprocess.STDOUT solves the problem. So here's how the change at the ret part looks like

        ret.update({'stdin':subprocess.PIPE,
                'stderr':subprocess.STDOUT,#here's the solution
                'startupinfo':si,
                'env':env})