Jordan
Jordan

Reputation: 329

Mac OS Swift: Writing crash log to text file

I have a simple Mac OS menu bar app (top right). It performs a couple basic server calls and organizes data. For reasons too long to explain, I can't build/test the app on the computer directly so what I've been doing is compiling and archiving on my development machine and then transferring the app over to be run on the server computer.

For debugging purposes, I have a function that will save my print statements to a text file along with a timestamp.

    let fileName = "errorLogs"
let DocumentDirURL = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
let fileURL = DocumentDirURL.appendingPathComponent(fileName).appendingPathExtension("txt")
var readString = ""
do {
    // Read the file contents
    readString = try String(contentsOf: fileURL)
} catch let error as NSError {
    print("Failed reading from URL: \(fileURL), Error: " + error.localizedDescription)
}
let modifiedString = "\(readString)\n\(Date()): \(text)"
do {
// Write to the file
try modifiedString.write(to: fileURL, atomically: true, encoding: String.Encoding.utf8)
} catch let error as NSError {
    print("Failed writing to URL: \(fileURL), Error: " + error.localizedDescription)
}

It works pretty well for runtime issues, however the problem is when the app crashes all together. I'd like to get an output like I would if I was testing the app in an Xcode simulator environment.

So my question is this. Is it possible to have a stack trace type debug log output to a text file so that I can get a better idea of where the app is crashing?

Upvotes: 1

Views: 874

Answers (1)

Jordan
Jordan

Reputation: 329

I actually ended up going with @MwcsMac's solution. I installed Xcode to the machine outside of the network then brought it back into the network where I'm able to run the app and test how I need. I'm not able to update Xcode however I don't really need to.

My error log solution above tips me off to problems and then opening and running the app through Xcode gives me the specifics. Thanks everyone!

Upvotes: 1

Related Questions