Skip to content

Instantly share code, notes, and snippets.

@jayrhynas
Last active November 20, 2015 02:14
Show Gist options
  • Save jayrhynas/0ec7165890494d9a7f81 to your computer and use it in GitHub Desktop.
Save jayrhynas/0ec7165890494d9a7f81 to your computer and use it in GitHub Desktop.
extension NSThread {
class func logCallStack(showRedacted showRedacted: Bool = false) {
#if DEBUG
let removeRedacted: String -> Bool = { symbol in
return showRedacted ? true : symbol.rangeOfString("<redacted>", options: []) == nil
}
let swiftSymbolRegex = try! NSRegularExpression(pattern: " (_T\\S+) \\+", options: [])
let demangle: String -> String = { symbol in
let symbolStr = symbol as NSString
guard let match = swiftSymbolRegex.firstMatchInString(symbol, options: [], range: NSMakeRange(0, symbolStr.length))
else { return symbol }
let range = match.rangeAtIndex(1)
let demangledName = _stdlib_demangleName(symbolStr.substringWithRange(range))
return symbolStr.stringByReplacingCharactersInRange(range, withString: demangledName)
}
let symbols = self.callStackSymbols()
.dropFirst(2) // drop first two symbols (logCallStack and @objc logCallStack)
.filter(removeRedacted)
.map(demangle)
.joinWithSeparator("\n")
print(symbols)
#endif
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment