美文网首页
Swift 使用 OC风格日志打印

Swift 使用 OC风格日志打印

作者: 韩小醋 | 来源:发表于2018-02-05 10:25 被阅读44次

OC风格

//#ifdef DEBUG
#ifndef __OPTIMIZE__
//调试状态
#define Log_fileName [NSString stringWithFormat:@"%s", __FILE__].lastPathComponent
#define NSLog(...) printf("➡️ 执行文件 %s-第%d行 \n调用方法: %s\nlog信息:\n%s\n\n",[Log_fileName UTF8String],__LINE__,__func__,[[NSString stringWithFormat:__VA_ARGS__] UTF8String]);
#else
//发布状态
#define NSLog(...) {}
#endif

swift风格

@inline(__always) func Blog(_ file: String = #file, function: String = #function, lineNum: Int = #line, format: String, _ arguments: CVarArg...) {
    #if DEBUG
        print("➡️ 执行文件 \(file.nsstring().lastPathComponent) "
            + "第\(lineNum)行 \n"
            + "调用方法: \(function)\n"
            + "log信息:\n\(String.init(format: format,arguments: arguments))\n"//这里被坑了,必须指定arguments参数名 负责推测不出来类型
        );
    #endif
}

使用

Blog( format: "%@🐱🐱🐱%@🐱🐱🐱⛽️%@-%@-%@-%@\n", "nihao","hanxiocu","hanxiocu", "hanxiocu", "hanxiocu");

相关文章

网友评论

      本文标题:Swift 使用 OC风格日志打印

      本文链接:https://www.haomeiwen.com/subject/rbjbzxtx.html