使用swift记录方法签名 [英] Logging Method signature using swift
问题描述
我正在尝试重写我的日志类,我想知道如何在swift文件中替换 PRETTY_FUNCTION 或NSStringFromSelector(_cmd)以跟踪方法调用?
$查看我刚刚发布的一个新库: nofollow> https://github.com/DaveWoodCom/XCGLogger这是Swift的调试日志库。
能够使用 #function
宏的关键是将它们设置为日志功能的默认值。然后编译器将使用期望值填充它们。
func log(logMessage:String,functionName:String = #function) {
print(\(functionName):\(logMessage))
}
然后只需调用:
log(my message)
它的工作原理如下:
whateverFunction():我的消息
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?
解决方案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: http://www.cerebralgardens.com/blog/entry/2014/06/09/the-first-essential-swift-3rd-party-library-to-include-in-your-project
这篇关于使用swift记录方法签名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!