From d864b67dc31fde24342b1a084be737dda5a6b077 Mon Sep 17 00:00:00 2001 From: Giacomo Travaglini Date: Thu, 11 Jun 2020 12:45:27 +0100 Subject: [PATCH] ext: Remove LogWrapper/TestLogWrapper from log.py This patch is removing: * LogWrapper (wrapping Log) * TestLogWrapper (wrapping LogWrapper) There is now a single Log class to be used for logging JIRA: https://gem5.atlassian.net/projects/GEM5/issues/GEM5-533 Change-Id: I038298565e2ccbe448664a538f888c96fdce8f4a Signed-off-by: Giacomo Travaglini Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/30234 Maintainer: Bobby R. Bruce Tested-by: kokoro Reviewed-by: Hoa Nguyen --- ext/testlib/log.py | 109 +++++++++++++----------------------------- ext/testlib/main.py | 4 +- ext/testlib/runner.py | 2 +- 3 files changed, 36 insertions(+), 79 deletions(-) diff --git a/ext/testlib/log.py b/ext/testlib/log.py index 451ee1dc6..1bdb373b2 100644 --- a/ext/testlib/log.py +++ b/ext/testlib/log.py @@ -110,7 +110,19 @@ class LibraryMessage(Record): class Log(object): - def __init__(self): + _result_typemap = { + wrappers.LoadedLibrary.__name__: LibraryResult, + wrappers.LoadedSuite.__name__: SuiteResult, + wrappers.LoadedTest.__name__: TestResult, + } + _status_typemap = { + wrappers.LoadedLibrary.__name__: LibraryStatus, + wrappers.LoadedSuite.__name__: SuiteStatus, + wrappers.LoadedTest.__name__: TestStatus, + } + + def __init__(self, test=None): + self.test = test self.handlers = [] self._opened = False # TODO Guards to methods self._closed = False # TODO Guards to methods @@ -133,39 +145,15 @@ class Log(object): for handler in self.handlers: handler.handle(record) - def add_handler(self, handler): - if self._opened: - raise Exception('Unable to add a handler once the log is open.') - self.handlers.append(handler) - - def close_handler(self, handler): - handler.close() - self.handlers.remove(handler) - -class LogWrapper(object): - _result_typemap = { - wrappers.LoadedLibrary.__name__: LibraryResult, - wrappers.LoadedSuite.__name__: SuiteResult, - wrappers.LoadedTest.__name__: TestResult, - } - _status_typemap = { - wrappers.LoadedLibrary.__name__: LibraryStatus, - wrappers.LoadedSuite.__name__: SuiteStatus, - wrappers.LoadedTest.__name__: TestStatus, - } - def __init__(self, log): - self.log_obj = log - - def log(self, *args, **kwargs): - self.log_obj.log(*args, **kwargs) - - # Library Logging Methods - # TODO Replace these methods in a test/create a wrapper? - # That way they still can log like this it's just hidden that they - # capture the current test. def message(self, message, level=LogLevel.Info, bold=False, **metadata): - self.log_obj.log(LibraryMessage(message=message, level=level, - bold=bold, **metadata)) + if self.test: + record = TestMessage(message=message, level=level, + test_uid=self.test.uid, suite_uid=self.test.parent_suite.uid) + else: + record = LibraryMessage(message=message, level=level, + bold=bold, **metadata) + + self.log(record) def error(self, message): self.message(message, LogLevel.Error) @@ -182,52 +170,21 @@ class LogWrapper(object): def trace(self, message): self.message(message, LogLevel.Trace) - # Ongoing Test Logging Methods def status_update(self, obj, status): - self.log_obj.log( - self._status_typemap[obj.__class__.__name__](obj, status)) + self.log( + self._status_typemap[obj.__class__.__name__](obj, status)) def result_update(self, obj, result): - self.log_obj.log( - self._result_typemap[obj.__class__.__name__](obj, result)) - - def test_message(self, test, message, level): - self.log_obj.log(TestMessage(message=message, level=level, - test_uid=test.uid, suite_uid=test.parent_suite.uid)) - - # NOTE If performance starts to drag on logging stdout/err - # replace metadata with just test and suite uid tags. - def test_stdout(self, test, suite, buf): - self.log_obj.log(TestStdout(buffer=buf, metadata=test.metadata)) - - def test_stderr(self, test, suite, buf): - self.log_obj.log(TestStderr(buffer=buf, metadata=test.metadata)) - - def close(self): - self.log_obj.close() - -class TestLogWrapper(object): - def __init__(self, log, test, suite): - self.log_obj = log - self.test = test - - def test_message(self, message, level): - self.log_obj.test_message(test=self.test, - message=message, level=level) - - def error(self, message): - self.test_message(message, LogLevel.Error) - - def warn(self, message): - self.test_message(message, LogLevel.Warn) + self.log( + self._result_typemap[obj.__class__.__name__](obj, result)) - def info(self, message): - self.test_message(message, LogLevel.Info) - - def debug(self, message): - self.test_message(message, LogLevel.Debug) + def add_handler(self, handler): + if self._opened: + raise Exception('Unable to add a handler once the log is open.') + self.handlers.append(handler) - def trace(self, message): - self.test_message(message, LogLevel.Trace) + def close_handler(self, handler): + handler.close() + self.handlers.remove(handler) -test_log = LogWrapper(Log()) +test_log = Log() diff --git a/ext/testlib/main.py b/ext/testlib/main.py index 9c9ed03dd..198b010ee 100644 --- a/ext/testlib/main.py +++ b/ext/testlib/main.py @@ -56,7 +56,7 @@ class RunLogHandler(): self.mp_handler = handlers.MultiprocessingHandlerWrapper( summary_handler, term_handler) self.mp_handler.async_process() - log.test_log.log_obj.add_handler(self.mp_handler) + log.test_log.add_handler(self.mp_handler) entry_message() def schedule_finalized(self, test_schedule): @@ -214,7 +214,7 @@ def do_list(): verbosity=configuration.config.verbose+log.LogLevel.Info, machine_only=configuration.config.quiet ) - log.test_log.log_obj.add_handler(term_handler) + log.test_log.add_handler(term_handler) entry_message() diff --git a/ext/testlib/runner.py b/ext/testlib/runner.py index bd50b813a..9b096376f 100644 --- a/ext/testlib/runner.py +++ b/ext/testlib/runner.py @@ -79,7 +79,7 @@ class TestParameters(object): def __init__(self, test, suite): self.test = test self.suite = suite - self.log = log.TestLogWrapper(log.test_log, test, suite) + self.log = log.Log(test) @helper.cacheresult def _fixtures(self): -- 2.30.2