Skip to content

Instantly share code, notes, and snippets.

@priore
Created July 23, 2018 12:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save priore/6425e22db011d9fc3a92fdda58578e50 to your computer and use it in GitHub Desktop.
Save priore/6425e22db011d9fc3a92fdda58578e50 to your computer and use it in GitHub Desktop.
Console logging output in a file
class ConsoleLog {
// to log even when a device is not connected to xcode
// it generates a file that you can then download from the Organizer
// select Devices -> select device -> select app -> Dowload container
// after you can able show the content of file and you navigate
// to Document folder for get the log file.
static func logToFile() {
#if DEBUG
if !self.isXcode() {
let docDirectory: NSString = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory,
FileManager.SearchPathDomainMask.userDomainMask, true)[0] as NSString
let logpath = docDirectory.appendingPathComponent("application.log")
let log = logpath.cString(using: .ascii)
freopen(log, "a+", stderr)
freopen(log, "a+", stdin)
freopen(log, "a+", stdout)
}
#endif
}
#if DEBUG
static private func isXcode() -> Bool {
var info = kinfo_proc()
var mib : [Int32] = [CTL_KERN, KERN_PROC, KERN_PROC_PID, getpid()]
var size = MemoryLayout<kinfo_proc>.stride
let junk = sysctl(&mib, UInt32(mib.count), &info, &size, nil, 0)
assert(junk == 0, "sysctl failed")
return (info.kp_proc.p_flag & P_TRACED) != 0
}
#endif
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment