反正有日志功能吗?

Is there anyway to make log function?

我想用一种方法打印 #file#function#line

我尝试了下面的代码,但问题出在这里。 无论我在哪里调用 logm(),它总是打印 logm 方法本身的信息,即使我将其声明为 @inline.

@inline(__always) func logm(items: Any...) {
    if let f = #file.componentsSeparatedByString("/").last {
        print("[\(f)][\(#function)][\(#line)]:", items)
    } else {
        print("[\(#function)][\(#line)]: ", items)
    }
}

有没有办法实现这种东西?为什么 @inline 会像我预期的那样工作?

您可以将文件、行和函数作为参数传递,并将指令作为默认值:

func logm(items: Any..., file: String = #file, line: Int = #line, function: String = #function) {
    if let f = file.componentsSeparatedByString("/").last {
        print("[\(f)][\(function)][\(line)]:", items)
    } else {
        print("[\(function)][\(line)]: ", items)
    }
}