Last active
March 9, 2020 12:04
-
-
Save pgp/f9026daf0e4b482761387fd95bc1acbb to your computer and use it in GitHub Desktop.
Colorize logcat file and print to console
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
import sys | |
import re | |
class bcolors: | |
HEADER = '\033[95m' | |
OKBLUE = '\033[94m' | |
OKGREEN = '\033[92m' | |
WARNING = '\033[93m' | |
FAIL = '\033[91m' | |
ENDC = '\033[0m' | |
BOLD = '\033[1m' | |
UNDERLINE = '\033[4m' | |
bcolorsMap = { | |
'b':'\033[94m', | |
'g' : '\033[92m', | |
'y' : '\033[93m', | |
'r' : '\033[91m', | |
'_' : '\033[0m' | |
} | |
logcat_regex = r"\d{2}-\d{2} \d{2}:\d{2}:\d{2}.\d{3} [0-9]+-[0-9]+\/[^ ]+ ([E|W|I|D])\/" | |
colorMap = { | |
'E': 'r', # red | |
'W': 'y', # yellow | |
'I': '_', # default | |
'D': 'b', # blue | |
} | |
htmlColorMap = { | |
'E': ('<font color="#EF2929">','</font>'), # red | |
'W': ('<font color="#FCE94F">','</font>'), # yellow | |
'I': ('',''), # default | |
'D': ('<font color="#729FCF">','</font>'), # blue | |
} | |
def logcat_file_to_colored_stdout(logcat_path): | |
lastColor = 'I' | |
with open(logcat_path) as f: | |
for row in f: | |
matches = list(re.finditer(logcat_regex, row)) | |
if matches: | |
matchgroups = list(matches[0].groups()) | |
if matchgroups: | |
lastColor = matchgroups[0] | |
cstdout(row,colorMap[lastColor]) # on rows that do not match the regex, reuse previous line color | |
wordWrapHeaders = '''<!DOCTYPE html> | |
<html> | |
<head> | |
<style> | |
pre { | |
white-space: pre-wrap; /* Since CSS 2.1 */ | |
white-space: -moz-pre-wrap; /* Mozilla, since 1999 */ | |
white-space: -pre-wrap; /* Opera 4-6 */ | |
white-space: -o-pre-wrap; /* Opera 7 */ | |
word-wrap: break-word; /* Internet Explorer 5.5+ */ | |
} | |
</style> | |
</head> | |
<body> | |
''' | |
wordWrapFooter = '''</body> | |
</html> | |
''' | |
headerFooterMap = { | |
True: (wordWrapHeaders.encode('utf-8'),wordWrapFooter.encode('utf-8')), | |
False: (b'',b'') | |
} | |
def logcat_file_to_colored_html(logcat_path, word_wrap=False): | |
lastColor = 'I' | |
with open(logcat_path) as f: | |
with open(logcat_path+'.html','wb') as g: | |
header,footer = headerFooterMap[word_wrap] | |
g.write(header) | |
g.write('<pre>'.encode('utf-8')) | |
for row in f: | |
matches = list(re.finditer(logcat_regex, row)) | |
if matches: | |
matchgroups = list(matches[0].groups()) | |
if matchgroups: | |
lastColor = matchgroups[0] | |
startTag,endTag = htmlColorMap[lastColor] | |
if row.endswith('\n'): | |
row = row[:-1] | |
g.write(f'{startTag}{row}{endTag}\n'.encode('utf-8')) # on rows that do not match the regex, reuse previous line color | |
g.write('</pre>'.encode('utf-8')) | |
g.write(footer) | |
if __name__ == '__main__': | |
logcat_file_to_colored_stdout(sys.argv[1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment