DEV Community

Cover image for Pretty Print, please
emin
emin

Posted on

 

Pretty Print, please

When doing some light debugging most of us tend reach for the good old print() statement right? Yeah, it's just me πŸ˜‰

image

You might add some &&& or === to make your print REALLY stand out from all that output, but after a while it gets really hard to find anything in there. That is where this helper method comes in!

And also, EMOJIS! πŸ‘

image


I found this awesome post by Andyy Hope where he really goes into detail how everything works, so if you are interested I recommend going over that whole post. It has 2 parts, and is WELL worth a read! 🀘

For all of you anxious to try it out right away, behold this helper method. ⬇️


Create a new Swift file in your project, and paste this code:

import Foundation

enum log {
    case ln(_: String)
    case obj(_: String, _: Any)
    case error(_: Error)
    case url(_: String)
    case any(_: Any)
    case date(_: NSDate)
}

postfix operator /

postfix func / (target: log?) {
    guard let target = target else { return }

    func log<T>(_ emoji: String, _ string: String = "", _ object: T) {
        #if DEBUG
        print(emoji + "\(string)β†’" + " " + "\(object)")
        #endif
    }

    switch target {
    case .ln(let line):
        log("✏️", "", line)

    case .obj(let string, let obj):
        log("πŸ“¦", string, obj)

    case .error(let error):
        log("❗️❗️❗️", "", error)

    case .url(let url):
        log("πŸ”—", "", url)

    case .any(let any):
        log("βšͺ️", "", any)

    case .date(let date):
        log("⏰", "", date)
    }
}
Enter fullscreen mode Exit fullscreen mode

To make use of that "DEBUG" flag, find the following:

  • Go to your Project Settings
  • Open Build Settings
  • In the search, type "Flag"
  • Open Swift Compiler - Custom Flags and Active Compilation Conditions
  • For Debug insert a value -> "DEBUG"

image


And now for the fun part! Anywhere in your code use this

log.ln("So long PRINT! Been nice knowin' ya!")/
Enter fullscreen mode Exit fullscreen mode

That prints: βœοΈβ†’ So long PRINT! Been nice knowin' ya!


!Important that little / at the very end is very important. That is a postfix operator and without it the log won't print out.

Andyy goes over that in detail in his post so head on there if you are interested how all that works.
You can even use your own character but Andyy recommended it and who am I to doubt Andyy πŸ˜…


Other available logs

Input Output
log.ln("Text")/
βœοΈβ†’ Text
log.obj("My Object", Object)/
πŸ“¦ My Object β†’ Object Properties
log.error(error)/
❗️❗️❗️→ Error Properties
log.url("undeadpixel.dev")/
πŸ”— β†’ undeadpixel.dev
log.date(Date())/
⏰ β†’ 2020-04-25 17:45:14 +0000
log.any("Whatever you want")/
βšͺ️ β†’ Whatever You want

It is also quite easy to spot the logs that you added. Sometimes when I want to search over the project for a specific print and I type "print(" in the searchBar, all of the prints from every file are there. Now number of results is far narrower so my own code is easier to spot! 😳

That's it! Feel free to modify the helper and add your own emoticons or customise it to your hearts content!


Thank you for reading, you superb person! β™₯️

Get in touch on Twitter

Cheers! 🍻

Top comments (0)