Created
October 23, 2016 00:04
-
-
Save loucimj/9c6653e019f1e14ee012f421bf31d6d9 to your computer and use it in GitHub Desktop.
Friendlier and safer print for debug console
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// DebugPrint.swift | |
// | |
// Created by Javier Loucim on 10/22/16. | |
// Copyright © 2016 Javier Loucim. All rights reserved. | |
// | |
import Foundation | |
func print(_ message:String , separator: String = " ", terminator: String = "\n", functionName: String = #function, fileName: String = #file, lineNumber: Int = #line) { | |
myPrint(message, separator: " ", terminator: "\n", functionName: functionName, fileName: fileName, lineNumber: lineNumber) | |
} | |
fileprivate func myPrint(_ items: Any..., separator: String = " ", terminator: String = "\n", functionName: String = #function, fileName: String = #file, lineNumber: Int = #line) { | |
#if DEBUG | |
let dateFormatter = DateFormatter() | |
dateFormatter.locale = NSLocale.current | |
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss.SSS" | |
let file = fileName.components(separatedBy: "/").last!.components(separatedBy: ".").first! | |
var idx = items.startIndex | |
let endIdx = items.endIndex | |
Swift.print("\(dateFormatter.string(from: NSDate() as Date)) [\(file):\(lineNumber)] \(functionName)", separator: " ", terminator: separator) | |
repeat { | |
Swift.print(items[idx], separator: separator, terminator: idx == (endIdx - 1) ? terminator : separator) | |
idx += 1 | |
} | |
while idx < endIdx | |
#endif | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment