def _indent(stream, indent, *msgs):
    """ write a message to a text stream, with indentation. Also ensures that
    the output encoding of the messages is safe for writing. """
    for x in range(0, indent):
        stream.write('  ')
    for x in msgs:
        stream.write(x.encode('ascii', 'backslashreplace').decode('ascii'))
    stream.write('\n')
