Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Possible to write swift println logs into file too?

Tags:

logging

swift

Is it an easy way to write logs into a text file too? I need a crash log to analyse when something went wrong. But I already use println al around in the code.

like image 793
János Avatar asked Dec 20 '22 07:12

János


1 Answers

Use String.writeToFile(<#path: String#>, atomically: <#Bool#>, encoding: <#NSStringEncoding#>, error: <#NSErrorPointer#>)

You could add this:

#if DEBUG
func println(s:String) {
  var error:NSError? = nil
  let path = "/Users/<me>/dump.txt"
  var dump = String(contentsOfFile: path, encoding: NSUTF8StringEncoding, error: nil)!
  "\(dump)\n\(s)".writeToFile(path, atomically:true, encoding:NSUTF8StringEncoding, error:&error)
}
#endif

See the #if DEBUG answer on SO how to use this compiler flag.

like image 173
qwerty_so Avatar answered Dec 24 '22 01:12

qwerty_so