Skip to content

Instantly share code, notes, and snippets.

@iThinker
Created April 22, 2017 23:07
Show Gist options
  • Save iThinker/689b60a7583ae109f9b789aa98bbba6a to your computer and use it in GitHub Desktop.
Save iThinker/689b60a7583ae109f9b789aa98bbba6a to your computer and use it in GitHub Desktop.
class Logger {
static var defaultLogger = Logger()
func log(_ message: String) {
print(message)
}
}
func tryAndLog<T>(_ executable: @autoclosure () throws -> T, withDefault defaultValue: T, logger: Logger = Logger.defaultLogger, file: String = #file, line: Int = #line) -> T {
do {
return try executable()
}
catch let error {
logger.log("Logged excpetion: \(error.localizedDescription), in \(file), at \(line)")
}
return defaultValue
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment