#file, #line, #function
temp
๐พ ็จๅผ๏ผ replit
// ----------------------
// โญ log function
// ----------------------
func log(
_ message: String,
// file : String = #file,
line : Int = #line,
function : String = #function,
note : String? = nil // additional info
) {
print("โน๏ธ \(message)\n (func: '\(function)', line: \(line))\n " + (note ?? ""))
}
// ----------------------------------------
// โญ top level: current module name
// ----------------------------------------
log("top level")
// ---------------------------------------------
// โญ inside getter/setter: property name
// ---------------------------------------------
var freeVar: Int {
get { log("inside getter"); return 0 } // freeVar โญ
set { log("inside setter") } // freeVar โญ
}
// call getter/setter
let x = freeVar
freeVar = 1
// ------------------------------------------------------
// โญ inside function: function name (with params)
// ------------------------------------------------------
func f(x: Int) {
log("inside function") // f(x:) โญ
}
func add(_ a: Int, _ b: Int) {
log("inside function", note: "(column: \(#column))\n") // add(_:_:) โญ
// (line: 44, column: 47)
}
// call the functions
f(x: 1)
add(1, 2)
// ---------------------------------------------
// โญ inside init/subscript: that keyword
// โญ inside method : method name
// ---------------------------------------------
struct A {
init(value: Int) {
log("inside 'init'") // init(value:) โญ
}
subscript(index: Int) -> Int {
log("inside 'subscript'") // subscript(_:) โญ
return 0
}
func method(x: Int, y: Int) {
log("inside method") // method(x:y:) โญ
}
}
// call these members
let a = A(value: 1)
_ = a[0]
a.method(x: 1, y: 2)
Swift Ref โฉ Expressions โฉ Primary Expressions โฉ Literal Expression
Macros in Swift? - custom log function using #file, #line, #function โญ๏ธ (๐#file)
log() - use #file, #line ... to log debug message.
Last updated