diff --git a/__init__.py b/__init__.py index 2c0e84d..e96b7f4 100644 --- a/__init__.py +++ b/__init__.py @@ -69,10 +69,11 @@ class tcp_base(object): COM_TIMEOUT = 0.5 IS_CLIENT = False - def __init__(self, host, port, channel_name=None): + def __init__(self, host, port, channel_name=None, rx_tx_log_lvl=logging.INFO): self.host = host self.port = port self.init_channel_name(channel_name) + self.__rx_tx_log_lvl__ = rx_tx_log_lvl self.__socket__ = None self.__data_available_callback__ = None self.__supress_data_available_callback__ = False @@ -93,7 +94,7 @@ class tcp_base(object): self.__supress_data_available_callback__ = False def __clean_receive_buffer__(self): - self.logger.debug("Cleaning up receive-buffer") + self.logger.debug("%s Cleaning up receive-buffer", self.__log_prefix__()) self.__receive_buffer__ = b'' def __connection_lost__(self): @@ -101,13 +102,16 @@ class tcp_base(object): self.__connection__.close() self.__connection__ = None self.__client_address__ = None - self.logger.info('Connection lost...') + self.logger.info('%s Connection lost...', self.__log_prefix__()) if self.__disconnect_callback__ is not None: self.__disconnect_callback__() def __del__(self): self.close() + def __log_prefix__(self): + return 'comm-client:' if self.IS_CLIENT else 'comm-server:' + def __receive_task__(self, queue_inst): if self.__connection__ is not None: try: @@ -119,7 +123,7 @@ class tcp_base(object): time.sleep(.05) else: if len(data) > 0: - self.logger.info('RX <- "%s"', stringtools.hexlify(data)) + self.logger.log(self.__rx_tx_log_lvl__, '%s RX <- "%s"', self.__log_prefix__(), stringtools.hexlify(data)) self.__receive_buffer__ += data else: self.__connection_lost__() @@ -187,7 +191,7 @@ class tcp_base(object): if self.__connection__ is None: return None if time.time() > tm + timeout: - self.logger.warning('TIMEOUT (%ss): Not enough data in buffer. Requested %s and buffer size is %d.', repr(timeout), repr(num or 'all'), len(self.__receive_buffer__)) + self.logger.warning('%s TIMEOUT (%ss): Not enough data in buffer. Requested %s and buffer size is %d.', self.__log_prefix__(), repr(timeout), repr(num or 'all'), len(self.__receive_buffer__)) return None time.sleep(0.05) if num is None: @@ -226,7 +230,7 @@ class tcp_base(object): """ self.__disconnect_callback__ = callback - def send(self, data, timeout=1, log_lvl=logging.INFO): + def send(self, data, timeout=1): """ This method sends data via the initiated communication channel. @@ -234,8 +238,6 @@ class tcp_base(object): :type data: bytes :param timeout: The timeout for sending data (e.g. time to establish new connection). :type timeout: float - :param log_lvl: The log level to log outgoing TX-data - :type log_lvl: int :return: True if data had been sent, otherwise False. :rtype: bool """ @@ -247,15 +249,15 @@ class tcp_base(object): except BlockingIOError: time.sleep(.1) # try again till timeout exceeds except BrokenPipeError: - self.logger.exception('Exception while sending data') + self.logger.exception('%s Exception while sending data', self.__log_prefix__()) self.__connection_lost__() return False else: - self.logger.log(log_lvl, 'TX -> "%s"', stringtools.hexlify(data)) + self.logger.log(self.__rx_tx_log_lvl__, '%s TX -> "%s"', self.__log_prefix__(), stringtools.hexlify(data)) return True else: time.sleep(.1) # give some time to establish the connection - self.logger.warning('Cound NOT send -> "%s"', stringtools.hexlify(data)) + self.logger.warning('%s Cound NOT send -> "%s"', self.__log_prefix__(), stringtools.hexlify(data)) return False @@ -290,7 +292,7 @@ class tcp_server(tcp_base): self.__socket__.settimeout(self.COM_TIMEOUT) self.__socket__.setblocking(False) if not self.__listening_message_displayed__: - self.logger.info('Server listening to %s:%d', self.host, self.port) + self.logger.info('%s Server listening to %s:%d', self.__log_prefix__(), self.host, self.port) self.__listening_message_displayed__ = True try: self.__connection__, self.__client_address__ = self.__socket__.accept() @@ -300,7 +302,7 @@ class tcp_server(tcp_base): else: time.sleep(.05) else: - self.logger.info('Connection established... (from %s)', self.client_address()) + self.logger.info('%s Connection established... (from %s:%s)', self.__log_prefix__(), self.client_address(), self.port) self.__clean_receive_buffer__() self.__connection__.setblocking(False) if self.__connect_callback__ is not None: @@ -345,7 +347,7 @@ class tcp_client(tcp_base): self.__connection__ = None time.sleep(.05) else: - self.logger.info('Connection to %s:%s established', self.host, self.port) + self.logger.info('%s Connection established... (to %s:%s)', self.__log_prefix__(), self.host, self.port) self.__clean_receive_buffer__() self.__connection__ = self.__socket__ if self.__connect_callback__ is not None: @@ -374,11 +376,11 @@ class tcp_base_stp(tcp_base): """ def __init__(self, host, port, channel_name=None): - tcp_base.__init__(self, host, port, channel_name=channel_name) + tcp_base.__init__(self, host, port, channel_name=channel_name, rx_tx_log_lvl=logging.DEBUG) self.__stp__ = stringtools.stp.stp() def __clean_receive_buffer__(self): - self.logger.debug("Cleaning up receive-buffer") + self.logger.debug("%s Cleaning up receive-buffer", self.__log_prefix__()) self.__receive_buffer__ = [] def __receive_task__(self, queue_inst): @@ -394,10 +396,10 @@ class tcp_base_stp(tcp_base): time.sleep(.05) else: if len(data) > 0: - self.logger.debug('RX <- "%s"', stringtools.hexlify(data)) + self.logger.log(self.__rx_tx_log_lvl__, '%s RX <- "%s"', self.__log_prefix__(), stringtools.hexlify(data)) content = self.__stp__.process(data) for msg in content: - self.logger.info('RX <- "%s"', stringtools.hexlify(msg)) + self.logger.info('%s RX <- "%s"', self.__log_prefix__(), stringtools.hexlify(msg)) self.__receive_buffer__.append(msg) else: self.__connection_lost__() @@ -420,7 +422,7 @@ class tcp_base_stp(tcp_base): except TypeError: return None - def send(self, data, timeout=1, log_lvl=logging.INFO): + def send(self, data, timeout=1): """ This method sends one stp message via the initiated communication channel. @@ -428,13 +430,11 @@ class tcp_base_stp(tcp_base): :type data: bytes :param timeout: The timeout for sending data (e.g. time to establish new connection). :type timeout: float - :param log_lvl: The log level to log outgoing TX-data - :type log_lvl: int :return: True if data had been sent, otherwise False. :rtype: bool """ - if tcp_base.send(self, stringtools.stp.build_frame(data), timeout=timeout, log_lvl=logging.DEBUG): - self.logger.log(log_lvl, 'TX -> "%s"', stringtools.hexlify(data)) + if tcp_base.send(self, stringtools.stp.build_frame(data), timeout=timeout): + self.logger.info('%s TX -> "%s"', self.__log_prefix__(), stringtools.hexlify(data)) return True else: return False diff --git a/_docs_/index.html b/_docs_/index.html index 80321ee..2decfd1 100644 --- a/_docs_/index.html +++ b/_docs_/index.html @@ -176,7 +176,7 @@
Module Documentation:
tcp_socket.
tcp_base
(host, port, channel_name=None)¶tcp_socket.
tcp_base
(host, port, channel_name=None, rx_tx_log_lvl=20)¶
This is the base class for other classes in this module.
Parameters: |
|
---|
Parameters: |
|
---|