Is there a Swift alternative for NSLog(@“%s”, __PRETTY_FUNCTION__)

前端 未结 11 1125
梦谈多话
梦谈多话 2020-12-02 07:39

In Objective C you can log the method that is being called using:

NSLog(@\"%s\", __PRETTY_FUNCTION__)

Usually this is used from a logging m

相关标签:
11条回答
  • 2020-12-02 08:31

    Or slight function modification with:

    func logFunctionName(file:String = __FILE__, fnc:String = __FUNCTION__, line:(Int)=__LINE__) {
        var className = file.lastPathComponent.componentsSeparatedByString(".")
        println("\(className[0]):\(fnc):\(line)")
    

    }

    /* will produce an execution trace like: AppDelegate:application(_:didFinishLaunchingWithOptions:):18 Product:init(type:name:year:price:):34 FirstViewController:viewDidLoad():15 AppDelegate:applicationDidBecomeActive:62 */

    0 讨论(0)
  • 2020-12-02 08:33

    Swift 4
    Here's my approach:

    func pretty_function(_ file: String = #file, function: String = #function, line: Int = #line) {
    
        let fileString: NSString = NSString(string: file)
    
        if Thread.isMainThread {
            print("file:\(fileString.lastPathComponent) function:\(function) line:\(line) [M]")
        } else {
            print("file:\(fileString.lastPathComponent) function:\(function) line:\(line) [T]")
        }
    }
    

    Make this a global function and just call

    pretty_function()
    

    Bonus: You will see the thread is executed on, [T] for a background thread and [M] for the Main thread.

    0 讨论(0)
  • 2020-12-02 08:35

    Swift 3.0

    public func LogFunction<T>(object: T, filename: String = #file, line: Int = #line, funcname: String = #function) {
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "MM/dd/yyyy HH:mm:ss:SSS"
        let process = ProcessInfo.processInfo()
        let threadId = "?"
        print("\(dateFormatter.string(from:Date())) \(process.processName) [\(process.processIdentifier):\(threadId)] \(filename)(\(line)) \(funcname)::: \(object)")
    }
    
    0 讨论(0)
  • 2020-12-02 08:38

    I prefer to define a global log function:

    [Swift 3.1]

    func ZYLog(_ object: Any?, filename: String = #file, line: Int = #line, funcname: String = #function) {
        #if DEBUG
        print("****\(Date()) \(filename)(\(line)) \(funcname):\r\(object ?? "nil")\n")
        #endif
    }
    

    [Swift 3.0]

    func ZYLog<T>(_ object: T?, filename: String = #file, line: Int = #line, funcname: String = #function) {
        #if DEBUG
        print("****\(Date()) \(filename)(\(line)) \(funcname):\r\(object)\n")
        #endif
    }
    

    [Swift 2.0]

    func ZYLog<T>(object: T, filename: String = __FILE__, line: Int = __LINE__, funcname: String = __FUNCTION__) {
        println("****\(filename.lastPathComponent)(\(line)) \(funcname):\r\(object)\n")
    }
    

    the output is something like:

    ****ZYHttpSessionManager.swift(78) POST(_:parameters:success:failure:):
    [POST] user/login, {
        "auth_key" = xxx;
        "auth_type" = 0;
        pwd = xxx;
        user = "xxx";
    }
    
    ****PointViewController.swift(162) loadData():
    review/list [limit: 30, skip: 0]
    
    ****ZYHttpSessionManager.swift(66) GET(_:parameters:success:failure:):
    [GET] review/list, {
        "auth_key" = xxx;
        uuid = "xxx";
    }
    
    0 讨论(0)
  • 2020-12-02 08:41

    Starting from Swift 2.2 we should use:

    • #file (String) The name of the file in which it appears.
    • #line (Int) The line number on which it appears.
    • #column (Int) The column number in which it begins.
    • #function (String) The name of the declaration in which it appears.

    From The Swift Programming Language (Swift 3.1) at page 894.

    func specialLiterals() {
        print("#file literal from file: \(#file)")
        print("#function literal from function: \(#function)")
        print("#line: \(#line) -> #column: \(#column)")
    }
    // Output:
    // #file literal from file: My.playground
    // #function literal from function: specialLiterals()
    // #line: 10 -> #column: 42
    
    0 讨论(0)
提交回复
热议问题