2020-01-17 11:03:22 -08:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
import zmq
|
2021-06-03 06:21:04 -04:00
|
|
|
from typing import NoReturn
|
|
|
|
|
|
2020-01-17 11:03:22 -08:00
|
|
|
import cereal.messaging as messaging
|
2023-08-20 20:49:55 -07:00
|
|
|
from openpilot.common.logging_extra import SwagLogFileFormatter
|
2023-09-07 11:32:47 -07:00
|
|
|
from openpilot.system.hardware.hw import Paths
|
2023-12-06 17:27:51 -08:00
|
|
|
from openpilot.common.swaglog import get_file_handler
|
2020-01-17 11:03:22 -08:00
|
|
|
|
2020-04-02 11:28:20 -07:00
|
|
|
|
2021-06-03 06:21:04 -04:00
|
|
|
def main() -> NoReturn:
|
2021-03-25 13:30:09 -07:00
|
|
|
log_handler = get_file_handler()
|
|
|
|
|
log_handler.setFormatter(SwagLogFileFormatter(None))
|
|
|
|
|
log_level = 20 # logging.INFO
|
2020-01-17 11:03:22 -08:00
|
|
|
|
2023-06-14 22:48:51 -07:00
|
|
|
ctx = zmq.Context.instance()
|
2020-01-17 11:03:22 -08:00
|
|
|
sock = ctx.socket(zmq.PULL)
|
2023-09-07 11:32:47 -07:00
|
|
|
sock.bind(Paths.swaglog_ipc())
|
2020-01-17 11:03:22 -08:00
|
|
|
|
|
|
|
|
# and we publish them
|
2022-01-07 00:30:04 +01:00
|
|
|
log_message_sock = messaging.pub_sock('logMessage')
|
|
|
|
|
error_log_message_sock = messaging.pub_sock('errorLogMessage')
|
2020-01-17 11:03:22 -08:00
|
|
|
|
2023-06-14 22:48:51 -07:00
|
|
|
try:
|
|
|
|
|
while True:
|
|
|
|
|
dat = b''.join(sock.recv_multipart())
|
|
|
|
|
level = dat[0]
|
|
|
|
|
record = dat[1:].decode("utf-8")
|
|
|
|
|
if level >= log_level:
|
|
|
|
|
log_handler.emit(record)
|
2020-01-17 11:03:22 -08:00
|
|
|
|
2023-06-14 22:48:51 -07:00
|
|
|
if len(record) > 2*1024*1024:
|
|
|
|
|
print("WARNING: log too big to publish", len(record))
|
2024-06-30 08:34:46 +08:00
|
|
|
print(record[:100])
|
2023-06-14 22:48:51 -07:00
|
|
|
continue
|
2022-01-07 00:30:04 +01:00
|
|
|
|
2023-06-14 22:48:51 -07:00
|
|
|
# then we publish them
|
2023-12-03 10:50:17 -08:00
|
|
|
msg = messaging.new_message(None, valid=True, logMessage=record)
|
2023-06-14 22:48:51 -07:00
|
|
|
log_message_sock.send(msg.to_bytes())
|
|
|
|
|
|
|
|
|
|
if level >= 40: # logging.ERROR
|
2023-12-03 10:50:17 -08:00
|
|
|
msg = messaging.new_message(None, valid=True, errorLogMessage=record)
|
2023-06-14 22:48:51 -07:00
|
|
|
error_log_message_sock.send(msg.to_bytes())
|
|
|
|
|
finally:
|
|
|
|
|
sock.close()
|
|
|
|
|
ctx.term()
|
2023-06-15 22:05:44 -07:00
|
|
|
|
|
|
|
|
# can hit this if interrupted during a rollover
|
|
|
|
|
try:
|
|
|
|
|
log_handler.close()
|
|
|
|
|
except ValueError:
|
|
|
|
|
pass
|
2020-04-02 11:28:20 -07:00
|
|
|
|
2020-01-17 11:03:22 -08:00
|
|
|
if __name__ == "__main__":
|
|
|
|
|
main()
|