Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 additions & 9 deletions src_py/hat/syslog/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ def _logging_handler_thread(state):
time.sleep(state.reconnect_delay)
continue

msg = None
try:
while True:
with state.cv:
Expand All @@ -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):
Expand Down Expand Up @@ -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()),
Expand Down