Skip to content

Commit 633f848

Browse files
committed
Improve Python logging and format async code
Configures Python logging to use a StreamHandler with a custom formatter and replaces all root handlers. Also reformats async Isolate.run() calls for better readability.
1 parent 9327e4d commit 633f848

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

src/serious_python_android/lib/src/cpython.dart

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ if not getattr(sys, "__serious_python_logcat_configured__", False):
5252
pass
5353
5454
sys.stdout = sys.stderr = _LogcatWriter()
55+
handler = logging.StreamHandler(sys.stderr)
56+
handler.setFormatter(logging.Formatter("%(levelname)s %(message)s"))
57+
root = logging.getLogger()
58+
root.handlers[:] = [handler]
5559
''';
5660

5761
CPython getCPython(String dynamicLibPath) {
@@ -66,14 +70,15 @@ Future<String> runPythonProgramFFI(bool sync, String dynamicLibPath,
6670
"Python run#$runId start (sync=$sync, script=${script.isNotEmpty}, program=$pythonProgramPath)");
6771
if (sync) {
6872
// Sync run: do not involve ports (avoids GC/close races).
69-
final result = _runPythonProgram(dynamicLibPath, pythonProgramPath, script);
73+
final result =
74+
_runPythonProgram(dynamicLibPath, pythonProgramPath, script);
7075
spDebug("Python run#$runId done (resultLength=${result.length})");
7176
return result;
7277
} else {
7378
// Async run: use Isolate.run() to avoid manual port lifecycle issues.
7479
try {
75-
final result = await Isolate.run(() =>
76-
_runPythonProgram(dynamicLibPath, pythonProgramPath, script));
80+
final result = await Isolate.run(
81+
() => _runPythonProgram(dynamicLibPath, pythonProgramPath, script));
7782
spDebug("Python run#$runId done (resultLength=${result.length})");
7883
return result;
7984
} catch (e, st) {

0 commit comments

Comments
 (0)