Last active
August 22, 2023 03:41
-
-
Save atomicbird/d3e6f40663ab481f3d3c73428a2a9046 to your computer and use it in GitHub Desktop.
Sometimes you just want to print a message that tells you a line of code was executed. Inspired by a tweet from Paige Sun: https://twitter.com/_PaigeSun/status/1161132108875796480
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// Log the current filename and function, with an optional extra message. Call this with no arguments to simply print the current file and function. Log messages will include an Emoji selected from a list in the function, based on the hash of the filename, to make it easier to see which file a message comes from. | |
/// - Parameter message: Optional message to include | |
/// - file: Don't use; Swift will fill in the file name | |
/// - function: Don't use, Swift will fill in the function name | |
/// - line: Don't use, Swift will fill in the line number | |
func logMilestone(_ message: String? = nil, file: String = #file, function: String = #function, line: Int = #line) -> Void { | |
#if DEBUG | |
// Feel free to change the list of Emojis, but don't make it shorter, because a longer list is better. | |
let logEmojis = ["π","π","π±","π","πΊ","π½","πΎ","π€","π","π","π","π§ ","π","π§€","πΆ","π±","π","πΉ","π¦","π»","π¨","π΅","π¦","π¦","π","π₯","π₯","βοΈ","π","π₯","π½","π","πΏ","πΉ","π","β€οΈ","π§‘","π","π","π","π","π"] | |
let logEmoji = logEmojis[abs(file.hashValue % logEmojis.count)] | |
if let message = message { | |
print("Milestone: \(logEmoji) \((file as NSString).lastPathComponent):\(line) \(function): \(message)") | |
} else { | |
print("Milestone: \(logEmoji) \((file as NSString).lastPathComponent):\(line) \(function)") | |
} | |
#endif | |
} | |
/// Convenience to log an error using `logMilestone`-style logging with file location and an Emoji | |
/// - Parameters: | |
/// - error: Any `Error` | |
/// - file: Don't use; Swift will fill in the file name | |
/// - function: Don't use, Swift will fill in the function name | |
/// - line: Don't use, Swift will fill in the line number | |
func logMilestone(_ error: Error, file: String = #file, function: String = #function, line: Int = #line) -> Void { | |
logMilestone(error.localizedDescription, file: file, function: function, line: line) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I wrote a blog post about this at https://atomicbird.com/blog/emoji-logging/