Skip to content
This repository has been archived by the owner on Oct 1, 2024. It is now read-only.

Commit

Permalink
Move log exporting into settings
Browse files Browse the repository at this point in the history
  • Loading branch information
josh committed Apr 24, 2024
1 parent c8ed6e2 commit 6724542
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 25 deletions.
25 changes: 0 additions & 25 deletions Aware/macOS/MenuBar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,36 +90,11 @@ struct MenuBarContentView: View {
var body: some View {
SettingsLink()
.keyboardShortcut(",")
// FIXME: Remove before next release
Button("Export Logs") {
Task<Void, Never>(priority: .background) {
exportLogs()
}
}.keyboardShortcut("e")
Divider()
Button("Quit") {
NSApplication.shared.terminate(nil)
}.keyboardShortcut("q")
}

func exportLogs() {
let store = try! OSLogStore(scope: .currentProcessIdentifier)
let predicate = NSPredicate(format: "subsystem == 'com.awaremac.Aware'")
let date = Date.now.addingTimeInterval(-3600)
let position = store.position(date: date)
let data = try! store
.getEntries(at: position, matching: predicate)
.compactMap { $0 as? OSLogEntryLog }
.map { "[\($0.date.formatted(date: .omitted, time: .standard))] [\($0.category)] \($0.composedMessage)\n" }
.joined()
.data(using: .utf8)!
let fileURL = FileManager.default.urls(for: .libraryDirectory, in: .userDomainMask)
.first!
.appendingPathComponent("Logs", isDirectory: true)
.appendingPathComponent("Aware.log")
try! data.write(to: fileURL)
NSWorkspace.shared.activateFileViewerSelecting([fileURL])
}
}

#endif
31 changes: 31 additions & 0 deletions Aware/macOS/SettingsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ struct SettingsView: View {

@State private var lastLoginItemRegistration: Result<Bool, Error>?

@State private var exportingLogs: Bool = false
@State private var showExportErrored: Bool = false

var body: some View {
Form {
Section {
Expand Down Expand Up @@ -58,6 +61,34 @@ struct SettingsView: View {
Toggle("Open at Login", isOn: openAtLogin)
}
}

Spacer()
.frame(width: 0, height: 0)
.padding(.top)

Section {
Button(exportingLogs ? "Exporting Developer Logs..." : "Export Developer Logs") {
self.exportingLogs = true
Task<Void, Never>(priority: .low) {
do {
let logURL = try await exportLogs()
NSWorkspace.shared.activateFileViewerSelecting([logURL])
self.showExportErrored = false
} catch {
self.showExportErrored = true
}
self.exportingLogs = false
}
}
.disabled(exportingLogs)
.alert(isPresented: $showExportErrored) {
Alert(
title: Text("Export Error"),
message: Text("Couldn't export logs"),
dismissButton: .default(Text("OK"))
)
}
}
}
.padding()
.frame(width: 350)
Expand Down

0 comments on commit 6724542

Please sign in to comment.