其他分享
首页 > 其他分享> > Swift 打印 log 所在的文件和行数

Swift 打印 log 所在的文件和行数

作者:互联网

在 OC 中,我们经常用到下面这种打印,使用 NSlog打印 log 所在的文件和行数。

#ifdef DEBUG 
#define NSLog(format, ...) printf("\n[%s] %s [第%d行] %s\n", __TIME__, __FUNCTION__, __LINE__, [[NSString stringWithFormat:format, ## __VA_ARGS__] UTF8String]);
#else
#define NSLog(format, ...)
#endif

在 swif 中可以这样处理

//获取打印的文件名、打印行数、打印函数
public func printLog<T>(_ msg: T,
                        file: NSString = #file,
                        line: Int = #line,
                        fn: String = #function) {
    #if DEBUG
    let prefix = "\(file.lastPathComponent) \(fn) [第\(line)行] \(msg)";
    print(prefix)
    #endif
}

举个栗子吧

printLog("555555")
//下面是打印结果
//AppDelegate.swift application(_:didFinishLaunchingWithOptions:) [第37行] 555555

标签:__,log,format,打印,NSLog,file,line,Swift,行数
来源: https://blog.csdn.net/u014651417/article/details/120576128