Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor logging settings to use DittoLogLevel directly #71

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion iOS/DittoPOS.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +640,7 @@
repositoryURL = "https://github.com/getditto/DittoSwiftTools";
requirement = {
kind = upToNextMajorVersion;
minimumVersion = 6.0.0;
minimumVersion = 7.0.0;
};
};
/* End XCRemoteSwiftPackageReference section */
Expand Down
21 changes: 12 additions & 9 deletions iOS/DittoPOS/Data/DittoService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,16 @@ final class DittoInstance {
}
}

let defaultLoggingOption: DittoLogger.LoggingOptions = .error
let defaultLoggingOption: DittoLogLevel = .error

// Used to constrain orders subscriptions to 1 day old or newer
let OrderTTL: TimeInterval = 60 * 60 * 24 //24hrs

@MainActor class DittoService: ObservableObject {
@Published var loggingOption: DittoLogger.LoggingOptions

@State var isLoggingEnabled = DittoLogger.enabled

@Published var loggingOption: DittoLogLevel
private var cancellables = Set<AnyCancellable>()

@Published private(set) var allLocations = [Location]()
Expand Down Expand Up @@ -283,14 +286,14 @@ extension DittoService {
extension DittoService {

private func resetLogging() {
let logOption = Settings.dittoLoggingOption
switch logOption {
case .disabled:
DittoLogger.enabled = false
default:
DittoLogger.enabled = true
DittoLogger.minimumLogLevel = DittoLogLevel(rawValue: logOption.rawValue)!
// Update DittoLogger's enabled state based on the @State var
DittoLogger.enabled = isLoggingEnabled

if isLoggingEnabled {
// Set the minimum log level based on Settings.dittoLoggingOption
DittoLogger.minimumLogLevel = Settings.dittoLoggingOption

// Configure the log file URL if available
if let logFileURL = LogFileConfig.createLogFileURL() {
DittoLogger.setLogFileURL(logFileURL)
}
Expand Down
62 changes: 53 additions & 9 deletions iOS/DittoPOS/Settings/SettingsModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import Foundation
struct Settings {
static var defaults = UserDefaults.standard

static var dittoLoggingOption: DittoLogger.LoggingOptions {
static var dittoLoggingOption: DittoLogLevel {
get { defaults.storedLoggingOption }
set(value) { defaults.storedLoggingOption = value }
}
Expand Down Expand Up @@ -81,7 +81,6 @@ extension Settings {

extension UserDefaults {
public struct UserDefaultsKeys {
static var loggingOption: String { "live.ditto.DittoPOS.loggingOption" }
static var currentLocationId: String { "live.ditto.DittoPOS.currentLocationId" }
static var selectedTab: String { "live.ditto.DittoPOS.selectedTab" }
//rename: keep legacy "userKey" key
Expand Down Expand Up @@ -118,16 +117,61 @@ extension UserDefaults {
}
}

var storedLoggingOption: DittoLogger.LoggingOptions {
var storedLoggingOption: DittoLogLevel {
get {
let logOption = integer(forKey: UserDefaultsKeys.loggingOption)
guard logOption != 0 else {
return DittoLogger.LoggingOptions(rawValue: defaultLoggingOption.rawValue)!
/// Migrates the stored logging option from the legacy key to the new system.
///
/// This function checks if a value exists under the legacy key (`"live.ditto.DittoPOS.loggingOption"`) in `UserDefaults`.
/// If a non-zero value is found, it is manually mapped to the appropriate `DittoLogLevel`
/// and stored using the new storage mechanism. The legacy key is removed after migration
/// to ensure it is no longer used.
///
/// - Important: This function must be called before accessing or restoring the current logging option
/// to ensure any legacy values are migrated correctly.
func migrateFromLegacyStorage() {
let oldKey = "live.ditto.DittoPOS.loggingOption"

// Check if a value exists for the old key
guard object(forKey: oldKey) != nil else { return }

// Retrieve the integer value stored with the legacy key
let rawValue = integer(forKey: oldKey)

// Manually map legacy values to the correct DittoLogLevel
let mappedLogLevel: DittoLogLevel?
switch rawValue {
case 0:
mappedLogLevel = nil // 0 indicates no logging, skip migration
case 1:
mappedLogLevel = .error
case 2:
mappedLogLevel = .warning
case 3:
mappedLogLevel = .info
case 4:
mappedLogLevel = .debug
default:
mappedLogLevel = nil // Unknown values, skip migration
}

// Save the mapped value if valid
if let logLevel = mappedLogLevel {
logLevel.saveToStorage()
}

// Remove the old key
removeObject(forKey: oldKey)
}
return DittoLogger.LoggingOptions(rawValue: logOption)!

// Perform migration before restoring the value
migrateFromLegacyStorage()

// Restore the log level from storage, defaulting to `.error` if no value is found
return DittoLogLevel.restoreFromStorage()
}
set(value) {
set(value.rawValue, forKey: UserDefaultsKeys.loggingOption)
set {
// Save the new log level to storage
newValue.saveToStorage()
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion iOS/DittoPOS/Settings/SettingsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ struct SettingsView: View {
}
}
Section(header: Text("Exports")) {
NavigationLink(destination: LoggingDetailsView(loggingOption: $dittoService.loggingOption)) {
NavigationLink(destination: LoggingDetailsView(ditto: ditto)) {
DittoToolsListItem(title: "Logging", systemImage: "square.split.1x2", color: .green)
}

Expand Down