I have noticed a significant decrease in performance of the console logs when printing large amounts of text in Xcode 10. For instance, this happens when printing collection server data. Is there any known solution or information regarding this?
Asked
Active
Viewed 1,227 times
3 Answers
1
I have the same problem in Swift with the print
command, but only if the debugger is attached.
I solved it by checking if the debugger is attached and then using NSLog
instead of print
.
To check if your app is running in debugger mode, see here: Detect if Swift app is being run from Xcode
Consider that with Xcode 8 + iOS 10 and above the log entries are truncated, see here: NSLog on devices in iOS 10 / Xcode 8 seems to truncate? Why?
Edit 05.03.2019:
I fixed it by using the solution from https://stackoverflow.com/a/39907046/6330248
Swift 4.2:
extension String {
func components(withLength length: Int) -> [String] {
guard length > 0 else {
return [self]
}
return stride(from: 0, to: self.count, by: length).map {
let start = self.index(self.startIndex, offsetBy: $0)
let end = self.index(start, offsetBy: length, limitedBy: self.endIndex) ?? self.endIndex
return String(self[start..<end])
}
}
}
func amIBeingDebugged() -> 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
}
func FixedNSLog(_ format: String, _ args: CVarArg...) {
let maxStringLength = 800
let string = String(format: format, arguments: args)
let substrings = string.components(withLength: maxStringLength)
let substringsCount = substrings.count
if substringsCount > 1 {
NSLog("!!! WARNING !!! The following message is printed by \(substringsCount) parts, because of Xcode 10 console issues")
}
substrings.forEach { (substring) in
NSLog(substring)
}
}
func log(_ message: String) {
if amIBeingDebugged() {
FixedNSLog(message)
}
else {
print(message)
}
}

Philipp S.
- 51
- 7
-
but what if you need to debug? – Hogdotmac Oct 15 '18 at 10:58
0
Swift:
Refer this: Xcode takes long time to print debug results.
public func PrintLogs(_ message: Any, file: String = #file, function: String = #function, line: Int = #line) {
#if DEBUG
let className = file.components(separatedBy: "/").last ?? ""
let classNameArr = className.components(separatedBy: ".")
NSLog("\n\n--> Class Name: \(classNameArr[0]) \n--> Function Name: \(function) \n--> Line: \(line)")
print("--> Log Message: \(message)")
#endif
}
Usage: Call PrintLogs("Hello") instead of print("Hello")
Sample Output:
--> Class Name: HomeViewController
--> Function Name: logTest()
--> Line: 81
--> Log Message: Hello

Rajesh Loganathan
- 11,129
- 4
- 78
- 90