From 3c7b11c149c2384e0f84d9d68f3535369410c807 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Freise?= Date: Tue, 23 Apr 2019 11:41:14 +0200 Subject: [PATCH] Clean up logger-calls - more semetrical through out the code --- coapthon/client/coap.py | 4 ++-- coapthon/forward_proxy/coap.py | 10 +++++----- coapthon/http_proxy/http_coap_proxy.py | 4 ++-- coapthon/layers/forwardLayer.py | 5 +++++ coapthon/layers/messagelayer.py | 12 ++++++------ coapthon/layers/observelayer.py | 2 +- coapthon/messages/message.py | 7 +++++-- coapthon/reverse_proxy/coap.py | 6 ++++-- coapthon/serializer.py | 5 +++-- coapthon/server/coap.py | 13 ++++--------- 10 files changed, 37 insertions(+), 31 deletions(-) diff --git a/coapthon/client/coap.py b/coapthon/client/coap.py index 919429c..f199b2c 100644 --- a/coapthon/client/coap.py +++ b/coapthon/client/coap.py @@ -148,7 +148,7 @@ class CoAP(object): :param message: the message to send """ host, port = message.destination - logger.debug("send_datagram - " + str(message)) + logger.info("send_datagram - " + str(message)) serializer = Serializer() raw_message = serializer.serialize(message) @@ -263,7 +263,7 @@ class CoAP(object): message = serializer.deserialize(datagram, source) if isinstance(message, Response): - logger.debug("receive_datagram - " + str(message)) + logger.info("receive_datagram - " + str(message)) transaction, send_ack = self._messageLayer.receive_response(message) if transaction is None: # pragma: no cover continue diff --git a/coapthon/forward_proxy/coap.py b/coapthon/forward_proxy/coap.py index 977d2fa..8d29f8d 100644 --- a/coapthon/forward_proxy/coap.py +++ b/coapthon/forward_proxy/coap.py @@ -139,7 +139,7 @@ class CoAP(object): except socket.timeout: continue try: - #Start a new thread not to block other requests + # Start a new thread not to block other requests args = ((data, client_address), ) t = threading.Thread(target=self.receive_datagram, args=args) t.daemon = True @@ -188,20 +188,20 @@ class CoAP(object): rst.code = message self.send_datagram(rst) return - logger.debug("receive_datagram - " + str(message)) + logger.info("receive_datagram - " + str(message)) if isinstance(message, Request): transaction = self._messageLayer.receive_request(message) if transaction.request.duplicated and transaction.completed: - logger.debug("message duplicated,transaction completed") + logger.debug("message duplicated, transaction completed") transaction = self._observeLayer.send_response(transaction) transaction = self._blockLayer.send_response(transaction) transaction = self._messageLayer.send_response(transaction) self.send_datagram(transaction.response) return elif transaction.request.duplicated and not transaction.completed: - logger.debug("message duplicated,transaction NOT completed") + logger.debug("message duplicated, transaction NOT completed") self._send_ack(transaction) return @@ -268,7 +268,7 @@ class CoAP(object): """ if not self.stopped.isSet(): host, port = message.destination - logger.debug("send_datagram - " + str(message)) + logger.info("send_datagram - " + str(message)) serializer = Serializer() message = serializer.serialize(message) diff --git a/coapthon/http_proxy/http_coap_proxy.py b/coapthon/http_proxy/http_coap_proxy.py index 83b689c..ec21b03 100644 --- a/coapthon/http_proxy/http_coap_proxy.py +++ b/coapthon/http_proxy/http_coap_proxy.py @@ -175,7 +175,7 @@ class HCProxyHandler(BaseHTTPRequestHandler): logger.debug(payload) coap_response = self.client.put(self.coap_uri.path, payload) self.client.stop() - logger.debug("Server response: %s", coap_response.pretty_print()) + logger.info("Server response: %s", coap_response.pretty_print()) self.set_http_response(coap_response) def do_DELETE(self): @@ -185,7 +185,7 @@ class HCProxyHandler(BaseHTTPRequestHandler): self.do_initial_operations() coap_response = self.client.delete(self.coap_uri.path) self.client.stop() - logger.debug("Server response: %s", coap_response.pretty_print()) + logger.info("Server response: %s", coap_response.pretty_print()) self.set_http_response(coap_response) def do_CONNECT(self): diff --git a/coapthon/layers/forwardLayer.py b/coapthon/layers/forwardLayer.py index e36f68d..22f521a 100644 --- a/coapthon/layers/forwardLayer.py +++ b/coapthon/layers/forwardLayer.py @@ -1,4 +1,5 @@ import copy +import logging from coapthon.messages.request import Request from coapclient import HelperClient from coapthon.messages.response import Response @@ -8,6 +9,8 @@ from coapthon.utils import parse_uri __author__ = 'Giacomo Tanganelli' +logger = logging.getLogger(__name__) + class ForwardLayer(object): """ @@ -146,8 +149,10 @@ class ForwardLayer(object): request.destination = transaction.resource.remote_server request.payload = transaction.request.payload request.code = transaction.request.code + logger.info("forward_request - " + str(request)) response = client.send_request(request) client.stop() + logger.info("forward_response - " + str(response)) transaction.response.payload = response.payload transaction.response.code = response.code transaction.response.options = response.options diff --git a/coapthon/layers/messagelayer.py b/coapthon/layers/messagelayer.py index da455bf..a1b72cc 100644 --- a/coapthon/layers/messagelayer.py +++ b/coapthon/layers/messagelayer.py @@ -72,7 +72,7 @@ class MessageLayer(object): :rtype : Transaction :return: the edited transaction """ - logger.debug("receive_request - " + str(request)) + logger.info("receive_request - " + str(request)) try: host, port = request.source except AttributeError: @@ -101,7 +101,7 @@ class MessageLayer(object): :rtype : Transaction :return: the transaction to which the response belongs to """ - logger.debug("receive_response - " + str(response)) + logger.info("receive_response - " + str(response)) try: host, port = response.source except AttributeError: @@ -148,7 +148,7 @@ class MessageLayer(object): :rtype : Transaction :return: the transaction to which the message belongs to """ - logger.debug("receive_empty - " + str(message)) + logger.info("receive_empty - " + str(message)) try: host, port = message.source except AttributeError: @@ -201,7 +201,7 @@ class MessageLayer(object): :rtype : Transaction :return: the created transaction """ - logger.debug("send_request - " + str(request)) + logger.info("send_request - " + str(request)) assert isinstance(request, Request) try: host, port = request.destination @@ -233,7 +233,7 @@ class MessageLayer(object): :rtype : Transaction :return: the edited transaction """ - logger.debug("send_response - " + str(transaction.response)) + logger.info("send_response - " + str(transaction.response)) if transaction.response.type is None: if transaction.request.type == defines.Types["CON"] and not transaction.request.acknowledged: transaction.response.type = defines.Types["ACK"] @@ -268,7 +268,7 @@ class MessageLayer(object): :type message: Message :param message: the ACK or RST message to send """ - logger.debug("send_empty - " + str(message)) + logger.info("send_empty - " + str(message)) if transaction is None: try: host, port = message.destination diff --git a/coapthon/layers/observelayer.py b/coapthon/layers/observelayer.py index ae9f3fd..5df6a39 100644 --- a/coapthon/layers/observelayer.py +++ b/coapthon/layers/observelayer.py @@ -188,7 +188,7 @@ class ObserveLayer(object): :param message: the message """ - logger.debug("Remove Subcriber") + logger.info("Remove Subcriber") host, port = message.destination key_token = hash(str(host) + str(port) + str(message.token)) try: diff --git a/coapthon/messages/message.py b/coapthon/messages/message.py index d678227..2bd8c5e 100644 --- a/coapthon/messages/message.py +++ b/coapthon/messages/message.py @@ -454,7 +454,7 @@ class Message(object): for e in etag: option = Option() option.number = defines.OptionRegistry.ETAG.number - if not isinstance(e, bytes): + if not isinstance(e, bytes): e = bytes(e, "utf-8") option.value = e self.add_option(option) @@ -695,7 +695,10 @@ class Message(object): .format(source=self._source, destination=self._destination, type=inv_types[self._type], mid=self._mid, code=defines.Codes.LIST[self._code].name, token=self._token) for opt in self._options: - msg += "{name}: {value}, ".format(name=opt.name, value=opt.value) + if 'Block' in opt.name: + msg += "{name}: {value}, ".format(name=opt.name, value=parse_blockwise(opt.value)) + else: + msg += "{name}: {value}, ".format(name=opt.name, value=opt.value) msg += "]" if self.payload is not None: if isinstance(self.payload, dict): diff --git a/coapthon/reverse_proxy/coap.py b/coapthon/reverse_proxy/coap.py index ed05686..e74346b 100644 --- a/coapthon/reverse_proxy/coap.py +++ b/coapthon/reverse_proxy/coap.py @@ -273,7 +273,8 @@ class CoAP(object): rst.code = message self.send_datagram(rst) return - logger.debug("receive_datagram - " + str(message)) + + logger.info("receive_datagram - " + str(message)) if isinstance(message, Request): transaction = self._messageLayer.receive_request(message) @@ -319,6 +320,7 @@ class CoAP(object): transaction = self._blockLayer.send_response(transaction) transaction = self._cacheLayer.send_response(transaction) + else: transaction = self._forwardLayer.receive_request_reverse(transaction) @@ -353,7 +355,7 @@ class CoAP(object): """ if not self.stopped.isSet(): host, port = message.destination - logger.debug("send_datagram - " + str(message)) + logger.info("send_datagram - " + str(message)) serializer = Serializer() message = serializer.serialize(message) diff --git a/coapthon/serializer.py b/coapthon/serializer.py index bd5593f..34a2e0f 100644 --- a/coapthon/serializer.py +++ b/coapthon/serializer.py @@ -70,6 +70,7 @@ class Serializer(object): # the first 4 bits of the byte represent the option delta # delta = self._reader.read(4).uint num, option_length, pos = Serializer.read_option_value_len_from_byte(next_byte, pos, values) + logger.debug("option value (delta): %d len: %d", num, option_length) current_option += num # read option try: @@ -81,8 +82,7 @@ class Serializer(object): else: # If the non-critical option is unknown # (vendor-specific, proprietary) - just skip it - #log.err("unrecognized option %d" % current_option) - pass + logger.warning("unrecognized option %d", current_option) else: if option_length == 0: value = None @@ -313,6 +313,7 @@ class Serializer(object): length = struct.unpack("!B", values[pos].to_bytes(1, "big"))[0] + 13 pos += 1 elif l_nibble == 14: + s = struct.Struct("!H") length = s.unpack_from(values[pos:].to_bytes(2, "big"))[0] + 269 pos += 2 else: diff --git a/coapthon/server/coap.py b/coapthon/server/coap.py index dfc25a1..e667bfa 100644 --- a/coapthon/server/coap.py +++ b/coapthon/server/coap.py @@ -1,5 +1,4 @@ -import logging.config -import os +import logging import random import socket import struct @@ -17,17 +16,13 @@ from coapthon.messages.request import Request from coapthon.messages.response import Response from coapthon.resources.resource import Resource from coapthon.serializer import Serializer -from coapthon.utils import Tree, create_logging +from coapthon.utils import Tree __author__ = 'Giacomo Tanganelli' -if not os.path.isfile("logging.conf"): - create_logging() - logger = logging.getLogger(__name__) -logging.config.fileConfig("logging.conf", disable_existing_loggers=False) class CoAP(object): @@ -155,7 +150,7 @@ class CoAP(object): self.send_datagram(rst) continue - logger.debug("receive_datagram - " + str(message)) + logger.info("receive_datagram - " + str(message)) if isinstance(message, Request): transaction = self._messageLayer.receive_request(message) if transaction.request.duplicated and transaction.completed: @@ -247,7 +242,7 @@ class CoAP(object): """ if not self.stopped.isSet(): host, port = message.destination - logger.debug("send_datagram - " + str(message)) + logger.info("send_datagram - " + str(message)) serializer = Serializer() message = serializer.serialize(message) self._socket.sendto(message, (host, port))