Clean up logger-calls - more semetrical through out the code

Mesure-preliminaire
Björn Freise 5 years ago
parent b77fc78dde
commit 3c7b11c149

@ -148,7 +148,7 @@ class CoAP(object):
:param message: the message to send :param message: the message to send
""" """
host, port = message.destination host, port = message.destination
logger.debug("send_datagram - " + str(message)) logger.info("send_datagram - " + str(message))
serializer = Serializer() serializer = Serializer()
raw_message = serializer.serialize(message) raw_message = serializer.serialize(message)
@ -263,7 +263,7 @@ class CoAP(object):
message = serializer.deserialize(datagram, source) message = serializer.deserialize(datagram, source)
if isinstance(message, Response): if isinstance(message, Response):
logger.debug("receive_datagram - " + str(message)) logger.info("receive_datagram - " + str(message))
transaction, send_ack = self._messageLayer.receive_response(message) transaction, send_ack = self._messageLayer.receive_response(message)
if transaction is None: # pragma: no cover if transaction is None: # pragma: no cover
continue continue

@ -139,7 +139,7 @@ class CoAP(object):
except socket.timeout: except socket.timeout:
continue continue
try: try:
#Start a new thread not to block other requests # Start a new thread not to block other requests
args = ((data, client_address), ) args = ((data, client_address), )
t = threading.Thread(target=self.receive_datagram, args=args) t = threading.Thread(target=self.receive_datagram, args=args)
t.daemon = True t.daemon = True
@ -188,20 +188,20 @@ class CoAP(object):
rst.code = message rst.code = message
self.send_datagram(rst) self.send_datagram(rst)
return return
logger.debug("receive_datagram - " + str(message)) logger.info("receive_datagram - " + str(message))
if isinstance(message, Request): if isinstance(message, Request):
transaction = self._messageLayer.receive_request(message) transaction = self._messageLayer.receive_request(message)
if transaction.request.duplicated and transaction.completed: 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._observeLayer.send_response(transaction)
transaction = self._blockLayer.send_response(transaction) transaction = self._blockLayer.send_response(transaction)
transaction = self._messageLayer.send_response(transaction) transaction = self._messageLayer.send_response(transaction)
self.send_datagram(transaction.response) self.send_datagram(transaction.response)
return return
elif transaction.request.duplicated and not transaction.completed: 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) self._send_ack(transaction)
return return
@ -268,7 +268,7 @@ class CoAP(object):
""" """
if not self.stopped.isSet(): if not self.stopped.isSet():
host, port = message.destination host, port = message.destination
logger.debug("send_datagram - " + str(message)) logger.info("send_datagram - " + str(message))
serializer = Serializer() serializer = Serializer()
message = serializer.serialize(message) message = serializer.serialize(message)

@ -175,7 +175,7 @@ class HCProxyHandler(BaseHTTPRequestHandler):
logger.debug(payload) logger.debug(payload)
coap_response = self.client.put(self.coap_uri.path, payload) coap_response = self.client.put(self.coap_uri.path, payload)
self.client.stop() 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) self.set_http_response(coap_response)
def do_DELETE(self): def do_DELETE(self):
@ -185,7 +185,7 @@ class HCProxyHandler(BaseHTTPRequestHandler):
self.do_initial_operations() self.do_initial_operations()
coap_response = self.client.delete(self.coap_uri.path) coap_response = self.client.delete(self.coap_uri.path)
self.client.stop() 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) self.set_http_response(coap_response)
def do_CONNECT(self): def do_CONNECT(self):

@ -1,4 +1,5 @@
import copy import copy
import logging
from coapthon.messages.request import Request from coapthon.messages.request import Request
from coapclient import HelperClient from coapclient import HelperClient
from coapthon.messages.response import Response from coapthon.messages.response import Response
@ -8,6 +9,8 @@ from coapthon.utils import parse_uri
__author__ = 'Giacomo Tanganelli' __author__ = 'Giacomo Tanganelli'
logger = logging.getLogger(__name__)
class ForwardLayer(object): class ForwardLayer(object):
""" """
@ -146,8 +149,10 @@ class ForwardLayer(object):
request.destination = transaction.resource.remote_server request.destination = transaction.resource.remote_server
request.payload = transaction.request.payload request.payload = transaction.request.payload
request.code = transaction.request.code request.code = transaction.request.code
logger.info("forward_request - " + str(request))
response = client.send_request(request) response = client.send_request(request)
client.stop() client.stop()
logger.info("forward_response - " + str(response))
transaction.response.payload = response.payload transaction.response.payload = response.payload
transaction.response.code = response.code transaction.response.code = response.code
transaction.response.options = response.options transaction.response.options = response.options

@ -72,7 +72,7 @@ class MessageLayer(object):
:rtype : Transaction :rtype : Transaction
:return: the edited transaction :return: the edited transaction
""" """
logger.debug("receive_request - " + str(request)) logger.info("receive_request - " + str(request))
try: try:
host, port = request.source host, port = request.source
except AttributeError: except AttributeError:
@ -101,7 +101,7 @@ class MessageLayer(object):
:rtype : Transaction :rtype : Transaction
:return: the transaction to which the response belongs to :return: the transaction to which the response belongs to
""" """
logger.debug("receive_response - " + str(response)) logger.info("receive_response - " + str(response))
try: try:
host, port = response.source host, port = response.source
except AttributeError: except AttributeError:
@ -148,7 +148,7 @@ class MessageLayer(object):
:rtype : Transaction :rtype : Transaction
:return: the transaction to which the message belongs to :return: the transaction to which the message belongs to
""" """
logger.debug("receive_empty - " + str(message)) logger.info("receive_empty - " + str(message))
try: try:
host, port = message.source host, port = message.source
except AttributeError: except AttributeError:
@ -201,7 +201,7 @@ class MessageLayer(object):
:rtype : Transaction :rtype : Transaction
:return: the created transaction :return: the created transaction
""" """
logger.debug("send_request - " + str(request)) logger.info("send_request - " + str(request))
assert isinstance(request, Request) assert isinstance(request, Request)
try: try:
host, port = request.destination host, port = request.destination
@ -233,7 +233,7 @@ class MessageLayer(object):
:rtype : Transaction :rtype : Transaction
:return: the edited 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.response.type is None:
if transaction.request.type == defines.Types["CON"] and not transaction.request.acknowledged: if transaction.request.type == defines.Types["CON"] and not transaction.request.acknowledged:
transaction.response.type = defines.Types["ACK"] transaction.response.type = defines.Types["ACK"]
@ -268,7 +268,7 @@ class MessageLayer(object):
:type message: Message :type message: Message
:param message: the ACK or RST message to send :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: if transaction is None:
try: try:
host, port = message.destination host, port = message.destination

@ -188,7 +188,7 @@ class ObserveLayer(object):
:param message: the message :param message: the message
""" """
logger.debug("Remove Subcriber") logger.info("Remove Subcriber")
host, port = message.destination host, port = message.destination
key_token = hash(str(host) + str(port) + str(message.token)) key_token = hash(str(host) + str(port) + str(message.token))
try: try:

@ -454,7 +454,7 @@ class Message(object):
for e in etag: for e in etag:
option = Option() option = Option()
option.number = defines.OptionRegistry.ETAG.number option.number = defines.OptionRegistry.ETAG.number
if not isinstance(e, bytes): if not isinstance(e, bytes):
e = bytes(e, "utf-8") e = bytes(e, "utf-8")
option.value = e option.value = e
self.add_option(option) 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, .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) code=defines.Codes.LIST[self._code].name, token=self._token)
for opt in self._options: 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 += "]" msg += "]"
if self.payload is not None: if self.payload is not None:
if isinstance(self.payload, dict): if isinstance(self.payload, dict):

