Skip to content

Commit

Permalink
No Bug: Brave Talk logs update (brave/brave-ios#8681)
Browse files Browse the repository at this point in the history
  • Loading branch information
iccub authored Jan 22, 2024
1 parent a470306 commit 140d84c
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 66 deletions.
77 changes: 77 additions & 0 deletions Sources/Brave/Frontend/Settings/Debug/BraveTalkLogsView.swift
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

This file was deleted.

3 changes: 2 additions & 1 deletion Sources/Brave/Frontend/Settings/SettingsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -864,7 +864,8 @@ class SettingsViewController: TableViewController {
Row(
text: "Brave Talk Logs",
selection: { [unowned self] in
self.navigationController?.pushViewController(BraveTalkLogsViewController(), animated: true)
let controller = UIHostingController(rootView: BraveTalkLogsView())
self.navigationController?.pushViewController(controller, animated: true)
}, accessory: .disclosureIndicator, cellClass: MultilineValue1Cell.self),
Row(
text: "Retention Preferences Debug Menu",
Expand Down

0 comments on commit 140d84c

Please sign in to comment.