Browse Source

scripts: logging/dictionary: refactor common parser functions...

... and put them into the LogParser class file instead of
the verisoned parser. This is in preparation for introducing
a new parser version.

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
pull/75198/head
Daniel Leung 1 year ago committed by Alberto Escolar
parent
commit
ecc642b2df
  1. 38
      scripts/logging/dictionary/dictionary_parser/log_parser.py
  2. 37
      scripts/logging/dictionary/dictionary_parser/log_parser_v1.py

38
scripts/logging/dictionary/dictionary_parser/log_parser.py

@ -9,6 +9,41 @@ Abstract Class for Dictionary-based Logging Parsers @@ -9,6 +9,41 @@ Abstract Class for Dictionary-based Logging Parsers
"""
import abc
from colorama import Fore
from .data_types import DataTypes
LOG_LEVELS = [
('none', Fore.WHITE),
('err', Fore.RED),
('wrn', Fore.YELLOW),
('inf', Fore.GREEN),
('dbg', Fore.BLUE)
]
def get_log_level_str_color(lvl):
"""Convert numeric log level to string"""
if lvl < 0 or lvl >= len(LOG_LEVELS):
return ("unk", Fore.WHITE)
return LOG_LEVELS[lvl]
def formalize_fmt_string(fmt_str):
"""Replace unsupported formatter"""
new_str = fmt_str
for spec in ['d', 'i', 'o', 'u', 'x', 'X']:
# Python doesn't support %ll for integer specifiers, so remove extra 'l'
new_str = new_str.replace("%ll" + spec, "%l" + spec)
# Python doesn't support %hh for integer specifiers, so remove extra 'h'
new_str = new_str.replace("%hh" + spec, "%h" + spec)
# No %p for pointer either, so use %x
new_str = new_str.replace("%p", "0x%x")
return new_str
class LogParser(abc.ABC):
@ -16,6 +51,9 @@ class LogParser(abc.ABC): @@ -16,6 +51,9 @@ class LogParser(abc.ABC):
def __init__(self, database):
self.database = database
self.data_types = DataTypes(self.database)
@abc.abstractmethod
def parse_log_data(self, logdata, debug=False):
"""Parse log data"""

37
scripts/logging/dictionary/dictionary_parser/log_parser_v1.py

@ -17,20 +17,12 @@ import struct @@ -17,20 +17,12 @@ import struct
import colorama
from colorama import Fore
from .log_parser import LogParser
from .log_parser import (LogParser, get_log_level_str_color, formalize_fmt_string)
from .data_types import DataTypes
HEX_BYTES_IN_LINE = 16
LOG_LEVELS = [
('none', Fore.WHITE),
('err', Fore.RED),
('wrn', Fore.YELLOW),
('inf', Fore.GREEN),
('dbg', Fore.BLUE)
]
# Need to keep sync with struct log_dict_output_msg_hdr in
# include/logging/log_output_dict.h.
#
@ -68,31 +60,6 @@ FMT_DROPPED_CNT = "H" @@ -68,31 +60,6 @@ FMT_DROPPED_CNT = "H"
logger = logging.getLogger("parser")
def get_log_level_str_color(lvl):
"""Convert numeric log level to string"""
if lvl < 0 or lvl >= len(LOG_LEVELS):
return ("unk", Fore.WHITE)
return LOG_LEVELS[lvl]
def formalize_fmt_string(fmt_str):
"""Replace unsupported formatter"""
new_str = fmt_str
for spec in ['d', 'i', 'o', 'u', 'x', 'X']:
# Python doesn't support %ll for integer specifiers, so remove extra 'l'
new_str = new_str.replace("%ll" + spec, "%l" + spec)
# Python doesn't support %hh for integer specifiers, so remove extra 'h'
new_str = new_str.replace("%hh" + spec, "%h" + spec)
# No %p for pointer either, so use %x
new_str = new_str.replace("%p", "0x%x")
return new_str
class LogParserV1(LogParser):
"""Log Parser V1"""
def __init__(self, database):
@ -116,8 +83,6 @@ class LogParserV1(LogParser): @@ -116,8 +83,6 @@ class LogParserV1(LogParser):
else:
self.fmt_msg_timestamp = endian + FMT_MSG_TIMESTAMP_32
self.data_types = DataTypes(self.database)
def __get_string(self, arg, arg_offset, string_tbl):
one_str = self.database.find_string(arg)

Loading…
Cancel
Save