os_log into a App's local file

Hi Team,


I went through WWDC 2018 video on 405_measuring_performance_using_logging and would like to replace existing logging mechanism with os_log to gain its benifits with signposts to measure performance.


But there is main blocker as listed below:

1. Current logging mechanism provides easy way to share the App logs by click of button,without need of external systems like Mac/Windows/iTune and its specific to our app resulting in minimum file size in KBs.


How could we achieve this by migrating to os_log, as it logs into a common bulky sysdiagnose file?

1.Could we wrap os_log to log to a file in App's documents folder?

Simple sample code will be helpful, as I could not find more info on wrapping os_log.


2. Could I programatically access sysdiagnose with filters of "my catogery" and export into App's local folder to share as before?


Thanks a ton in Advance!

Answered by DTS Engineer in 315922022

1. Could we wrap os_log to log to a file in App's documents folder?

Wrapping

os_log
is not recommended. Specifically, it’s a bad idea to wrap
os_log
with a function because you undermine a lot of its smarts with regards efficiently storing constant strings. In C-based languages you can get around this by wrapping it with a macro, but that option is not available in Swift.

2. Could I programatically access sysdiagnose with filters of "my catogery" and export into App's local folder to share as before?

There’s no way to do this currently, but it would make a fine enhancement request.

FYI, and I only just learnt this myself, you don’t need to use iTunes to grab a sysdiagnose log. You can export it via Settings > Privacy > Analytics > Analytics Data. That doesn’t help with the file size, but it does smooth over one obstacle.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"
Accepted Answer

1. Could we wrap os_log to log to a file in App's documents folder?

Wrapping

os_log
is not recommended. Specifically, it’s a bad idea to wrap
os_log
with a function because you undermine a lot of its smarts with regards efficiently storing constant strings. In C-based languages you can get around this by wrapping it with a macro, but that option is not available in Swift.

2. Could I programatically access sysdiagnose with filters of "my catogery" and export into App's local folder to share as before?

There’s no way to do this currently, but it would make a fine enhancement request.

FYI, and I only just learnt this myself, you don’t need to use iTunes to grab a sysdiagnose log. You can export it via Settings > Privacy > Analytics > Analytics Data. That doesn’t help with the file size, but it does smooth over one obstacle.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Thank you Eskimo.

Could you please let me know more details on "In C-based languages you can get around this by wrapping it with a macro, but that option is not available in Swift."

Basic need is to use os_log to log to a local file instead of common file. Do we have macro that we could modify to direct it to log into local folder?


export it via Settings > Privacy > Analytics > Analytics Data solves the need to connect to iTunes but the file is so huge that it cannot be sent via mail and our customers are mobile only users with no access to Mac/Windows to download and send.


Thanks again!

Swift does not support macros, so you can’t use that technique in Swift code. The alternatively is to write your own function that appends the data to a log file you control. There are serious negatives to do this, however. Specifically,

os_log
was designed to deal with a high volume of logging while minimising the impact of those writes on the flash storage.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

I was tempted to use os_log in future developments but it's still a pain to retrieve logs remotely. We actually have this framework on MacOS but it requires admin permission : https://docs.developer.st.apple.com/documentation/oslog


Fetching logs limited to our app process should be possible both on MacOS (without admin rights) and iOS. Is there any enhancement scheduled on the topic ? There's also https://github.com/apple/swift-log but it seems to be in its development phase and intended to provide a simple API facade.


Thanks in advance !

Swift does not support macros, so you can’t use that technique in Swift code.

Is it possible now since macro was introduced in Swift 5.9

os_log into a App's local file
 
 
Q