From bc3ccac030420c6fc7d6d145dc0db14fdb154782 Mon Sep 17 00:00:00 2001 From: acidvegas Date: Tue, 30 May 2023 03:10:29 -0400 Subject: [PATCH] Added the ability to search for strings with hilighting in the results using ANSI colors --- parser.py | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/parser.py b/parser.py index 508bf66..af2e5e9 100644 --- a/parser.py +++ b/parser.py @@ -5,12 +5,12 @@ import json import os import sys -def parse(data, raw=True): +def parse(option, data, raw=True): if not raw: data = ' '.join(line.split()[3:]) if data[:1] == ':': data = data[1:] - print(data) + print(data.replace(option, f'\033[31m{option}\033[0m')) return data # Main @@ -24,7 +24,11 @@ if len(sys.argv) >= 2: found = list() for log in logs: with open('logs/'+log) as logfile: - data = json.loads(logfile.read()) + try: + data = json.loads(logfile.read()) + except: + print('error: failed to load ' + log) + break if option in data: data = data[option] if type(data) == str: @@ -32,9 +36,18 @@ if len(sys.argv) >= 2: elif type(data) == list: for item in data: found.append(parse(item, raw)) + else: + for item in data: + _data = data[item] + if type(_data) == str and option in _data: + found.append(parse(option, item, raw)) + elif type(_data) == list: + for _item in _data: + if option in _item: + found.append(parse(option, _item, raw)) if found: print(f'\nfound {len(found)} results in {len(logs)} logs') else: print('usage: python parser.py [clean]\n') - print(' may be any item in the snapshots (001, NOTICE, 464, etc)') + print(' may be any item in the snapshots (001, NOTICE, 464, etc) or a string to search') print(' [clean] may be optionally used to display a cleaner output') \ No newline at end of file