diff options
author | Ruomei Yan <ruomei.yan@arm.com> | 2022-12-13 22:02:21 +0000 |
---|---|---|
committer | Ruomei Yan <ruomei.yan@arm.com> | 2023-01-16 16:31:23 +0000 |
commit | 4eb3fef8e5876c69dc6bac70fdc010805d5b97f2 (patch) | |
tree | 88beca8a30954f020b7f34c0a3f9df780b966244 /src/mlia/utils/logging.py | |
parent | 5800fc990ed1e36ce7d06670f911fbb12a0ec771 (diff) | |
download | mlia-4eb3fef8e5876c69dc6bac70fdc010805d5b97f2.tar.gz |
MLIA-741/2 Report test results
- add version extraction function in compat.py
- create Metadata, MLIAMetadata, TOSAMetadata and MetadataDisplay classes
- update the reporting functions so tosa and mlia version will be displayed in output json
- update unit test test_configure_and_get_tosa_advisor to mock the get_events function
- update the copyright information of all changed/added files
- handle exception and report to json when program crashes
- write new context managers for capturing stderr and stdout
- support reporting stderr to json output
- support reporting model checksum and model name to json output
- made changes in test_e2e.py handling {model_name} replacement in --output
- add unit tests
Change-Id: I6629fd1c5754378e6accd488217c83d87c7eb6f1
Diffstat (limited to 'src/mlia/utils/logging.py')
-rw-r--r-- | src/mlia/utils/logging.py | 37 |
1 files changed, 32 insertions, 5 deletions
diff --git a/src/mlia/utils/logging.py b/src/mlia/utils/logging.py index cf7ad27..0659dcf 100644 --- a/src/mlia/utils/logging.py +++ b/src/mlia/utils/logging.py @@ -1,4 +1,4 @@ -# SPDX-FileCopyrightText: Copyright 2022, Arm Limited and/or its affiliates. +# SPDX-FileCopyrightText: Copyright 2022-2023, Arm Limited and/or its affiliates. # SPDX-License-Identifier: Apache-2.0 """Logging utility functions.""" from __future__ import annotations @@ -54,10 +54,10 @@ def redirect_output( @contextmanager -def redirect_raw( - logger: logging.Logger, output: TextIO, log_level: int +def process_raw_output( + consumer: Callable[[str], None], output: TextIO ) -> Generator[None, None, None]: - """Redirect output using file descriptors.""" + """Process output on file descriptor level.""" with tempfile.TemporaryFile(mode="r+") as tmp: old_output_fd: int | None = None try: @@ -73,7 +73,21 @@ def redirect_raw( tmp.seek(0) for line in tmp.readlines(): - logger.log(log_level, line.rstrip()) + consumer(line) + + +@contextmanager +def redirect_raw( + logger: logging.Logger, output: TextIO, log_level: int +) -> Generator[None, None, None]: + """Redirect output using file descriptors.""" + + def consumer(line: str) -> None: + """Redirect output to the logger.""" + logger.log(log_level, line.rstrip()) + + with process_raw_output(consumer, output): + yield @contextmanager @@ -94,6 +108,19 @@ def redirect_raw_output( yield +@contextmanager +def capture_raw_output(output: TextIO) -> Generator[list[str], None, None]: + """Capture output as list of strings.""" + result: list[str] = [] + + def consumer(line: str) -> None: + """Save output for later processing.""" + result.append(line) + + with process_raw_output(consumer, output): + yield result + + class LogFilter(logging.Filter): """Configurable log filter.""" |