Skip to content

Commit 0af71b2

Browse files
authored
Flush Logs on WillTerminate or WillResignActive Notifications (#6909)
1 parent e0809f1 commit 0af71b2

18 files changed

+319
-26
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
- Add attributes data to `SentryScope` (#6830)
88
- Add `SentryScope` attributes into log messages (#6834)
99

10+
### Improvements
11+
12+
- Flush Logs on `WillTerminate` or `WillResignActive` Notifications (#6909)
13+
1014
### Fixes
1115

1216
- Disabled automatic session tracking in system extensions to prevent extension blocking and unwanted dock icon behavior (#6962) (#6962)

Sentry.xcodeproj/project.pbxproj

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -700,10 +700,12 @@
700700
92235CAC2E15369900865983 /* SentryLogBatcher.swift in Sources */ = {isa = PBXBuildFile; fileRef = 92235CAB2E15369900865983 /* SentryLogBatcher.swift */; };
701701
92235CAE2E15549C00865983 /* SentryLogger.swift in Sources */ = {isa = PBXBuildFile; fileRef = 92235CAD2E15549C00865983 /* SentryLogger.swift */; };
702702
92235CB02E155B2600865983 /* SentryLoggerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 92235CAF2E155B2600865983 /* SentryLoggerTests.swift */; };
703+
925189AC2EDDA6A300557BD1 /* FlushLogsIntegration.swift in Sources */ = {isa = PBXBuildFile; fileRef = 925189AA2EDDA6A300557BD1 /* FlushLogsIntegration.swift */; };
703704
9264E1EB2E2E385E00B077CF /* SentryLogMessage.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9264E1EA2E2E385B00B077CF /* SentryLogMessage.swift */; };
704705
9264E1ED2E2E397C00B077CF /* SentryLogMessageTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9264E1EC2E2E397400B077CF /* SentryLogMessageTests.swift */; };
705706
92672BB629C9A2A9006B021C /* SentryBreadcrumb+Private.h in Headers */ = {isa = PBXBuildFile; fileRef = 92672BB529C9A2A9006B021C /* SentryBreadcrumb+Private.h */; settings = {ATTRIBUTES = (Private, ); }; };
706707
927A5CC42DD7626B00B82404 /* SentryEnvelopeItemHeaderTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 927A5CC32DD7626400B82404 /* SentryEnvelopeItemHeaderTests.swift */; };
708+
927D21FB2ED5DE8A00916D31 /* FlushLogsIntegrationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 927D21FA2ED5DE7F00916D31 /* FlushLogsIntegrationTests.swift */; };
707709
928207C42E251B8F009285A4 /* SentryScope+PrivateSwift.h in Headers */ = {isa = PBXBuildFile; fileRef = 928207C32E251B8F009285A4 /* SentryScope+PrivateSwift.h */; };
708710
9286059529A5096600F96038 /* SentryGeo.h in Headers */ = {isa = PBXBuildFile; fileRef = 9286059429A5096600F96038 /* SentryGeo.h */; settings = {ATTRIBUTES = (Public, ); }; };
709711
9286059729A5098900F96038 /* SentryGeo.m in Sources */ = {isa = PBXBuildFile; fileRef = 9286059629A5098900F96038 /* SentryGeo.m */; };
@@ -2058,10 +2060,12 @@
20582060
92235CAB2E15369900865983 /* SentryLogBatcher.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentryLogBatcher.swift; sourceTree = "<group>"; };
20592061
92235CAD2E15549C00865983 /* SentryLogger.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentryLogger.swift; sourceTree = "<group>"; };
20602062
92235CAF2E155B2600865983 /* SentryLoggerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentryLoggerTests.swift; sourceTree = "<group>"; };
2063+
925189AA2EDDA6A300557BD1 /* FlushLogsIntegration.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FlushLogsIntegration.swift; sourceTree = "<group>"; };
20612064
9264E1EA2E2E385B00B077CF /* SentryLogMessage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentryLogMessage.swift; sourceTree = "<group>"; };
20622065
9264E1EC2E2E397400B077CF /* SentryLogMessageTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentryLogMessageTests.swift; sourceTree = "<group>"; };
20632066
92672BB529C9A2A9006B021C /* SentryBreadcrumb+Private.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "SentryBreadcrumb+Private.h"; path = "include/HybridPublic/SentryBreadcrumb+Private.h"; sourceTree = "<group>"; };
20642067
927A5CC32DD7626400B82404 /* SentryEnvelopeItemHeaderTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentryEnvelopeItemHeaderTests.swift; sourceTree = "<group>"; };
2068+
927D21FA2ED5DE7F00916D31 /* FlushLogsIntegrationTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FlushLogsIntegrationTests.swift; sourceTree = "<group>"; };
20652069
928207C32E251B8F009285A4 /* SentryScope+PrivateSwift.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = "SentryScope+PrivateSwift.h"; path = "include/SentryScope+PrivateSwift.h"; sourceTree = "<group>"; };
20662070
9286059429A5096600F96038 /* SentryGeo.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = SentryGeo.h; path = Public/SentryGeo.h; sourceTree = "<group>"; };
20672071
9286059629A5098900F96038 /* SentryGeo.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SentryGeo.m; sourceTree = "<group>"; };
@@ -3502,6 +3506,7 @@
35023506
7B944FA924697E9700A10721 /* Integrations */ = {
35033507
isa = PBXGroup;
35043508
children = (
3509+
927D21F42ED5DE7800916D31 /* Log */,
35053510
F498EBA52EE0B8E400F57509 /* SentrySwiftIntegrationInstallerTests.swift */,
35063511
843FB3422D156B9900558F18 /* Feedback */,
35073512
7BF6505D292B77D100BBA5A8 /* MetricKit */,
@@ -4177,6 +4182,31 @@
41774182
name = Transaction;
41784183
sourceTree = "<group>";
41794184
};
4185+
9246A2352ED5CFDC002FA318 /* AppState */ = {
4186+
isa = PBXGroup;
4187+
children = (
4188+
FA4C32972DF7513F001D7B01 /* SentryAppState.swift */,
4189+
FA560F5A2E8C876A00F2AF7F /* SentryAppStateManager.swift */,
4190+
);
4191+
path = AppState;
4192+
sourceTree = "<group>";
4193+
};
4194+
925189AB2EDDA6A300557BD1 /* Log */ = {
4195+
isa = PBXGroup;
4196+
children = (
4197+
925189AA2EDDA6A300557BD1 /* FlushLogsIntegration.swift */,
4198+
);
4199+
path = Log;
4200+
sourceTree = "<group>";
4201+
};
4202+
927D21F42ED5DE7800916D31 /* Log */ = {
4203+
isa = PBXGroup;
4204+
children = (
4205+
927D21FA2ED5DE7F00916D31 /* FlushLogsIntegrationTests.swift */,
4206+
);
4207+
path = Log;
4208+
sourceTree = "<group>";
4209+
};
41804210
D4009EA02D77196F0007AF30 /* ViewCapture */ = {
41814211
isa = PBXGroup;
41824212
children = (
@@ -4445,9 +4475,9 @@
44454475
D800942328F82E8D005D3943 /* Swift */ = {
44464476
isa = PBXGroup;
44474477
children = (
4478+
9246A2352ED5CFDC002FA318 /* AppState */,
44484479
FAAB95CC2EA18B260030A2DB /* SentryDependencyContainer.swift */,
44494480
FAAB95B92EA1633E0030A2DB /* State */,
4450-
FA560F5A2E8C876A00F2AF7F /* SentryAppStateManager.swift */,
44514481
F429D37E2E8532A300DBF387 /* Networking */,
44524482
F4FE9E062E6248BB0014FED5 /* SentryCrash */,
44534483
FABB48B22E59310D0071397E /* Transaction */,
@@ -4460,7 +4490,6 @@
44604490
D856272A2A374A6800FB8062 /* Tools */,
44614491
D8B665BB2B95F5A100BD0E7B /* module.modulemap */,
44624492
FA4C32962DF7513F001D7B00 /* SentryExperimentalOptions.swift */,
4463-
FA4C32972DF7513F001D7B01 /* SentryAppState.swift */,
44644493
FA6251FE2EB52DD700BFC967 /* SentryHub.swift */,
44654494
FA6252052EB5489B00BFC967 /* SentryClient.swift */,
44664495
FA27EC152EB9236000F2ECF7 /* Options.swift */,
@@ -4819,6 +4848,7 @@
48194848
D8CAC02D2BA0663E00E38F34 /* Integrations */ = {
48204849
isa = PBXGroup;
48214850
children = (
4851+
925189AB2EDDA6A300557BD1 /* Log */,
48224852
FAB0073C2E9F47DE001C806A /* Session */,
48234853
FAE579B42E7DBE9400B710F9 /* SentryGlobalEventProcessor.swift */,
48244854
FAD882C12EDAADF90055AA44 /* SwiftAsyncIntegration.swift */,
@@ -5760,6 +5790,7 @@
57605790
63AA75EF1EB8B3C400D153DE /* SentryClient.m in Sources */,
57615791
D4B0DC7F2DA9257A00DE61B6 /* SentryRenderVideoResult.swift in Sources */,
57625792
FAAB964E2EA698730030A2DB /* SentryDebugImageProvider.swift in Sources */,
5793+
925189AC2EDDA6A300557BD1 /* FlushLogsIntegration.swift in Sources */,
57635794
7B7D873624864C9D00D2ECFF /* SentryCrashDefaultMachineContextWrapper.m in Sources */,
57645795
63FE712F20DA4C1100CDBAE8 /* SentryCrashSysCtl.c in Sources */,
57655796
62212B872D520CB00062C2FA /* SentryEventCodable.swift in Sources */,
@@ -6304,6 +6335,7 @@
63046335
D88817DD26D72BA500BF2251 /* SentryTraceContextTests.swift in Sources */,
63056336
D4E3F35D2D4A864600F79E2B /* SentryNSDictionarySanitizeTests.swift in Sources */,
63066337
7B984A9F28E572AF001F4BEE /* CrashReport.swift in Sources */,
6338+
927D21FB2ED5DE8A00916D31 /* FlushLogsIntegrationTests.swift in Sources */,
63076339
D4AF00252D2E93C400F5F3D7 /* SentryNSFileManagerSwizzlingTests.m in Sources */,
63086340
D46712622DCD059900D4074A /* SentryRedactDefaultOptionsTests.swift in Sources */,
63096341
D480F9DB2DE47AF2009A0594 /* SentryScopePersistentStoreTests.swift in Sources */,

SentryTestUtils/Sources/TestClient.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,4 +167,9 @@ public class TestClient: SentryClientInternal {
167167
captureLogInvocations.record((castLog, scope))
168168
}
169169
}
170+
171+
public var captureLogsInvocations = Invocations<Void>()
172+
public override func captureLogs() {
173+
captureLogsInvocations.record(())
174+
}
170175
}

Sources/Sentry/SentryClient.m

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,8 @@ - (instancetype)initWithOptions:(SentryOptions *)options
115115
self.locale = locale;
116116
self.timezone = timezone;
117117
self.attachmentProcessors = [[NSMutableArray alloc] init];
118-
self.logBatcher = [[SentryLogBatcher alloc]
119-
initWithOptions:options
120-
dispatchQueue:SentryDependencyContainer.sharedInstance.dispatchQueueWrapper
121-
delegate:self];
118+
119+
self.logBatcher = [[SentryLogBatcher alloc] initWithOptions:options delegate:self];
122120

123121
// The SDK stores the installationID in a file. The first call requires file IO. To avoid
124122
// executing this on the main thread, we cache the installationID async here.
@@ -1106,6 +1104,11 @@ - (void)_swiftCaptureLog:(NSObject *)log withScope:(SentryScope *)scope
11061104
}
11071105
}
11081106

1107+
- (void)captureLogs
1108+
{
1109+
[self.logBatcher captureLogs];
1110+
}
1111+
11091112
- (void)captureLogsData:(NSData *)data with:(NSNumber *)itemCount
11101113
{
11111114
SentryEnvelopeItem *envelopeItem =

Sources/Sentry/_SentryDispatchQueueWrapperInternal.m

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,17 @@ @implementation _SentryDispatchQueueWrapperInternal
77

88
- (instancetype)init
99
{
10-
// DISPATCH_QUEUE_SERIAL_WITH_AUTORELEASE_POOL is requires iOS 10. Since we are targeting
11-
// iOS 9 we need to manually add the autoreleasepool.
12-
dispatch_queue_attr_t attributes = dispatch_queue_attr_make_with_qos_class(
13-
DISPATCH_QUEUE_SERIAL, DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
14-
self = [self initWithName:"io.sentry.default" attributes:attributes];
10+
self = [self initWithName:"io.sentry.default"];
11+
return self;
12+
}
13+
14+
- (instancetype)initWithName:(const char *)name
15+
{
16+
if (self = [super init]) {
17+
dispatch_queue_attr_t attributes
18+
= dispatch_queue_attr_make_with_qos_class(DISPATCH_QUEUE_SERIAL, QOS_CLASS_DEFAULT, 0);
19+
_queue = dispatch_queue_create(name, attributes);
20+
}
1521
return self;
1622
}
1723

Sources/Sentry/include/SentryClient+Private.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ NS_ASSUME_NONNULL_BEGIN
8282

8383
- (void)_swiftCaptureLog:(NSObject *)log withScope:(SentryScope *)scope;
8484

85+
- (void)captureLogs;
86+
8587
@end
8688

8789
NS_ASSUME_NONNULL_END

Sources/Sentry/include/_SentryDispatchQueueWrapperInternal.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ NS_ASSUME_NONNULL_BEGIN
1212

1313
@property (strong, nonatomic) dispatch_queue_t queue;
1414

15+
- (instancetype)initWithName:(const char *)name;
16+
1517
- (instancetype)initWithName:(const char *)name
1618
attributes:(nullable dispatch_queue_attr_t)attributes;
1719

File renamed without changes.

Sources/Swift/Core/Integrations/Integrations.swift

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,20 @@ private struct AnyIntegration {
3434
@_spi(Private) @objc public final class SentrySwiftIntegrationInstaller: NSObject {
3535
@objc public class func install(with options: Options) {
3636
let dependencies = SentryDependencyContainer.sharedInstance()
37-
let commonIntegrations: [AnyIntegration] = [
37+
38+
var integrations: [AnyIntegration] = [
3839
.init(SwiftAsyncIntegration.self),
3940
.init(SentryAutoSessionTrackingIntegration.self)
4041
]
42+
4143
#if os(iOS) && !SENTRY_NO_UIKIT
42-
let integrations: [AnyIntegration] = commonIntegrations + [.init(UserFeedbackIntegration<SentryDependencyContainer>.self)]
43-
#else
44-
let integrations: [AnyIntegration] = commonIntegrations
44+
integrations.append(.init(UserFeedbackIntegration<SentryDependencyContainer>.self))
45+
#endif
46+
47+
#if ((os(iOS) || os(tvOS) || (swift(>=5.9) && os(visionOS))) && !SENTRY_NO_UIKIT) || os(macOS)
48+
integrations.append(.init(FlushLogsIntegration<SentryDependencyContainer>.self))
4549
#endif
50+
4651
integrations.forEach { anyIntegration in
4752
guard let integration = anyIntegration.install(options, dependencies) else { return }
4853

0 commit comments

Comments
 (0)