mirror of
https://github.com/FBukovina/Atoll.git
synced 2026-10-27 23:24:32 +00:00
77 lines
No EOL
2.2 KiB
Swift
77 lines
No EOL
2.2 KiB
Swift
import Foundation
|
|
import SwiftUI
|
|
|
|
enum LogCategory: String {
|
|
case lifecycle = "🔄"
|
|
case memory = "💾"
|
|
case performance = "⚡️"
|
|
case ui = "🎨"
|
|
case network = "🌐"
|
|
case error = "❌"
|
|
case warning = "⚠️"
|
|
case success = "✅"
|
|
case debug = "🔍"
|
|
}
|
|
|
|
struct Logger {
|
|
static func log(
|
|
_ message: String,
|
|
category: LogCategory,
|
|
file: String = #file,
|
|
function: String = #function,
|
|
line: Int = #line
|
|
) {
|
|
let fileName = (file as NSString).lastPathComponent
|
|
let timestamp = ISO8601DateFormatter().string(from: Date())
|
|
print("\(category.rawValue) [\(timestamp)] [\(fileName):\(line)] \(function) - \(message)")
|
|
}
|
|
|
|
static func trackMemory(
|
|
file: String = #file,
|
|
function: String = #function,
|
|
line: Int = #line
|
|
) {
|
|
var info = mach_task_basic_info()
|
|
var count = mach_msg_type_number_t(MemoryLayout<mach_task_basic_info>.size)/4
|
|
|
|
let kerr: kern_return_t = withUnsafeMutablePointer(to: &info) {
|
|
$0.withMemoryRebound(to: integer_t.self, capacity: 1) {
|
|
task_info(mach_task_self_,
|
|
task_flavor_t(MACH_TASK_BASIC_INFO),
|
|
$0,
|
|
&count)
|
|
}
|
|
}
|
|
|
|
if kerr == KERN_SUCCESS {
|
|
let usedMB = Double(info.resident_size) / 1024.0 / 1024.0
|
|
log(String(format: "Memory used: %.2f MB", usedMB),
|
|
category: .memory,
|
|
file: file,
|
|
function: function,
|
|
line: line)
|
|
}
|
|
}
|
|
}
|
|
|
|
extension View {
|
|
func trackLifecycle(_ identifier: String) -> some View {
|
|
self.modifier(ViewLifecycleTracker(identifier: identifier))
|
|
}
|
|
}
|
|
|
|
struct ViewLifecycleTracker: ViewModifier {
|
|
let identifier: String
|
|
|
|
func body(content: Content) -> some View {
|
|
content
|
|
.onAppear {
|
|
Logger.log("\(identifier) appeared", category: .lifecycle)
|
|
Logger.trackMemory()
|
|
}
|
|
.onDisappear {
|
|
Logger.log("\(identifier) disappeared", category: .lifecycle)
|
|
Logger.trackMemory()
|
|
}
|
|
}
|
|
} |