Add own logger
@@ -5,6 +5,11 @@ import struct
sel = selectors.DefaultSelector()
+logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
+ level=20)
+logger = logging.getLogger(__name__)
+
+
class Network:
def __init__(self, host, port):
@@ -14,20 +19,16 @@ class Network:
self.sock = None
self.sel = sel
- logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
- level=20)
- self.logger = logging.getLogger(__name__)
-
self.clients = list()
def initialise(self):
self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.sock.bind(self.addr)
- logging.info('Binded socket to {}'.format(self.addr))
+ logger.info('Binded socket to {}'.format(self.addr))
self.sel.register(self.sock, selectors.EVENT_READ | selectors.EVENT_WRITE)
def send(self, msg, dest_addr):
- logging.info('Network - sending interface')
+ logger.info('Network - sending interface')
if type(dest_addr) is list:
for dest in dest_addr:
self._sendto(msg, dest)
@@ -35,29 +36,29 @@ class Network:
self._sendto(msg, dest_addr)
def _sendto(self, msg, address):
- logging.info('Network - Sending')
+ logger.info('Network - Sending')
msg = struct.pack('f', msg) # need to move encoding to dedicated message processing
self.sock.sendto(msg, address)
- logging.info('Network - Sent data packet to {}'.format(address))
+ logger.info('Network - Sent data packet to {}'.format(address))
def receive(self):
r_msg, client_addr = self.sock.recvfrom(41)
- logging.info('Received data packet from {}'.format(client_addr))
+ logger.info('Received data packet from {}'.format(client_addr))
self.add_client(client_addr)
r_msg = struct.unpack('f', r_msg) # need to move decoding to dedicated message processing
return r_msg
def process_events(self, mask, in_queue, out_queue):
if mask and selectors.EVENT_READ:
- logging.info('Network - Receiving')
+ logger.info('Network - Receiving')
msg = self.receive()
# would need to process msg beforehand
in_queue.put(msg)
- logging.info('Network - Placed message in the queue')
+ logger.info('Network - Placed message in the queue')
if mask and selectors.EVENT_WRITE:
msg = out_queue.get()
- logging.info('Network - Got message from the queue')
+ logger.info('Network - Got message from the queue')
self.send(msg, self.clients)
# return in_queue, out_queue: not needed, processing done on original objects
@@ -68,6 +69,6 @@ class Network:
def close(self):
self.sel.unregister(self.sock)
- logging.info('Unregistered socket from selectors')
+ logger.info('Unregistered socket from selectors')
self.sock.close()
- logging.info('Closed socket')
+ logger.info('Closed socket')
