From c48f8fbc65b233a4fec57c93123d463274a6d253 Mon Sep 17 00:00:00 2001 From: Matt Bruce Date: Wed, 14 Jan 2026 16:17:20 -0600 Subject: [PATCH] Add Logger --- .gitignore | 8 ++++-- Config/App.xcconfig | 1 + Config/WatchApp.xcconfig | 1 + .../SecureStorageSampleApp.swift | 1 + .../Views/EncryptedStorageDemo.swift | 1 + .../Views/FileSystemDemo.swift | 19 ++++++++++++-- SecureStorageSample/Views/KeychainDemo.swift | 6 +++++ .../Views/PlatformSyncDemo.swift | 5 +++- .../Views/UserDefaultsDemo.swift | 9 +++++-- .../Constants/StorageServiceIdentifiers.swift | 15 +++++++++-- .../Sources/SharedKit/Utilities/Logger.swift | 26 +++++++++++++++++++ 11 files changed, 83 insertions(+), 9 deletions(-) create mode 100644 localPackages/SharedPackage/Sources/SharedKit/Utilities/Logger.swift diff --git a/.gitignore b/.gitignore index 8a6510b..702b716 100644 --- a/.gitignore +++ b/.gitignore @@ -2,5 +2,9 @@ .build/ .swiftpm/ DerivedData/ -*.xcodeproj/project.xcworkspace/xcuserdata -*.xcodeproj/xcuserdata + +# Xcode User Data +xcuserdata/ +*.xcodeproj/project.xcworkspace/xcuserdata/ +*.xcodeproj/xcuserdata/ +*.xcworkspace/xcuserdata/ diff --git a/Config/App.xcconfig b/Config/App.xcconfig index cd5e16d..cc576f1 100644 --- a/Config/App.xcconfig +++ b/Config/App.xcconfig @@ -5,5 +5,6 @@ INFOPLIST_KEY_CFBundleDisplayName = SecureStorage SWIFT_UPCOMING_FEATURE_MEMBER_IMPORT_VISIBILITY = YES INFOPLIST_KEY_BaseBundleID = $(BASE_BUNDLE_ID) INFOPLIST_KEY_TeamID = $(TEAM_ID) +INFOPLIST_KEY_AppGroupID = $(APP_GROUP_ID) CODE_SIGN_ENTITLEMENTS = SecureStorageSample/SecureStorageSample.entitlements diff --git a/Config/WatchApp.xcconfig b/Config/WatchApp.xcconfig index 40ac0d3..26474c7 100644 --- a/Config/WatchApp.xcconfig +++ b/Config/WatchApp.xcconfig @@ -8,5 +8,6 @@ WATCHOS_DEPLOYMENT_TARGET = 10.0 SWIFT_UPCOMING_FEATURE_MEMBER_IMPORT_VISIBILITY = YES INFOPLIST_KEY_BaseBundleID = $(BASE_BUNDLE_ID) INFOPLIST_KEY_TeamID = $(TEAM_ID) +INFOPLIST_KEY_AppGroupID = $(APP_GROUP_ID) CODE_SIGN_ENTITLEMENTS = SecureStorageSample Watch App/SecureStorageSample Watch App.entitlements diff --git a/SecureStorageSample/SecureStorageSampleApp.swift b/SecureStorageSample/SecureStorageSampleApp.swift index fa36d30..92a94de 100644 --- a/SecureStorageSample/SecureStorageSampleApp.swift +++ b/SecureStorageSample/SecureStorageSampleApp.swift @@ -12,6 +12,7 @@ import SharedKit @main struct SecureStorageSampleApp: App { init() { + StorageServiceIdentifiers.logConfiguration() _ = WatchConnectivityService.shared Task { // 1. Global Encryption Configuration diff --git a/SecureStorageSample/Views/EncryptedStorageDemo.swift b/SecureStorageSample/Views/EncryptedStorageDemo.swift index 12ee51e..5fe530d 100644 --- a/SecureStorageSample/Views/EncryptedStorageDemo.swift +++ b/SecureStorageSample/Views/EncryptedStorageDemo.swift @@ -8,6 +8,7 @@ import SwiftUI import LocalData +@MainActor struct EncryptedStorageDemo: View { @State private var logEntry = "" @State private var storedLogs: [String] = [] diff --git a/SecureStorageSample/Views/FileSystemDemo.swift b/SecureStorageSample/Views/FileSystemDemo.swift index 936a14b..ce9eb98 100644 --- a/SecureStorageSample/Views/FileSystemDemo.swift +++ b/SecureStorageSample/Views/FileSystemDemo.swift @@ -9,6 +9,7 @@ import SwiftUI import LocalData import SharedKit +@MainActor struct FileSystemDemo: View { @State private var profileName = "" @State private var profileEmail = "" @@ -191,7 +192,14 @@ struct FileSystemDemo: View { Task { do { let key = StorageKeys.UserProfileFileKey(directory: selectedDirectory) - storedProfile = try await StorageRouter.shared.get(key) + let profile = try await StorageRouter.shared.get(key) + storedProfile = profile + + // Sync to text fields + profileName = profile.name + profileEmail = profile.email + profileAge = profile.age.map(String.init) ?? "" + statusMessage = "✓ Loaded from file system" } catch StorageError.notFound { storedProfile = nil @@ -243,7 +251,14 @@ struct FileSystemDemo: View { Task { do { let key = StorageKeys.AppGroupUserProfileKey(directory: selectedDirectory) - appGroupProfile = try await StorageRouter.shared.get(key) + let profile = try await StorageRouter.shared.get(key) + appGroupProfile = profile + + // Sync to text fields + profileName = profile.name + profileEmail = profile.email + profileAge = profile.age.map(String.init) ?? "" + appGroupStatusMessage = "✓ App Group loaded from file system" } catch StorageError.notFound { appGroupProfile = nil diff --git a/SecureStorageSample/Views/KeychainDemo.swift b/SecureStorageSample/Views/KeychainDemo.swift index 4b40fb1..405dcb7 100644 --- a/SecureStorageSample/Views/KeychainDemo.swift +++ b/SecureStorageSample/Views/KeychainDemo.swift @@ -8,6 +8,7 @@ import SwiftUI import LocalData +@MainActor struct KeychainDemo: View { @State private var username = "" @State private var password = "" @@ -141,6 +142,11 @@ struct KeychainDemo: View { ) let credential = try await StorageRouter.shared.get(key) storedCredential = "Username: \(credential.username)\nPassword: ****" + + // Sync to fields + username = credential.username + password = credential.password + statusMessage = "✓ Retrieved from Keychain" } catch StorageError.notFound { storedCredential = "" diff --git a/SecureStorageSample/Views/PlatformSyncDemo.swift b/SecureStorageSample/Views/PlatformSyncDemo.swift index 1ddaabe..326d6f2 100644 --- a/SecureStorageSample/Views/PlatformSyncDemo.swift +++ b/SecureStorageSample/Views/PlatformSyncDemo.swift @@ -8,6 +8,7 @@ import SwiftUI import LocalData +@MainActor struct PlatformSyncDemo: View { @State private var settingValue = "" @State private var storedValue = "" @@ -181,7 +182,9 @@ struct PlatformSyncDemo: View { availability: selectedPlatform, syncPolicy: selectedSync ) - storedValue = try await StorageRouter.shared.get(key) + let value = try await StorageRouter.shared.get(key) + storedValue = value + settingValue = value // Sync to field statusMessage = "✓ Loaded value" } catch StorageError.notFound { storedValue = "" diff --git a/SecureStorageSample/Views/UserDefaultsDemo.swift b/SecureStorageSample/Views/UserDefaultsDemo.swift index 2b264a7..afe3ac7 100644 --- a/SecureStorageSample/Views/UserDefaultsDemo.swift +++ b/SecureStorageSample/Views/UserDefaultsDemo.swift @@ -8,6 +8,7 @@ import SwiftUI import LocalData +@MainActor struct UserDefaultsDemo: View { @State private var inputText = "" @State private var storedValue = "" @@ -155,7 +156,9 @@ struct UserDefaultsDemo: View { Task { do { let key = StorageKeys.AppVersionKey() - storedValue = try await StorageRouter.shared.get(key) + let value = try await StorageRouter.shared.get(key) + storedValue = value + inputText = value // Sync to field statusMessage = "✓ Loaded successfully" } catch StorageError.notFound { storedValue = "" @@ -201,7 +204,9 @@ struct UserDefaultsDemo: View { Task { do { let key = StorageKeys.AppGroupUserDefaultsKey() - appGroupStoredValue = try await StorageRouter.shared.get(key) + let value = try await StorageRouter.shared.get(key) + appGroupStoredValue = value + inputText = value // Sync to field statusMessage = "✓ App Group loaded successfully" } catch StorageError.notFound { appGroupStoredValue = "" diff --git a/localPackages/SharedPackage/Sources/SharedKit/Constants/StorageServiceIdentifiers.swift b/localPackages/SharedPackage/Sources/SharedKit/Constants/StorageServiceIdentifiers.swift index dca545f..b5f4add 100644 --- a/localPackages/SharedPackage/Sources/SharedKit/Constants/StorageServiceIdentifiers.swift +++ b/localPackages/SharedPackage/Sources/SharedKit/Constants/StorageServiceIdentifiers.swift @@ -2,9 +2,10 @@ import Foundation public enum StorageServiceIdentifiers { public static var bundleIdentifier: String { - Bundle.main.object(forInfoDictionaryKey: "BaseBundleID") as? String ?? + let identifier = Bundle.main.object(forInfoDictionaryKey: "BaseBundleID") as? String ?? Bundle.main.bundleIdentifier ?? "com.example.securestorage" + return identifier } private static var teamIDPrefix: String { @@ -15,7 +16,17 @@ public enum StorageServiceIdentifiers { } public static var appGroupIdentifier: String { - "group.\(bundleIdentifier)" + let identifier = Bundle.main.object(forInfoDictionaryKey: "AppGroupID") as? String ?? + "group.\(bundleIdentifier.lowercased())" + return identifier + } + + public static func logConfiguration() { + Logger.debug("--- STORAGE CONFIGURATION ---") + Logger.debug("Bundle ID: \(bundleIdentifier)") + Logger.debug("Team ID Prefix: \(teamIDPrefix)") + Logger.debug("App Group ID: \(appGroupIdentifier)") + Logger.debug("---------------------------") } public static var keychainCredentials: String { diff --git a/localPackages/SharedPackage/Sources/SharedKit/Utilities/Logger.swift b/localPackages/SharedPackage/Sources/SharedKit/Utilities/Logger.swift new file mode 100644 index 0000000..22c3476 --- /dev/null +++ b/localPackages/SharedPackage/Sources/SharedKit/Utilities/Logger.swift @@ -0,0 +1,26 @@ +import Foundation + +/// Public logging utility for the SecureStorage workspace. +public enum Logger { + public static var isLoggingEnabled = true + + public static func debug(_ message: String) { + #if DEBUG + if isLoggingEnabled { + print(" {SECURE_STORAGE} ℹ️ \(message)") + } + #endif + } + + public static func error(_ message: String, error: Error? = nil) { + #if DEBUG + var logMessage = " {SECURE_STORAGE} ❌ \(message)" + if let error = error { + logMessage += " | Error: \(error.localizedDescription)" + } + if isLoggingEnabled { + print(logMessage) + } + #endif + } +}