Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
308 views
in Technique[技术] by (71.8m points)

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

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

NSLog(@"%s", __PRETTY_FUNCTION__)

Usually this is used from a logging macro.

Although Swift does not support macro's (I think) I still would like to use a generic log statement that includes the name of the function that was called. Is that possible in Swift?

Update: I now use this global function for logging which can be found here: https://github.com/evermeer/Stuff#print And which you can install using:

pod 'Stuff/Print'

Here is the code:

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("
(level.description()) .(level) ? (dateFormatter.string(from: Foundation.Date())) ?? (process.processName) [(process.processIdentifier):(threadId)] ?? (file)((line)) ?? (funcname) ??
(object)")
        }
    }
}

Which you can use like this:

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)    

Which will result in:

?? .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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Swift has #file, #function, #line and #column. From Swift Programming Language:

#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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...