From patchwork Fri Nov 4 11:05:18 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Juraj_Linke=C5=A1?= X-Patchwork-Id: 119483 X-Patchwork-Delegate: thomas@monjalon.net Return-Path: X-Original-To: patchwork@inbox.dpdk.org Delivered-To: patchwork@inbox.dpdk.org Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 76005A00C5; Fri, 4 Nov 2022 12:06:03 +0100 (CET) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 514EC42D3D; Fri, 4 Nov 2022 12:05:39 +0100 (CET) Received: from lb.pantheon.sk (lb.pantheon.sk [46.229.239.20]) by mails.dpdk.org (Postfix) with ESMTP id 9E76742D2C for ; Fri, 4 Nov 2022 12:05:37 +0100 (CET) Received: from localhost (localhost [127.0.0.1]) by lb.pantheon.sk (Postfix) with ESMTP id E100E1BA5AD; Fri, 4 Nov 2022 12:05:36 +0100 (CET) X-Virus-Scanned: amavisd-new at siecit.sk Received: from lb.pantheon.sk ([127.0.0.1]) by localhost (lb.pantheon.sk [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id LloK4K_XWtvg; Fri, 4 Nov 2022 12:05:33 +0100 (CET) Received: from entguard.lab.pantheon.local (unknown [46.229.239.141]) by lb.pantheon.sk (Postfix) with ESMTP id F082B1BA5AB; Fri, 4 Nov 2022 12:05:27 +0100 (CET) From: =?utf-8?q?Juraj_Linke=C5=A1?= To: thomas@monjalon.net, Honnappa.Nagarahalli@arm.com, ohilyard@iol.unh.edu, lijuan.tu@intel.com, kda@semihalf.com, bruce.richardson@intel.com Cc: dev@dpdk.org, =?utf-8?q?Juraj_Linke=C5=A1?= Subject: [PATCH v8 4/9] dts: add basic logging facility Date: Fri, 4 Nov 2022 11:05:18 +0000 Message-Id: <20221104110523.511367-5-juraj.linkes@pantheon.tech> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221104110523.511367-1-juraj.linkes@pantheon.tech> References: <20221103151934.450887-1-juraj.linkes@pantheon.tech> <20221104110523.511367-1-juraj.linkes@pantheon.tech> MIME-Version: 1.0 X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org The logging module provides loggers distinguished by two attributes, a custom format and a verbosity switch. The loggers log to both console and more verbosely to files. Signed-off-by: Owen Hilyard Signed-off-by: Juraj Linkeš --- .gitignore | 3 + dts/framework/logger.py | 113 ++++++++++++++++++++++++++++++++++++++ dts/framework/settings.py | 23 ++++++++ 3 files changed, 139 insertions(+) create mode 100644 dts/framework/logger.py diff --git a/.gitignore b/.gitignore index 212c7aa28e..01a47a7606 100644 --- a/.gitignore +++ b/.gitignore @@ -36,6 +36,9 @@ TAGS # ignore python bytecode files *.pyc +# DTS results +dts/output + # ignore default build directory, and directories from test-meson-builds.sh build build-* diff --git a/dts/framework/logger.py b/dts/framework/logger.py new file mode 100644 index 0000000000..a31fcc8242 --- /dev/null +++ b/dts/framework/logger.py @@ -0,0 +1,113 @@ +# SPDX-License-Identifier: BSD-3-Clause +# Copyright(c) 2010-2014 Intel Corporation +# Copyright(c) 2022 PANTHEON.tech s.r.o. +# Copyright(c) 2022 University of New Hampshire + +""" +DTS logger module with several log level. DTS framework and TestSuite logs +are saved in different log files. +""" + +import logging +import os.path +from typing import TypedDict + +from .settings import SETTINGS + +date_fmt = "%Y/%m/%d %H:%M:%S" +stream_fmt = "%(asctime)s - %(name)s - %(levelname)s - %(message)s" + + +class LoggerDictType(TypedDict): + logger: "DTSLOG" + name: str + node: str + + +# List for saving all using loggers +Loggers: list[LoggerDictType] = [] + + +class DTSLOG(logging.LoggerAdapter): + """ + DTS log class for framework and testsuite. + """ + + logger: logging.Logger + node: str + sh: logging.StreamHandler + fh: logging.FileHandler + verbose_fh: logging.FileHandler + + def __init__(self, logger: logging.Logger, node: str = "suite"): + self.logger = logger + # 1 means log everything, this will be used by file handlers if their level + # is not set + self.logger.setLevel(1) + + self.node = node + + # add handler to emit to stdout + sh = logging.StreamHandler() + sh.setFormatter(logging.Formatter(stream_fmt, date_fmt)) + sh.setLevel(logging.INFO) # console handler default level + + if SETTINGS.verbose is True: + sh.setLevel(logging.DEBUG) + + self.logger.addHandler(sh) + self.sh = sh + + logging_path_prefix = os.path.join(SETTINGS.output_dir, node) + + fh = logging.FileHandler(f"{logging_path_prefix}.log") + fh.setFormatter( + logging.Formatter( + fmt="%(asctime)s - %(name)s - %(levelname)s - %(message)s", + datefmt=date_fmt, + ) + ) + + self.logger.addHandler(fh) + self.fh = fh + + # This outputs EVERYTHING, intended for post-mortem debugging + # Also optimized for processing via AWK (awk -F '|' ...) + verbose_fh = logging.FileHandler(f"{logging_path_prefix}.verbose.log") + verbose_fh.setFormatter( + logging.Formatter( + fmt="%(asctime)s|%(name)s|%(levelname)s|%(pathname)s|%(lineno)d|" + "%(funcName)s|%(process)d|%(thread)d|%(threadName)s|%(message)s", + datefmt=date_fmt, + ) + ) + + self.logger.addHandler(verbose_fh) + self.verbose_fh = verbose_fh + + super(DTSLOG, self).__init__(self.logger, dict(node=self.node)) + + def logger_exit(self) -> None: + """ + Remove stream handler and logfile handler. + """ + for handler in (self.sh, self.fh, self.verbose_fh): + handler.flush() + self.logger.removeHandler(handler) + + +def getLogger(name: str, node: str = "suite") -> DTSLOG: + """ + Get logger handler and if there's no handler for specified Node will create one. + """ + global Loggers + # return saved logger + logger: LoggerDictType + for logger in Loggers: + if logger["name"] == name and logger["node"] == node: + return logger["logger"] + + # return new logger + dts_logger: DTSLOG = DTSLOG(logging.getLogger(name), node) + Loggers.append({"logger": dts_logger, "name": name, "node": node}) + return dts_logger diff --git a/dts/framework/settings.py b/dts/framework/settings.py index 007ab46c32..b6c5bba2b9 100644 --- a/dts/framework/settings.py +++ b/dts/framework/settings.py @@ -57,6 +57,8 @@ def __call__( @dataclass(slots=True, frozen=True) class _Settings: config_file_path: str + output_dir: str + verbose: bool def _get_parser() -> argparse.ArgumentParser: @@ -71,6 +73,25 @@ def _get_parser() -> argparse.ArgumentParser: "and targets.", ) + parser.add_argument( + "--output-dir", + "--output", + action=_env_arg("DTS_OUTPUT_DIR"), + default="output", + required=False, + help="[DTS_OUTPUT_DIR] Output directory where dts logs and results are saved.", + ) + + parser.add_argument( + "-v", + "--verbose", + action=_env_arg("DTS_VERBOSE"), + default="N", + required=False, + help="[DTS_VERBOSE] Set to 'Y' to enable verbose output, logging all messages " + "to the console.", + ) + return parser @@ -78,6 +99,8 @@ def _get_settings() -> _Settings: parsed_args = _get_parser().parse_args() return _Settings( config_file_path=parsed_args.config_file, + output_dir=parsed_args.output_dir, + verbose=(parsed_args.verbose == "Y"), )