1#!/usr/bin/env python3 2# 3# Copyright (C) 2020 The Android Open Source Project 4# 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16# 17 18# A tool that can take two logcat files from an Android device 19# and produce a compact rendering of the delta between them 20# Namely, it strips most of the information from the output 21# (timestamp, process id, ...) that is often not critical in 22# reconstructing the state of the device operation 23# and then compares the component and message for each line 24 25# It requires 'diff' and 'colordiff' to be installed 26 27import argparse 28import os.path 29import re 30import subprocess 31import sys 32import tempfile 33 34RE_TEXT = '^[\d]+-[\d]+ [\d]+:[\d]+:[\d]+.[\d]+[ ]+[\d]+[ ]+[\d]+ [DIEWF] (?P<component>.+): (?P<message>.*)$' 35 36# TODO(egranata): should we also support a by-component mode? 37class Logcat(object): 38 def __init__(self, fp, keep_mismatches=False): 39 self.filename = fp 40 self.mismatches = 0 41 self.messages = [] 42 self.rgx = re.compile(RE_TEXT, re.MULTILINE) 43 with open(fp, 'r') as f: 44 for line in f.readlines(): 45 match = self.rgx.match(line) 46 if match: 47 component = match.group('component') 48 message = match.group('message') 49 self.messages.append("%s: %s" % (component, message)) 50 elif keep_mismatches: 51 self.messages.append(line) 52 else: 53 self.mismatches += 1 54 def save(self): 55 # Keep this file around so we can use it by name elsewhere 56 with tempfile.NamedTemporaryFile(mode='w', 57 delete=False, prefix=os.path.basename(self.filename)) as f: 58 for msg in self.messages: 59 f.write("%s\n" % msg) 60 return f 61 62def entry(): 63 parser = argparse.ArgumentParser(description='Compare two Android logs') 64 parser.add_argument('files', metavar='<file>', type=str, nargs=2, 65 help='The two files to compare') 66 parser.add_argument('--keep-misformatted-lines', '-k', action='store_true', default=False) 67 args = parser.parse_args() 68 69 logcat1 = Logcat(args.files[0], 70 keep_mismatches=args.keep_misformatted_lines) 71 logcat2 = Logcat(args.files[1], 72 keep_mismatches=args.keep_misformatted_lines) 73 74 file1 = logcat1.save() 75 file2 = logcat2.save() 76 77 if logcat1.mismatches != 0 or logcat2.mismatches != 0: 78 print("%d lines were ignored; run with --keep-misformatted-lines to include them" \ 79 % (logcat1.mismatches + logcat2.mismatches)) 80 81 out = subprocess.getoutput('diff --minimal -u -W 200 "%s" "%s" | colordiff' % ( \ 82 file1.name, file2.name)) 83 print(out) 84 85 os.remove(file1.name) 86 os.remove(file2.name) 87 88if __name__ == '__main__': 89 entry() 90