This repository has been archived by the owner on May 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 444
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
No Bug: Brave Talk logs update (#8681)
- Loading branch information
Showing
3 changed files
with
79 additions
and
66 deletions.
There are no files selected for viewing
77 changes: 77 additions & 0 deletions
77
Sources/Brave/Frontend/Settings/Debug/BraveTalkLogsView.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// Copyright 2024 The Brave Authors. All rights reserved. | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
import Foundation | ||
import SwiftUI | ||
import os.log | ||
import OSLog | ||
|
||
struct BraveTalkLogsView: View { | ||
@State private var logs: String = "" | ||
@State private var isLoading: Bool = true | ||
@State private var showShareSheet: Bool = false | ||
@State private var fileURL: URL? | ||
|
||
var body: some View { | ||
VStack { | ||
if isLoading { | ||
ProgressView() | ||
} else { | ||
ScrollView { | ||
Text(logs.isEmpty ? "No logs" : logs) | ||
.padding() | ||
} | ||
} | ||
} | ||
.navigationBarItems(trailing: ShareButton()) | ||
.task { | ||
logs = await getLogs() | ||
fileURL = createTemporaryLogFile() | ||
isLoading = false | ||
} | ||
} | ||
|
||
@ViewBuilder private func ShareButton() -> some View { | ||
if #available(iOS 16, *) { | ||
ShareLink(item: fileURL ?? URL(string: "disabled")!) | ||
.disabled(fileURL == nil || logs.isEmpty) | ||
} else { | ||
EmptyView() | ||
} | ||
} | ||
|
||
private func createTemporaryLogFile() -> URL { | ||
let temporaryDirectoryURL = FileManager.default.temporaryDirectory | ||
let logFileURL = temporaryDirectoryURL.appendingPathComponent("Logs.txt") | ||
|
||
try? logs.write(to: logFileURL, atomically: true, encoding: .utf8) | ||
return logFileURL | ||
} | ||
|
||
private func getLogs() async -> String { | ||
do { | ||
let store = try OSLogStore(scope: .currentProcessIdentifier) | ||
|
||
return try store | ||
.getEntries() | ||
.compactMap { $0 as? OSLogEntryLog } | ||
.filter { $0.category == "BraveTalk" && $0.subsystem == Bundle.main.bundleIdentifier } | ||
.map { "[\($0.date.formatted())] \($0.composedMessage)" } | ||
.joined(separator: "\n") | ||
} catch { | ||
Logger.module.error("\(error.localizedDescription, privacy: .public)") | ||
} | ||
|
||
return "" | ||
} | ||
} | ||
|
||
#if DEBUG | ||
struct BraveTalkLogsView_Previews: PreviewProvider { | ||
static var previews: some View { | ||
BraveTalkLogsView() | ||
} | ||
} | ||
#endif |
65 changes: 0 additions & 65 deletions
65
Sources/Brave/Frontend/Settings/Debug/BraveTalkLogsViewController.swift
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters