OSLogStore should be more flexible

During iOS and Mac development I sometimes encounter issues that cannot be reproduced on demand. Occasionally I’m unable to reproduce them at all, and must rely on a beta tester or customer to collect information to help me diagnose the problem. During these times it’s vital to be able to collect logs from a device that’s not connected to a debugger.

sysdiagnose is not a reasonable solution to this problem. I don’t need or want hundreds of megabytes of data when a few hundred kilobytes will suffice. The process of triggering a sysdiagnose, waiting for it to complete, and figuring out how to share it is arduous.

Previously I used NSLog for all of my logging, and freopen([logPath fileSystemRepresentation],"a+",stderr) to write them to disk. It worked well enough, but I’d like to take advantage of all the improvements offered by more modern logging options. I’ve been working on switching over to os_log for all of my logging, and using OSLogStore to collect logs and write them to disk. This has been a challenging process.

I’d prefer to simply let os_log work normally, and have the user press a button in the app to collect logs and write them to a file when it’s appropriate. Unfortunately, OSLogStore only has access to logs created by the current process. So this approach is only possible if the problem occurred in the main app, rather than an extension. Even then, it only works if the app has continued running since the problem occurred. It’s often helpful to know what was happening leading up to a crash, or an app being terminated by the watchdog.

Due to these limitations, the only solution I’ve found is to have OSLogStore periodically call getEntries(with:at:matching:) and write the logs to a file. I have a setting in my apps to turn this on and off, but it’s doing still far more work than should be necessary—it’s a waste of battery and SSD writes. The speed of this process is also an issue in extensions which have a limited amount of time to work. This is also a concern when the main app is running in the background.

In short: OSLogStore should be able to access all log messages created by an app and its extensions. This feedback has been filed with Apple under FB12021495

Additional reading

Posted February 28th, 2023 by Mike Piontek
Link to this entry: https://mikepiontek.com/go/9164

More recent posts