Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # Quick example of a binary logcat reader which fails when it encounters one of
- # the unexpected extra bytes occouring in some logcat entries.
- #
- # Run as:
- # $ adb logcat -d -B | ./readlog.py -
- #
- # - Hal
- from struct import unpack
- from collections import namedtuple
- from sys import argv, stdin, stderr, stdout
- import re
- # Define a log entry
- LogEntry = namedtuple("LogEntry", "payloadlen pid tid time nanotime priority \
- tag message")
- # hackish soloution to finding the two null terminated strings in our payload
- payloadpattern = re.compile("^(.*)\x00(.*)\x00$")
- def readEntry(logstream):
- rawheader = logstream.read(20)
- if len(rawheader) < 20: # check for EOF
- return None
- # Unpack the header into a 2 byte length and 4 4 byte ints (unsigned)
- header = unpack("HxxIIII", rawheader)
- payload = logstream.read(header[0]) # first element of header is msg len
- if len(payload) < header[0]:
- raise Exception("Insufficient bytes read for payload. Expected: "\
- + "{0}, got: {1}".format(1, 2))
- priority, = unpack("B", payload[0])
- # Find the two null terminated strings with our regex
- match = payloadpattern.match(payload[1:])
- if not match:
- raise Exception("Invalid payload")
- tag, message = match.groups()
- return LogEntry(header[0], header[1], header[2], header[3], header[4], \
- priority, tag, message)
- if __name__ == "__main__":
- if len(argv) != 2:
- print >> stderr, "Usage: {0} binarylogfile\n(or '-' to read stdin)"\
- .format(argv[0])
- exit(1)
- if argv[1] == "-":
- file = stdin
- else:
- file = open(argv[1])
- # Read all the entries
- while True:
- entry = readEntry(file)
- if entry:
- print entry
- else:
- break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement