codekingpro/portable-devtools
114k
1import io2import sys3from threading import RLock4from time import sleep, time5 6# The maximum length of a log message in bytes, including the level marker and7# tag, is defined as LOGGER_ENTRY_MAX_PAYLOAD at8# https://cs.android.com/android/platform/superproject/+/android-14.0.0_r1:system/logging/liblog/include/log/log.h;l=71.9# Messages longer than this will be truncated by logcat. This limit has already10# been reduced at least once in the history of Android (from 4076 to 4068 between11# API level 23 and 26), so leave some headroom.12MAX_BYTES_PER_WRITE = 400013 14# UTF-8 uses a maximum of 4 bytes per character, so limiting text writes to this15# size ensures that we can always avoid exceeding MAX_BYTES_PER_WRITE.16# However, if the actual number of bytes per character is smaller than that,17# then we may still join multiple consecutive text writes into binary18# writes containing a larger number of characters.19MAX_CHARS_PER_WRITE = MAX_BYTES_PER_WRITE // 420 21 22# When embedded in an app on current versions of Android, there's no easy way to23# monitor the C-level stdout and stderr. The testbed comes with a .c file to24# redirect them to the system log using a pipe, but that wouldn't be convenient25# or appropriate for all apps. So we redirect at the Python level instead.26def init_streams(android_log_write, stdout_prio, stderr_prio):27 if sys.executable:28 return # Not embedded in an app.29 30 global logcat31 logcat = Logcat(android_log_write)32 sys.stdout = TextLogStream(stdout_prio, "python.stdout", sys.stdout)33 sys.stderr = TextLogStream(stderr_prio, "python.stderr", sys.stderr)34 35 36class TextLogStream(io.TextIOWrapper):37 def __init__(self, prio, tag, original=None, **kwargs):38 # Respect the -u option.39 if original:40 kwargs.setdefault("write_through", original.write_through)41 fileno = original.fileno()42 else:43 fileno = None44 45 # The default is surrogateescape for stdout and backslashreplace for46 # stderr, but in the context of an Android log, readability is more47 # important than reversibility.48 kwargs.setdefault("encoding", "UTF-8")49 kwargs.setdefault("errors", "backslashreplace")50 51 super().__init__(BinaryLogStream(prio, tag, fileno), **kwargs)52 self._lock = RLock()53 self._pending_bytes = []54 self._pending_bytes_count = 055 56 def __repr__(self):57 return f"<TextLogStream {self.buffer.tag!r}>"58 59 def write(self, s):60 if not isinstance(s, str):61 raise TypeError(62 f"write() argument must be str, not {type(s).__name__}")63 64 # In case `s` is a str subclass that writes itself to stdout or stderr65 # when we call its methods, convert it to an actual str.66 s = str.__str__(s)67 68 # We want to emit one log message per line wherever possible, so split69 # the string into lines first. Note that "".splitlines() == [], so70 # nothing will be logged for an empty string.71 with self._lock:72 for line in s.splitlines(keepends=True):73 while line:74 chunk = line[:MAX_CHARS_PER_WRITE]75 line = line[MAX_CHARS_PER_WRITE:]76 self._write_chunk(chunk)77 78 return len(s)79 80 # The size and behavior of TextIOWrapper's buffer is not part of its public81 # API, so we handle buffering ourselves to avoid truncation.82 def _write_chunk(self, s):83 b = s.encode(self.encoding, self.errors)84 if self._pending_bytes_count + len(b) > MAX_BYTES_PER_WRITE:85 self.flush()86 87 self._pending_bytes.append(b)88 self._pending_bytes_count += len(b)89 if (90 self.write_through91 or b.endswith(b"\n")92 or self._pending_bytes_count > MAX_BYTES_PER_WRITE93 ):94 self.flush()95 96 def flush(self):97 with self._lock:98 self.buffer.write(b"".join(self._pending_bytes))99 self._pending_bytes.clear()100 self._pending_bytes_count = 0101 102 # Since this is a line-based logging system, line buffering cannot be turned103 # off, i.e. a newline always causes a flush.104 @property105 def line_buffering(self):106 return True107 108 109class BinaryLogStream(io.RawIOBase):110 def __init__(self, prio, tag, fileno=None):111 self.prio = prio112 self.tag = tag113 self._fileno = fileno114 115 def __repr__(self):116 return f"<BinaryLogStream {self.tag!r}>"117 118 def writable(self):119 return True120 121 def write(self, b):122 if type(b) is not bytes:123 try:124 b = bytes(memoryview(b))125 except TypeError:126 raise TypeError(127 f"write() argument must be bytes-like, not {type(b).__name__}"128 ) from None129 130 # Writing an empty string to the stream should have no effect.131 if b:132 logcat.write(self.prio, self.tag, b)133 return len(b)134 135 # This is needed by the test suite --timeout option, which uses faulthandler.136 def fileno(self):137 if self._fileno is None:138 raise io.UnsupportedOperation("fileno")139 return self._fileno140 141 142# When a large volume of data is written to logcat at once, e.g. when a test143# module fails in --verbose3 mode, there's a risk of overflowing logcat's own144# buffer and losing messages. We avoid this by imposing a rate limit using the145# token bucket algorithm, based on a conservative estimate of how fast `adb146# logcat` can consume data.147MAX_BYTES_PER_SECOND = 1024 * 1024148 149# The logcat buffer size of a device can be determined by running `logcat -g`.150# We set the token bucket size to half of the buffer size of our current minimum151# API level, because other things on the system will be producing messages as152# well.153BUCKET_SIZE = 128 * 1024154 155# https://cs.android.com/android/platform/superproject/+/android-14.0.0_r1:system/logging/liblog/include/log/log_read.h;l=39156PER_MESSAGE_OVERHEAD = 28157 158 159class Logcat:160 def __init__(self, android_log_write):161 self.android_log_write = android_log_write162 self._lock = RLock()163 self._bucket_level = 0164 self._prev_write_time = time()165 166 def write(self, prio, tag, message):167 # Encode null bytes using "modified UTF-8" to avoid them truncating the168 # message.169 message = message.replace(b"\x00", b"\xc0\x80")170 171 # On API level 30 and higher, Logcat will strip any number of leading172 # newlines. This is visible in all `logcat` modes, even --binary. Work173 # around this by adding a leading space, which shouldn't make any174 # difference to the log's usability.175 if message.startswith(b"\n"):176 message = b" " + message177 178 with self._lock:179 now = time()180 self._bucket_level += (181 (now - self._prev_write_time) * MAX_BYTES_PER_SECOND)182 183 # If the bucket level is still below zero, the clock must have gone184 # backwards, so reset it to zero and continue.185 self._bucket_level = max(0, min(self._bucket_level, BUCKET_SIZE))186 self._prev_write_time = now187 188 self._bucket_level -= PER_MESSAGE_OVERHEAD + len(tag) + len(message)189 if self._bucket_level < 0:190 sleep(-self._bucket_level / MAX_BYTES_PER_SECOND)191 192 self.android_log_write(prio, tag, message)193 