Skip to content

Commit

Permalink
2024-11-17 - Updated SimpleLogger package
Browse files Browse the repository at this point in the history
  • Loading branch information
markbattistella committed Nov 16, 2024
1 parent d1b220f commit 67ed808
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 49 deletions.
2 changes: 1 addition & 1 deletion Sources/SimpleLoggerUI/Extensions/OSLogEntryLog+Ext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@

import OSLog

extension OSLogEntryLog: Identifiable {}
extension OSLogEntryLog: @retroactive Identifiable {}
71 changes: 38 additions & 33 deletions Sources/SimpleLoggerUI/Screens/LogExportScreen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@
// Website: https://markbattistella.com
//

import SwiftUI
import SimpleLogger
import SwiftUI
import UniformTypeIdentifiers
import OSLog

/// A view that provides an interface for exporting logs, with various filtering and action options.
public struct LogExportScreen: View {

// MARK: - State Properties

@StateObject private var vm: LoggerManager
@State private var showFileExporter: Bool = false
@State private var logFileDocument: MultiTypeFileDocument?
Expand All @@ -22,18 +21,18 @@ public struct LogExportScreen: View {
@State private var showAlert: Bool = false
@State private var alertMessage: String = ""
@State private var showToast: Bool = false

// MARK: - Private Properties

private let navigationTitle: String
private let logger = Logger(category: .fileSystem)
private let logger = SimpleLogger(category: .fileSystem)

#if !os(macOS)
private let navigationBarTitleDisplayMode: NavigationBarItem.TitleDisplayMode
#endif

// MARK: - Initializer

#if !os(macOS)

/// Initializes the LogExportScreen with the required parameters.
Expand Down Expand Up @@ -67,11 +66,11 @@ public struct LogExportScreen: View {
self._vm = StateObject(wrappedValue: vm)
self.navigationTitle = navigationTitle
}

#endif

// MARK: - Body

public var body: some View {
Form {
filterTypeSection
Expand Down Expand Up @@ -118,17 +117,17 @@ public struct LogExportScreen: View {
// MARK: - Private Methods

extension LogExportScreen {

/// Copies logs to the clipboard in plain text format.
private func copyToClipboard() {
Task {
let logs = await vm.exportLogs(as: .plainText)
let logs = vm.exportLogs(as: .plainText)
let contentToPaste = logs.isEmpty ? "Nothing to paste" : logs
await setToPasteBoard(contentToPaste)
setToPasteBoard(contentToPaste)
showToast = true
}
}

/// Exports the log file in the specified format and presents the file exporter.
/// - Parameter type: The type of file format to export the logs as.
private func exportLogFile(type: UTType) {
Expand All @@ -151,7 +150,7 @@ extension LogExportScreen {
}
}
}

/// Shares the log file in the specified format.
///
/// - Parameter type: The type of file format for sharing the logs.
Expand All @@ -167,7 +166,7 @@ extension LogExportScreen {
}
return url
}

/// Creates a URL for saving the log file based on the current process and timestamp.
///
/// - Parameter type: The type of file format for the logs.
Expand All @@ -178,7 +177,7 @@ extension LogExportScreen {
let fullFile = "\(fileName).\(fileExtension)"
return URL.temporaryDirectory.appendingPathComponent(fullFile)
}

/// Handles the result of the file export operation.
///
/// - Parameter result: A result containing either a URL of the exported file or an error.
Expand All @@ -193,7 +192,7 @@ extension LogExportScreen {
}
self.logFileDocument = nil
}

/// Sets the provided string to the system clipboard.
///
/// - Parameter string: The string to be copied to the clipboard.
Expand Down Expand Up @@ -258,7 +257,7 @@ extension LogExportScreen {
Text(filterFooterText)
}
}

/// A section view for log export and filtering options.
private var filterOptions: some View {
Section {
Expand All @@ -271,27 +270,29 @@ extension LogExportScreen {
} header: {
Text("Options")
} footer: {
Text("If you activate **Exclude system logs** then only entries linked to this app's identifier will be extracted.")
Text(
"If you activate **Exclude system logs** then only entries linked to this app's identifier will be extracted."
)
}
}

/// A section view for action buttons including copy, export, and share log file.
private var actionButtonsSection: some View {
Section {
ActionButton("Copy to clipboard", systemImage: "doc.on.doc") {
copyToClipboard()
}

ActionButton("Export log file", systemImage: "square.and.arrow.down") {
exportLogFile(type: selectedExport)
}

ShareLink(item: shareLogFile(type: selectedExport)) {
Label("Share log file", systemImage: "square.and.arrow.up")
}
}
}

/// A segmented control to refine log filters based on selected type.
@ViewBuilder
private var filterBySegments: some View {
Expand All @@ -302,7 +303,7 @@ extension LogExportScreen {
case .preset: presetSegment
}
}

/// A view for selecting a specific date for log filtering.
private var specificDateSegment: some View {
DatePicker(
Expand All @@ -312,7 +313,7 @@ extension LogExportScreen {
displayedComponents: .date
)
}

/// A view for selecting a date range for log filtering.
private var dateRangeSegment: some View {
Group {
Expand All @@ -330,7 +331,7 @@ extension LogExportScreen {
)
}
}

/// A view for selecting an hour range within a specific date for log filtering.
private var hourRangeSegment: some View {
Group {
Expand All @@ -354,7 +355,7 @@ extension LogExportScreen {
)
}
}

/// A view for selecting preset options for quick log filtering.
private var presetSegment: some View {
Picker("Preset option", selection: $vm.selectedPreset) {
Expand All @@ -363,18 +364,22 @@ extension LogExportScreen {
}
}
}

/// Provides descriptive text for the footer of the filter section.
private var filterFooterText: String {
switch vm.filterType {
case .specificDate:
return "Select a specific date to filter logs from that day only. All times are considered within the selected date."
return
"Select a specific date to filter logs from that day only. All times are considered within the selected date."
case .dateRange:
return "Choose a start and end date to filter logs within a specific date range. Logs from both dates will be included."
return
"Choose a start and end date to filter logs within a specific date range. Logs from both dates will be included."
case .hourRange:
return "Set a specific date and a range of hours to narrow down logs to a precise time window within the chosen day."
return
"Set a specific date and a range of hours to narrow down logs to a precise time window within the chosen day."
case .preset:
return "Select a preset option to quickly apply common date and time filters without manual adjustments."
return
"Select a preset option to quickly apply common date and time filters without manual adjustments."
}
}
}
8 changes: 4 additions & 4 deletions Sources/SimpleLoggerUI/Screens/LogListScreen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
// Website: https://markbattistella.com
//

import SimpleLogger
import SwiftUI
import OSLog

/// A view that displays a list of log entries with search and filter functionalities. Users can
/// filter logs by category and level, and search within log messages.
Expand Down Expand Up @@ -43,9 +43,9 @@ public struct LogListScreen: View {
/// Filters logs based on the search text, selected categories, and selected levels.
private var filteredLogs: [OSLogEntryLog] {
logs.filter { log in
(searchText.isEmpty || log.composedMessage.localizedCaseInsensitiveContains(searchText)) &&
(selectedCategories.isEmpty || selectedCategories.contains(log.category)) &&
(selectedLevels.isEmpty || selectedLevels.contains(log.level))
(searchText.isEmpty || log.composedMessage.localizedCaseInsensitiveContains(searchText))
&& (selectedCategories.isEmpty || selectedCategories.contains(log.category))
&& (selectedLevels.isEmpty || selectedLevels.contains(log.level))
}
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/SimpleLoggerUI/Views/FilterSheet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
// Website: https://markbattistella.com
//

import SimpleLogger
import SwiftUI
import OSLog

/// A view that presents a filter sheet allowing users to select categories and log levels. The
/// selections are managed via bindings, enabling the parent view to respond to changes.
Expand Down
20 changes: 10 additions & 10 deletions Sources/SimpleLoggerUI/Views/LogCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,29 @@
// Website: https://markbattistella.com
//

import SimpleLogger
import SwiftUI
import OSLog

/// A view representing a log entry cell, displaying detailed information about the log entry
/// including its date, subsystem, category, level, and message.
internal struct LogCell: View {

// MARK: - Private Properties

/// The log entry to be displayed in the cell.
private let log: OSLogEntryLog

// MARK: - Initializer

/// Initializes the `LogCell` with a specific log entry.
///
/// - Parameter log: The log entry to display.
init(for log: OSLogEntryLog) {
self.log = log
}

// MARK: - Body

var body: some View {
VStack(alignment: .leading) {
headerSection
Expand All @@ -37,9 +37,9 @@ internal struct LogCell: View {
.listRowBackground(log.level.color.opacity(0.1))
.frame(maxWidth: .infinity, alignment: .leading)
}

// MARK: - Private View Components

/// A view displaying the header section of the log cell with a level icon and formatted date.
private var headerSection: some View {
HStack {
Expand All @@ -55,7 +55,7 @@ internal struct LogCell: View {
.font(.system(.footnote, design: .monospaced))
}
}

/// A view displaying the detailed information of the log entry including subsystem,
/// category, level, and message.
private var logDetailsSection: some View {
Expand Down

0 comments on commit 67ed808

Please sign in to comment.