Skip to content

Commit a899ded

Browse files
Kumar Siddaramayyafabiobaltieri
authored andcommitted
scripts: logging: dictionary: Add uart support
This commit adds support to dictionary logging to parse binary data directly from input serial and generate the ascii logs. Signed-off-by: Kumar Siddaramayya <[email protected]>
1 parent 43f9488 commit a899ded

File tree

3 files changed

+118
-28
lines changed

3 files changed

+118
-28
lines changed

scripts/logging/dictionary/log_parser.py

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env python3
22
#
33
# Copyright (c) 2021 Intel Corporation
4+
# Copyright (c) 2024 Nordic Semiconductor ASA
45
#
56
# SPDX-License-Identifier: Apache-2.0
67

@@ -17,8 +18,7 @@
1718
import sys
1819

1920
import dictionary_parser
20-
from dictionary_parser.log_database import LogDatabase
21-
21+
import parserlib
2222

2323
LOGGER_FORMAT = "%(message)s"
2424
logger = logging.getLogger("parser")
@@ -47,6 +47,7 @@ def read_log_file(args):
4747
Read the log from file
4848
"""
4949
logdata = None
50+
hexdata = ''
5051

5152
# Open log data file for reading
5253
if args.hex:
@@ -100,7 +101,6 @@ def read_log_file(args):
100101

101102
return logdata
102103

103-
104104
def main():
105105
"""Main function of log parser"""
106106
args = parse_args()
@@ -112,36 +112,12 @@ def main():
112112
else:
113113
logger.setLevel(logging.INFO)
114114

115-
# Read from database file
116-
database = LogDatabase.read_json_database(args.dbfile)
117-
if database is None:
118-
logger.error("ERROR: Cannot open database file: %s, exiting...", args.dbfile)
119-
sys.exit(1)
120-
121115
logdata = read_log_file(args)
122116
if logdata is None:
123117
logger.error("ERROR: cannot read log from file: %s, exiting...", args.logfile)
124118
sys.exit(1)
125119

126-
log_parser = dictionary_parser.get_parser(database)
127-
if log_parser is not None:
128-
logger.debug("# Build ID: %s", database.get_build_id())
129-
logger.debug("# Target: %s, %d-bit", database.get_arch(), database.get_tgt_bits())
130-
if database.is_tgt_little_endian():
131-
logger.debug("# Endianness: Little")
132-
else:
133-
logger.debug("# Endianness: Big")
134-
135-
logger.debug("# Database version: %d", database.get_version())
136-
137-
ret = log_parser.parse_log_data(logdata, debug=args.debug)
138-
if not ret:
139-
logger.error("ERROR: there were error(s) parsing log data")
140-
sys.exit(1)
141-
else:
142-
logger.error("ERROR: Cannot find a suitable parser matching database version!")
143-
sys.exit(1)
144-
120+
parserlib.parser(logdata, args.dbfile, logger)
145121

146122
if __name__ == "__main__":
147123
main()
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#!/usr/bin/env python3
2+
#
3+
# Copyright (c) 2024 Nordic Semiconductor ASA
4+
#
5+
# SPDX-License-Identifier: Apache-2.0
6+
7+
"""
8+
Log Parser for Dictionary-based Logging
9+
10+
This uses the JSON database file to decode the binary
11+
log data taken directly from input serialport and print
12+
the log messages.
13+
"""
14+
15+
import serial
16+
import time
17+
import argparse
18+
import logging
19+
import sys
20+
21+
import parserlib
22+
23+
LOGGER_FORMAT = "%(message)s"
24+
logger = logging.getLogger("parser")
25+
26+
def 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+
38+
def 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+
63+
if __name__ == "__main__":
64+
main()
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#!/usr/bin/env python3
2+
#
3+
# Copyright (c) 2021 Intel Corporation
4+
# Copyright (c) 2024 Nordic Semiconductor ASA
5+
#
6+
# SPDX-License-Identifier: Apache-2.0
7+
8+
"""
9+
Parser library for Dictionary-based Logging
10+
11+
This library along with dictionary_parser converts the
12+
input binary data to the log using log database.
13+
"""
14+
import sys
15+
import logging
16+
17+
import dictionary_parser
18+
from dictionary_parser.log_database import LogDatabase
19+
20+
def parser(logdata, dbfile, logger):
21+
"""function of serial parser"""
22+
# Read from database file
23+
database = LogDatabase.read_json_database(dbfile)
24+
25+
if not isinstance(logger, logging.Logger):
26+
raise ValueError("Invalid logger instance. Please configure the logger!")
27+
28+
if database is None:
29+
logger.error("ERROR: Cannot open database file: exiting...")
30+
sys.exit(1)
31+
32+
if logdata is None:
33+
logger.error("ERROR: cannot read log from file: exiting...")
34+
sys.exit(1)
35+
36+
log_parser = dictionary_parser.get_parser(database)
37+
if log_parser is not None:
38+
logger.debug("# Build ID: %s", database.get_build_id())
39+
logger.debug("# Target: %s, %d-bit", database.get_arch(), database.get_tgt_bits())
40+
if database.is_tgt_little_endian():
41+
logger.debug("# Endianness: Little")
42+
else:
43+
logger.debug("# Endianness: Big")
44+
45+
ret = log_parser.parse_log_data(logdata)
46+
if not ret:
47+
logger.error("ERROR: there were error(s) parsing log data")
48+
sys.exit(1)
49+
else:
50+
logger.error("ERROR: Cannot find a suitable parser matching database version!")

0 commit comments

Comments
 (0)