@ -273,7 +273,8 @@ class CoAP(object):
rst.code = message rst.code = message
self.send_datagram(rst) self.send_datagram(rst)
return return
logger.debug("receive_datagram - " + str(message))
logger.info("receive_datagram - " + str(message))
if isinstance(message, Request): if isinstance(message, Request):
transaction = self._messageLayer.receive_request(message) transaction = self._messageLayer.receive_request(message)
@ -319,6 +320,7 @@ class CoAP(object):
transaction = self._blockLayer.send_response(transaction) transaction = self._blockLayer.send_response(transaction)
transaction = self._cacheLayer.send_response(transaction) transaction = self._cacheLayer.send_response(transaction)
else: else:
transaction = self._forwardLayer.receive_request_reverse(transaction) transaction = self._forwardLayer.receive_request_reverse(transaction)
@ -353,7 +355,7 @@ class CoAP(object):
""" """
if not self.stopped.isSet(): if not self.stopped.isSet():
host, port = message.destination host, port = message.destination
logger.debug("send_datagram - " + str(message)) logger.info("send_datagram - " + str(message))
serializer = Serializer() serializer = Serializer()
message = serializer.serialize(message) message = serializer.serialize(message)

@ -70,6 +70,7 @@ class Serializer(object):
# the first 4 bits of the byte represent the option delta # the first 4 bits of the byte represent the option delta
# delta = self._reader.read(4).uint # delta = self._reader.read(4).uint
num, option_length, pos = Serializer.read_option_value_len_from_byte(next_byte, pos, values) 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 current_option += num
# read option # read option
try: try:
@ -81,8 +82,7 @@ class Serializer(object):
else: else:
# If the non-critical option is unknown # If the non-critical option is unknown
# (vendor-specific, proprietary) - just skip it # (vendor-specific, proprietary) - just skip it
#log.err("unrecognized option %d" % current_option) logger.warning("unrecognized option %d", current_option)
pass
else: else:
if option_length == 0: if option_length == 0:
value = None value = None
@ -313,6 +313,7 @@ class Serializer(object):
length = struct.unpack("!B", values[pos].to_bytes(1, "big"))[0] + 13 length = struct.unpack("!B", values[pos].to_bytes(1, "big"))[0] + 13
pos += 1 pos += 1
elif l_nibble == 14: elif l_nibble == 14:
s = struct.Struct("!H")
length = s.unpack_from(values[pos:].to_bytes(2, "big"))[0] + 269 length = s.unpack_from(values[pos:].to_bytes(2, "big"))[0] + 269
pos += 2 pos += 2
else: else:

@ -1,5 +1,4 @@
import logging.config import logging
import os
import random import random
import socket import socket
import struct import struct
@ -17,17 +16,13 @@ from coapthon.messages.request import Request
from coapthon.messages.response import Response from coapthon.messages.response import Response
from coapthon.resources.resource import Resource from coapthon.resources.resource import Resource
from coapthon.serializer import Serializer from coapthon.serializer import Serializer
from coapthon.utils import Tree, create_logging from coapthon.utils import Tree
__author__ = 'Giacomo Tanganelli' __author__ = 'Giacomo Tanganelli'
if not os.path.isfile("logging.conf"):
create_logging()
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
logging.config.fileConfig("logging.conf", disable_existing_loggers=False)
class CoAP(object): class CoAP(object):
@ -155,7 +150,7 @@ class CoAP(object):
self.send_datagram(rst) self.send_datagram(rst)
continue continue
logger.debug("receive_datagram - " + str(message)) logger.info("receive_datagram - " + str(message))
if isinstance(message, Request): if isinstance(message, Request):
transaction = self._messageLayer.receive_request(message) transaction = self._messageLayer.receive_request(message)
if transaction.request.duplicated and transaction.completed: if transaction.request.duplicated and transaction.completed:
@ -247,7 +242,7 @@ class CoAP(object):
""" """
if not self.stopped.isSet(): if not self.stopped.isSet():
host, port = message.destination host, port = message.destination
logger.debug("send_datagram - " + str(message)) logger.info("send_datagram - " + str(message))
serializer = Serializer() serializer = Serializer()
message = serializer.serialize(message) message = serializer.serialize(message)
self._socket.sendto(message, (host, port)) self._socket.sendto(message, (host, port))

Loading…
Cancel
Save