diff --git a/Model/Player/Backends/MPVClient.swift b/Model/Player/Backends/MPVClient.swift index 7d86262e..23a2d878 100644 --- a/Model/Player/Backends/MPVClient.swift +++ b/Model/Player/Backends/MPVClient.swift @@ -8,6 +8,10 @@ import Logging #endif final class MPVClient: ObservableObject { + static var logFile: URL { + YatteeApp.logsDirectory.appendingPathComponent("yattee-\(YatteeApp.build)-mpv-log.txt") + } + private var logger = Logger(label: "mpv-client") var mpv: OpaquePointer! @@ -36,11 +40,20 @@ final class MPVClient: ObservableObject { exit(1) } - #if DEBUG + if Defaults[.mpvEnableLogging] { + checkError(mpv_set_option_string( + mpv, + "log-file", + Self.logFile.absoluteString.replacingOccurrences(of: "file://", with: "") + )) checkError(mpv_request_log_messages(mpv, "debug")) - #else - checkError(mpv_request_log_messages(mpv, "warn")) - #endif + } else { + #if DEBUG + checkError(mpv_request_log_messages(mpv, "debug")) + #else + checkError(mpv_request_log_messages(mpv, "no")) + #endif + } #if os(macOS) checkError(mpv_set_option_string(mpv, "input-media-keys", "yes")) diff --git a/Shared/Defaults.swift b/Shared/Defaults.swift index f185f6b9..a337a4a7 100644 --- a/Shared/Defaults.swift +++ b/Shared/Defaults.swift @@ -101,6 +101,7 @@ extension Defaults.Keys { static let mpvCacheSecs = Key("mpvCacheSecs", default: "20") static let mpvCachePauseWait = Key("mpvCachePauseWait", default: "2") + static let mpvEnableLogging = Key("mpvEnableLogging", default: false) } enum ResolutionSetting: String, CaseIterable, Defaults.Serializable { diff --git a/Shared/Settings/AdvancedSettings.swift b/Shared/Settings/AdvancedSettings.swift index 2d3ac8d2..f8ca19a5 100644 --- a/Shared/Settings/AdvancedSettings.swift +++ b/Shared/Settings/AdvancedSettings.swift @@ -6,6 +6,12 @@ struct AdvancedSettings: View { @Default(.showMPVPlaybackStats) private var showMPVPlaybackStats @Default(.mpvCacheSecs) private var mpvCacheSecs @Default(.mpvCachePauseWait) private var mpvCachePauseWait + @Default(.mpvEnableLogging) private var mpvEnableLogging + + @EnvironmentObject private var player + + @State private var presentingShareSheet = false + @State private var filesToShare = [MPVClient.logFile] var body: some View { VStack(alignment: .leading) { @@ -17,6 +23,10 @@ struct AdvancedSettings: View { advancedSettings } #if os(iOS) + .sheet(isPresented: $presentingShareSheet) { + ShareSheet(activityItems: filesToShare) + .id("logs-\(filesToShare.count)") + } .listStyle(.insetGrouped) #endif #endif @@ -27,9 +37,29 @@ struct AdvancedSettings: View { .navigationTitle("Advanced") } + var logButton: some View { + Button { + #if os(macOS) + NSWorkspace.shared.selectFile(MPVClient.logFile.path, inFileViewerRootedAtPath: YatteeApp.logsDirectory.path) + #else + presentingShareSheet = true + #endif + } label: { + #if os(macOS) + let labelText = "Open logs in Finder" + #else + let labelText = "Share Logs..." + #endif + Text(labelText) + } + } + @ViewBuilder var advancedSettings: some View { Section(header: SettingsHeader(text: "MPV"), footer: mpvFooter) { showMPVPlaybackStatsToggle + #if !os(tvOS) + mpvEnableLoggingToggle + #endif HStack { Text("cache-secs") @@ -44,6 +74,10 @@ struct AdvancedSettings: View { TextField("cache-pause-wait", text: $mpvCachePauseWait) } .multilineTextAlignment(.trailing) + + if mpvEnableLogging { + logButton + } } Section(header: manifestHeader) { @@ -85,6 +119,10 @@ struct AdvancedSettings: View { Toggle("Show playback statistics", isOn: $showMPVPlaybackStats) } + var mpvEnableLoggingToggle: some View { + Toggle("Enable logging", isOn: $mpvEnableLogging) + } + #if os(macOS) private func onHover(_ inside: Bool) { if inside { diff --git a/Shared/Settings/SettingsView.swift b/Shared/Settings/SettingsView.swift index d8d87ce4..1cc0cb8f 100644 --- a/Shared/Settings/SettingsView.swift +++ b/Shared/Settings/SettingsView.swift @@ -226,7 +226,7 @@ struct SettingsView: View { case .locations: return 480 case .advanced: - return 300 + return 320 case .help: return 600 } diff --git a/Shared/YatteeApp.swift b/Shared/YatteeApp.swift index d6e19d15..1cff89de 100644 --- a/Shared/YatteeApp.swift +++ b/Shared/YatteeApp.swift @@ -20,6 +20,10 @@ struct YatteeApp: App { ProcessInfo.processInfo.environment["XCODE_RUNNING_FOR_PREVIEWS"] == "1" } + static var logsDirectory: URL { + URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true) + } + #if os(macOS) @NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate #elseif os(iOS)