【转】自定义print

转自:https://blog.csdn.net/bobbob32/article/details/104011204

public func Fprint(_ items: Any...,
                    separator: String = " ",
                   terminator: String = "\n",
                   _ file:String = #file,
                   _ function:String = #function,
                   _ line:Int = #line){
    var longStr:String = ""
    for value in items{
        var stringRepresentation:String = ""
        if let value = value as? CustomStringConvertible{
            stringRepresentation = value.description
        }else if let value = value as? CustomDebugStringConvertible{
            stringRepresentation = value.debugDescription
        }else if let value = value as? LosslessStringConvertible{
            stringRepresentation = value.description
        } else{
            // fatalError("glog only work for values that conform to CustomStringConvertible or CustomDebugStringConvertible")
            // print(objct)
        }

        //longStr = longStr + "," + stringRepresentation
        ///下面 的方式更加符合原版的print
        longStr += stringRepresentation
        
    }
    
    let gFormatter = DateFormatter()
    gFormatter.dateFormat = "HH:mm:ss:SSS"
    let timeStamp = gFormatter.string(from: Date())
    let queue = Thread.isMainThread ? "UI":"BG"
    let fileUrl = NSURL(string: file)?.lastPathComponent ?? "Unknown file"
    
    if longStr.count > items.count{
        print("FM \(timeStamp) {\(queue)} \(fileUrl) > \(function)[\(line)]: \(longStr)")
    }else{
        print("FM \(timeStamp) {\(queue)} \(fileUrl) > \(function)[\(line)]: \(items)")
    }
}

参考 https://www.jianshu.com/p/de222deded93
感谢 前人种树 后人乘凉

你可能感兴趣的:(【转】自定义print)