'Redirect output from the with block to a file.
Hijacks stdout / stderr and writes to the pipe
connected to the logger daemon'

def __enter__(self):

    self._force_color = color._force_color
    self._debug = tty._debug
    write = self.write
    try:
        self._stdout = os.dup(sys.stdout.fileno())
        self._stderr = os.dup(sys.stderr.fileno())
        os.dup2(write, sys.stdout.fileno())
        os.dup2(write, sys.stderr.fileno())
    except AttributeError:
        self.directAssignment = True
        self._stdout = sys.stdout
        self._stderr = sys.stderr
        output_redirect = os.fdopen(write, 'w')
        sys.stdout = output_redirect
        sys.stderr = output_redirect
    if self.force_color:
        color._force_color = True
    if self.debug:
        tty._debug = True
