Skip to content

Commit 3e96a65

Browse files
author
Brendan Whitfield
committed
first test with python native logging tools
1 parent d76acea commit 3e96a65

2 files changed

Lines changed: 27 additions & 15 deletions

File tree

obd/__init__.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,12 @@
4545
from .protocols import ECU
4646
from .utils import scan_serial, scanSerial, OBDStatus # TODO: scanSerial() deprecated
4747
from .debug import debug
48+
49+
import logging
50+
51+
logger = logging.getLogger(__name__)
52+
logger.setLevel(logging.INFO)
53+
54+
console_handler = logging.StreamHandler() # sends output to stderr
55+
console_handler.setFormatter(logging.Formatter("[%(name)s] %(message)s"))
56+
logger.addHandler(console_handler)

obd/obd.py

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,16 @@
3030
########################################################################
3131

3232

33+
import logging
34+
3335
from .__version__ import __version__
3436
from .elm327 import ELM327
3537
from .commands import commands
3638
from .OBDResponse import OBDResponse
3739
from .utils import scan_serial, OBDStatus
3840
from .debug import debug
3941

42+
logger = logging.getLogger(__name__)
4043

4144

4245
class OBD(object):
@@ -51,10 +54,10 @@ def __init__(self, portstr=None, baudrate=38400, protocol=None, fast=True):
5154
self.fast = fast
5255
self.__last_command = "" # used for running the previous command with a CR
5356

54-
debug("========================== python-OBD (v%s) ==========================" % __version__)
57+
logger.info("======================= python-OBD (v%s) =======================" % __version__)
5558
self.__connect(portstr, baudrate, protocol) # initialize by connecting and loading sensors
5659
self.__load_commands() # try to load the car's supported commands
57-
debug("=========================================================================")
60+
logger.info("===================================================================")
5861

5962

6063
def __connect(self, portstr, baudrate, protocol):
@@ -63,22 +66,22 @@ def __connect(self, portstr, baudrate, protocol):
6366
"""
6467

6568
if portstr is None:
66-
debug("Using scan_serial to select port")
69+
logger.info("Using scan_serial to select port")
6770
portnames = scan_serial()
68-
debug("Available ports: " + str(portnames))
71+
logger.info("Available ports: " + str(portnames))
6972

7073
if not portnames:
71-
debug("No OBD-II adapters found", True)
74+
logger.warning("No OBD-II adapters found")
7275
return
7376

7477
for port in portnames:
75-
debug("Attempting to use port: " + str(port))
78+
logger.info("Attempting to use port: " + str(port))
7679
self.port = ELM327(port, baudrate, protocol)
7780

7881
if self.port.status() >= OBDStatus.ELM_CONNECTED:
7982
break # success! stop searching for serial
8083
else:
81-
debug("Explicit port defined")
84+
logger.info("Explicit port defined")
8285
self.port = ELM327(portstr, baudrate, protocol)
8386

8487
# if the connection failed, close it
@@ -94,10 +97,10 @@ def __load_commands(self):
9497
"""
9598

9699
if self.status() != OBDStatus.CAR_CONNECTED:
97-
debug("Cannot load commands: No connection to car", True)
100+
logger.warning("Cannot load commands: No connection to car")
98101
return
99102

100-
debug("querying for supported PIDs (commands)...")
103+
logger.info("querying for supported PIDs (commands)...")
101104
pid_getters = commands.pid_getters()
102105
for get in pid_getters:
103106
# PID listing commands should sequentialy become supported
@@ -128,7 +131,7 @@ def __load_commands(self):
128131
if mode == 1 and commands.has_pid(2, pid):
129132
self.supported_commands.add(commands[2][pid])
130133

131-
debug("finished querying with %d commands supported" % len(self.supported_commands))
134+
logger.info("finished querying with %d commands supported" % len(self.supported_commands))
132135

133136

134137
def close(self):
@@ -139,7 +142,7 @@ def close(self):
139142
self.supported_commands = []
140143

141144
if self.port is not None:
142-
debug("Closing connection")
145+
logger.info("Closing connection")
143146
self.port.close()
144147
self.port = None
145148

@@ -226,16 +229,16 @@ def query(self, cmd, force=False):
226229
"""
227230

228231
if self.status() == OBDStatus.NOT_CONNECTED:
229-
debug("Query failed, no connection available", True)
232+
logger.warning("Query failed, no connection available")
230233
return OBDResponse()
231234

232235
if not force and not self.supports(cmd):
233-
debug("'%s' is not supported" % str(cmd), True)
236+
logger.warning("'%s' is not supported" % str(cmd))
234237
return OBDResponse()
235238

236239

237240
# send command and retrieve message
238-
debug("Sending command: %s" % str(cmd))
241+
logger.info("Sending command: %s" % str(cmd))
239242
cmd_string = self.__build_command_string(cmd)
240243
messages = self.port.send_and_parse(cmd_string)
241244

@@ -244,7 +247,7 @@ def query(self, cmd, force=False):
244247
self.__last_command = cmd_string
245248

246249
if not messages:
247-
debug("No valid OBD Messages returned", True)
250+
logger.info("No valid OBD Messages returned")
248251
return OBDResponse()
249252

250253
return cmd(messages) # compute a response object

0 commit comments

Comments
 (0)