Repository navigation
Write Python stdout/stderr to the console when BRIEFCASE_CONSOLE_OUTPUT=1 - #94
Conversation
…UT=1. When Briefcase launches an app on a physical iOS device, it streams the process console with `devicectl --console`. Python's stdout and stderr go to the Apple system log, so print() output never reached that console. If BRIEFCASE_CONSOLE_OUTPUT=1 is set in the environment, the launcher now leaves config.use_system_logger disabled on Python 3.14+, and on Python 3.13 and earlier restores sys.stdout/sys.stderr to the original streams on file descriptors 1 and 2 after initialization. Without the variable, behavior is unchanged. Assisted-by: Claude Code (claude-opus-5-5)
|
This looks good; however, as noted on my review on beeware/briefcase#3114, I think there’s an opportunity for a bigger cleanup here. The console logger is a workaround that we had to introduce because Xcode changed the visibility of stdout for iOS apps; if there’s a way to launch the app such that the workaround isn’t needed, then we can just disable stdout redirection, rather than making it an option. |
freakboy3742
left a comment
There was a problem hiding this comment.
After some more investigation - we will need to retain the ability to write to the system log by default, because if a user in the field needs to forward us their logs, they will only have the system log, not stdout/err.
However, the ability to view stdout/err directly massively improves the the user experience for the simulator, and means we can see logs for physical devices - so I think the PR is good to land!
Adds a "console output" mode to the iOS launcher (
main.m) so Briefcase can capture Python output when it runs an app on a physical device.When Briefcase runs an app on a physical device (beeware/briefcase#860), it launches it with
xcrun devicectl device process launch --consoleand reads the process's console to detect test results and errors. Python'ssys.stdoutandsys.stderrare written to the Apple system log, though, soprint()output never shows up there; only the launcher'sNSLoglines do.The contract
If the environment variable
BRIEFCASE_CONSOLE_OUTPUTis set to1when the app launches, Python'ssys.stdoutandsys.stderrwrite to the process's real stdout and stderr (file descriptors 1 and 2) instead of the system log. Briefcase passes this in the--environment-variablesargument todevicectl. Any other value, or no value, keeps the current behavior, so simulator logging and normal launches are unaffected. In console mode the launcher logs one extra line:Python stdout and stderr will be written to the console.All existingNSLogmessages stay as they are.Implementation
config.use_system_loggeris now set to!console_outputinstead of always1._apple_support.init_streams()always replacessys.stdoutandsys.stderrwithSystemLogwrappers during startup (all three support packages ship_apple_support.py). In console mode, the launcher runs a shortPyRun_SimpleStringafterPy_InitializeFromConfigthat rebindssys.stdoutandsys.stderrtosys.__stdout__andsys.__stderr__. Those are the original UTF-8 streams on fds 1 and 2, unbuffered because ofconfig.buffered_stdio = 0. If that call fails, the crash dialog is shown and the app exits.Verification
I generated a probe app from this branch with Briefcase 0.4.7 for Python 3.13 (support b18) and 3.14, built it for the iPhone 16 simulator, and launched it with
xcrun simctl launch --console-pty. The probe printsrepr(sys.stdout)to stdout andrepr(sys.stderr)to stderr, then raisesSystemExit(0).BRIEFCASE_CONSOLE_OUTPUTlog show)NSLoglines only<SystemLog (level 0)>/<SystemLog (level 16)>1<_io.TextIOWrapper name='<stdout>' ... encoding='utf-8'>and<stderr>NSLoglines only<SystemLog (level 0)>/<SystemLog (level 16)>1<_io.TextIOWrapper name='<stdout>' ... encoding='utf-8'>and<stderr>On the simulator the variable was passed as
SIMCTL_CHILD_BRIEFCASE_CONSOLE_OUTPUT=1. I did not test Python 3.11, 3.12, or 3.15 directly: 3.11 and 3.12 use the same_apple_supportpath as 3.13, and 3.15 uses the sameuse_system_loggerpath as 3.14. I also haven't run this on a physical device yet; that will happen through the Briefcase side of #860.Refs beeware/briefcase#860
PR Checklist:
Assisted-by: Claude Code (Claude Opus 5.5)