Objective-C(++) equivalent to Swift's fatalError()

I'm using Objective-C++ for a project. I realize Objective-C++ isn't used that much, but this is for a cross-platform game framework with a common C++ code base, which I think is a valid use case (there are some popular game frameworks that use Objective-C++ for this purpose).


A useful aspect of Swift's fatalError() is that you can submit a formatted string as a message, rather than just a string literal, and this message will appear in the crash log. For example, this:


let x = 1
let y = 2
fatalError("x = \(x), y = \(y)")


Produces this in the crash log:


Fatal error: x = 1, y = 2


assert() is of course available in Objective-C++, but it only allows for string literals as messages, not formatted strings. Both Objective-C and C++ exceptions allow for conveying a formatted message to the crash log, but I'm trying to avoid exceptions (among other things, it might be problematic to propagate exceptions through some of the libraries I'm using). There's also NSAssert() and NSCAssert(), but they use exceptions under the hood.


I could use Swift's fatalError() via a wrapper imported into Objective-C, but just as a matter of cleanliness and convenience I'm wondering if there are any Objective-C(++) options I've overlooked. The goal is to convey a formatted error message to the crash log without using exceptions, which fatalError() allows for, but which there doesn't seem to be an obvious way to do in Objective-C(++). Is there another way to do this that I'm overlooking?

NSLog?

Thanks for the suggestion. I'm trying to get a message into the crash log though, and I don't know of a way to do that with NSLog().

It's an interesting question. Logging a message before crashing is the usual way of leaving footprints in the log, but getting a custom message into a crash report is a bit unusual.


There might be a C (or POSIX) library function for aborting with a message, but I don't know offhand. Wrapping Swift's fatalError() in Obj-C might not be a bad idea.


Oddly, the best place to ask this question might be on the Swift forums (forums.swift.org). It seems likely that someone over there looked into this when building error handling in Swift, and might know the answer for macOS too.

Thanks for your reply.


Perhaps getting a message into the crash log is unusual - I don't know. fatalError() does it, and so do Objective-C and C++ exceptions. Android has a way to do it as well. It's only doing it in Objective-C without using exceptions that seems difficult.


I haven't had an opportunity to deal with crash reports from users yet, so I may have the wrong idea about how crash reporting works. I was under the impression that if the user opts in, crash logs will be shared with the developer. I wasn't under the impression that logged messages would be available in that way, which is why I was interested in including as much information as possible in the crash log itself.


Although there may not be that many cases where a custom crash log message would be useful, the particular case I have in mind is fatal errors from Duktape (an embeddable JavaScript engine). It seems like it would be useful to have any error messages generated by Duktape available in the crash log should a fatal error occur, hence my interest in custom messages.


If I can't find a solution here, I'll check out the Swift forum you mentioned.


Thanks again.

A useful aspect of Swift's

fatalError()
is that you can submit a formatted string as a message, rather than just a string literal, and this message will appear in the crash log.

Did you actually test this? On iOS? In my experience the iOS crash reporter will not include custom errors messages, but I’d be happy to be proven wrong (-:

Share and Enjoy

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

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

Yes, you're right. I was looking at a simulator crash log, where such messages are included, but I see they're not included in device crash logs.


So I guess this may be a stupid question, but is there a simple way to convey application-specific information about a crash to the developer?


As I mentioned earlier, a particular case I have in mind is fatal errors generated by an embeddable JavaScript engine. When this engine triggers a fatal error, it provides a string with information about what caused the error. Obviously this information would be useful to have when trying to diagnose such crashes, but I'm not sure if/how that can easily be accomplished.


As QuinceyMorris mentioned, it's common just to log such information, but as far as I know, the log output wouldn't be available to the developer (this is all with respect to published apps installed on end-user devices).


Obviously there are more heavyweight solutions, such as third-party crash reporting and analysis frameworks, but for now I'm just wondering if there's something simple and basic that I'm overlooking.


Thanks for your help.

Sorry. I misunderstood entirely.


I would suggest a lightweight crash reporting solution. Anything Apple does is out of your hands anyway. If you know you are going to crash, but you are going down in a relatively controlled re-entry, you can write your content into a text file somewhere convenient to your app with an easily-identifiable filename.


Then, when you get re-launched, look for that file. If you see it, ask the user if they would submit it to your crash reporting service. Then delete the file.

>When this engine triggers a fatal error, it provides a string with information about what caused the error.


How about - before the engine is started send a message to your server "Starting the engine with ID 125261826 with the following characteristics...." and then after the engine is successful send a message "the engine with ID 125261826 succeeded". Then on your end, ignore any messages that come with a corresponding succeeded ID.

Accepted Answer

So I guess this may be a stupid question, but is there a simple way to convey application-specific information about a crash to the developer?

I’ve yet to find a way that is both reasonable and supported. This isn’t accidental. I don’t fully understand the rationale here — I suspect it’s privacy related — but the iOS crash reporter specific prevents apps from exporting arbitrary strings.

One options that’s might work for you is to special case the most common errors coming out of your JavaScript engine and then, for the common ones, call a non-inlineable function whose name represents that failure. Alas this only tells you the error type, and you probably also want to know the line of code that failed.

As QuinceyMorris mentioned, it's common just to log such information, but as far as I know, the log output wouldn't be available to the developer …

Correct. If you’re in touch with a specific user you can ask them to send you a sysdiagnose log, but that’s not the same as getting this info for every failure.

You could log such errors to your own file and then report them (with the user’s consent, obviously) the next time the user runs your app.

Share and Enjoy

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

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

Yes, having thought about it, it makes sense that the contents of the crash log would be controlled for privacy reasons. In any case, it seems clear that some sort of custom or third-party solution will probably be required for this.


Thanks to everyone for the help.

Objective-C(++) equivalent to Swift's fatalError()
 
 
Q