-
-
Notifications
You must be signed in to change notification settings - Fork 371
Flush Logs on WillTerminate or WillResignActive Notifications
#6909
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
base: main
Are you sure you want to change the base?
Changes from all commits
be66378
fd55fdf
115a625
71f478e
162273e
3f4813d
aa1fbc9
6cdd1b0
8e36044
d502009
d22d16e
539d652
e214d5e
b493f18
13de622
e270fa1
19df878
0f74c74
6daca9e
e3f2df8
dd00065
f2e5b8b
f9a7ed8
d828f69
3b22387
0682b5b
fda13c2
2edc82f
7a57e7a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,7 +4,7 @@ | |
| #if (os(iOS) || os(tvOS) || (swift(>=5.9) && os(visionOS))) && !SENTRY_NO_UIKIT | ||
| import UIKit | ||
| private typealias CrossPlatformApplication = UIApplication | ||
| #elseif (os(macOS) || targetEnvironment(macCatalyst)) && !SENTRY_NO_UIKIT | ||
| #elseif os(macOS) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| import AppKit | ||
| private typealias CrossPlatformApplication = NSApplication | ||
| #endif | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| @_implementationOnly import _SentryPrivate | ||
|
|
||
| #if (os(iOS) || os(tvOS) || (swift(>=5.9) && os(visionOS))) && !SENTRY_NO_UIKIT | ||
| import UIKit | ||
| private typealias CrossPlatformApplication = UIApplication | ||
| #elseif os(macOS) | ||
| import AppKit | ||
| private typealias CrossPlatformApplication = NSApplication | ||
| #endif | ||
cursor[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| #if ((os(iOS) || os(tvOS) || (swift(>=5.9) && os(visionOS))) && !SENTRY_NO_UIKIT) || os(macOS) | ||
|
|
||
| protocol NotificationCenterProvider { | ||
| var notificationCenterWrapper: SentryNSNotificationCenterWrapper { get } | ||
| } | ||
|
|
||
| final class FlushLogsIntegration<Dependencies: NotificationCenterProvider>: NSObject, SwiftIntegration { | ||
|
|
||
| private let notificationCenter: SentryNSNotificationCenterWrapper | ||
|
|
||
| init?(with options: Options, dependencies: Dependencies) { | ||
| guard options.enableLogs else { | ||
| return nil | ||
| } | ||
|
|
||
| self.notificationCenter = dependencies.notificationCenterWrapper | ||
|
|
||
| super.init() | ||
|
|
||
| notificationCenter.addObserver( | ||
| self, | ||
| selector: #selector(willResignActive), | ||
| name: CrossPlatformApplication.willResignActiveNotification, | ||
| object: nil | ||
| ) | ||
|
|
||
| notificationCenter.addObserver( | ||
| self, | ||
| selector: #selector(willTerminate), | ||
| name: CrossPlatformApplication.willTerminateNotification, | ||
| object: nil | ||
| ) | ||
| } | ||
|
|
||
| func uninstall() { | ||
| notificationCenter.removeObserver( | ||
| self, | ||
| name: CrossPlatformApplication.willResignActiveNotification, | ||
| object: nil | ||
| ) | ||
|
|
||
| notificationCenter.removeObserver( | ||
| self, | ||
| name: CrossPlatformApplication.willTerminateNotification, | ||
| object: nil | ||
| ) | ||
| } | ||
|
|
||
| deinit { | ||
| uninstall() | ||
| } | ||
|
|
||
| @objc private func willResignActive() { | ||
| guard let client = SentrySDKInternal.currentHub().getClient() else { | ||
| return | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| } | ||
| client.captureLogs() | ||
| } | ||
|
|
||
| @objc private func willTerminate() { | ||
| guard let client = SentrySDKInternal.currentHub().getClient() else { | ||
| return | ||
| } | ||
| client.captureLogs() | ||
| } | ||
|
|
||
| static var name: String { | ||
| "FlushLogsIntegration" | ||
| } | ||
| } | ||
|
|
||
| #endif | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,7 +3,7 @@ | |
| #if (os(iOS) || os(tvOS) || (swift(>=5.9) && os(visionOS))) && !SENTRY_NO_UIKIT | ||
| import UIKit | ||
| typealias Application = UIApplication | ||
| #elseif (os(macOS) || targetEnvironment(macCatalyst)) && !SENTRY_NO_UIKIT | ||
| #elseif os(macOS) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| import AppKit | ||
| typealias Application = NSApplication | ||
| #endif | ||
|
|
@@ -50,7 +50,7 @@ typealias Application = NSApplication | |
| // WillTerminate is called no matter if started from the background or launched into the | ||
| // foreground. | ||
|
|
||
| #if ((os(iOS) || os(tvOS) || (swift(>=5.9) && os(visionOS))) && !SENTRY_NO_UIKIT) || ((os(macOS) || targetEnvironment(macCatalyst)) && !SENTRY_NO_UIKIT) | ||
| #if ((os(iOS) || os(tvOS) || (swift(>=5.9) && os(visionOS))) && !SENTRY_NO_UIKIT) || os(macOS) | ||
|
|
||
| // Call before subscribing to the notifications to avoid that didBecomeActive gets called before | ||
| // ending the cached session. | ||
|
|
@@ -84,7 +84,7 @@ typealias Application = NSApplication | |
| } | ||
|
|
||
| @objc public func removeObservers() { | ||
| #if ((os(iOS) || os(tvOS) || (swift(>=5.9) && os(visionOS))) && !SENTRY_NO_UIKIT) || ((os(macOS) || targetEnvironment(macCatalyst)) && !SENTRY_NO_UIKIT) | ||
| #if ((os(iOS) || os(tvOS) || (swift(>=5.9) && os(visionOS))) && !SENTRY_NO_UIKIT) || os(macOS) | ||
| // Remove the observers with the most specific detail possible, see | ||
| // https://developer.apple.com/documentation/foundation/nsnotificationcenter/1413994-removeobserver | ||
| notificationCenter.removeObserver(self, name: Application.didBecomeActiveNotification, object: nil) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
m: Thanks for adding an extra dispatch queue. What bugs me a bit is that theSentryLogBatcherspecifically needs the dispatch queue above to work correctly, so the init of this specific DispatchQueueWrapper should be in theSentryLogBatcher.swiftfile if possible, IMO. What about removing the SentryDispatchQueueWrapper param from the convenience init and let the convenience init create the SentryDispatchQueueWrapper. We anyways use the other init method for tests.