我向waf build命令添加了一个自定义记录器,如下所示。该wscript
的输出如下所示。
但是我想在终端上打印 normal waf记录器,然后将normal + custom logger输出写入日志文件。这可能吗?问题是 normal 输出在大多数情况下是足够的,并且自定义记录器添加的冗长性大大降低了构建速度。
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
top = '.'
out = 'build'
VERSION = '0.0.0'
APPNAME = 'app'
from waflib import Configure, Logs
import logging
Configure.autoconfig = True
def options(opt):
opt.load('compiler_c')
def configure(conf):
conf.load('compiler_c')
conf.path.make_node('main.c').write(
'#include <stdio.h>\n\nint main(int argc, char* argv[]) {\n return 0;\n}\n')
def build(bld):
import sys
import os
log_file = os.path.join(out, 'build.log')
bld.logger = Logs.make_logger(log_file, out)
hdlr = logging.StreamHandler(sys.stdout)
formatter = logging.Formatter('%(message)s')
hdlr.setFormatter(formatter)
bld.logger.addHandler(hdlr)
bld.program(target='app', source='main.c')
产生的输出是这样的:
$ python waf
Configuring the project
Setting top to : /cygdrive/d/log
Setting out to : /cygdrive/d/log/build
Checking for 'gcc' (C compiler) : /usr/bin/gcc
Waf: Entering directory `/cygdrive/d/log/build'
[1/2] Compiling main.c
['/usr/bin/gcc', '../main.c', '-c', '-o/cygdrive/d/log/build/main.c.1.o']
[2/2] Linking build/app.exe
['/usr/bin/gcc', '-Wl,--enable-auto-import', 'main.c.1.o', '-o/cygdrive/d/log/build/app.exe', '-Wl,-Bstatic', '-Wl,-Bdynamic']
Waf: Leaving directory `/cygdrive/d/log/build'
'build' finished successfully (0.473s)
我需要的是这样的终端输出:
[1/2] Compiling main.c
[2/2] Linking build/app.exe
和这样的日志文件输出:
[1/2] Compiling [32mmain.c[0m
['/usr/bin/gcc', '../main.c', '-c', '-o/cygdrive/d/log/build/main.c.1.o']
[2/2] Linking [33mbuild/app.exe[0m
['/usr/bin/gcc', '-Wl,--enable-auto-import', 'main.c.1.o', '-o/cygdrive/d/log/build/app.exe', '-Wl,-Bstatic', '-Wl,-Bdynamic']
奖金:如何在日志文件输出中仅仅删除不可打印的字符?