Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I save the fatalError message to the iOS crash log?

I have an iOS application written in Swift 2 in Xcode 8.2.1, that's built for iOS 10.2.

I've had a number of crash reports from TestFlight and despite symbolication, none of the crash logs show any program state besides the stack-traces (no argument values, no locals, no heap objects, etc).

...but inside those functions I can see code which is likely to fail (e.g. a forced unwrap) but the crash log isn't telling me where or why it's failing.

When debugging in Xcode, I can use fatalError(message: String) where I can put my own message like "functionFoo returned nil" or "variable bar == \"" + bar + "\"", except when deployed using TestFlight or the App Store the fatalError will be hit and the program terminates, but the message value is not saved to the crash log, making it pointless.

In other environments, like C#/.NET and Java I can simply throw new SomeExceptionType("my message") and all information is available in whatever global catch(Exception) handler I have.

How can I achieve the same goal in iOS / Swift?

like image 937
Dai Avatar asked Sep 07 '25 11:09

Dai


2 Answers

Maybe this can help you

import os

let osLogger = Logger(subsystem: yourBundleIdentifier, category: yourCategory)
osLogger.log(level: .debug, "\(yourCategoryName): \(message)")
like image 147
Ruben Nahatakyan Avatar answered Sep 09 '25 09:09

Ruben Nahatakyan


I can suggest two approaches:

  1. Use a third party SDK for tracking crashes, such as Crashlytics (Firebase) or Sentry. You can also send a custom error to these platforms. Also they provide great statistics for all crashes/errors inside your app.

  2. This one is more for a debug configuration (suitable for QA testing). Write a class for logging your event into a file inside of your documents directory. Create a simple ViewController that can open/export this file (DEBUG mode only). Cover all essential places in your code with logs and deliver the build to your QA, so they can attach a detailed steps for reproducing error.

like image 26
Daniil Avatar answered Sep 09 '25 09:09

Daniil