From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id AC43BA04FD; Fri, 29 Jul 2022 12:56:22 +0200 (CEST) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 47D6742C32; Fri, 29 Jul 2022 12:56:04 +0200 (CEST) Received: from lb.pantheon.sk (lb.pantheon.sk [46.229.239.20]) by mails.dpdk.org (Postfix) with ESMTP id D65F342C1F for ; Fri, 29 Jul 2022 12:55:59 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by lb.pantheon.sk (Postfix) with ESMTP id 96DE713125D; Fri, 29 Jul 2022 12:55:58 +0200 (CEST) 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 e11Yfoh1oXAC; Fri, 29 Jul 2022 12:55:56 +0200 (CEST) Received: from entguard.lab.pantheon.local (unknown [46.229.239.141]) by lb.pantheon.sk (Postfix) with ESMTP id CC4C613125F; Fri, 29 Jul 2022 12:55:52 +0200 (CEST) From: =?UTF-8?q?Juraj=20Linke=C5=A1?= To: thomas@monjalon.net, david.marchand@redhat.com, ronan.randles@intel.com, Honnappa.Nagarahalli@arm.com, ohilyard@iol.unh.edu, lijuan.tu@intel.com Cc: dev@dpdk.org, =?UTF-8?q?Juraj=20Linke=C5=A1?= Subject: [PATCH v4 3/9] dts: add basic logging facility Date: Fri, 29 Jul 2022 10:55:44 +0000 Message-Id: <20220729105550.1382664-4-juraj.linkes@pantheon.tech> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220729105550.1382664-1-juraj.linkes@pantheon.tech> References: <20220728100044.1318484-1-juraj.linkes@pantheon.tech> <20220729105550.1382664-1-juraj.linkes@pantheon.tech> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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š --- dts/framework/__init__.py | 3 + dts/framework/logger.py | 124 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 127 insertions(+) create mode 100644 dts/framework/__init__.py create mode 100644 dts/framework/logger.py diff --git a/dts/framework/__init__.py b/dts/framework/__init__.py new file mode 100644 index 0000000000..3c30bccf43 --- /dev/null +++ b/dts/framework/__init__.py @@ -0,0 +1,3 @@ +# SPDX-License-Identifier: BSD-3-Clause +# Copyright(c) 2022 PANTHEON.tech s.r.o. +# diff --git a/dts/framework/logger.py b/dts/framework/logger.py new file mode 100644 index 0000000000..920ce0fb15 --- /dev/null +++ b/dts/framework/logger.py @@ -0,0 +1,124 @@ +# 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 +# + +import logging +import os.path +from typing import TypedDict + +""" +DTS logger module with several log level. DTS framework and TestSuite log +will saved into different log files. +""" +verbose = False +date_fmt = "%d/%m/%Y %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 +global Loggers +Loggers: list[LoggerDictType] = [] + + +def set_verbose() -> None: + global verbose + verbose = True + + +class DTSLOG(logging.LoggerAdapter): + """ + DTS log class for framework and testsuite. + """ + + node: str + logger: logging.Logger + sh: logging.StreamHandler + fh: logging.FileHandler + verbose_handler: logging.FileHandler + + def __init__(self, logger: logging.Logger, node: str = "suite"): + global log_dir + + self.logger = logger + self.logger.setLevel(1) # 1 means log everything + + self.node = node + + # add handler to emit to stdout + sh = logging.StreamHandler() + sh.setFormatter(logging.Formatter(stream_fmt, date_fmt)) + + sh.setLevel(logging.DEBUG) # file handler default level + global verbose + if verbose is True: + sh.setLevel(logging.DEBUG) + else: + sh.setLevel(logging.INFO) # console handler defaultlevel + + self.logger.addHandler(sh) + self.sh = sh + + if not os.path.exists("output"): + os.mkdir("output") + + fh = logging.FileHandler(f"output/{node}.log") + fh.setFormatter( + logging.Formatter( + fmt="%(asctime)s - %(name)s - %(levelname)s - %(message)s", + datefmt=date_fmt, + ) + ) + + fh.setLevel(1) # We want all the logs we can get in the file + self.logger.addHandler(fh) + self.fh = fh + + # This outputs EVERYTHING, intended for post-mortem debugging + # Also optimized for processing via AWK (awk -F '|' ...) + verbose_handler = logging.FileHandler(f"output/{node}.verbose.log") + verbose_handler.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, + ) + ) + + verbose_handler.setLevel(1) # We want all the logs we can get in the file + self.logger.addHandler(verbose_handler) + self.verbose_handler = verbose_handler + + 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_handler): + 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 -- 2.30.2