2021-10-20 00:31:22 -07:00
|
|
|
import SwiftUI
|
|
|
|
|
import Foundation
|
2021-10-23 10:27:10 -07:00
|
|
|
import UniformTypeIdentifiers
|
2021-10-20 00:31:22 -07:00
|
|
|
|
|
|
|
|
struct MeshLog: View {
|
2021-11-16 10:14:20 -08:00
|
|
|
let logFile = MeshLogger.logFile
|
2021-10-20 00:31:22 -07:00
|
|
|
var text = ""
|
|
|
|
|
@State private var logs = [String]()
|
2021-10-23 10:27:10 -07:00
|
|
|
@State private var isExporting: Bool = false
|
2021-10-24 01:26:04 -07:00
|
|
|
@State private var document: LogDocument = LogDocument(logFile: "MESHTASTIC MESH ACTIVITY LOG\n")
|
2021-11-29 15:59:06 -08:00
|
|
|
|
2021-10-20 00:31:22 -07:00
|
|
|
var body: some View {
|
2023-03-06 10:33:18 -08:00
|
|
|
|
2021-10-20 00:31:22 -07:00
|
|
|
List(logs, id: \.self, rowContent: Text.init)
|
|
|
|
|
.task {
|
|
|
|
|
do {
|
|
|
|
|
let url = logFile!
|
|
|
|
|
logs.removeAll()
|
2022-09-30 17:10:03 -07:00
|
|
|
var lineCount = 0
|
|
|
|
|
let lineLimit = 500
|
|
|
|
|
// Get the number of lines
|
|
|
|
|
for try await _ in url.lines {
|
|
|
|
|
lineCount += 1
|
|
|
|
|
}
|
|
|
|
|
// Set the record to start with if we have more lines than the limit
|
2022-09-30 21:06:52 -07:00
|
|
|
var startingLog = 0
|
2022-09-30 17:10:03 -07:00
|
|
|
if lineCount > lineLimit {
|
|
|
|
|
startingLog = lineCount - lineLimit
|
2021-10-20 00:31:22 -07:00
|
|
|
}
|
2022-09-30 17:10:03 -07:00
|
|
|
var lineNumber = 0
|
|
|
|
|
for try await log in url.lines {
|
|
|
|
|
if lineNumber >= startingLog {
|
|
|
|
|
logs.append(log)
|
|
|
|
|
document.logFile.append("\(log) \n")
|
|
|
|
|
}
|
|
|
|
|
lineNumber += 1
|
|
|
|
|
}
|
|
|
|
|
logs.reverse()
|
2021-10-20 00:31:22 -07:00
|
|
|
} catch {
|
|
|
|
|
// Stop adding logs when an error is thrown
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-10-24 01:26:04 -07:00
|
|
|
.fileExporter(
|
|
|
|
|
isPresented: $isExporting,
|
|
|
|
|
document: document,
|
|
|
|
|
contentType: UTType.plainText,
|
|
|
|
|
defaultFilename: "mesh-activity-log",
|
2021-11-29 17:09:27 -08:00
|
|
|
onCompletion: { result in
|
2021-10-24 01:26:04 -07:00
|
|
|
if case .success = result {
|
|
|
|
|
print("Mesh activity log download: success.")
|
|
|
|
|
} else {
|
|
|
|
|
print("Mesh activity log download \(result).")
|
|
|
|
|
}
|
2021-10-23 10:27:10 -07:00
|
|
|
}
|
2021-10-24 01:26:04 -07:00
|
|
|
)
|
2021-10-20 00:31:22 -07:00
|
|
|
.textSelection(.enabled)
|
2022-12-30 20:04:43 -08:00
|
|
|
.font(.caption)
|
2023-03-06 10:33:18 -08:00
|
|
|
|
2021-11-29 15:59:06 -08:00
|
|
|
HStack(alignment: .center) {
|
2021-10-20 00:31:22 -07:00
|
|
|
Spacer()
|
2022-07-02 23:45:33 -07:00
|
|
|
Button(role: .destructive) {
|
2021-10-20 00:31:22 -07:00
|
|
|
let text = ""
|
|
|
|
|
do {
|
|
|
|
|
try text.write(to: logFile!, atomically: false, encoding: .utf8)
|
|
|
|
|
logs.removeAll()
|
|
|
|
|
} catch {
|
|
|
|
|
print(error)
|
|
|
|
|
}
|
2022-07-02 23:45:33 -07:00
|
|
|
} label: {
|
|
|
|
|
Label("Clear Log", systemImage: "trash.fill")
|
2021-10-20 00:31:22 -07:00
|
|
|
}
|
2022-07-02 23:45:33 -07:00
|
|
|
.buttonStyle(.bordered)
|
|
|
|
|
.buttonBorderShape(.capsule)
|
|
|
|
|
.controlSize(.large)
|
2021-10-20 00:31:22 -07:00
|
|
|
.padding()
|
|
|
|
|
Spacer()
|
2023-03-06 10:33:18 -08:00
|
|
|
|
2022-07-02 23:45:33 -07:00
|
|
|
Button {
|
2021-10-23 10:27:10 -07:00
|
|
|
isExporting = true
|
2022-07-02 23:45:33 -07:00
|
|
|
} label: {
|
2022-07-07 00:29:52 -07:00
|
|
|
Label("Save Log", systemImage: "square.and.arrow.down")
|
2021-10-20 00:31:22 -07:00
|
|
|
}
|
2022-07-02 23:45:33 -07:00
|
|
|
.buttonStyle(.bordered)
|
|
|
|
|
.buttonBorderShape(.capsule)
|
|
|
|
|
.controlSize(.large)
|
2021-10-20 00:31:22 -07:00
|
|
|
.padding()
|
|
|
|
|
Spacer()
|
|
|
|
|
}
|
|
|
|
|
.padding(.bottom, 10)
|
2022-12-30 20:04:43 -08:00
|
|
|
.navigationTitle("mesh.log")
|
2021-10-20 00:31:22 -07:00
|
|
|
}
|
|
|
|
|
}
|