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 39B12A04FD; Wed, 22 Jun 2022 14:15:14 +0200 (CEST) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 6797D4281B; Wed, 22 Jun 2022 14:14:59 +0200 (CEST) Received: from lb.pantheon.sk (lb.pantheon.sk [46.229.239.20]) by mails.dpdk.org (Postfix) with ESMTP id 6F0494281B for ; Wed, 22 Jun 2022 14:14:56 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by lb.pantheon.sk (Postfix) with ESMTP id AF6893146B; Wed, 22 Jun 2022 14:14:55 +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 PJKwmepIb7td; Wed, 22 Jun 2022 14:14:54 +0200 (CEST) Received: from entguard.lab.pantheon.local (unknown [46.229.239.141]) by lb.pantheon.sk (Postfix) with ESMTP id 01C713146E; Wed, 22 Jun 2022 14:14:50 +0200 (CEST) From: =?UTF-8?q?Juraj=20Linke=C5=A1?= To: thomas@monjalon.net, david.marchand@redhat.com, jerinjacobk@gmail.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 v1 4/8] dts: add basic logging facility Date: Wed, 22 Jun 2022 12:14:44 +0000 Message-Id: <20220622121448.3304251-5-juraj.linkes@pantheon.tech> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220622121448.3304251-1-juraj.linkes@pantheon.tech> References: <20220622121448.3304251-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. Signed-off-by: Juraj Linkeš --- dts/framework/logger.py | 86 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 dts/framework/logger.py diff --git a/dts/framework/logger.py b/dts/framework/logger.py new file mode 100644 index 0000000000..1bda7c58c5 --- /dev/null +++ b/dts/framework/logger.py @@ -0,0 +1,86 @@ +# SPDX-License-Identifier: BSD-3-Clause +# Copyright(c) 2010-2014 Intel Corporation +# + +import logging + +""" +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 = "%(name)30s: %(message)s" + +# List for saving all using loggers +global Loggers +Loggers = [] + + +def set_verbose(): + global verbose + verbose = True + + +class DTSLOG(logging.LoggerAdapter): + """ + DTS log class for framework and testsuite. + """ + + def __init__(self, logger, node="suite"): + global log_dir + + self.logger = logger + self.logger.setLevel(logging.DEBUG) + + self.node = node + super(DTSLOG, self).__init__(self.logger, dict(node=self.node)) + + self.sh = None + + # add handler to emit to stdout + sh = logging.StreamHandler() + self.__log_handler(sh) + + def __log_handler(self, sh): + """ + Config stream handler and file handler. + """ + 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 default level + + self.logger.addHandler(sh) + + if self.sh is not None: + self.logger.removeHandler(self.sh) + + self.sh = sh + + def logger_exit(self): + """ + Remove stream handler and logfile handler. + """ + if self.sh is not None: + self.logger.removeHandler(self.sh) + + +def getLogger(name, node="suite"): + """ + Get logger handler and if there's no handler for specified Node will create one. + """ + global Loggers + # return saved logger + for logger in Loggers: + if logger["name"] == name and logger["node"] == node: + return logger["logger"] + + # return new logger + logger = DTSLOG(logging.getLogger(name), node) + Loggers.append({"logger": logger, "name": name, "node": node}) + return logger -- 2.20.1