swift 替代print 方法更具體的日誌輸出

直接貼代碼了,可以支持多參數,在別人基礎上進行修改

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
    }
    
    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)")
    }
}

參考文章

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章