You might try something like this:
public let kMyLogLevel = 2
#if DEBUG
public func MyLog(text: String) { if kMyLogLevel <= 0 { NSLog(text) } }
public func MyLog1(text: String) { if kMyLogLevel <= 1 { NSLog(text) } }
public func MyLog2(text: String) { if kMyLogLevel <= 2 { NSLog(text) } }
public func MyLog3(text: String) { if kMyLogLevel <= 3 { NSLog(text) } }
public func MyLog4(text: String) { if kMyLogLevel <= 4 { NSLog(text) } }
public func MyLog5(text: String) { if kMyLogLevel <= 5 { NSLog(text) } }
#else
public func MyLog(text: String) { }
public func MyLog1(text: String) { }
public func MyLog2(text: String) { }
public func MyLog3(text: String) { }
public func MyLog4(text: String) { }
public func MyLog5(text: String) { }
#endif
This can be put in any module at file scope (i.e., not inside a class or other type.)
Note that the input is not a variable argument list, rather a simple text string. The Swift way is to use String interpolation, like so:
MyLog("The value is: \(value)")
Also, Swift doesn't support preprocessor macros in the same way you're used to with Objective-C. In Swift, you can only provide conditional compilation flags and you can only test for existence of those flags (they cannot be assigned a value.) In addition, you cannot create these flags in code (there is no #define
). These flags must be sent into the compiler's command line (ex: -DDEBUG
). See the top answer to this question for more information.
Swift doesn't provide a DEBUG
flag by default, you'll need to set that in the build options for your project/target. See the question linked to above for details on this.
The DEBUG
flag works fine for conditional compilation, but not for your log level. For this, common practice is to use a constant (via the let
keyword.)