Skip to content

Instantly share code, notes, and snippets.

@HereOrCode
Last active June 12, 2022 05:50
Show Gist options
  • Save HereOrCode/5cd3c5579b0ec0013e4afd9b2cd62849 to your computer and use it in GitHub Desktop.
Save HereOrCode/5cd3c5579b0ec0013e4afd9b2cd62849 to your computer and use it in GitHub Desktop.
Printing file name, function name, line number in Swift.
func log(_ items: Any...,
withShortFileName: Bool = true,
file: String = #file,
function: String = #function,
line: Int = #line)
{
#if DEBUG
var fileName: String = ""
if withShortFileName {
fileName = (file as NSString).lastPathComponent
} else {
fileName = file
}
print("\n########Debug Start########")
print("Detail Info:")
print("File: ", fileName)
print("Function: ", function)
print("Line: ", line)
print("Detail:")
print(items)
print("########Debug End############ \n")
#endif
}
/*
How to use:
let model = MyTreeViewModel()
log(model)
If you want a good-looking print result, you can try SwiftPrettyPrint
https://github.com/YusukeHosonuma/SwiftPrettyPrint
*/
/*
Display results
########Debug Start########
Detail Info:
File: MainViewController.swift
Function: tableView(_:cellForRowAt:)
Line: 135
Detail:
[
data(
id: 1,
content: "Hello World"
)
]
########Debug End############
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment