Log console helper


#1

Визуализация консольного вывода, может кому и пригодится

enum Log {

    static let level: Level = .verbose

    enum Level: Int {
        case none    = 0
        case verbose = 3
        case info    = 2
        case error   = 1
        case debug   = 4

        func shouldLog(_ level: Level) -> Bool {
            return level.rawValue <= self.rawValue
        }
    }

    static func assertFailure(_ message: @autoclosure () -> String,
                              file: StaticString = #file,
                              line: UInt = #line) {
        #if DEBUG
        assertionFailure(message(), file: file, line: line)
        #endif
    }

    static func assert(_ condition: @autoclosure () -> Bool,
                       _ message: @autoclosure () -> String,
                       file: StaticString = #file,
                       line: UInt = #line) {
        if !condition() {
            assertFailure(message(), file: file, line: line)
        }
    }

    static func verbose(_ message: @autoclosure () -> String,
                        file: StaticString = #file,
                        line: UInt = #line) {
        #if DEBUG
        if level.shouldLog(.verbose) {
            print("📘 [VERBOSE] \(message())")
        }
        #endif
    }

    static func info(_ message: @autoclosure () -> String,
                     file: StaticString = #file,
                     line: UInt = #line) {
        #if DEBUG
        if level.shouldLog(.info) {
            print("📗 [INFO] \(message())")
        }
        #endif
    }

    static func debug(_ message: @autoclosure () -> String,
                      file: StaticString = #file,
                      line: UInt = #line) {
        #if DEBUG
        if level.shouldLog(.error) {
            print("📙 [DEBUG] \(message())")
        }
        #endif
    }

    static func error(_ message: @autoclosure () -> String,
                      file: StaticString = #file,
                      line: UInt = #line) {
        #if DEBUG
        if level.shouldLog(.error) {
            print("📕 [ERROR] \(message())")
        }
        #endif
    }

}

#2

неплохо бы добавить пример использования.


#3

Log.error(error.localizedDescription)

Там все просто.