Skip to content

Commit

Permalink
Merge pull request #161 from jacobschaer/logging
Browse files Browse the repository at this point in the history
Logging fix
  • Loading branch information
christoph2 committed Mar 6, 2024
2 parents 89add6b + 34baf7d commit b307be6
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 6 deletions.
18 changes: 18 additions & 0 deletions docs/configuration.rst
Expand Up @@ -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.

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.
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
~~~
Expand Down
5 changes: 5 additions & 0 deletions pyxcp/cmdline.py
Expand Up @@ -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()
Expand Down Expand Up @@ -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:
Expand Down
3 changes: 0 additions & 3 deletions pyxcp/logger.py
Expand Up @@ -2,9 +2,6 @@
# -*- coding: utf-8 -*-
import logging

logging.basicConfig()


class Logger(object):

LOGGER_BASE_NAME = "pyxcp"
Expand Down
5 changes: 2 additions & 3 deletions pyxcp/master/master.py
Expand Up @@ -8,7 +8,6 @@
.. [1] XCP Specification, Part 2 - Protocol Layer Specification
"""
import functools
import logging
import struct
import traceback
import warnings
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit b307be6

Please sign in to comment.