Add export for logs
This commit is contained in:
@@ -1,6 +1,12 @@
|
||||
import Combine
|
||||
import Foundation
|
||||
import Library
|
||||
import SwiftUI
|
||||
#if canImport(UIKit)
|
||||
import UIKit
|
||||
#elseif canImport(AppKit)
|
||||
import AppKit
|
||||
#endif
|
||||
|
||||
@MainActor
|
||||
public class LogViewModel: ObservableObject {
|
||||
@@ -9,6 +15,9 @@ public class LogViewModel: ObservableObject {
|
||||
@Published public var searchText = ""
|
||||
@Published public var isSearching = false
|
||||
@Published public var filteredLogs: [LogEntry] = []
|
||||
@Published public var alert: Alert?
|
||||
@Published public var showFileExporter = false
|
||||
@Published public var logFileURL: URL?
|
||||
|
||||
private let commandClient: CommandClient
|
||||
|
||||
@@ -53,4 +62,35 @@ public class LogViewModel: ObservableObject {
|
||||
commandClient.logList.removeAll()
|
||||
isPaused = false
|
||||
}
|
||||
|
||||
#if !os(tvOS)
|
||||
public func getLogsText() -> String {
|
||||
filteredLogs.map(\.message).joined(separator: "\n")
|
||||
}
|
||||
|
||||
public func copyToClipboard() {
|
||||
let text = getLogsText()
|
||||
#if os(iOS)
|
||||
UIPasteboard.general.string = text
|
||||
#elseif os(macOS)
|
||||
NSPasteboard.general.clearContents()
|
||||
NSPasteboard.general.setString(text, forType: .string)
|
||||
#endif
|
||||
}
|
||||
|
||||
public func prepareLogFile() {
|
||||
do {
|
||||
let text = getLogsText()
|
||||
let dateFormatter = DateFormatter()
|
||||
dateFormatter.dateFormat = "yyyy-MM-dd-HH:mm:ss"
|
||||
let dateString = dateFormatter.string(from: Date())
|
||||
let tempDirectory = FileManager.default.temporaryDirectory
|
||||
let fileURL = tempDirectory.appendingPathComponent("logs-\(dateString).txt")
|
||||
try text.write(to: fileURL, atomically: true, encoding: .utf8)
|
||||
logFileURL = fileURL
|
||||
} catch {
|
||||
alert = Alert(error)
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user