proper logging, add signal handler for graceful termination

This commit is contained in:
2025-05-23 13:52:42 +02:00
parent 04bf09ae5a
commit ef35d18f37
6 changed files with 52 additions and 39 deletions

View File

@ -3,6 +3,7 @@ import os
import json
import time
import socket
import logging
from collections import namedtuple
from select import select
from streams import Stream, Base, StreamDead
@ -112,7 +113,7 @@ class SecopStream(Stream):
except Exception as e:
# probably other end closed
print(self.uri, repr(e))
logging.info('%r on %s', e, self.uri)
SECOP_UDP_PORT = 10767
@ -154,7 +155,7 @@ class ScanReply(UdpStream):
sock.sendto(json.dumps(dict(SECoP='discover')).encode('utf-8'),
('255.255.255.255', SECOP_UDP_PORT))
except OSError as e:
print('could not send the broadcast:', e)
logging.info('could not send the broadcast %r:', e)
self.socket = sock
self.select_read[sock.fileno()] = self
@ -193,12 +194,11 @@ class TrySecopConnect(Base):
try:
self.socket.sendall(b'*IDN?\n')
self.idn_sent = True
print('SEND IDN', self.uri)
logging.debug('SEND IDN %s', self.uri)
self.select_read[self.fno] = self
return
except Exception as e:
print('NO CONN TO', self.uri)
print(e)
logging.info('NO CONN TO %s %r', self.uri, e)
else:
reply = b''
try:
@ -206,12 +206,12 @@ class TrySecopConnect(Base):
if chunk:
self.idn += chunk
if b'SECoP' in self.idn:
print('CONN TO', self.uri)
logging.info('connected to %s', self.uri)
yield SecopStream, self.uri, {'stream': self.uri}
if b'\n' not in self.idn:
return
except Exception as e:
print(e)
except Exception:
logging.exception('receiving')
self.select_read.pop(self.fno)