调试 - DLog

iOS开发中,常常需要打印日志Debug程序,NSLog输出过于单一常常不能满足我们的需求,DLog 能输出行号、类名、方法命更便于调试。

  • 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版本中调用DLog,在Release版本中不调用

  • Swift中的DLog
#if DEBUG
    func DLog(_ 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(_ object: T, filename: String = #file, function: String = #function, line: Int = #line) {

    }
#endif

你可能感兴趣的:(调试 - DLog)