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
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 */
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.
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)")
}
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";
}
Starting from Swift 2.2 we should use:
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