Search code examples
ioscmdswiftios8

Logging Method signature using swift


I am trying to rewrite my logging class and I would like to know how to substitute PRETTY_FUNCTION or NSStringFromSelector(_cmd) in a swift file in order to track the method calls?


Solution

  • Check out a new library I've just published: https://github.com/DaveWoodCom/XCGLogger

    It's a debug logging library for Swift.

    The key to being able to use the #function macros, is to set them as default values to your logging function. The compiler will then fill them in using the expected values.

    func log(logMessage: String, functionName: String = #function) {
        print("\(functionName): \(logMessage)")
    }
    

    Then just call:

    log("my message")
    

    And it works as expected giving you something like:

    whateverFunction(): my message
    

    More info on how this works: https://www.cerebralgardens.com/blog/entry/2014/06/09/the-first-essential-swift-3rd-party-library-to-include-in-your-project