-
-
Save xmzio/fccd29fc945de7924b71 to your computer and use it in GitHub Desktop.
// | |
// Macros.swift | |
// | |
// Created by Xavier Muñiz on 6/12/14. | |
import Foundation | |
// dLog and aLog macros to abbreviate NSLog. | |
// Use like this: | |
// | |
// dLog("Log this!") | |
// | |
#if DEBUG | |
func dLog(message: String, filename: String = __FILE__, function: String = __FUNCTION__, line: Int = __LINE__) { | |
NSLog("[\(filename.lastPathComponent):\(line)] \(function) - \(message)") | |
} | |
#else | |
func dLog(message: String, filename: String = __FILE__, function: String = __FUNCTION__, line: Int = __LINE__) { | |
} | |
#endif | |
func aLog(message: String, filename: String = __FILE__, function: String = __FUNCTION__, line: Int = __LINE__) { | |
NSLog("[\(filename.lastPathComponent):\(line)] \(function) - \(message)") | |
} |
Thanks! This works very well. I added a couple of functions to include a 'uLog', that shows an alert with the error.
This is my Macros.swift file now:
import Foundation
import UIKit
// dLog and aLog macros to abbreviate NSLog.
// Use like this:
//
// dLog("Log this!")
//
#if DEBUG
func dLog(message: String, filename: String = __FILE__, function: String = __FUNCTION__, line: Int = __LINE__) {
NSLog("[\(filename.lastPathComponent):\(line)] \(function) - \(message)")
}
func uLog(message: String, filename: String = __FILE__, function: String = __FUNCTION__, line: Int = __LINE__) {
let alertView = UIAlertView(title: "[\(filename.lastPathComponent):\(line)]", message: "\(function) - \(message)", delegate:nil, cancelButtonTitle:"OK")
alertView.show()
}
#else
func dLog(message: String, filename: String = __FILE__, function: String = __FUNCTION__, line: Int = __LINE__) { }
func uLog(message: String, filename: String = __FILE__, function: String = __FUNCTION__, line: Int = __LINE__) { }
#endif
func aLog(message: String, filename: String = __FILE__, function: String = __FUNCTION__, line: Int = __LINE__) {
NSLog("[\(filename.lastPathComponent):\(line)] \(function) - \(message)")
}
If you have a % sign in your message:
var strWithPercent = "ABC%DEF"
dLog("\(strWithPercent)")
This would crash.
Can be fixed by sending the computed string as a variable argument instead:
NSLog("%@","[\(filename.lastPathComponent):\(line)] \(function) - \(message)")
I also removed the file extension from the file name because it saves space :)
NSLog("%@","[\(filename.lastPathComponent.stringByDeletingPathExtension):\(line)] \(function) - \(message)")
This is what I ended up with
#if DEBUG
func dLog(message: String, filename: String = __FILE__, function: String = __FUNCTION__, line: Int = __LINE__) {
NSLog("%@","[\(filename.lastPathComponent.stringByDeletingPathExtension):\(line)] \(function) - \(message)")
}
#else
func dLog(message: String, filename: String = __FILE__, function: String = __FUNCTION__, line: Int = __LINE__) {
}
#endif
func aLog(message: String, filename: String = __FILE__, function: String = __FUNCTION__, line: Int = __LINE__) {
NSLog("%@","[\(filename.lastPathComponent.stringByDeletingPathExtension):\(line)] \(function) - \(message)")
}
Hey! This is an old thread, but has been useful to me. What I don't like with the above solutions is that the message variable (with lots of possible string interpolation) gets run regardless of whether DEBUG is set or not. To solve this, laze evaluation with @autoclosure can be used (see https://developer.apple.com/swift/blog/?id=4). When I do that, I get the following which seems to work well (and also works around the issue mentioned by @timzai)
import Foundation
// dLog and aLog macros to abbreviate NSLog.
// Use like this:
//
// dLog("Log this!")
//
#if DEBUG
func dLog(@autoclosure message: () -> String, filename: String = __FILE__, function: String = __FUNCTION__, line: Int = __LINE__) {
NSLog("[\(filename.lastPathComponent):\(line)] \(function) - %@", message())
}
#else
func dLog(@autoclosure message: () -> String, filename: String = __FILE__, function: String = __FUNCTION__, line: Int = __LINE__) {
}
#endif
func aLog(message: String, filename: String = __FILE__, function: String = __FUNCTION__, line: Int = __LINE__) {
NSLog("[\(filename.lastPathComponent):\(line)] \(function) - %@", message)
}
I went ahead an made String into AnyObject so that I could log out a dictionary.
Its sad that String in Swift 2.0 doesn't have lastPathComponent :(
We will need to use NSString if we still need that lastPathComponent.
https://forums.developer.apple.com/thread/13580
With Swift 2.2 you now use #filename
, #line
, etc. Here's what I have to include the fix for the %
and the .lastPathComponent
import Foundation
#if DEBUG
func dLog(message: String, filename: String = #file, function: String = #function, line: Int = #line) {
NSLog("%@","[\((filename as NSString).lastPathComponent):\(line)] \(function) - \(message)")
}
#else
func dLog(message: String, filename: String = #file, function: String = #function, line: Int = #line) {
}
#endif
func aLog(message: String, filename: String = #file, function: String = #function, line: Int = #line) {
NSLog("%@","[\((filename as NSString).lastPathComponent):\(line)] \(function) - \(message)")
}
I'm using it like this, a little more compact:
func dLog(message: String, filename: String = #file, function: String = #function, line: Int = #line) {
#if DEBUG
NSLog("[\((filename as NSString).lastPathComponent):\(line)] \(function) - \(message)")
#endif
}
In Objective-C it was widely used as DLog, and ALog with capital D and A, so i am going to keep the same convention in Swift. in my own code
As per Jean Le Moignan http://stackoverflow.com/questions/24003291/ifdef-replacement-in-swift-language
Now, you must set the "DEBUG" symbol elsewhere, though. Set it in the "Swift Compiler - Custom Flags" section, "Other Swift Flags" line. You add the DEBUG symbol with the -D DEBUG entry.
As usual, you can set a different value when in Debug or when in Release.
I tested it in real code and it works; it doesn't seem to be recognized in a playground though.