From e4a55f978cf54f9a057f45e96379ee6e23d1e27e Mon Sep 17 00:00:00 2001 From: Jacob Schaer Date: Tue, 5 Mar 2024 22:37:00 -0800 Subject: [PATCH 1/6] Examples and refactor logging --- docs/configuration.rst | 18 ++++++++++++++++++ pyxcp/cmdline.py | 5 +++++ pyxcp/logger.py | 3 --- pyxcp/master/master.py | 5 ++--- 4 files changed, 25 insertions(+), 6 deletions(-) diff --git a/docs/configuration.rst b/docs/configuration.rst index e0e3768..3296aba 100644 --- a/docs/configuration.rst +++ b/docs/configuration.rst @@ -15,6 +15,24 @@ General pyXCP Parameters * `ALIGNMENT`: int, False, 1 -- 1 | 2 | 4, byte alignment. * `DISCONNECT_RESPONSE_OPTIONAL`: bool, False, False -- Don't require response from DISCONNECT service. +Logging: +^^^^^^^^ + +When using one of the included examples/scripts, a default logger will be created for console output. +When used as a library, the application should create log handlers. +The pyxcp library loggers will be descendents of the base logger with name "pyxcp". +See: https://docs.python.org/3/howto/logging.html. For example, one can use to create a root logger with defaults (stdout) use:: + + import logging + logging.basicConfig() + +Or, for more specific control:: + + import logging + logger = logging.getLogger('pyxcp') + ch = logging.StreamHandler() + ch.setLevel(logging.DEBUG) + logger.addHandler(ch) eth ~~~ diff --git a/pyxcp/cmdline.py b/pyxcp/cmdline.py index e31bf99..b56daa6 100644 --- a/pyxcp/cmdline.py +++ b/pyxcp/cmdline.py @@ -5,12 +5,14 @@ and create a XCP master instance. """ import argparse +import logging from pyxcp.config import readConfiguration from pyxcp.master import Master from pyxcp.transport.can import registered_drivers from pyxcp.transport.can import try_to_install_system_supplied_drivers + try_to_install_system_supplied_drivers() CAN_DRIVERS = registered_drivers() @@ -51,6 +53,9 @@ def args(self): def run(self, policy=None): """""" + # Create a default logging context if run as command line + logging.basicConfig() + self._args = self.parser.parse_args() args = self.args if args.conf is None: diff --git a/pyxcp/logger.py b/pyxcp/logger.py index 4ccccc9..fb755b3 100644 --- a/pyxcp/logger.py +++ b/pyxcp/logger.py @@ -2,9 +2,6 @@ # -*- coding: utf-8 -*- import logging -logging.basicConfig() - - class Logger(object): LOGGER_BASE_NAME = "pyxcp" diff --git a/pyxcp/master/master.py b/pyxcp/master/master.py index 91ef317..450dd57 100644 --- a/pyxcp/master/master.py +++ b/pyxcp/master/master.py @@ -8,7 +8,6 @@ .. [1] XCP Specification, Part 2 - Protocol Layer Specification """ import functools -import logging import struct import traceback import warnings @@ -19,6 +18,7 @@ from typing import List from typing import Optional from typing import Union +from logger import Logger from pyxcp import checksum from pyxcp import types @@ -92,8 +92,7 @@ def __init__(self, transportName, config=None, policy=None): self.ctr = 0 self.succeeded = True self.config = Configuration(self.PARAMETER_MAP or {}, config or {}) - self.logger = logging.getLogger("pyXCP") - self.logger.setLevel(self.config.get("LOGLEVEL")) + self.logger = Logger("master.Master", level=self.config.get("LOGLEVEL")) disable_error_handling(self.config.get("DISABLE_ERROR_HANDLING")) self.transport = createTransport(transportName, config, policy) From 7c84412f922c8a3949079531a2c42f2c8d35e8ae Mon Sep 17 00:00:00 2001 From: Jacob Schaer Date: Tue, 5 Mar 2024 22:58:26 -0800 Subject: [PATCH 2/6] Import fix and header inconsistency --- docs/configuration.rst | 4 ++-- pyxcp/master/master.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/configuration.rst b/docs/configuration.rst index 3296aba..b104945 100644 --- a/docs/configuration.rst +++ b/docs/configuration.rst @@ -15,8 +15,8 @@ General pyXCP Parameters * `ALIGNMENT`: int, False, 1 -- 1 | 2 | 4, byte alignment. * `DISCONNECT_RESPONSE_OPTIONAL`: bool, False, False -- Don't require response from DISCONNECT service. -Logging: -^^^^^^^^ +Note about LOGLEVEL: +~~~~~~~~~~~~~~~~~~~~ When using one of the included examples/scripts, a default logger will be created for console output. When used as a library, the application should create log handlers. diff --git a/pyxcp/master/master.py b/pyxcp/master/master.py index 450dd57..65c5a79 100644 --- a/pyxcp/master/master.py +++ b/pyxcp/master/master.py @@ -18,7 +18,6 @@ from typing import List from typing import Optional from typing import Union -from logger import Logger from pyxcp import checksum from pyxcp import types @@ -36,6 +35,7 @@ from pyxcp.master.errorhandler import disable_error_handling from pyxcp.master.errorhandler import wrapped from pyxcp.transport.base import createTransport +from pyxcp.logger import Logger from pyxcp.utils import decode_bytes from pyxcp.utils import delay from pyxcp.utils import SHORT_SLEEP From e3bba3c0a3aae6fa4c4d684dbc19c40b58a4abf8 Mon Sep 17 00:00:00 2001 From: Jacob Schaer Date: Tue, 5 Mar 2024 23:01:43 -0800 Subject: [PATCH 3/6] Update testing.yml Allow manual runs --- .github/workflows/testing.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/testing.yml b/.github/workflows/testing.yml index 6648c3f..4d34207 100644 --- a/.github/workflows/testing.yml +++ b/.github/workflows/testing.yml @@ -8,6 +8,7 @@ on: branches: [ master ] pull_request: branches: [ master ] + on: workflow_dispatch jobs: build: From 31c3db28e397c769ba8ef0fdc189b1c81d03bedf Mon Sep 17 00:00:00 2001 From: Jacob Schaer Date: Tue, 5 Mar 2024 23:02:29 -0800 Subject: [PATCH 4/6] Update testing.yml --- .github/workflows/testing.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/testing.yml b/.github/workflows/testing.yml index 4d34207..6302c94 100644 --- a/.github/workflows/testing.yml +++ b/.github/workflows/testing.yml @@ -8,7 +8,7 @@ on: branches: [ master ] pull_request: branches: [ master ] - on: workflow_dispatch + workflow_dispatch: jobs: build: From 17b819007adad0869c73321dd2da9faea6467383 Mon Sep 17 00:00:00 2001 From: Jacob Schaer Date: Tue, 5 Mar 2024 23:05:06 -0800 Subject: [PATCH 5/6] Update testing.yml --- .github/workflows/testing.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.github/workflows/testing.yml b/.github/workflows/testing.yml index 6302c94..0b11d80 100644 --- a/.github/workflows/testing.yml +++ b/.github/workflows/testing.yml @@ -5,10 +5,9 @@ name: Run tests. on: push: - branches: [ master ] + branches: [ master, logging ] pull_request: branches: [ master ] - workflow_dispatch: jobs: build: From 34baf7d7298569c3d793a07daa6766f2e71c42b2 Mon Sep 17 00:00:00 2001 From: Jacob Schaer Date: Tue, 5 Mar 2024 23:06:30 -0800 Subject: [PATCH 6/6] Update testing.yml Revert --- .github/workflows/testing.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/testing.yml b/.github/workflows/testing.yml index 0b11d80..6648c3f 100644 --- a/.github/workflows/testing.yml +++ b/.github/workflows/testing.yml @@ -5,7 +5,7 @@ name: Run tests. on: push: - branches: [ master, logging ] + branches: [ master ] pull_request: branches: [ master ]