Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Parse Unity3d error and warning log from file.
# -*- coding: utf-8 -*-
import sys
import re
import csv
import StringIO
WARNINGS = dict(
_never_used_=lambda x : x.get('message').find(' never used') != -1,
_obsolete_=lambda x : x.get('message').find('is obso') != -1,
_unreachable_=lambda x : x.get('message').find('Unreachable code ') != -1,
_invalid_inherit_member_variable_=lambda x : x.get('message').find('Use the new keyword if hiding was intended') != -1,
_invalid_overriding_=lambda x : x.get('message').find('add the override keyword') != -1,
_never_assigned_=lambda x : x.get('message').find('never assigned to') != -1,
)
BY_MESSAGE = dict(_etc_=list(), )
re_warning = re.compile('(?P<source>.*)\((?P<line_number>[\d][\d]*),(?P<column>[\d][\d]*)\): warning (?P<code>CS[\d][\d]*): (?P<message>.*)')
_file = file(sys.argv[1])
_line_count = sum(1 for line in _file)
_file.seek(0, 0, )
print '############################################################'
print 'parsing and fetching `warning` messges from Unity build log.'
print '############################################################'
print
print 'trying to parse %d number of lines.' % _line_count,
print ' ',
_n = -1
_percent_previous = 0
for i in _file :
_n += 1
_percent = int((float(_n) / float(_line_count)) * 100)
if _percent_previous != _percent and _percent % 5 == 0 :
_percent_previous = _percent
sys.stdout.write('> %s%% ' % (_percent, ), )
sys.stdout.flush()
if len(i) > 500 :
continue
_m = re_warning.search(i)
if not _m :
continue
_o = _m.groupdict()
_o['line_number'] = int(_o['line_number'], )
_o['column'] = int(_o['column'], )
_key = '_etc_'
for k, v in WARNINGS.items() :
if v(_o, ) :
_key = k
BY_MESSAGE.setdefault(_key, list(), )
BY_MESSAGE[_key].append(_o, )
sys.stdout.write('> 100%: done.', )
sys.stdout.flush()
print
_header = ('source', 'line_number', 'column', 'code', 'message', )
_output = list()
for k, v in BY_MESSAGE.items() :
for i in v :
_output.append(map(lambda x : i.get(x), _header, ), )
_output.sort()
_output_file = StringIO.StringIO()
_csv_writer = csv.writer(_output_file, dialect='excel', )
_csv_writer.writerow(_header, )
_prev = None
for i in _output :
if _prev == i :
continue
_csv_writer.writerow(i, )
_prev = i
print _output_file.getvalue()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment