Skip to content

Instantly share code, notes, and snippets.

@aheze
Created May 18, 2023 03:15
Show Gist options
  • Select an option

  • Save aheze/a845b06837cfa615f757ac14d129db28 to your computer and use it in GitHub Desktop.

Select an option

Save aheze/a845b06837cfa615f757ac14d129db28 to your computer and use it in GitHub Desktop.
Custom logging system
enum Debug {
enum Level: String {
case error = "Error"
case warning = "Warning"
case log = "Log"
case network = "Network"
case success = "Success"
case note = "Note"
case sound = "Sound"
var critical: Bool {
switch self {
case .error, .warning:
return true
default:
return false
}
}
var emoji: String {
switch self {
case .error:
return "⁉️"
case .warning:
return "⚠️"
case .log:
return "ℹ️"
case .network:
return "🛜"
case .success:
return "✅"
case .note:
return "🔵"
case .sound:
return "🎸"
}
}
}
struct Log {
var date = Date.now
var level: Level
var string: String
var description: String {
"[Debug-\(level.emoji)-\(level)] - \(string)"
}
var fullDescription: String {
"[\(date.string)-\(level.emoji)-\(level)] - \(string)"
}
}
static var logs = [Log]()
static var logsString: String {
"""
Logs for \(Date.now.string)
Device properties:
name: \(UIDevice.current.name)
model: \(UIDevice.current.model)
systemName: \(UIDevice.current.systemName)
systemVersion: \(UIDevice.current.systemVersion)
orientation: \(UIDevice.current.orientation)
isFlat: \(UIDevice.current.orientation.isFlat)
isPortrait: \(UIDevice.current.orientation.isPortrait)
isLandscape: \(UIDevice.current.orientation.isLandscape)
\(logs.map { $0.fullDescription }.joined(separator: "\n"))
"""
}
/// print a function
static func log(_ item: Any, level: Level = .log) {
let log = Log(level: level, string: String(describing: item))
Debug.logs.append(log)
print(log.description)
if level.critical {
TabsViewModel.shared?.objectWillChange.send()
}
}
}
extension Date {
static let debugDateFormatter: DateFormatter = {
var debugDateFormatter = DateFormatter()
debugDateFormatter.dateFormat = "MMM d, h:mm a"
return debugDateFormatter
}()
/// string for debug
var string: String {
return Date.debugDateFormatter.string(from: self)
}
}
Debug.log("Hello!", level: .success)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment