I need to log to OSLog
and into a file in parallel (due to OSLogStore
not being able to provide old logs).
In Objective-C I can accomplish this with a macro using __FILE__
and __LINE__
in the macro implementation which still reference the position in the original code.
I tried to create a Swift macro to make this work from Swift. However, log()
takes the file and line number of the macro definition file instead of the position in the calling code. So when I click on the metadata link, I’m taken to the macro instead of the calling location.
Is there any solution to that? #file
and #line
are correctly set in the macro but there’s no way to specify file and line number to the log()
function.
2
Answers
I had a similar problem, solved it by dumping
OSLogStore
contents to file once a while.Unfortunately there’s no way for you to do this*. Apple in their infinitive wisdom didn’t think anyone would ever do such a thing.
The recommendations from the Apple forums by their resident inhouse Eskimo is that you write a Swift Macro to wrap it, basically writing two lines of logging for every log statement.
* Strictly speaking not true. You could wrap the C APIs yourself, but that’s generally not recommended