moninj,corelog: fix/cleanup exception handling (#1897)

This commit is contained in:
Sebastien Bourdeauducq 2022-07-02 17:48:18 +08:00
parent 02b086c9e5
commit 388b81af19
2 changed files with 46 additions and 40 deletions

View File

@ -91,6 +91,10 @@ class CommMonInj:
self.injection_status_cb(channel, override, value)
else:
raise ValueError("Unknown packet type", ty)
except asyncio.CancelledError:
raise
except:
logger.error("Moninj connection terminating with exception", exc_info=True)
finally:
if self.disconnect_cb is not None:
self.disconnect_cb()

View File

@ -40,44 +40,49 @@ async def get_logs_sim(host):
async def get_logs(host):
reader, writer = await async_open_connection(
host,
1380,
after_idle=1,
interval=1,
max_fails=3,
)
writer.write(b"ARTIQ management\n")
endian = await reader.readexactly(1)
if endian == b"e":
endian = "<"
elif endian == b"E":
endian = ">"
else:
raise IOError("Incorrect reply from device: expected e/E.")
writer.write(struct.pack("B", Request.PullLog.value))
await writer.drain()
try:
reader, writer = await async_open_connection(
host,
1380,
after_idle=1,
interval=1,
max_fails=3,
)
writer.write(b"ARTIQ management\n")
endian = await reader.readexactly(1)
if endian == b"e":
endian = "<"
elif endian == b"E":
endian = ">"
else:
raise IOError("Incorrect reply from device: expected e/E.")
writer.write(struct.pack("B", Request.PullLog.value))
await writer.drain()
while True:
length, = struct.unpack(endian + "l", await reader.readexactly(4))
log = await reader.readexactly(length)
while True:
length, = struct.unpack(endian + "l", await reader.readexactly(4))
log = await reader.readexactly(length)
for line in log.decode("utf-8").splitlines():
m = re.match(r"^\[.+?\] (TRACE|DEBUG| INFO| WARN|ERROR)\((.+?)\): (.+)$", line)
levelname = m.group(1)
if levelname == 'TRACE':
level = logging.TRACE
elif levelname == 'DEBUG':
level = logging.DEBUG
elif levelname == ' INFO':
level = logging.INFO
elif levelname == ' WARN':
level = logging.WARN
elif levelname == 'ERROR':
level = logging.ERROR
name = 'firmware.' + m.group(2).replace('::', '.')
text = m.group(3)
log_with_name(name, level, text)
for line in log.decode("utf-8").splitlines():
m = re.match(r"^\[.+?\] (TRACE|DEBUG| INFO| WARN|ERROR)\((.+?)\): (.+)$", line)
levelname = m.group(1)
if levelname == 'TRACE':
level = logging.TRACE
elif levelname == 'DEBUG':
level = logging.DEBUG
elif levelname == ' INFO':
level = logging.INFO
elif levelname == ' WARN':
level = logging.WARN
elif levelname == 'ERROR':
level = logging.ERROR
name = 'firmware.' + m.group(2).replace('::', '.')
text = m.group(3)
log_with_name(name, level, text)
except asyncio.CancelledError:
raise
except:
logger.error("Logging connection terminating with exception", exc_info=True)
def main():
@ -99,8 +104,7 @@ def main():
_, pending = loop.run_until_complete(asyncio.wait(
[signal_handler.wait_terminate(),
server.wait_terminate(),
get_logs_task
],
get_logs_task],
return_when=asyncio.FIRST_COMPLETED))
for task in pending:
task.cancel()
@ -108,8 +112,6 @@ def main():
loop.run_until_complete(server.stop())
finally:
pass
except Exception:
logger.error("Termination due to exception", exc_info=True)
finally:
signal_handler.teardown()
finally: