Kill StringIO.

This commit is contained in:
Jake Wharton
2013-06-12 08:18:53 -07:00
parent ba9c5383a8
commit e1d788e2ff
+30 -36
View File
@@ -22,7 +22,6 @@ limitations under the License.
# Package name restriction by Jake Wharton, http://jakewharton.com # Package name restriction by Jake Wharton, http://jakewharton.com
import os, sys, re, fcntl, termios, struct import os, sys, re, fcntl, termios, struct
from StringIO import StringIO
if len(sys.argv) != 2: if len(sys.argv) != 2:
print 'Target package name required.' print 'Target package name required.'
@@ -54,15 +53,16 @@ def colorize(message, fg=None, bg=None):
def indent_wrap(message): def indent_wrap(message):
wrap_area = WIDTH - HEADER_SIZE wrap_area = WIDTH - HEADER_SIZE
messagebuf = StringIO() messagebuf = ''
current = 0 current = 0
while current < len(message): while current < len(message):
next = min(current + wrap_area, len(message)) next = min(current + wrap_area, len(message))
messagebuf.write(message[current:next]) messagebuf += message[current:next]
if next < len(message): if next < len(message):
messagebuf.write('\n%s' % (' ' * HEADER_SIZE)) messagebuf += '\n'
messagebuf += ' ' * HEADER_SIZE
current = next current = next
return messagebuf.getvalue() return messagebuf
LAST_USED = [RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN] LAST_USED = [RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN]
@@ -101,7 +101,8 @@ TAGTYPES = {
PID_START = re.compile(r'^Start proc ([a-zA-Z0-9._]+) for ([a-z]+ [^:]+): pid=(\d+) uid=(\d+) gids=(.*)\r?$') PID_START = re.compile(r'^Start proc ([a-zA-Z0-9._]+) for ([a-z]+ [^:]+): pid=(\d+) uid=(\d+) gids=(.*)\r?$')
PID_KILL = re.compile(r'^Killing (\d+):([a-zA-Z0-9._]+)/[^:]+: (.*)\r?$') PID_KILL = re.compile(r'^Killing (\d+):([a-zA-Z0-9._]+)/[^:]+: (.*)\r?$')
PID_DEATH = re.compile(r'^No longer want ([a-zA-Z0-9._]+) \(pid (\d+)\): .*\r?$') PID_LEAVE = re.compile(r'^No longer want ([a-zA-Z0-9._]+) \(pid (\d+)\): .*\r?$')
PID_DEATH = re.compile(r'^\rProcess ([a-zA-Z0-9._]+) \(pid (\d+)\) has died.?$')
LOG_LINE = re.compile(r'^([A-Z])/([^\(]+)\( *(\d+)\): (.*)\r?$') LOG_LINE = re.compile(r'^([A-Z])/([^\(]+)\( *(\d+)\): (.*)\r?$')
input = os.popen('adb logcat') input = os.popen('adb logcat')
@@ -124,15 +125,13 @@ while True:
if line_package == package: if line_package == package:
pid = line_pid pid = line_pid
linebuf = StringIO() linebuf = '\n\n\n'
linebuf.write('\n' * 3) linebuf += colorize(' ' * (HEADER_SIZE - 1), bg=WHITE)
linebuf.write(colorize(' ' * (HEADER_SIZE - 1), bg=WHITE)) linebuf += indent_wrap(' Process created for %s\n' % target)
linebuf.write(indent_wrap(' Process created for %s\n' % target)) linebuf += colorize(' ' * (HEADER_SIZE - 1), bg=WHITE)
linebuf.write(colorize(' ' * (HEADER_SIZE - 1), bg=WHITE)) linebuf += ' PID: %s UID: %s GIDs: %s' % (line_pid, line_uid, line_gids)
linebuf.write(' PID: %s UID: %s GIDs: %s' % (line_pid, line_uid, line_gids)) linebuf += '\n'
linebuf.write('\n') print linebuf
print linebuf.getvalue()
kill = PID_KILL.match(message) kill = PID_KILL.match(message)
if kill is not None: if kill is not None:
@@ -140,13 +139,11 @@ while True:
if 'ActivityManager' == tag and pid == line_pid and package == line_package: if 'ActivityManager' == tag and pid == line_pid and package == line_package:
pid = None pid = None
linebuf = StringIO() linebuf = '\n'
linebuf.write('\n') linebuf += colorize(' ' * (HEADER_SIZE - 1), bg=RED)
linebuf.write(colorize(' ' * (HEADER_SIZE - 1), bg=RED)) linebuf += ' Process killed because %s' % reason
linebuf.write(' Process killed because %s' % reason) linebuf += '\n\n\n'
linebuf.write('\n' * 3) print linebuf
print linebuf.getvalue()
death = PID_DEATH.match(message) death = PID_DEATH.match(message)
if death is not None: if death is not None:
@@ -154,38 +151,35 @@ while True:
if 'ActivityManager' == tag and pid == line_pid and package == line_package: if 'ActivityManager' == tag and pid == line_pid and package == line_package:
pid = None pid = None
linebuf = StringIO() linebuf = '\n'
linebuf.write('\n') linebuf += colorize(' ' * (HEADER_SIZE - 1), bg=RED)
linebuf.write(colorize(' ' * (HEADER_SIZE - 1), bg=RED)) linebuf += ' Process killed because no longer wanted\n\n\n'
linebuf.write(' Process killed because no longer wanted') print linebuf
linebuf.write('\n' * 3)
print linebuf.getvalue()
if pid is None or owner != pid: if pid is None or owner != pid:
continue continue
linebuf = StringIO() linebuf = ''
# right-align tag title and allocate color if needed # right-align tag title and allocate color if needed
tag = tag.strip() tag = tag.strip()
color = allocate_color(tag) color = allocate_color(tag)
tag = tag[-TAG_WIDTH:].rjust(TAG_WIDTH) tag = tag[-TAG_WIDTH:].rjust(TAG_WIDTH)
linebuf.write(colorize(tag, fg=color)) linebuf += colorize(tag, fg=color)
linebuf.write(' ') linebuf += ' '
# write out level colored edge # write out level colored edge
if level not in TAGTYPES: break if level not in TAGTYPES: break
linebuf.write(TAGTYPES[level]) linebuf += TAGTYPES[level]
linebuf.write(' ') linebuf += ' '
# format tag message using rules # format tag message using rules
for matcher in RULES: for matcher in RULES:
replace = RULES[matcher] replace = RULES[matcher]
message = matcher.sub(replace, message) message = matcher.sub(replace, message)
linebuf.write(indent_wrap(message)) linebuf += indent_wrap(message)
line = linebuf.getvalue() line = linebuf
print line print line
if len(line) == 0: break if len(line) == 0: break