From 7e3d9518827ef33073c6cc039cf972e879592820 Mon Sep 17 00:00:00 2001 From: Jussi Pakkanen Date: Mon, 8 Jul 2013 20:04:02 +0300 Subject: [PATCH] Output to console and log file. --- meson.py | 1 + mlog.py | 20 ++++++++++++++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/meson.py b/meson.py index 379972a39..5d32f9887 100755 --- a/meson.py +++ b/meson.py @@ -95,6 +95,7 @@ class MesonApp(): def generate(self): env = environment.Environment(self.source_dir, self.build_dir, self.meson_script_file, options) + mlog.initialize(env.get_log_dir()) b = build.Build(env) intr = interpreter.Interpreter(b) intr.run() diff --git a/mlog.py b/mlog.py index 7800959e8..fb9418e07 100644 --- a/mlog.py +++ b/mlog.py @@ -19,6 +19,13 @@ information about Meson runs. Some output goes to screen, some to logging dir and some goes to both.""" colorize_console = platform.system().lower() != 'windows' and os.isatty(sys.stdout.fileno()) +log_dir = None +log_file = None + +def initialize(logdir): + global log_dir, log_file + log_dir = logdir + log_file = open(os.path.join(logdir, 'meson-log.txt'), 'w') class AnsiDecorator(): plain_code = "\033[0m" @@ -30,6 +37,7 @@ class AnsiDecorator(): def get_text(self, with_codes): if with_codes: return self.code + self.text + AnsiDecorator.plain_code + return self.text def bold(text): return AnsiDecorator(text, "\033[1m") @@ -43,13 +51,21 @@ def green(text): def cyan(text): return AnsiDecorator(text, "\033[1;36m") -def log(*args): +def process_markup(args, keep): arr = [] for arg in args: if isinstance(arg, str): arr.append(arg) elif isinstance(arg, AnsiDecorator): - arr.append(arg.get_text(colorize_console)) + arr.append(arg.get_text(keep)) else: arr.append(str(arg)) + return arr + +def log(*args): + arr = process_markup(args, False) + if log_file is not None: + print(*arr, file=log_file) # Log file never gets ANSI codes. + if colorize_console: + arr = process_markup(args, True) print(*arr)