diff --git a/pygatt/classes.py b/pygatt/classes.py index 0be31ccf..3d034a06 100644 --- a/pygatt/classes.py +++ b/pygatt/classes.py @@ -29,14 +29,14 @@ class BluetoothLEDevice(object): """BluetoothLEDevice Object.""" - logger = logging.getLogger(__name__) - logger.setLevel(pygatt.constants.LOG_LEVEL) - console_handler = logging.StreamHandler() - console_handler.setLevel(pygatt.constants.LOG_LEVEL) - formatter = logging.Formatter(pygatt.constants.LOG_FORMAT) - console_handler.setFormatter(formatter) - logger.addHandler(console_handler) - logger.propagate = False + _logger = logging.getLogger(__name__) + if not _logger.handlers: + _logger.setLevel(pygatt.constants.LOG_LEVEL) + _console_handler = logging.StreamHandler() + _console_handler.setLevel(pygatt.constants.LOG_LEVEL) + _console_handler.setFormatter(pygatt.constants.LOG_FORMAT) + _logger.addHandler(_console_handler) + _logger.propagate = False def __init__(self, mac_address, hci_device='hci0', app_options=''): self.handles = {} @@ -58,7 +58,7 @@ def __init__(self, mac_address, hci_device='hci0', app_options=''): '-I' ])) - self.logger.debug('gatttool_cmd=%s', gatttool_cmd) + self._logger.debug('gatttool_cmd=%s', gatttool_cmd) self.con = pexpect.spawn(gatttool_cmd) self.con.expect(r'\[LE\]>', timeout=1) @@ -81,12 +81,12 @@ def __del__(self): def bond(self): """Securely Bonds to the BLE device.""" - self.logger.info('Bonding.') + self._logger.info('Bonding.') self.con.sendline('sec-level medium') def connect(self, timeout=pygatt.constants.DEFAULT_CONNECT_TIMEOUT_S): """Connect to the device.""" - self.logger.info('Connecting with timeout=%s', timeout) + self._logger.info('Connecting with timeout=%s', timeout) try: with self.connection_lock: self.con.sendline('connect') @@ -97,7 +97,7 @@ def connect(self, timeout=pygatt.constants.DEFAULT_CONNECT_TIMEOUT_S): def disconnect(self): """Send gatttool disconnect command""" - self.logger.info('Disconnecting...') + self._logger.info('Disconnecting...') self.con.sendline('disconnect') def get_handle(self, uuid): @@ -180,13 +180,13 @@ def char_write(self, handle, value, wait_for_response=False): cmd = 'char-write-%s 0x%02x %s' % (cmd, handle, hexstring) - self.logger.debug('Sending cmd=%s', cmd) + self._logger.debug('Sending cmd=%s', cmd) self.con.sendline(cmd) if wait_for_response: self._expect('Characteristic value was written successfully') - self.logger.debug('Sent cmd=%s', cmd) + self._logger.debug('Sent cmd=%s', cmd) def char_read_uuid(self, uuid, timeout=pygatt.constants.DEFAULT_TIMEOUT_S): """ @@ -233,7 +233,7 @@ def subscribe(self, uuid, callback=None, indication=False): :return: :rtype: """ - self.logger.info( + self._logger.info( 'Subscribing to uuid=%s with callback=%s and indication=%s', uuid, callback, indication) definition_handle = self.get_handle(uuid) @@ -247,7 +247,7 @@ def subscribe(self, uuid, callback=None, indication=False): else: properties = bytearray([0x01, 0x00]) - self.logger.debug(locals()) + self._logger.debug(locals()) try: self.lock.acquire() @@ -268,7 +268,7 @@ def _handle_notification(self, msg): """ Handles notification? """ - self.logger.debug('Handling notification msg=%s', msg) + self._logger.debug('Handling notification msg=%s', msg) handle, _, value = string.split(msg.strip(), maxsplit=5)[3:] handle = int(handle, 16) value = bytearray.fromhex(value) @@ -284,12 +284,12 @@ def _handle_notification(self, msg): def stop(self): """Stops?""" - self.logger.info('Stopping') + self._logger.info('Stopping') self.running = False def run(self): """Runs...?""" - self.logger.info('Running...') + self._logger.info('Running...') while self.running: with self.connection_lock: try: diff --git a/pygatt/constants.py b/pygatt/constants.py index 823450c7..2c986a19 100644 --- a/pygatt/constants.py +++ b/pygatt/constants.py @@ -13,9 +13,10 @@ import logging -LOG_LEVEL = logging.DEBUG -LOG_FORMAT = ('%(asctime)s %(levelname)s %(name)s.%(funcName)s:%(lineno)d' - ' - %(message)s') +LOG_LEVEL = logging.INFO +LOG_FORMAT = logging.Formatter( + '%(asctime)s %(levelname)s %(name)s.%(funcName)s:%(lineno)d - pygatt ' + '- %(message)s') DEFAULT_TIMEOUT_S = .5 DEFAULT_ASYNC_TIMEOUT_S = .5 diff --git a/setup.py b/setup.py index 3d5f5fe3..0b0d34dd 100644 --- a/setup.py +++ b/setup.py @@ -8,7 +8,7 @@ __title__ = 'pygatt' -__version__ = '1.2.0' +__version__ = '1.3.0' __author__ = 'Greg Albrecht ' __license__ = 'Apache License, Version 2.0' __copyright__ = 'Copyright 2015 Orion Labs, Inc.' @@ -47,7 +47,7 @@ def publish(): install_requires=['pexpect >= 3.3'], setup_requires=[ 'coverage >= 3.7.1', - 'nose >= 1.3.1' + 'nose >= 1.3.7' ], package_dir={'pygatt': 'pygatt'}, zip_safe=False,