Skip to content

Conversation

loinsir
Copy link

@loinsir loinsir commented Sep 30, 2025

Summary

Add Swift print() and debugPrint() output capture to FLEX System Log viewer. Swift print statements now appear in System Log with [SwiftPrint]
prefix alongside NSLog and os_log messages.

Changes

Core Implementation

  • FLEXSwiftPrintRedirector: New Objective-C class that redirects stdout/stderr to capture Swift print output
    • Uses NSPipe to intercept stdout/stderr file descriptors
    • Implements line buffering to handle partial pipe reads correctly
    • Callback-based architecture for direct FLEX integration without going through NSLog/os_log
    • Preserves original console output while capturing for FLEX

Integration

  • System Log view automatically enables Swift print redirection on load
  • Captured messages are directly delivered to FLEX via message handler callback
  • All Swift print output appears with [SwiftPrint] prefix for easy identification

Example App

  • Added Swift print test statements in example app timer for demonstration

Technical Details

Why not use NSLog?

  • Initially attempted to redirect Swift print to NSLog/os_log, but this caused <compose failure [corrupt log]> errors in the os_log system
  • Direct message delivery via callbacks bypasses os_log entirely, avoiding corruption issues

Line Buffering

  • NSPipe may deliver partial data chunks that split lines
  • Implemented buffering to accumulate partial lines until newline is found
  • Ensures complete log messages are displayed without truncation

Test Plan

  1. Run FLEXample app
  2. Open FLEX System Log viewer
  3. Observe Swift print statements appearing with [SwiftPrint] prefix
  4. Verify console output still works normally

@loinsir loinsir force-pushed the master branch 4 times, most recently from 901a844 to 6871cdd Compare September 30, 2025 09:32
- Implement stdout/stderr pipe redirection in Objective-C
- Capture Swift print() and debugPrint() output
- Use callback-based message handler for direct FLEX integration
- Add line buffering to handle partial pipe reads correctly
- Set message handler to receive captured Swift print output
- Enable redirection when System Log view loads
- Swift print statements now appear with [SwiftPrint] prefix
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant