Remove println() for release version iOS Swift

前端 未结 19 1523
被撕碎了的回忆
被撕碎了的回忆 2020-11-29 15:55

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

19条回答
  •  囚心锁ツ
    2020-11-29 16:33

    Tested with Swift 2.1 & Xcode 7.1.1

    There's an easy way to exclude all print statements from release versions, once you know that empty functions are removed by the Swift compiler.

    Side note : In the era of Objective-C, there was a pre-parser which could be used to remove NSLog statements before the compiler kicked in, like described in my answer here. But since Swift no longer has a pre-parser this approach is no longer valid.

    Here's what I use today as an advanced and easily configurable log function, without ever having to worry about removing it in release builds. Also by setting different compiler flags, you can tweak the information that is logged as needed.

    You can tweak the function as needed, any suggestion to improve it is welcome!

    // Gobal log() function
    //
    // note that empty functions are removed by the Swift compiler -> use #if $endif to enclose all the code inside the log()
    // these log() statements therefore do not need to be removed in the release build !
    //
    // to enable logging
    //
    // Project -> Build Settings -> Swift Compiler - Custom flags -> Other Swift flags -> Debug
    // add one of these 3 possible combinations :
    //
    //      -D kLOG_ENABLE
    //      -D kLOG_ENABLE -D kLOG_DETAILS
    //      -D kLOG_ENABLE -D kLOG_DETAILS -D kLOG_THREADS
    //
    // you can just call log() anywhere in the code, or add a message like log("hello")
    //
    func log(message: String = "", filePath: String = #file, line: Int = #line, function: String = #function) {
                #if kLOG_ENABLE
    
                #if kLOG_DETAILS
    
                var threadName = ""
                #if kLOG_THREADS
                    threadName = NSThread.currentThread().isMainThread ? "MAIN THREAD" : (NSThread.currentThread().name ?? "UNKNOWN THREAD")
                    threadName = "[" + threadName + "] "
                #endif
    
                let fileName = NSURL(fileURLWithPath: filePath).URLByDeletingPathExtension?.lastPathComponent ?? "???"
    
                var msg = ""
                if message != "" {
                    msg = " - \(message)"
                }
    
                NSLog("-- " + threadName + fileName + "(\(line))" + " -> " + function + msg)
            #else
                NSLog(message)
            #endif
        #endif
    }
    

    Here's where you set the compiler flags :

    An example output with all flags on looks like this :

       2016-01-13 23:48:38.026 FoodTracker[48735:4147607] -- [MAIN THREAD] ViewController(19) -> viewDidLoad() - hello
    

    The code with the log() looks like this :

        override func viewDidLoad() { log("hello")
        super.viewDidLoad()
    
       // Handle the text field's user input through delegate callbacks
       nameTextField.delegate = self
    }
    

提交回复
热议问题