priore

Console logging output in a file

Jul 18th, 2018
160
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     // to log even when a device is not connected to xcode
  2.     // it generates a file that you can then download from the Organizer
  3.     // select Devices -> select device -> select app ->  Dowload container
  4.     // after you can able show the content of file and you navigate
  5.     // to Document folder for get the log file.
  6.     func logToFile() {
  7.        
  8.         #if DEBUG
  9.         if !self.isXcode() {
  10.             let docDirectory: NSString = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory,
  11.                                                                              FileManager.SearchPathDomainMask.userDomainMask, true)[0] as NSString
  12.             let logpath = docDirectory.appendingPathComponent("application.log")
  13.             let log = logpath.cString(using: .ascii)
  14.            
  15.             freopen(log, "a+", stderr)
  16.             freopen(log, "a+", stdin)
  17.             freopen(log, "a+", stdout)
  18.         }
  19.         #endif
  20.     }
  21.    
  22.     #if DEBUG
  23.     func isXcode() -> Bool {
  24.        
  25.         var info = kinfo_proc()
  26.         var mib : [Int32] = [CTL_KERN, KERN_PROC, KERN_PROC_PID, getpid()]
  27.         var size = MemoryLayout<kinfo_proc>.stride
  28.         let junk = sysctl(&mib, UInt32(mib.count), &info, &size, nil, 0)
  29.         assert(junk == 0, "sysctl failed")
  30.         return (info.kp_proc.p_flag & P_TRACED) != 0
  31.     }
  32.     #endif
RAW Paste Data