StikDebug/StikJIT/Utilities/LogManager.swift
Stephen f3d2dec2e5
Semi-Rewrite – Major Refactor & Codebase Cleanup (#328)
* Change default UPLOAD_IPA value to 'false'

* Part 1 of the great debloat

* Remove Mini Tools, adopt native alerts in StikJITApp, and update HomeView UI

* Refactor views to use native SwiftUI alerts instead of CustomErrorView

* Update ProfileView to use minimal List UI style with native alerts

* Fix up homeview

* Squash: Redesign HomeView, remove Feature Flags, enable all tabs, refactor alerts, and updates

- Redesign HomeView to a simpler style with 'Enable JIT' action.
- Remove 'Help & Info' and 'Quick Launch' from Home.
- Fix DDI false positive by starting heartbeat on HomeView appear.
- Restore robust DDI checks and update app name to 'StikDebug'.
- Remove FeatureFlags and Beta tags; enable 'profiles', 'processes', 'location' tabs by default.
- Remove 'Devices' tab; add 'Target Device IP' field in Settings.
- Hardcode 'Settings' tab to 4th position.
- Refactor 'SettingsView' tab customization to be reorder-only.
- Remove 'Appearance' theme section from Settings.
- Refactor custom error cards to use native SwiftUI/UIKit alerts across the app (HomeView, ConsoleLogsView, ProfileView, etc).
- Restore JS callback support and script execution logic in HomeView.
- Add necessary Color extensions.
- Update project version to 3.0.0.

* Clean up: Remove In-App Purchases, update checker, and unused IPA installer code

* delete more bloat

* fix build issue

* Remove widget to save on App IDs

* Attempt heartbeat restart when Enable JIT button is pressed

* Get rid of PIP

* Update Tab Naming

* remove more bloat

* final changes for today

* Remove unused packages

* UX Improvements

* revert bundle id

* Update README.md

* remove old idevice

* add updated idevice

* Fix ddi false positive errors and add templates for GitHub stuff

* Remove more unused stuff.

* Fix links and update README content

* Update compatibility table in README.md

* Improve performance of console logs

* Remove old code

* Change default UPLOAD_IPA value to 'true'

* remove the welcome sheet

* import support for .mobiledevicepair

* Add back LC JIT stuff

* Fix heartbeat issues with LC JIT

* update the build yml for nightly stuff

* Update README.md

* Update README.md

* Update GitHub Actions workflow for IPA build

* Update GitHub Actions workflow for nightly builds

* Add URL scheme to kill tasks stikjit://kill-process?pid=1234

* fix build issues

* Added App Intents

* Unify the error handling style across the app
2026-02-25 13:44:00 -05:00

102 lines
3.2 KiB
Swift

//
// LogManager.swift
// StikJIT
//
// Created by neoarz on 3/29/25.
//
import Foundation
final class LogManager: ObservableObject {
static let shared = LogManager()
@Published var logs: [LogEntry] = []
@Published var errorCount: Int = 0
struct LogEntry: Identifiable, Sendable {
let id: UUID
let timestamp: Date
let type: LogType
let message: String
enum LogType: String, Sendable {
case info = "INFO"
case error = "ERROR"
case debug = "DEBUG"
case warning = "WARNING"
}
init(timestamp: Date, type: LogType, message: String) {
self.id = UUID()
self.timestamp = timestamp
self.type = type
self.message = message
}
}
private static let redundantPrefixes: [String] = [
"Info: ", "INFO: ", "Information: ",
"Error: ", "ERROR: ", "ERR: ",
"Debug: ", "DEBUG: ", "DBG: ",
"Warning: ", "WARN: ", "WARNING: "
]
private init() {
addInfoLog("StikJIT starting up")
addInfoLog("Initializing environment")
}
func addLog(message: String, type: LogEntry.LogType) {
let clean = Self.redundantPrefixes
.first(where: { message.hasPrefix($0) })
.map { String(message.dropFirst($0.count)) } ?? message
DispatchQueue.main.async {
self.logs.append(LogEntry(timestamp: Date(), type: type, message: clean))
if type == .error { self.errorCount += 1 }
if self.logs.count > 1000 { self.logs.removeFirst(100) }
}
}
func addInfoLog(_ message: String) { addLog(message: message, type: .info) }
func addErrorLog(_ message: String) { addLog(message: message, type: .error) }
func addDebugLog(_ message: String) { addLog(message: message, type: .debug) }
func addWarningLog(_ message: String) { addLog(message: message, type: .warning) }
func setLogs(_ entries: [LogEntry]) {
DispatchQueue.main.async {
self.logs = entries
self.errorCount = entries.filter { $0.type == .error }.count
}
}
func appendLogs(_ entries: [LogEntry], maxTotal: Int = 1000) {
DispatchQueue.main.async {
self.logs.append(contentsOf: entries)
self.errorCount += entries.filter { $0.type == .error }.count
if self.logs.count > maxTotal {
let excess = self.logs.count - maxTotal
let removed = self.logs.prefix(excess)
self.logs.removeFirst(excess)
let removedErrors = removed.filter { $0.type == .error }.count
self.errorCount = max(0, self.errorCount - removedErrors)
}
}
}
func clearLogs() {
DispatchQueue.main.async {
self.logs.removeAll()
self.errorCount = 0
}
}
func removeOldestLogs(count: Int) {
DispatchQueue.main.async {
let removed = self.logs.prefix(count)
self.logs.removeFirst(count)
let removedErrors = removed.filter { $0.type == .error }.count
self.errorCount = max(0, self.errorCount - removedErrors)
}
}
}