I would like to globally ignore all println()
calls in my Swift code if I am not in a Debug build. I can\'t find any robust step by step instructions for this a
I ended up using this:
#if DEBUG
func dLog(_ item: @autoclosure () -> Any, _ file: String = #file, _ function: String = #function, _ line: Int = #line) {
print("\(Date()) [\((file as NSString).lastPathComponent):\(line) \(function)] \(item())")
}
#else
func dLog(_ item: @autoclosure () -> Any) {}
#endif
It is quite compact, prints some useful informations (timestamp, swift file name, code line, function name) and at least in my tests I could not find any logged strings in the application binary file when opened in hex editior.
Here is a function that I use, which works perfectly in Swift 3:
func gLog<T>( _ object: @autoclosure() -> 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 = Thread.isMainThread ? "UI" : "BG"
let gFormatter = DateFormatter()
gFormatter.dateFormat = "HH:mm:ss:SSS"
let timestamp = gFormatter.string(from: Date())
print("✅ \(timestamp) {\(queue)} \(fileURL) > \(function)[\(line)]: " + stringRepresentation + "\n")
#endif
}
Here is an example of the output it generates:
Explanation:
the green checkmark is used to enable you to quickly see your print (gLog) messages in the console, where they can sometimes get lost in a sea of other messages
the time/date stamp
the thread it is being run on -- in my case it is either the MainThread (which I call UI), or not the MainThread (which I call BG, for background thread)
the name of the file that the gLog message resides in
the function within the file that the gLog message resides in
the line number of the gLog message
the actual gLog message you would like to print out
Hope this is useful to someone else!
You could define debug_println
whose contents would be roughly:
#if DEBUG
println()
#endif
XCode 8 introduced a few new build settings.
In particular one referred to Active Compilation Conditions
does in a similar way what Other Flags settings did.
"Active Compilation Conditions" is a new build setting for passing conditional compilation flags to the Swift compiler.
As per XCode 8 (tested in 8.3.2) you will get this by default:
So without any config you can write the following:
#if DEBUG
print("⚠️ Something weird happened")
#endif
I strongly recommend you that if you use this approach extensively create a class/struct/function that wraps this logging logic. You may want to extend this further down the road.
My Project was developed in Objective C, but from the past year I have started merging new code in Swift, So In Swift below solution worked for me, I have added that code in My Swift constant file :
func print(_ items: Any..., separator: String = " ", terminator: String = "\n") {
#if DEBUG
items.forEach {
Swift.print($0, separator: separator, terminator: terminator)
}
#endif
}
My Solution is use this code in AppDelegate before class
// Disable console log in live app
#if !arch(x86_64) && !arch(i386)
public func debugPrint(items: Any..., separator: String = " ", terminator: String = "\n") {
}
public func print(_ items: Any..., separator: String = " ", terminator: String = "\n") {
}
#endif
class AppDelegate: UIResponder, UIApplicationDelegate {
// App Delegate Code
}