So this answer Do I need to disable NSLog before release Application? gives a great way to disable NSLog in a production environment, but unfortunately, this solution does n
I just found this amazing global function: https://gist.github.com/Abizern/a81f31a75e1ad98ff80d
This matches Nate Cook's answer and additionally prints the filename and line number of the encountered debug print statement.
Check out the Apple Swift blog entry on writing assert here
Briefly, the answer is to write DLog as:
func DLog(message:String, function:String = __FUNCTION__) {
#if !NDEBUG
NSLog("%@, %@", function, message)
#endif
}
Pure magic _isDebugAssertConfiguration()
does all stuff instead of the Custom Flags
-related mess:
func log<T>(argument: T, file: String = #file, line: Int = #line, function: String = #function) {
guard _isDebugAssertConfiguration() else {
return
}
let fileName = NSURL(fileURLWithPath: file, isDirectory: false).URLByDeletingPathExtension?.lastPathComponent ?? "Unknown"
print("\(fileName)@\(line)/\(function): \(argument)")
}
See more info (and options) about it here: https://stackoverflow.com/a/34532569/496389 .
#if DEBUG
#else
public func NSLog(_ format: String, _ args: CVarArg...)
{
// noop
}
#endif
The quest leading to this: swift debugPrint vs print
I agree with uclagamer's answer, which is essentially a souped up print function wrapped in a preprocessor conditional:
func gLog<T>(@autoclosure object: () -> T, _ file: String = __FILE__, _ function: String = __FUNCTION__, _ line: Int = __LINE__)
{
#if DEBUG
let value = object()
let stringRepresentation: String
if let value = value as? CustomDebugStringConvertible
{
stringRepresentation = value.debugDescription
}
else if let value = value as? CustomStringConvertible
{
stringRepresentation = value.description
}
else
{
fatalError("gLog only works for values that conform to CustomDebugStringConvertible or CustomStringConvertible")
}
let fileURL = NSURL(string: file)?.lastPathComponent ?? "Unknown file"
let queue = NSThread.isMainThread() ? "UI" : "BG"
let gFormatter = NSDateFormatter()
gFormatter.dateFormat = "HH:mm:ss:SSS"
let timestamp = gFormatter.stringFromDate(NSDate())
print("\(timestamp) \(queue) = \(fileURL) | \(function)[\(line)]: " + stringRepresentation)
#endif
}
This is updated for Swift 2.0. All credit goes to Abider Nasir. His original blog post (and linked gist) can be found here:
http://abizern.org/2015/02/01/debug-logging-in-swift/
IMPORTANT NOTE: if anyone is struggling to find the "Swift Compiler - Custom Flags section of Build Settings", which has been mentioned many, many times, you need to make sure that "All" build settings are shown, not just the "Basic" build settings, as is the default.
You'll need to set up a compiler flag to use the Swift preprocessor - go to the Swift Compiler - Custom Flags section of Build Settings to set up a -D DEBUG
flag:
Then in your code you can define a DLog()
function and only print your message if the DEBUG
flag is set:
func DLog(message: String, function: String = #function) {
#if DEBUG
println("\(function): \(message)")
#endif
}