From 97b1dca7b878cffec3204b5e4e4a949f60215039 Mon Sep 17 00:00:00 2001 From: Thomas Nagy Date: Tue, 1 Apr 2014 23:06:10 +0200 Subject: [PATCH] Issue 1374 - close config.log --- waflib/Context.py | 12 ++++++++++++ waflib/Logs.py | 29 ++++++++++++++++++++++++----- waflib/Scripting.py | 6 +++++- 3 files changed, 41 insertions(+), 6 deletions(-) diff --git a/waflib/Context.py b/waflib/Context.py index 3cce32aa..196feacf 100644 --- a/waflib/Context.py +++ b/waflib/Context.py @@ -188,6 +188,18 @@ class Context(ctx): """ return id(self) + def finalize(self): + """ + Use to free resources such as open files potentially held by the logger + """ + try: + logger = self.logger + except AttributeError: + pass + else: + Logs.free_logger(logger) + delattr(self, 'logger') + def load(self, tool_list, *k, **kw): """ Load a Waf tool as a module, and try calling the function named :py:const:`waflib.Context.Context.fun` from it. diff --git a/waflib/Logs.py b/waflib/Logs.py index 2ea4c064..2b0817d3 100644 --- a/waflib/Logs.py +++ b/waflib/Logs.py @@ -269,8 +269,15 @@ def make_logger(path, name): from waflib import Logs bld.logger = Logs.make_logger('test.log', 'build') bld.check(header_name='sadlib.h', features='cxx cprogram', mandatory=False) + + # have the file closed immediately + Logs.free_logger(bld.logger) + + # stop logging bld.logger = None + The method finalize() of the command will try to free the logger, if any + :param path: file name to write the log output to :type path: string :param name: logger name (loggers are reused) @@ -284,7 +291,7 @@ def make_logger(path, name): logger.setLevel(logging.DEBUG) return logger -def make_mem_logger(name, to_log, size=10000): +def make_mem_logger(name, to_log, size=8192): """ Create a memory logger to avoid writing concurrently to the main logger """ @@ -298,7 +305,19 @@ def make_mem_logger(name, to_log, size=10000): logger.setLevel(logging.DEBUG) return logger -def pprint(col, str, label='', sep='\n'): +def free_logger(logger): + """ + Free the resources held by the loggers created through make_logger or make_mem_logger. + This is used for file cleanup and for handler removal (logger objects are re-used). + """ + try: + for x in logger.handlers: + x.close() + logger.removeHandler(x) + except Exception as e: + pass + +def pprint(col, msg, label='', sep='\n'): """ Print messages in color immediately on stderr:: @@ -307,12 +326,12 @@ def pprint(col, str, label='', sep='\n'): :param col: color name to use in :py:const:`Logs.colors_lst` :type col: string - :param str: message to display - :type str: string or a value that can be printed by %s + :param msg: message to display + :type msg: string or a value that can be printed by %s :param label: a message to add after the colored output :type label: string :param sep: a string to append at the end (line separator) :type sep: string """ - info("%s%s%s %s" % (colors(col), str, colors.NORMAL, label), extra={'terminator':sep}) + info("%s%s%s %s" % (colors(col), msg, colors.NORMAL, label), extra={'terminator':sep}) diff --git a/waflib/Scripting.py b/waflib/Scripting.py index 2a9488be..047ad21e 100644 --- a/waflib/Scripting.py +++ b/waflib/Scripting.py @@ -234,7 +234,11 @@ def run_commands(): run_command('init') while Options.commands: cmd_name = Options.commands.pop(0) - ctx = run_command(cmd_name) + try: + ctx = run_command(cmd_name) + finally: + # Issue 1374 + ctx.finalize() Logs.info('%r finished successfully (%s)' % (cmd_name, str(ctx.log_timer))) run_command('shutdown')