diff --git a/src_py/hat/syslog/handler.py b/src_py/hat/syslog/handler.py index 098c148..ff6e7a6 100644 --- a/src_py/hat/syslog/handler.py +++ b/src_py/hat/syslog/handler.py @@ -151,6 +151,7 @@ def _logging_handler_thread(state): time.sleep(state.reconnect_delay) continue + msg = None try: while True: with state.cv: @@ -161,23 +162,24 @@ def _logging_handler_thread(state): return if state.dropped[0]: - msg = _create_dropped_msg( + msg = None + dropped_msg = _create_dropped_msg( state.dropped[0], '_logging_handler_thread', 0) + _send_message(s, dropped_msg, state.comm_type == common.CommType.UDP) state.dropped[0] = 0 + continue else: msg = state.queue.popleft() - msg_bytes = encoder.msg_to_str(msg).encode() - - if state.comm_type == common.CommType.UDP: - s.send(msg_bytes) - - else: - s.send(f'{len(msg_bytes)} '.encode() + msg_bytes) + _send_message(s, msg, state.comm_type == common.CommType.UDP) + msg = None except Exception: - pass + # When failing with msg assigned, put the msg back. + # After reconnecting we will try it again. + if msg is not None: + state.queue.appendleft(msg) finally: with contextlib.suppress(Exception): @@ -212,6 +214,14 @@ def _record_to_msg(record): msg=record.getMessage()) +def _send_message(s, msg, only_bytes=False): + msg_bytes = encoder.msg_to_str(msg).encode() + if only_bytes: + s.send(msg_bytes) + else: + s.send(f'{len(msg_bytes)} '.encode() + msg_bytes) + + def _create_dropped_msg(dropped, func_name, lineno): hat_data = {'name': __name__, 'thread': str(threading.get_ident()),