build_logs.py 文件源码

python
阅读 31 收藏 0 点赞 0 评论 0

项目:SoCFoundationFlow 作者: mattaw 项目源码 文件源码
def init(ctx):
    global LOGFILE
    filename = os.path.abspath(LOGFILE)
    try:
        os.makedirs(os.path.dirname(os.path.abspath(filename)))
    except OSError:
        pass

    if hasattr(os, 'O_NOINHERIT'):
        fd = os.open(LOGFILE, os.O_CREAT | os.O_TRUNC | os.O_WRONLY | os.O_NOINHERIT)
        fileobj = os.fdopen(fd, 'w')
    else:
        fileobj = open(LOGFILE, 'w')
    old_stderr = sys.stderr

    # sys.stdout has already been replaced, so __stdout__ will be faster
    #sys.stdout = log_to_file(sys.stdout, fileobj, filename)
    #sys.stderr = log_to_file(sys.stderr, fileobj, filename)
    def wrap(stream):
        if stream.isatty():
            return ansiterm.AnsiTerm(stream)
        return stream
    sys.stdout = log_to_file(wrap(sys.__stdout__), fileobj, filename)
    sys.stderr = log_to_file(wrap(sys.__stderr__), fileobj, filename)

    # now mess with the logging module...
    for x in Logs.log.handlers:
        try:
            stream = x.stream
        except AttributeError:
            pass
        else:
            if id(stream) == id(old_stderr):
                x.stream = sys.stderr
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号