Skip to content

Instantly share code, notes, and snippets.

@N3X15
Created December 23, 2014 05:06
Show Gist options
  • Save N3X15/dec56e65516fda4b0acb to your computer and use it in GitHub Desktop.
Save N3X15/dec56e65516fda4b0acb to your computer and use it in GitHub Desktop.
SS13 Log Parsing Tool
import os, sys, re, argparse
REG_ADMIN_PM = re.compile('ADMIN: (HELP|PM)')
REG_ADMIN_CHAT = re.compile('ADMIN: MOD') # msay
argp = argparse.ArgumentParser()
argp.add_argument('filename', type=str, help="Filename to grep")
argp.add_argument('outfile', type=str, help="Report filename")
argp.add_argument('-s', '--start', type=int, default=0, nargs='?', help='Line number to start at')
argp.add_argument('-e', '--end', type=int, default=None, nargs='?', help='Line number to end at')
argp.add_argument('--include-msay', default=False, action='store_true', help='Include MSay/Asay.')
argp.add_argument('--exclude-ahelp', default=False, action='store_true', help='Exclude Adminhelps/PMs.')
argp.add_argument('--all-msay', default=False, action='store_true', help='Include ALL msay/asay activity, not just stuff that matches --search-for.')
argp.add_argument('--search-for', default=None, type=str, help='Only include lines containing this regular expression. (e.g. a ckey)')
args = argp.parse_args()
ln = 0
searchfor = None
if args.search_for is not None:
searchfor = re.compile(args.search_for, re.IGNORECASE)
with open(args.filename, 'r') as log:
with open(args.outfile, 'w') as out:
out.write('Report generated with logtool.py.\n')
out.write(' {}\n\n'.format(' '.join(sys.argv)))
for line in log:
ln += 1
if args.start > ln: continue
if args.end is not None and args.end <= ln: continue
if line == args.start: print('>>> Starting log processing at line {}.'.format(line))
if line == args.end: print('>>> Ending log processing at line {}.'.format(line))
line = line.strip()
if line == '': continue
include_line = False
force_include = False
if REG_ADMIN_PM.search(line) is not None and not args.exclude_ahelp:
include_line = True
elif REG_ADMIN_CHAT.search(line) is not None and args.include_msay:
include_line = True
force_include = args.all_msay
if searchfor is not None and not force_include:
if searchfor.search(line) is None:
continue
if include_line:
out.write('{}:{}:{}\n'.format(args.filename, ln, line))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment