|
| 1 | +// |
| 2 | +// NKLogger.swift |
| 3 | +// NetworkKit |
| 4 | +// |
| 5 | +// Created by Raghav Ahuja on 18/10/19. |
| 6 | +// Copyright © 2019 Raghav Ahuja. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import Foundation |
| 10 | + |
| 11 | +final public class NKLogger { |
| 12 | + |
| 13 | + /// Allows Logs to be Printed in Debug Console. |
| 14 | + /// Default value is `true` |
| 15 | + public var isLoggingEnabled: Bool = true |
| 16 | + |
| 17 | + public static let `default` = NKLogger() |
| 18 | + |
| 19 | + /** |
| 20 | + Creates a `NKLogger`. |
| 21 | + */ |
| 22 | + public init() { } |
| 23 | + |
| 24 | + /** |
| 25 | + Writes the textual representations of the given items into the standard output. |
| 26 | + |
| 27 | + - parameter items: Zero or more items to print.. |
| 28 | + - parameter separator: A string to print between each item. The default is a single space (" "). |
| 29 | + - parameter terminator: The string to print after all items have been printed. The default is a newline ("\n"). |
| 30 | + |
| 31 | + */ |
| 32 | + func print(_ items: Any..., separator: String = " ", terminator: String = "\n") { |
| 33 | + #if DEBUG |
| 34 | + guard NKConfiguration.allowLoggingOnAllSessions, isLoggingEnabled else { return } |
| 35 | + Swift.print(items, separator: separator, terminator: terminator) |
| 36 | + #endif |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + Writes the textual representations of the given items most suitable for debugging into the standard output. |
| 41 | + |
| 42 | + - parameter items: Zero or more items to print. |
| 43 | + - parameter separator: A string to print between each item. The default is a single space (" "). |
| 44 | + - parameter terminator: The string to print after all items have been printed. The default is a newline ("\n"). |
| 45 | + |
| 46 | + */ |
| 47 | + func debugPrint(_ items: Any..., separator: String = " ", terminator: String = "\n") { |
| 48 | + #if DEBUG |
| 49 | + guard NKConfiguration.allowLoggingOnAllSessions, isLoggingEnabled else { return } |
| 50 | + Swift.debugPrint(items, separator: separator, terminator: terminator) |
| 51 | + #endif |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + Handles APIRequest logging sent by the `NetworkKit`. |
| 56 | + |
| 57 | + - parameter request: URLRequest |
| 58 | + - parameter apiName: API name. |
| 59 | + */ |
| 60 | + func logAPIRequest(request: URLRequest?, apiName: String?) { |
| 61 | + #if DEBUG |
| 62 | + guard NKConfiguration.allowLoggingOnAllSessions, isLoggingEnabled else { return } |
| 63 | + |
| 64 | + Swift.print( |
| 65 | + """ |
| 66 | + ------------------------------------------------------------ |
| 67 | + API Call Request for: |
| 68 | + Name: \(apiName ?? "nil") |
| 69 | + \(request?.debugDescription ?? "") |
| 70 | + |
| 71 | + """ |
| 72 | + ) |
| 73 | + #endif |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + Print JSON sent by the `NetworkKit`. |
| 78 | + |
| 79 | + - parameter data: Input Type to be printed |
| 80 | + - parameter apiName: API name. |
| 81 | + */ |
| 82 | + func printJSON<Input>(data: Input, apiName: String) { |
| 83 | + #if DEBUG |
| 84 | + guard NKConfiguration.allowLoggingOnAllSessions, isLoggingEnabled else { return } |
| 85 | + guard let data = data as? Data else { |
| 86 | + return |
| 87 | + } |
| 88 | + |
| 89 | + do { |
| 90 | + let object = try JSONSerialization.jsonObject(with: data, options: []) |
| 91 | + let newData = try JSONSerialization.data(withJSONObject: object, options: .prettyPrinted) |
| 92 | + |
| 93 | +// Swift.print( |
| 94 | +// """ |
| 95 | +// ------------------------------------------------------------ |
| 96 | +// Printing JSON for: |
| 97 | +// API Name: \(apiName) |
| 98 | +// JSON: |
| 99 | +// |
| 100 | + // """) |
| 101 | + Swift.print(""" |
| 102 | + ------------------------------------------------------------ |
| 103 | + JSON: |
| 104 | + |
| 105 | + """) |
| 106 | + Swift.print(String(data: newData, encoding: .utf8) ?? "nil") |
| 107 | + Swift.print("------------------------------------------------------------") |
| 108 | + |
| 109 | + } catch { |
| 110 | + |
| 111 | + } |
| 112 | + #endif |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + Handles errors sent by the `NetworkKit`. |
| 117 | + |
| 118 | + - parameter error: Error occurred. |
| 119 | + - parameter file: Source file name. |
| 120 | + - parameter line: Source line number. |
| 121 | + - parameter function: Source function name. |
| 122 | + */ |
| 123 | + @inline(__always) |
| 124 | + func log(error: Error, file: StaticString = #file, line: UInt = #line, function: StaticString = #function) { |
| 125 | + #if DEBUG |
| 126 | + guard NKConfiguration.allowLoggingOnAllSessions, isLoggingEnabled else { return } |
| 127 | + |
| 128 | + Swift.print("⚠️ [NetworkKit: Error] \((String(describing: file) as NSString).lastPathComponent):\(line) \(function)\n ↪︎ \(error as NSError)\n") |
| 129 | + #endif |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + Handles assertions made throughout the `NetworkKit`. |
| 134 | + |
| 135 | + - parameter condition: Assertion condition. |
| 136 | + - parameter message: Assertion failure message. |
| 137 | + - parameter file: Source file name. |
| 138 | + - parameter line: Source line number. |
| 139 | + - parameter function: Source function name. |
| 140 | + */ |
| 141 | + @inline(__always) |
| 142 | + func assert(_ condition: @autoclosure () -> Bool, _ message: @autoclosure () -> String, file: StaticString = #file, line: UInt = #line, function: StaticString = #function) { |
| 143 | + |
| 144 | + #if DEBUG |
| 145 | + let condition = condition() |
| 146 | + |
| 147 | + if condition { return } |
| 148 | + |
| 149 | + let message = message() |
| 150 | + |
| 151 | + Swift.print("❗ [NetworkKit: Assertion Failure] \((String(describing: file) as NSString).lastPathComponent):\(line) \(function)\n ↪︎ \(message)\n") |
| 152 | + Swift.assert(condition, message, file: file, line: line) |
| 153 | + #endif |
| 154 | + } |
| 155 | + |
| 156 | + /** |
| 157 | + Handles assertion failures made throughout the `NetworkKit`. |
| 158 | + |
| 159 | + - parameter message: Assertion failure message. |
| 160 | + - parameter file: Source file name. |
| 161 | + - parameter line: Source line number. |
| 162 | + - parameter function: Source function name. |
| 163 | + */ |
| 164 | + @inlinable public func assertionFailure(_ message: @autoclosure () -> String = String(), file: StaticString = #file, line: UInt = #line, function: StaticString = #function) { |
| 165 | + let message = message() |
| 166 | + Swift.print("❗ [NetworkKit: Assertion Failure] \((String(describing: file) as NSString).lastPathComponent):\(line) \(function)\n ↪︎ \(message)\n") |
| 167 | + Swift.assertionFailure(message, file: file, line: line) |
| 168 | + } |
| 169 | + |
| 170 | + /** |
| 171 | + Handles precondition failures made throughout the `NetworkKit`. |
| 172 | + |
| 173 | + - parameter message: Assertion failure message. |
| 174 | + - parameter file: Source file name. |
| 175 | + - parameter line: Source line number. |
| 176 | + - parameter function: Source function name. |
| 177 | + */ |
| 178 | + @inlinable public func preconditionFailure(_ message: @autoclosure () -> String = String(), file: StaticString = #file, line: UInt = #line, function: StaticString = #function) -> Never { |
| 179 | + let message = message() |
| 180 | + Swift.print("❗ [NetworkKit: Assertion Failure] \((String(describing: file) as NSString).lastPathComponent):\(line) \(function)\n ↪︎ \(message)\n") |
| 181 | + Swift.preconditionFailure(message, file: file, line: line) |
| 182 | + } |
| 183 | + |
| 184 | + /** |
| 185 | + Handles preconditions made throughout the `NetworkKit`. |
| 186 | + |
| 187 | + - parameter condition: Precondition to be satisfied. |
| 188 | + - parameter message: Precondition failure message. |
| 189 | + - parameter file: Source file name. |
| 190 | + - parameter line: Source line number. |
| 191 | + - parameter function: Source function name. |
| 192 | + */ |
| 193 | + @inline(__always) |
| 194 | + func precondition(_ condition: @autoclosure () -> Bool, _ message: @autoclosure () -> String, file: StaticString = #file, line: UInt = #line, function: StaticString = #function) { |
| 195 | + |
| 196 | + #if DEBUG |
| 197 | + let condition = condition() |
| 198 | + |
| 199 | + if condition { return } |
| 200 | + |
| 201 | + let message = message() |
| 202 | + |
| 203 | + Swift.print("❗ [NetworkKit: Precondition Failure] \((String(describing: file) as NSString).lastPathComponent):\(line) \(function)\n ↪︎ \(message)\n") |
| 204 | + Swift.preconditionFailure(message, file: file, line: line) |
| 205 | + #endif |
| 206 | + } |
| 207 | + |
| 208 | + /** |
| 209 | + Handles fatal errors made throughout the `NetworkKit`. |
| 210 | + - Important: Implementers should guarantee that this function doesn't return, either by calling another `Never` function such as `fatalError()` or `abort()`, or by raising an exception. |
| 211 | + |
| 212 | + - parameter message: Fatal error message. |
| 213 | + - parameter file: Source file name. |
| 214 | + - parameter line: Source line number. |
| 215 | + - parameter function: Source function name. |
| 216 | + */ |
| 217 | + @inline(__always) |
| 218 | + func fatalError(_ message: @autoclosure () -> String, file: StaticString = #file, line: UInt = #line, function: StaticString = #function) -> Never { |
| 219 | + |
| 220 | + #if DEBUG |
| 221 | + let message = message() |
| 222 | + Swift.print("❗ [NetworkKit: Fatal Error] \((String(describing: file) as NSString).lastPathComponent):\(line) \(function)\n ↪︎ \(message)\n") |
| 223 | + Swift.fatalError(message, file: file, line: line) |
| 224 | + #endif |
| 225 | + } |
| 226 | +} |
0 commit comments