#/usr/bin/env python import math import os import sys import re map_regex = re.compile (r'^(\d+\.\d+) cache_mapped: 0x([0-9a-f]+) (.+)$') map_timestamp_group = 1 map_address_group = 2 map_filename_group = 3 icon_regex = re.compile (r"^(\d+\.\d+) get_icon: cache:0x([0-9a-f]+) directory:'(.+)' icon_name:'(.+)' offset:(\d+) length:(\d+)") icon_timestamp_group = 1 icon_cache_id_group = 2 icon_directory_group = 3 icon_name_group = 4 icon_offset_group = 5 icon_length_group = 6 class MapEvent: def __init__ (self, timestamp, address, filename): self.timestamp = timestamp self.address = address self.filename = filename class IconEvent: def __init__ (self, timestamp, cache_name, name, offset, length, app_name): self.timestamp = timestamp self.cache_name = cache_name self.name = name self.offset = offset self.length = length self.app_name = app_name class IconLog: def __init__ (self): self.icons = [] def add_file (self, filename): r = re.compile (r"^[0-9]+-(.+)\.icon-log") m = r.search (filename) if m: app_name = m.group (1) else: app_name = "" file = open (filename, "r") maps = {} for str in file: self.parse_line (maps, app_name, str) file.close () def try_parse_map_event (self, str): m = map_regex.search (str) if m: timestamp = float (m.group (map_timestamp_group)) address = m.group (map_address_group) filename = m.group (map_filename_group) return MapEvent (timestamp, address, filename) else: return None def try_parse_icon_event (self, maps, app_name, str): m = icon_regex.search (str) if m: timestamp = float (m.group (icon_timestamp_group)) cache_id = m.group (icon_cache_id_group) directory = m.group (icon_directory_group) name = directory + "/" + m.group (icon_name_group) offset = int (m.group (icon_offset_group)) length = int (m.group (icon_length_group)) map = maps[cache_id] return IconEvent (timestamp, map.filename, name, offset, length, app_name) def parse_line (self, maps, app_name, str): event = self.try_parse_map_event (str) if event: self.add_map (maps, event) return event = self.try_parse_icon_event (maps, app_name, str) if event: self.add_icon (event) return def add_map (self, maps, map_event): maps[map_event.address] = map_event def add_icon (self, icon_event): self.icons.append (icon_event) pass def sort_icons (self): def compare_icons (a, b): if a.timestamp < b.timestamp: return -1 elif a.timestamp > b.timestamp: return 1 else: return 0 self.icons.sort (cmp = compare_icons) if __name__ == '__main__': if len (sys.argv) < 2: print "usage: %s foo.icon-log bar.icon-log ..." % sys.argv[0] sys.exit (1) icon_log = IconLog () for filename in sys.argv[1:]: icon_log.add_file (filename) icon_log.sort_icons () caches = {} for icon in icon_log.icons: if caches.has_key (icon.cache_name): icons = caches[icon.cache_name] else: icons = [] caches[icon.cache_name] = icons icons.append (icon) for (cache_name, icons) in caches.items(): print cache_name for i in icons: print " %.6f %s\t%s\t%s\t%s" % (i.timestamp, i.name, i.offset, i.length, i.app_name) print