dragonpilot - 基於 openpilot 的開源駕駛輔助系統
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
1.5 KiB

5 years ago
#!/usr/bin/env python3
import zmq
from typing import NoReturn
5 years ago
import cereal.messaging as messaging
from openpilot.common.logging_extra import SwagLogFileFormatter
from openpilot.system.swaglog import get_file_handler
from system.swaglog import SWAGLOG_IPC
5 years ago
def main() -> NoReturn:
log_handler = get_file_handler()
log_handler.setFormatter(SwagLogFileFormatter(None))
log_level = 20 # logging.INFO
5 years ago
ctx = zmq.Context.instance()
5 years ago
sock = ctx.socket(zmq.PULL)
sock.bind(f"ipc://{SWAGLOG_IPC}")
5 years ago
# and we publish them
log_message_sock = messaging.pub_sock('logMessage')
error_log_message_sock = messaging.pub_sock('errorLogMessage')
5 years ago
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)
5 years ago
if len(record) > 2*1024*1024:
print("WARNING: log too big to publish", len(record))
print(print(record[:100]))
continue
# then we publish them
msg = messaging.new_message()
msg.logMessage = record
log_message_sock.send(msg.to_bytes())
if level >= 40: # logging.ERROR
msg = messaging.new_message()
msg.errorLogMessage = record
error_log_message_sock.send(msg.to_bytes())
finally:
sock.close()
ctx.term()
# can hit this if interrupted during a rollover
try:
log_handler.close()
except ValueError:
pass
5 years ago
if __name__ == "__main__":
main()