是否有 NSLog(@"%s", __PRETTY_FUNCTION__) 的 Swift 替代方案

2024-02-14

在 Objective C 中,您可以使用以下命令记录正在调用的方法:

NSLog(@"%s", __PRETTY_FUNCTION__)

通常这在日志记录宏中使用。

虽然 Swift 不支持宏(我认为),但我仍然想使用包含被调用函数名称的通用日志语句。这在斯威夫特中可能吗?

Update:我现在使用这个全局函数进行日志记录,可以在这里找到:https://github.com/evermeer/Stuff#print https://github.com/evermeer/Stuff#print您可以使用以下命令安装:

pod 'Stuff/Print'

这是代码:

public class Stuff {

    public enum logLevel: Int {
        case info = 1
        case debug = 2
        case warn = 3
        case error = 4
        case fatal = 5
        case none = 6

        public func description() -> String {
            switch self {
            case .info:
                return "❓"
            case .debug:
                return "✳️"
            case .warn:
                return "⚠️"
            case .error:
                return "????"
            case .fatal:
                return "????"
            case .none:
                return ""
            }
        }
    }

    public static var minimumLogLevel: logLevel = .info

    public static func print<T>(_ object: T, _ level: logLevel = .debug, filename: String = #file, line: Int = #line, funcname: String = #function) {
        if level.rawValue >= Stuff.minimumLogLevel.rawValue {
            let dateFormatter = DateFormatter()
            dateFormatter.dateFormat = "MM/dd/yyyy HH:mm:ss:SSS"
            let process = ProcessInfo.processInfo
            let threadId = "?"
            let file = URL(string: filename)?.lastPathComponent ?? ""
            Swift.print("\n\(level.description()) .\(level) ⏱ \(dateFormatter.string(from: Foundation.Date())) ???? \(process.processName) [\(process.processIdentifier):\(threadId)] ???? \(file)(\(line)) ⚙️ \(funcname) ➡️\r\t\(object)")
        }
    }
}

你可以这样使用:

Stuff.print("Just as the standard print but now with detailed information")
Stuff.print("Now it's a warning", .warn)
Stuff.print("Or even an error", .error)

Stuff.minimumLogLevel = .error
Stuff.print("Now you won't see normal log output")
Stuff.print("Only errors are shown", .error)

Stuff.minimumLogLevel = .none
Stuff.print("Or if it's disabled you won't see any log", .error)    

这将导致:

✳️ .debug ⏱ 02/13/2017 09:52:51:852 ???? xctest [18960:?] ???? PrintStuffTests.swift(15) ⚙️ testExample() ➡️
    Just as the standard print but now with detailed information

⚠️ .warn ⏱ 02/13/2017 09:52:51:855 ???? xctest [18960:?] ???? PrintStuffTests.swift(16) ⚙️ testExample() ➡️
    Now it's a warning

???? .error ⏱ 02/13/2017 09:52:51:855 ???? xctest [18960:?] ???? PrintStuffTests.swift(17) ⚙️ testExample() ➡️
    Or even an error

???? .error ⏱ 02/13/2017 09:52:51:855 ???? xctest [18960:?] ???? PrintStuffTests.swift(21) ⚙️ testExample() ➡️
    Only errors are shown

斯威夫特有#file, #function, #line and #column. From 斯威夫特编程语言 https://developer.apple.com/library/prerelease/ios/documentation/swift/conceptual/swift_programming_language/Expressions.html:

#file- 字符串 - 它出现的文件的名称。

#line- Int - 它出现的行号。

#column- Int - 它开始的列号。

#function- 字符串 - 它出现的声明的名称。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

是否有 NSLog(@"%s", __PRETTY_FUNCTION__) 的 Swift 替代方案 的相关文章

随机推荐