iOS開(kāi)發(fā)中,常常需要打印日志Debug程序,NSLog輸出過(guò)于單一常常不能滿足我們的需求,DLog 能輸出行號(hào)、類名、方法命更便于調(diào)試。
- OC中的DLog
DLog
#ifdef DEBUG
#define DLog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);
#else
#define DLog(...)
#endif
ALog
#define ALog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);
在工程Bulid Settings的other C Flags的Debug中加入-DDEBUG,就能在工程Debug版本中調(diào)用DLog,在Release版本中不調(diào)用
- Swift中的DLog
#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