Last active
October 30, 2020 16:15
-
-
Save rnkyr/c3366b8746b425bedbcedaf3d21b8580 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| // | |
| // Logger.swift | |
| // | |
| // Created by Roman Kyrylenko on 5/25/17. | |
| // Copyright Β© 2017 pr0ctopus. All rights reserved. | |
| // | |
| import Foundation | |
| #if canImport(Firebase) | |
| import Firebase | |
| #endif | |
| enum LogLevel: Int { | |
| case test, verbose, success, warn, fail | |
| func glyph() -> String { | |
| switch self { | |
| case .test: return "π" | |
| case .verbose: return "π" | |
| case .success: return "π" | |
| case .warn: return "π" | |
| case .fail: return "π" | |
| } | |
| } | |
| } | |
| public enum Logger { | |
| static let currentLogLevel = LogLevel.test | |
| public static func success(_ string: @autoclosure () -> String, file: String = #file, line: Int = #line) { | |
| log(.success, string, file: file, line: line) | |
| } | |
| public static func error(_ string: @autoclosure () -> String, file: String = #file, line: Int = #line) { | |
| log(.fail, string, file: file, line: line) | |
| } | |
| public static func test(_ string: @autoclosure () -> String, file: String = #file, line: Int = #line) { | |
| log(.test, string, file: file, line: line) | |
| } | |
| public static func verbose(_ string: @autoclosure () -> String, file: String = #file, line: Int = #line) { | |
| log(.verbose, string, file: file, line: line) | |
| } | |
| public static func warn(_ string: @autoclosure () -> String, file: String = #file, line: Int = #line) { | |
| log(.warn, string, file: file, line: line) | |
| } | |
| private static func log(_ level: LogLevel, _ string: () -> String, file: String = #file, line: Int = #line) { | |
| #if DEBUG | |
| if currentLogLevel.rawValue > level.rawValue { | |
| return | |
| } | |
| let startIndex = file.range(of: "/", options: .backwards)?.upperBound | |
| let fileName = file[startIndex!...] | |
| print(" \(level.glyph()) [\(NSDate())] \(fileName)(\(line)) | \(string())") | |
| #elseif canImport(Firebase) | |
| if level.rawValue >= LogLevel.warn.rawValue { | |
| let startIndex = file.range(of: "/", options: .backwards)?.upperBound | |
| let fileName = file[startIndex!...] | |
| Crashlytics.crashlytics().log(" \(level.glyph()) [\(NSDate())] \(fileName)(\(line)) | \(string())") | |
| } | |
| #endif | |
| } | |
| } | |
| extension Logger { | |
| public static func error(_ error: Error, file: String = #file, line: Int = #line) { | |
| self.error("\(error)", file: file, line: line) | |
| } | |
| public static func warn(_ error: Error, file: String = #file, line: Int = #line) { | |
| self.warn("\(error)", file: file, line: line) | |
| } | |
| } | |
| extension Logger { | |
| static func assert(_ condition: @autoclosure () -> Bool, _ message: @autoclosure () -> String, file: StaticString = #file, line: UInt = #line) { | |
| #if DEBUG | |
| Swift.assert(condition(), message(), file: file, line: line) | |
| #endif | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment