-
Notifications
You must be signed in to change notification settings - Fork 265
feat: [SDK-4976] add Swift adapters for KMP logger #1702
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
047d54d
feat: [SDK-4976] add Swift adapters for KMP logger
fadi-george bb48cf0
chore(sdk): bump KMP submodule
fadi-george 833293d
refactor(logger): move KMP adapters to OneSignalOSCore
fadi-george 65e43f0
fix: [SDK-4976] harden iOS KMP logger adapters
fadi-george abed34c
fix: [SDK-4976] preserve process and Catalyst compatibility
fadi-george 9a286be
refactor: [SDK-4976] align logger names with Android
fadi-george File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Submodule OneSignal-KMP-SDK
updated
7 files
152 changes: 152 additions & 0 deletions
152
iOS_SDK/OneSignalSDK/OneSignal.xcodeproj/project.pbxproj
Large diffs are not rendered by default.
Oops, something went wrong.
240 changes: 240 additions & 0 deletions
240
iOS_SDK/OneSignalSDK/OneSignalOSCore/Source/Logging/FileLogStore.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,240 @@ | ||
| /* | ||
| Modified MIT License | ||
|
|
||
| Copyright 2026 OneSignal | ||
|
|
||
| Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| of this software and associated documentation files (the "Software"), to deal | ||
| in the Software without restriction, including without limitation the rights | ||
| to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| copies of the Software, and to permit persons to whom the Software is | ||
| furnished to do so, subject to the following conditions: | ||
|
|
||
| 1. The above copyright notice and this permission notice shall be included in | ||
| all copies or substantial portions of the Software. | ||
|
|
||
| 2. All copies of substantial portions of the Software may only be used in connection | ||
| with services provided by OneSignal. | ||
|
|
||
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| THE SOFTWARE. | ||
| */ | ||
|
|
||
| // Kotlin/Native does not produce a Mac Catalyst framework slice. | ||
| #if !targetEnvironment(macCatalyst) | ||
|
|
||
| import Darwin | ||
| import Foundation | ||
| import OneSignalCore | ||
| @_implementationOnly import OneSignalKMP | ||
|
|
||
| /// Persists encoded crash logs so they can be uploaded after the app restarts. | ||
| /// | ||
| /// Writes are synchronous and durable because fatal handlers may terminate the | ||
| /// process immediately after `save` returns. Directory scans and cleanup run on | ||
| /// a utility queue to keep disk I/O off the caller. | ||
| final class FileLogStore: ILogFileStore { | ||
| /// Complete records use `.otlp`; interrupted durable writes leave | ||
| /// `.otlp.tmp` files that are safe to reap after the minimum-age gate. | ||
| private static let ownedFileSuffix = ".otlp" | ||
| private static let temporaryFileSuffix = ".otlp.tmp" | ||
| private static let queueLabel = "com.onesignal.logger.file-store" | ||
|
|
||
| private let rootURL: URL | ||
| private let fileManager: FileManager | ||
| private let ioQueue = DispatchQueue(label: queueLabel, qos: .utility) | ||
|
|
||
| init(rootPath: String, fileManager: FileManager = .default) { | ||
| self.rootURL = URL(fileURLWithPath: rootPath, isDirectory: true) | ||
| self.fileManager = fileManager | ||
| try? createRootDirectory() | ||
| } | ||
|
|
||
| func save(bytes: KotlinByteArray) -> Bool { | ||
| guard bytes.size > 0 else { | ||
| return false | ||
| } | ||
| do { | ||
| try createRootDirectory() | ||
| let timestamp = Int64(Date().timeIntervalSince1970 * 1_000) | ||
| let id = "\(timestamp)-\(UUID().uuidString)\(Self.ownedFileSuffix)" | ||
| try writeDurably(bytes.data, to: rootURL.appendingPathComponent(id)) | ||
| return true | ||
| } catch { | ||
| return false | ||
| } | ||
| } | ||
|
|
||
| func listReadable( | ||
| minAgeMillis: Int64, | ||
| completionHandler: @escaping ([StoredLogFile]?, Error?) -> Void | ||
| ) { | ||
| ioQueue.async { | ||
| do { | ||
| let entries = try self.readableEntries(minAgeMillis: minAgeMillis) | ||
| completionHandler(entries, nil) | ||
| } catch { | ||
| OneSignalLog.onesignalLog( | ||
| .LL_WARN, | ||
| message: "FileLogStore listReadable failed: \(error.localizedDescription)" | ||
| ) | ||
| completionHandler([], nil) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func delete(id: String, completionHandler: @escaping (Error?) -> Void) { | ||
| ioQueue.async { | ||
| guard self.isSafeEntryId(id) else { | ||
| completionHandler(nil) | ||
| return | ||
| } | ||
|
|
||
| do { | ||
| let url = self.rootURL.appendingPathComponent(id) | ||
| if self.fileManager.fileExists(atPath: url.path) { | ||
| try self.fileManager.removeItem(at: url) | ||
| } | ||
| } catch { | ||
| OneSignalLog.onesignalLog( | ||
| .LL_WARN, | ||
| message: "FileLogStore delete failed: \(error.localizedDescription)" | ||
| ) | ||
| } | ||
| completionHandler(nil) | ||
| } | ||
| } | ||
|
|
||
| func deleteUnrecognizedEntries( | ||
| minAgeMillis: Int64, | ||
| completionHandler: @escaping (KotlinInt?, Error?) -> Void | ||
| ) { | ||
| ioQueue.async { | ||
| var deleted = 0 | ||
| do { | ||
| for url in try self.fileURLs() | ||
| where url.lastPathComponent.hasSuffix(Self.temporaryFileSuffix) { | ||
| guard try self.isOldEnough(url, minAgeMillis: minAgeMillis) else { | ||
| continue | ||
| } | ||
| try self.fileManager.removeItem(at: url) | ||
| deleted += 1 | ||
| } | ||
| } catch { | ||
| OneSignalLog.onesignalLog( | ||
| .LL_WARN, | ||
| message: "FileLogStore cleanup failed: \(error.localizedDescription)" | ||
| ) | ||
| } | ||
| completionHandler(KotlinInt(int: Int32(deleted)), nil) | ||
| } | ||
| } | ||
|
|
||
| private func readableEntries(minAgeMillis: Int64) throws -> [StoredLogFile] { | ||
| try fileURLs() | ||
| .filter { $0.lastPathComponent.hasSuffix(Self.ownedFileSuffix) } | ||
| .filter { try isOldEnough($0, minAgeMillis: minAgeMillis) } | ||
| .compactMap { url in | ||
| guard let data = try? Data(contentsOf: url) else { | ||
| return nil | ||
| } | ||
| return StoredLogFile(id: url.lastPathComponent, bytes: data.kotlinByteArray) | ||
| } | ||
| } | ||
|
|
||
| private func fileURLs() throws -> [URL] { | ||
| guard fileManager.fileExists(atPath: rootURL.path) else { | ||
| return [] | ||
| } | ||
| return try fileManager.contentsOfDirectory( | ||
| at: rootURL, | ||
| includingPropertiesForKeys: [.contentModificationDateKey, .isRegularFileKey], | ||
| options: [.skipsHiddenFiles] | ||
| ).filter { | ||
| (try? $0.resourceValues(forKeys: [.isRegularFileKey]).isRegularFile) == true | ||
| } | ||
| } | ||
|
|
||
| private func isOldEnough(_ url: URL, minAgeMillis: Int64) throws -> Bool { | ||
| let values = try url.resourceValues(forKeys: [.contentModificationDateKey]) | ||
| guard let modifiedAt = values.contentModificationDate else { | ||
| return false | ||
| } | ||
| return Date().timeIntervalSince(modifiedAt) * 1_000 >= Double(max(0, minAgeMillis)) | ||
| } | ||
|
|
||
| private func isSafeEntryId(_ id: String) -> Bool { | ||
| !id.isEmpty && URL(fileURLWithPath: id).lastPathComponent == id | ||
| } | ||
|
|
||
| private func createRootDirectory() throws { | ||
| try fileManager.createDirectory(at: rootURL, withIntermediateDirectories: true) | ||
| } | ||
|
|
||
| private func writeDurably(_ data: Data, to targetURL: URL) throws { | ||
| let temporaryURL = targetURL.appendingPathExtension("tmp") | ||
| let descriptor = open(temporaryURL.path, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR) | ||
| guard descriptor >= 0 else { | ||
| throw POSIXError(POSIXErrorCode(rawValue: errno) ?? .EIO) | ||
| } | ||
| var isClosed = false | ||
| defer { | ||
| if !isClosed { | ||
| close(descriptor) | ||
| } | ||
| } | ||
| do { | ||
| try fileManager.setAttributes( | ||
| [.protectionKey: FileProtectionType.completeUntilFirstUserAuthentication], | ||
| ofItemAtPath: temporaryURL.path | ||
| ) | ||
| try data.withUnsafeBytes { rawBuffer in | ||
| guard var pointer = rawBuffer.baseAddress else { | ||
| return | ||
| } | ||
| var remaining = rawBuffer.count | ||
| while remaining > 0 { | ||
| let count = Darwin.write(descriptor, pointer, remaining) | ||
| if count < 0 && errno == EINTR { | ||
| continue | ||
| } | ||
| guard count > 0 else { | ||
| throw POSIXError(POSIXErrorCode(rawValue: errno) ?? .EIO) | ||
| } | ||
| pointer = pointer.advanced(by: count) | ||
| remaining -= count | ||
| } | ||
| } | ||
| guard fsync(descriptor) == 0 else { | ||
| throw POSIXError(POSIXErrorCode(rawValue: errno) ?? .EIO) | ||
| } | ||
| guard close(descriptor) == 0 else { | ||
| throw POSIXError(POSIXErrorCode(rawValue: errno) ?? .EIO) | ||
| } | ||
| isClosed = true | ||
| try fileManager.moveItem(at: temporaryURL, to: targetURL) | ||
| try syncDirectory() | ||
| } catch { | ||
| try? fileManager.removeItem(at: temporaryURL) | ||
| throw error | ||
| } | ||
| } | ||
|
|
||
| private func syncDirectory() throws { | ||
| let descriptor = open(rootURL.path, O_RDONLY) | ||
| guard descriptor >= 0 else { | ||
| throw POSIXError(POSIXErrorCode(rawValue: errno) ?? .EIO) | ||
| } | ||
| defer { close(descriptor) } | ||
| guard fsync(descriptor) == 0 else { | ||
| throw POSIXError(POSIXErrorCode(rawValue: errno) ?? .EIO) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #endif |
55 changes: 55 additions & 0 deletions
55
iOS_SDK/OneSignalSDK/OneSignalOSCore/Source/Logging/IOSLogger.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| /* | ||
| Modified MIT License | ||
|
|
||
| Copyright 2026 OneSignal | ||
|
|
||
| Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| of this software and associated documentation files (the "Software"), to deal | ||
| in the Software without restriction, including without limitation the rights | ||
| to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| copies of the Software, and to permit persons to whom the Software is | ||
| furnished to do so, subject to the following conditions: | ||
|
|
||
| 1. The above copyright notice and this permission notice shall be included in | ||
| all copies or substantial portions of the Software. | ||
|
|
||
| 2. All copies of substantial portions of the Software may only be used in connection | ||
| with services provided by OneSignal. | ||
|
|
||
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| THE SOFTWARE. | ||
| */ | ||
|
|
||
| // Kotlin/Native does not produce a Mac Catalyst framework slice. | ||
| #if !targetEnvironment(macCatalyst) | ||
|
|
||
| import Foundation | ||
| import OneSignalCore | ||
| @_implementationOnly import OneSignalKMP | ||
|
|
||
| /// Routes diagnostic messages from the shared logger through the iOS SDK's | ||
| /// existing logging pipeline. | ||
| final class IOSLogger: ILogger { | ||
| func error(message: String) { | ||
| OneSignalLog.onesignalLog(.LL_ERROR, message: message) | ||
| } | ||
|
|
||
| func warn(message: String) { | ||
| OneSignalLog.onesignalLog(.LL_WARN, message: message) | ||
| } | ||
|
|
||
| func info(message: String) { | ||
| OneSignalLog.onesignalLog(.LL_INFO, message: message) | ||
| } | ||
|
|
||
| func debug(message: String) { | ||
| OneSignalLog.onesignalLog(.LL_DEBUG, message: message) | ||
| } | ||
| } | ||
|
|
||
| #endif |
46 changes: 46 additions & 0 deletions
46
iOS_SDK/OneSignalSDK/OneSignalOSCore/Source/Logging/KotlinByteArray+Data.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| /* | ||
| Modified MIT License | ||
|
|
||
| Copyright 2026 OneSignal | ||
|
|
||
| Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| of this software and associated documentation files (the "Software"), to deal | ||
| in the Software without restriction, including without limitation the rights | ||
| to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| copies of the Software, and to permit persons to whom the Software is | ||
| furnished to do so, subject to the following conditions: | ||
|
|
||
| 1. The above copyright notice and this permission notice shall be included in | ||
| all copies or substantial portions of the Software. | ||
|
|
||
| 2. All copies of substantial portions of the Software may only be used in connection | ||
| with services provided by OneSignal. | ||
|
|
||
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| THE SOFTWARE. | ||
| */ | ||
|
|
||
| // Kotlin/Native does not produce a Mac Catalyst framework slice. | ||
| #if !targetEnvironment(macCatalyst) | ||
|
|
||
| import Foundation | ||
| @_implementationOnly import OneSignalKMP | ||
|
|
||
| extension KotlinByteArray { | ||
| var data: Data { | ||
| AppleByteArrayInterop.shared.toNSData(bytes: self) | ||
| } | ||
| } | ||
|
|
||
| extension Data { | ||
| var kotlinByteArray: KotlinByteArray { | ||
| AppleByteArrayInterop.shared.toByteArray(data: self) | ||
| } | ||
| } | ||
|
|
||
| #endif | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.