Last active
August 29, 2015 14:14
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
#encoding='utf-8' | |
"""readable_state.py: convert magic number to readable state.""" | |
__author__ = "legendmohe" | |
from pprint import pprint | |
import argparse | |
import json | |
import re | |
g_conf_file = "readable_state.json" | |
g_regx_islog = re.compile(r"^.+\s{1,3}?\d+\s{1,3}?\d+ [IDEVW] ") | |
g_regx_dump_begin = re.compile(r"------ SYSTEM LOG.*") | |
g_regx_dump_end = re.compile(r"\[logcat: \d+s elapsed\]") | |
def convert(line, conf): | |
global g_regx_islog | |
if not g_regx_islog.match(line): | |
return None # filter log lone | |
log_head = line[:33] | |
line = line[33:].strip() # remove log head | |
divider = line.find(":") | |
tag = line[:divider] | |
line = line[divider + 2:] | |
cf_defines = conf['define'] | |
if not tag.strip() in conf['tags']: | |
return log_head + tag + ': ' + line # filter not defined log | |
# print(log_head + tag + ': ' + line) | |
cf_targets = conf['tags'][tag.strip()] # strip for space | |
for target in cf_targets: | |
m = re.search(target['regex'], line) | |
if not m is None: | |
define_dict = cf_defines[target['define']] | |
type_array = target['type'] | |
result = "" | |
for idx, val in enumerate(m.groups()): | |
arg = define_dict[type_array[idx]] | |
readable = arg[val] | |
result += line[:m.start(idx + 1)] | |
result += val + " (" + readable + ") " | |
if idx + 1 == m.lastindex: | |
line = line[m.end(idx + 1):] | |
else: | |
line = line[m.end(idx + 1):m.start(idx + 2)] | |
result += line | |
return log_head + tag + ': ' + result | |
return log_head + tag + ': ' + line | |
def convert_file(file_path, conf): | |
result = "" | |
with open(file_path, 'r') as input_file: | |
in_log = False | |
for line in input_file: | |
if not in_log: | |
if g_regx_dump_begin.match(line): | |
in_log = True | |
continue | |
if g_regx_dump_end.match(line): | |
break | |
tmp = convert(line, conf) | |
if not tmp is None: | |
# print(tmp) | |
result += tmp + '\n' | |
if len(result) != 0: | |
with open(file_path + '_conv.log', 'w') as output_file: | |
output_file.write(result) | |
else: | |
print("no result!") | |
def setup(conf_path): | |
with open(conf_path, 'r') as conf_file: | |
content = conf_file.read() | |
data = json.loads(content.replace('\\', '\\\\')) | |
return data | |
def main(file_paths): | |
global g_conf_file | |
conf_data = setup(g_conf_file) | |
for file_path in file_paths: | |
convert_file(file_path, conf_data) | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser() | |
parser.add_argument( | |
'paths', type=str, nargs='+', help='file path list') | |
args = parser.parse_args() | |
if len(args.paths) != 0: | |
main(args.paths) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment