DPDK patches and discussions
 help / color / mirror / Atom feed
From: "Juraj Linkeš" <juraj.linkes@pantheon.tech>
To: thomas@monjalon.net, david.marchand@redhat.com,
	Honnappa.Nagarahalli@arm.com, ohilyard@iol.unh.edu,
	lijuan.tu@intel.com, kda@semihalf.com,
	bruce.richardson@intel.com
Cc: dev@dpdk.org, "Juraj Linkeš" <juraj.linkes@pantheon.tech>
Subject: [PATCH v5 04/10] dts: add basic logging facility
Date: Mon, 26 Sep 2022 14:17:07 +0000	[thread overview]
Message-ID: <20220926141713.2415010-5-juraj.linkes@pantheon.tech> (raw)
In-Reply-To: <20220926141713.2415010-1-juraj.linkes@pantheon.tech>

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 <ohilyard@iol.unh.edu>
Signed-off-by: Juraj Linkeš <juraj.linkes@pantheon.tech>
---
 dts/framework/__init__.py |   4 ++
 dts/framework/logger.py   | 115 ++++++++++++++++++++++++++++++++++++++
 dts/framework/settings.py |  12 ++++
 3 files changed, 131 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..6cdc0639db
--- /dev/null
+++ b/dts/framework/__init__.py
@@ -0,0 +1,4 @@
+# SPDX-License-Identifier: BSD-3-Clause
+# Copyright(c) 2022 PANTHEON.tech s.r.o.
+# Copyright(c) 2022 University of New Hampshire
+#
diff --git a/dts/framework/logger.py b/dts/framework/logger.py
new file mode 100644
index 0000000000..6917006115
--- /dev/null
+++ b/dts/framework/logger.py
@@ -0,0 +1,115 @@
+# 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
+
+from .settings import SETTINGS
+
+"""
+DTS logger module with several log level. DTS framework and TestSuite logs
+are saved in different log files.
+"""
+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
+global Loggers
+Loggers: list[LoggerDictType] = []
+
+
+class DTSLOG(logging.LoggerAdapter):
+    """
+    DTS log class for framework and testsuite.
+    """
+
+    node: str
+    logger: logging.Logger
+    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
+
+        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,
+            )
+        )
+
+        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"output/{node}.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 d1a955502b..ed43ba33a0 100644
--- a/dts/framework/settings.py
+++ b/dts/framework/settings.py
@@ -57,6 +57,7 @@ def __call__(
 @dataclass(slots=True, frozen=True)
 class _Settings:
     config_file_path: str
+    verbose: bool
 
 
 def _get_parser() -> argparse.ArgumentParser:
@@ -71,6 +72,16 @@ def _get_parser() -> argparse.ArgumentParser:
         "and targets.",
     )
 
+    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 +89,7 @@ def _get_settings() -> _Settings:
     parsed_args = _get_parser().parse_args()
     return _Settings(
         config_file_path=parsed_args.config_file,
+        verbose=(parsed_args.verbose == "Y"),
     )
 
 
-- 
2.30.2


  parent reply	other threads:[~2022-09-26 14:17 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-26 14:17 [PATCH v5 00/10] dts: ssh connection to a node Juraj Linkeš
2022-09-26 14:17 ` [PATCH v5 01/10] dts: add project tools config Juraj Linkeš
2022-09-26 14:17 ` [PATCH v5 02/10] dts: add developer tools Juraj Linkeš
2022-09-26 14:17 ` [PATCH v5 03/10] dts: add config parser module Juraj Linkeš
2022-09-26 14:17 ` Juraj Linkeš [this message]
2022-09-26 14:17 ` [PATCH v5 05/10] dts: add remote session abstraction Juraj Linkeš
2022-09-26 14:17 ` [PATCH v5 06/10] dts: add ssh connection module Juraj Linkeš
2022-09-27 10:12   ` Stanislaw Kardach
2022-09-30  8:10     ` Juraj Linkeš
2022-09-26 14:17 ` [PATCH v5 07/10] dts: add node base class Juraj Linkeš
2022-09-26 14:17 ` [PATCH v5 08/10] dts: add dts workflow module Juraj Linkeš
2022-09-26 14:17 ` [PATCH v5 09/10] dts: add dts executable script Juraj Linkeš
2022-09-26 14:17 ` [PATCH v5 10/10] maintainers: add dts maintainers Juraj Linkeš
2022-10-11 15:40 ` [PATCH v5 00/10] dts: ssh connection to a node Owen Hilyard

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220926141713.2415010-5-juraj.linkes@pantheon.tech \
    --to=juraj.linkes@pantheon.tech \
    --cc=Honnappa.Nagarahalli@arm.com \
    --cc=bruce.richardson@intel.com \
    --cc=david.marchand@redhat.com \
    --cc=dev@dpdk.org \
    --cc=kda@semihalf.com \
    --cc=lijuan.tu@intel.com \
    --cc=ohilyard@iol.unh.edu \
    --cc=thomas@monjalon.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).