Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disabling NSLog For Production In Swift Project

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 not seem to work for Swift projects. My approach was to place the following code in the bridging header .h file that I am using for some pods in my project.

#ifdef DEBUG
    #define DLog(...) NSLog(@"%s %@", __PRETTY_FUNCTION__, [NSString stringWithFormat:__VA_ARGS__])
#else
    #define DLog(...) do { } while (0)
#endif

However, using DLog in Swift code is causing the compiler to state that they are unrecongnized symbols. Is there somewhere else I should be placing this #ifdef or is there a different solution for Swift project in general?

like image 846
thatidiotguy Avatar asked Nov 12 '14 15:11

thatidiotguy


3 Answers

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:

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
}
like image 132
Nate Cook Avatar answered Oct 26 '22 16:10

Nate Cook


I am a pretty junior-level developer and need things defined a little more clearly to follow along at times. I had been having trouble with a similar issue, replacing println() instead, and when i asked that question my inquiry was marked as a duplicate of this question.

After lots of research, trial, error, error, error, and ERROR!!, I found that the sequence I needed to follow is:

  1. Click on the project name at the top of the File Navigator at the left of the Xcode project window. This is line that has the name of the project, how many build targets there are, and the iOS SDK version.
  2. Choose the Build Settings tab and scroll down to the "Swift Compiler - Custom Flags" section near the bottom. Click the Down Arrow next to Other Flags to expand the section.
  3. Click on the Debug line to select it. Place your mouse cursor over the right side of the line and double-click. A list view will appear. Click the + button at the lower left of the list view to add a value. A text field will become active.
  4. In the text field, enter the text -D DEBUG and press Return to commit the line.
  5. Add a new Swift file to your project. You are going to want to make a custom class for the file, so enter text along the lines of the following:

    class Log {
    
       var intFor : Int
    
      init() {
        intFor = 42
      }
    
      func DLog(message: String, function: String = __FUNCTION__) {
        #if DEBUG
          println("\(function): \(message)")
        #endif
      }
    }
    

(I was having trouble getting the class init to be accepted by Xcode today, so the init may be a bit more heavyweight than necessary.)

  1. Now you will need to reference your custom class in any class in which you intend to use the new custom function in place of println() Add this as a property in every applicable class:

       let logFor = Log()
    
  2. Now you can replace any instances of println() with logFor.DLog(). The output also includes the name of the function in which the line was called.

  3. Note that inside class functions I couldn't call the function unless I made a copy of the function as a class function in that class, and println() is also a bit more flexible with the input, so I couldn't use this in every instance in my code.
like image 45
Nate Birkholz Avatar answered Oct 26 '22 17:10

Nate Birkholz


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
}
like image 6
David Berry Avatar answered Oct 26 '22 16:10

David Berry