2022-04-12 13:09:14 +00:00
|
|
|
//
|
|
|
|
// DebugLogView.swift
|
|
|
|
// Passepartout
|
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 2/19/22.
|
2024-01-14 13:34:21 +00:00
|
|
|
// Copyright (c) 2024 Davide De Rosa. All rights reserved.
|
2022-04-12 13:09:14 +00:00
|
|
|
//
|
|
|
|
// https://github.com/passepartoutvpn
|
|
|
|
//
|
|
|
|
// This file is part of Passepartout.
|
|
|
|
//
|
|
|
|
// Passepartout is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// Passepartout is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Passepartout. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Combine
|
2022-06-23 21:31:01 +00:00
|
|
|
import PassepartoutLibrary
|
2023-04-04 07:50:45 +00:00
|
|
|
import SwiftUI
|
2022-04-12 13:09:14 +00:00
|
|
|
|
|
|
|
struct DebugLogView: View {
|
2022-10-16 06:33:32 +00:00
|
|
|
private let title: String
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
private let url: URL
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2024-01-07 11:11:16 +00:00
|
|
|
private let shareFilename: String
|
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
private let timer: AnyPublisher<Date, Never>
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
@State private var logLines: [String] = []
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
@State private var isSharing = false
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-10-16 06:33:32 +00:00
|
|
|
private let maxBytes = UInt64(Constants.Log.maxBytes)
|
2022-04-12 13:09:14 +00:00
|
|
|
|
|
|
|
private let appName = Constants.Global.appName
|
|
|
|
|
|
|
|
private let appVersion = Constants.Global.appVersionString
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2024-01-07 11:11:16 +00:00
|
|
|
init(title: String, url: URL, filename: String, refreshInterval: TimeInterval?) {
|
2022-10-16 06:33:32 +00:00
|
|
|
self.title = title
|
2022-04-12 13:09:14 +00:00
|
|
|
self.url = url
|
2024-01-07 11:11:16 +00:00
|
|
|
shareFilename = filename
|
|
|
|
if let refreshInterval {
|
2022-10-16 06:33:32 +00:00
|
|
|
timer = Timer.TimerPublisher(interval: refreshInterval, runLoop: .main, mode: .common)
|
|
|
|
.autoconnect()
|
|
|
|
.eraseToAnyPublisher()
|
|
|
|
} else {
|
|
|
|
timer = Empty(outputType: Date.self, failureType: Never.self)
|
|
|
|
.eraseToAnyPublisher()
|
|
|
|
}
|
2022-04-12 13:09:14 +00:00
|
|
|
}
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
var body: some View {
|
|
|
|
ScrollViewReader { scrollProxy in
|
|
|
|
ScrollView(showsIndicators: true) {
|
|
|
|
contentView
|
2022-04-21 13:56:26 +00:00
|
|
|
}.onAppear {
|
2022-04-12 13:09:14 +00:00
|
|
|
refreshLog(scrollingToLatestWith: scrollProxy)
|
2022-04-21 13:56:26 +00:00
|
|
|
}
|
2022-08-27 20:16:41 +00:00
|
|
|
}
|
2023-12-16 19:58:54 +00:00
|
|
|
#if !os(tvOS)
|
2022-08-27 20:16:41 +00:00
|
|
|
#if targetEnvironment(macCatalyst)
|
|
|
|
.toolbar {
|
|
|
|
Button(action: copyDebugLog) {
|
|
|
|
themeCopyImage.asSystemImage
|
|
|
|
}.disabled(logLines.isEmpty)
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
.toolbar {
|
2022-04-21 13:56:26 +00:00
|
|
|
if !isSharing {
|
|
|
|
Button(action: shareDebugLog) {
|
|
|
|
themeShareImage.asSystemImage
|
|
|
|
}.disabled(logLines.isEmpty)
|
|
|
|
} else {
|
|
|
|
ProgressView()
|
|
|
|
}
|
2023-12-16 19:58:54 +00:00
|
|
|
}
|
|
|
|
.sheet(isPresented: $isSharing, content: sharingActivityView)
|
|
|
|
#endif
|
2022-08-27 20:16:41 +00:00
|
|
|
#endif
|
2022-04-21 13:56:26 +00:00
|
|
|
.edgesIgnoringSafeArea([.leading, .trailing])
|
|
|
|
.onReceive(timer, perform: refreshLog)
|
2022-10-16 06:33:32 +00:00
|
|
|
.navigationTitle(title)
|
2022-04-23 09:42:26 +00:00
|
|
|
.themeDebugLogStyle()
|
2022-04-12 13:09:14 +00:00
|
|
|
}
|
2023-07-03 14:54:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: -
|
2022-04-12 13:09:14 +00:00
|
|
|
|
2023-07-03 14:54:43 +00:00
|
|
|
private extension DebugLogView {
|
|
|
|
var contentView: some View {
|
2022-04-12 13:09:14 +00:00
|
|
|
LazyVStack {
|
|
|
|
ForEach(logLines.indices, id: \.self) {
|
|
|
|
Text(logLines[$0])
|
|
|
|
.frame(maxWidth: .infinity, alignment: .leading)
|
|
|
|
}
|
2023-03-17 20:55:47 +00:00
|
|
|
}// .padding()
|
2022-04-26 19:39:21 +00:00
|
|
|
// TODO: layout, a slight padding would be nice, but it glitches on first touch
|
2022-04-12 13:09:14 +00:00
|
|
|
}
|
2023-03-17 20:55:47 +00:00
|
|
|
|
2023-12-16 19:58:54 +00:00
|
|
|
#if !os(tvOS)
|
2023-07-03 14:54:43 +00:00
|
|
|
func sharingActivityView() -> some View {
|
2022-04-12 13:09:14 +00:00
|
|
|
ActivityView(activityItems: sharingItems)
|
|
|
|
}
|
2023-12-16 19:58:54 +00:00
|
|
|
#endif
|
2022-04-12 13:09:14 +00:00
|
|
|
|
2023-07-03 14:54:43 +00:00
|
|
|
var sharingItems: [Any] {
|
2022-04-12 13:09:14 +00:00
|
|
|
let raw = logLines.joined(separator: "\n")
|
2022-08-27 20:16:41 +00:00
|
|
|
let data = DebugLog(content: raw)
|
|
|
|
.decoratedData(appName, appVersion)
|
2022-04-12 13:09:14 +00:00
|
|
|
|
|
|
|
let path = NSTemporaryDirectory().appending(shareFilename)
|
|
|
|
let url = URL(fileURLWithPath: path)
|
|
|
|
do {
|
|
|
|
try data.write(to: url)
|
|
|
|
return [url]
|
|
|
|
} catch {
|
|
|
|
// highly unlikely to happen
|
|
|
|
assertionFailure("Unable to save temporary debug log file: \(error)")
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-03 14:54:43 +00:00
|
|
|
// MARK: -
|
|
|
|
|
|
|
|
private extension DebugLogView {
|
|
|
|
func refreshLog(_: Date) {
|
|
|
|
refreshLog(scrollingToLatestWith: nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func refreshLog(scrollingToLatestWith scrollProxy: ScrollViewProxy?) {
|
|
|
|
logLines = url.trailingLines(bytes: maxBytes)
|
|
|
|
if let scrollProxy = scrollProxy {
|
|
|
|
scrollToLatestUpdate(scrollProxy)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-16 19:58:54 +00:00
|
|
|
#if !os(tvOS)
|
2023-07-03 14:54:43 +00:00
|
|
|
func shareDebugLog() {
|
|
|
|
guard !logLines.isEmpty else {
|
|
|
|
assertionFailure("Log is empty, why could it share?")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
isSharing = true
|
|
|
|
}
|
|
|
|
|
|
|
|
func copyDebugLog() {
|
2022-08-27 20:16:41 +00:00
|
|
|
guard !logLines.isEmpty else {
|
|
|
|
assertionFailure("Log is empty, why could it copy?")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
let raw = logLines.joined(separator: "\n")
|
|
|
|
let content = DebugLog(content: raw)
|
|
|
|
.decoratedString(appName, appVersion)
|
|
|
|
|
|
|
|
Utils.copyToPasteboard(content)
|
|
|
|
}
|
2023-12-16 19:58:54 +00:00
|
|
|
#endif
|
2022-08-27 20:16:41 +00:00
|
|
|
|
2023-07-03 14:54:43 +00:00
|
|
|
func scrollToLatestUpdate(_ proxy: ScrollViewProxy) {
|
2022-04-12 13:09:14 +00:00
|
|
|
proxy.maybeScrollTo(logLines.count - 1, anchor: .bottomLeading)
|
|
|
|
}
|
|
|
|
}
|