Page MenuHomePhabricator

Supress console output from 3rd parties
Open, LowPublic

Description

The 3rd party libs generate console output which should be supressed. Below a piece of example code how this could be done.

A recipe for redirecting std output:

original_stdout = sys.stdout
original_stderr = sys.stderr
sys.stdout = sys.stderr = out_string = io.StringIO()

if self.__silent:

    try:
        self.__no_key_options[0](*self.__no_key_options[1:], **self.__options)
    except Exception:
        sys.stdout = original_stdout
        sys.stderr = original_stderr
        raise
    sys.stdout = original_stdout
    sys.stderr = original_stderr
    return

try:
    proc = ThreadWithReturn(target=self.__no_key_options[0],
                            args=self.__no_key_options[1:],
                            kwargs=self.__options)
    proc.start()
    while proc.is_alive():
        if self.__nested:
            print(out_string.getvalue(), end='')
        else:
            self.__log['command']['text_output'] = out_string.getvalue().split('\n')
            self.update_log()
            time.sleep(5)
    self.__py_function_return, exception = proc.get_retval()

except Exception as err:
    exception = err
sys.stdout = original_stdout

sys.stderr = original_stderr

Event Timeline

s635r triaged this task as Low priority.May 8 2019, 10:38 AM
s635r created this task.