我们常常需要优化控制台的输出样式,比如方法名,方法行数等,这时候可以使用一些常用的dlog
,在swift 3.0
中,可以这样定义:
//MARK: Macros
/**
dLog macros to abbreviate NSLog.
Use like this:
dLog("Log this!")
**/
#if DEBUG
func dLog<T>(_ object: T, filename: String = #file, function: String = #function, line: Int = #line) {
let fileString = filename as NSString
let fileLastPathComponent = fileString.lastPathComponent as NSString
let filename = fileLastPathComponent.deletingPathExtension
print("[\(filename):\(line)] \(function) - \(object)")
}
#else
func dLog<T>(_ object: T, filename: String = #file, function: String = #function, line: Int = #line) {
}
#endif
输出样式如下:
time xx[xx:xx] [filename: line] function() - a called
time xx[xx:xx] [filename: line] function() - b called
参考资料:[https://github.com/ArtSabintsev/Magic/blob/master/Magic.swift]