1#!/usr/bin/env python3 2# 3# Copyright (c) 2024 Nordic Semiconductor ASA 4# 5# SPDX-License-Identifier: Apache-2.0 6 7""" 8Log Parser for Dictionary-based Logging 9 10This uses the JSON database file to decode the binary 11log data taken directly from input serialport and print 12the log messages. 13""" 14 15import argparse 16import logging 17import sys 18import time 19 20import parserlib 21import serial 22 23LOGGER_FORMAT = "%(message)s" 24logger = logging.getLogger("parser") 25 26def parse_args(): 27 """Parse command line arguments""" 28 argparser = argparse.ArgumentParser(allow_abbrev=False) 29 30 argparser.add_argument("dbfile", help="Dictionary Logging Database file") 31 argparser.add_argument("serialPort", help="Port where the logs are generated") 32 argparser.add_argument("baudrate", help="Serial Port baud rate") 33 argparser.add_argument("--debug", action="store_true", 34 help="Print extra debugging information") 35 36 return argparser.parse_args() 37 38def main(): 39 """function of serial parser""" 40 args = parse_args() 41 42 if args.dbfile is None or '.json' not in args.dbfile: 43 logger.error("ERROR: invalid log database path: %s, exiting...", args.dbfile) 44 sys.exit(1) 45 46 logging.basicConfig(format=LOGGER_FORMAT) 47 48 if args.debug: 49 logger.setLevel(logging.DEBUG) 50 else: 51 logger.setLevel(logging.INFO) 52 53 # Parse the log every second from serial port 54 with serial.Serial(args.serialPort, args.baudrate) as ser: 55 ser.timeout = 2 56 while True: 57 size = ser.inWaiting() 58 if size: 59 data = ser.read(size) 60 parserlib.parser(data, args.dbfile, logger) 61 time.sleep(1) 62 63if __name__ == "__main__": 64 main() 65