Skip to content

Commit 0dea8a6

Browse files
authored
Add extensive debug logging (#4704)
* Add extensive debug logging This is to help adopters understand what the state of the library is * Fix variable type
1 parent 73a3bef commit 0dea8a6

17 files changed

+148
-8
lines changed

GoogleDataTransport/GDTCORLibrary/GDTCORConsoleLogger.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@
2525

2626
void GDTCORLog(GDTCORMessageCode code, NSString *format, ...) {
2727
// Don't log anything in not debug builds.
28-
#ifndef NDEBUG
28+
#if !NDEBUG
2929
NSString *logFormat = [NSString stringWithFormat:@"%@[%@] %@", kGDTCORConsoleLogger,
3030
GDTCORMessageCodeEnumToString(code), format];
3131
va_list args;
3232
va_start(args, format);
3333
NSLogv(logFormat, args);
3434
va_end(args);
35-
#endif // NDEBUG
35+
#endif // !NDEBUG
3636
}

GoogleDataTransport/GDTCORLibrary/GDTCOREvent.m

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#import <GoogleDataTransport/GDTCOREvent.h>
1818

1919
#import <GoogleDataTransport/GDTCORAssert.h>
20+
#import <GoogleDataTransport/GDTCORConsoleLogger.h>
2021
#import <GoogleDataTransport/GDTCORStoredEvent.h>
2122

2223
#import "GDTCORLibrary/Private/GDTCOREvent_Private.h"
@@ -35,6 +36,8 @@ - (nullable instancetype)initWithMappingID:(NSString *)mappingID target:(NSInteg
3536
_target = target;
3637
_qosTier = GDTCOREventQosDefault;
3738
}
39+
GDTCORLogDebug("Event %@ created. mappingID: %@ target:%ld qos:%ld", self, _mappingID,
40+
(long)_target, (long)_qosTier);
3841
return self;
3942
}
4043

@@ -45,6 +48,7 @@ - (instancetype)copy {
4548
copy.qosTier = _qosTier;
4649
copy.clockSnapshot = _clockSnapshot;
4750
copy.customPrioritizationParams = _customPrioritizationParams;
51+
GDTCORLogDebug("Copying event %@ to event %@", self, copy);
4852
return copy;
4953
}
5054

GoogleDataTransport/GDTCORLibrary/GDTCORLifecycle.m

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#import "GDTCORLibrary/Public/GDTCORLifecycle.h"
1818

19+
#import <GoogleDataTransport/GDTCORConsoleLogger.h>
1920
#import <GoogleDataTransport/GDTCOREvent.h>
2021

2122
#import "GDTCORLibrary/Private/GDTCORRegistrar_Private.h"
@@ -71,47 +72,59 @@ - (void)dealloc {
7172
- (void)applicationDidEnterBackground:(NSNotification *)notification {
7273
GDTCORApplication *application = [GDTCORApplication sharedApplication];
7374
if ([[GDTCORTransformer sharedInstance] respondsToSelector:@selector(appWillBackground:)]) {
75+
GDTCORLogDebug("%@", @"Signaling GDTCORTransformer that the app is backgrounding.");
7476
[[GDTCORTransformer sharedInstance] appWillBackground:application];
7577
}
7678
if ([[GDTCORStorage sharedInstance] respondsToSelector:@selector(appWillBackground:)]) {
79+
GDTCORLogDebug("%@", @"Signaling GDTCORStorage that the app is backgrounding.");
7780
[[GDTCORStorage sharedInstance] appWillBackground:application];
7881
}
7982
if ([[GDTCORUploadCoordinator sharedInstance] respondsToSelector:@selector(appWillBackground:)]) {
83+
GDTCORLogDebug("%@", @"Signaling GDTCORUploadCoordinator that the app is backgrounding.");
8084
[[GDTCORUploadCoordinator sharedInstance] appWillBackground:application];
8185
}
8286
if ([[GDTCORRegistrar sharedInstance] respondsToSelector:@selector(appWillBackground:)]) {
87+
GDTCORLogDebug("%@", @"Signaling GDTCORRegistrar that the app is backgrounding.");
8388
[[GDTCORRegistrar sharedInstance] appWillBackground:application];
8489
}
8590
}
8691

8792
- (void)applicationWillEnterForeground:(NSNotification *)notification {
8893
GDTCORApplication *application = [GDTCORApplication sharedApplication];
8994
if ([[GDTCORTransformer sharedInstance] respondsToSelector:@selector(appWillForeground:)]) {
95+
GDTCORLogDebug("%@", @"Signaling GDTCORTransformer that the app is foregrounding.");
9096
[[GDTCORTransformer sharedInstance] appWillForeground:application];
9197
}
9298
if ([[GDTCORStorage sharedInstance] respondsToSelector:@selector(appWillForeground:)]) {
99+
GDTCORLogDebug("%@", @"Signaling GDTCORStorage that the app is foregrounding.");
93100
[[GDTCORStorage sharedInstance] appWillForeground:application];
94101
}
95102
if ([[GDTCORUploadCoordinator sharedInstance] respondsToSelector:@selector(appWillForeground:)]) {
103+
GDTCORLogDebug("%@", @"Signaling GDTCORUploadCoordinator that the app is foregrounding.");
96104
[[GDTCORUploadCoordinator sharedInstance] appWillForeground:application];
97105
}
98106
if ([[GDTCORRegistrar sharedInstance] respondsToSelector:@selector(appWillForeground:)]) {
107+
GDTCORLogDebug("%@", @"Signaling GDTCORRegistrar that the app is foregrounding.");
99108
[[GDTCORRegistrar sharedInstance] appWillForeground:application];
100109
}
101110
}
102111

103112
- (void)applicationWillTerminate:(NSNotification *)notification {
104113
GDTCORApplication *application = [GDTCORApplication sharedApplication];
105114
if ([[GDTCORTransformer sharedInstance] respondsToSelector:@selector(appWillTerminate:)]) {
115+
GDTCORLogDebug("%@", @"Signaling GDTCORTransformer that the app is terminating.");
106116
[[GDTCORTransformer sharedInstance] appWillTerminate:application];
107117
}
108118
if ([[GDTCORStorage sharedInstance] respondsToSelector:@selector(appWillTerminate:)]) {
119+
GDTCORLogDebug("%@", @"Signaling GDTCORStorage that the app is terminating.");
109120
[[GDTCORStorage sharedInstance] appWillTerminate:application];
110121
}
111122
if ([[GDTCORUploadCoordinator sharedInstance] respondsToSelector:@selector(appWillTerminate:)]) {
123+
GDTCORLogDebug("%@", @"Signaling GDTCORUploadCoordinator that the app is terminating.");
112124
[[GDTCORUploadCoordinator sharedInstance] appWillTerminate:application];
113125
}
114126
if ([[GDTCORRegistrar sharedInstance] respondsToSelector:@selector(appWillTerminate:)]) {
127+
GDTCORLogDebug("%@", @"Signaling GDTCORRegistrar that the app is terminating.");
115128
[[GDTCORRegistrar sharedInstance] appWillTerminate:application];
116129
}
117130
}

GoogleDataTransport/GDTCORLibrary/GDTCORPlatform.m

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#import <GoogleDataTransport/GDTCORPlatform.h>
1818

1919
#import <GoogleDataTransport/GDTCORAssert.h>
20+
#import <GoogleDataTransport/GDTCORConsoleLogger.h>
2021

2122
#import "GDTCORLibrary/Private/GDTCORRegistrar_Private.h"
2223

@@ -60,6 +61,10 @@ @interface GDTCORApplication ()
6061
@implementation GDTCORApplication
6162

6263
+ (void)load {
64+
GDTCORLogDebug(
65+
"%@", @"GDT is initializing. Please note that if you quit the app via the "
66+
"debugger and not through a lifecycle event, event data will remain on disk but "
67+
"storage won't have a reference to them since the singleton wasn't saved to disk.");
6368
#if TARGET_OS_IOS || TARGET_OS_TV
6469
// If this asserts, please file a bug at https://github.com/firebase/firebase-ios-sdk/issues.
6570
GDTCORFatalAssert(
@@ -127,13 +132,22 @@ - (instancetype)init {
127132

128133
- (GDTCORBackgroundIdentifier)beginBackgroundTaskWithName:(NSString *)name
129134
expirationHandler:(void (^)(void))handler {
130-
return [[self sharedApplicationForBackgroundTask] beginBackgroundTaskWithName:name
131-
expirationHandler:handler];
135+
GDTCORBackgroundIdentifier bgID =
136+
[[self sharedApplicationForBackgroundTask] beginBackgroundTaskWithName:name
137+
expirationHandler:handler];
138+
#if !NDEBUG
139+
if (bgID != GDTCORBackgroundIdentifierInvalid) {
140+
GDTCORLogDebug("Creating background task with name:%@ bgID:%ld", name, (long)bgID);
141+
}
142+
#endif // !NDEBUG
143+
return bgID;
132144
}
133145

134146
- (void)endBackgroundTask:(GDTCORBackgroundIdentifier)bgID {
135147
if (bgID != GDTCORBackgroundIdentifierInvalid) {
148+
GDTCORLogDebug("Ending background task with ID:%ld was successful", (long)bgID);
136149
[[self sharedApplicationForBackgroundTask] endBackgroundTask:bgID];
150+
return;
137151
}
138152
}
139153

@@ -176,18 +190,21 @@ - (void)iOSApplicationDidEnterBackground:(NSNotification *)notif {
176190
_isRunningInBackground = YES;
177191

178192
NSNotificationCenter *notifCenter = [NSNotificationCenter defaultCenter];
193+
GDTCORLogDebug("%@", @"GDTCORPlatform is sending a notif that the app is backgrounding.");
179194
[notifCenter postNotificationName:kGDTCORApplicationDidEnterBackgroundNotification object:nil];
180195
}
181196

182197
- (void)iOSApplicationWillEnterForeground:(NSNotification *)notif {
183198
_isRunningInBackground = NO;
184199

185200
NSNotificationCenter *notifCenter = [NSNotificationCenter defaultCenter];
201+
GDTCORLogDebug("%@", @"GDTCORPlatform is sending a notif that the app is foregrounding.");
186202
[notifCenter postNotificationName:kGDTCORApplicationWillEnterForegroundNotification object:nil];
187203
}
188204

189205
- (void)iOSApplicationWillTerminate:(NSNotification *)notif {
190206
NSNotificationCenter *notifCenter = [NSNotificationCenter defaultCenter];
207+
GDTCORLogDebug("%@", @"GDTCORPlatform is sending a notif that the app is terminating.");
191208
[notifCenter postNotificationName:kGDTCORApplicationWillTerminateNotification object:nil];
192209
}
193210
#endif // TARGET_OS_IOS || TARGET_OS_TV
@@ -197,6 +214,7 @@ - (void)iOSApplicationWillTerminate:(NSNotification *)notif {
197214
#if TARGET_OS_OSX
198215
- (void)macOSApplicationWillTerminate:(NSNotification *)notif {
199216
NSNotificationCenter *notifCenter = [NSNotificationCenter defaultCenter];
217+
GDTCORLogDebug("%@", @"GDTCORPlatform is sending a notif that the app is terminating.");
200218
[notifCenter postNotificationName:kGDTCORApplicationWillTerminateNotification object:nil];
201219
}
202220
#endif // TARGET_OS_OSX

GoogleDataTransport/GDTCORLibrary/GDTCORReachability.m

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ + (SCNetworkReachabilityFlags)currentFlags {
6161
dispatch_sync([GDTCORReachability sharedInstance] -> _reachabilityQueue, ^{
6262
GDTCORReachability *reachability = [GDTCORReachability sharedInstance];
6363
currentFlags = reachability->_flags ? reachability->_flags : reachability->_callbackFlags;
64+
GDTCORLogDebug("Initial reachability flags determined: %d", currentFlags);
6465
});
6566
return currentFlags;
6667
}
@@ -91,6 +92,7 @@ - (instancetype)init {
9192
dispatch_async(_reachabilityQueue, ^{
9293
Boolean valid = SCNetworkReachabilityGetFlags(self->_reachabilityRef, &self->_flags);
9394
if (!valid) {
95+
GDTCORLogDebug("%@", @"Determining reachability failed.");
9496
self->_flags = 0;
9597
}
9698
});
@@ -109,6 +111,7 @@ - (void)setCallbackFlags:(SCNetworkReachabilityFlags)flags {
109111
static void GDTCORReachabilityCallback(SCNetworkReachabilityRef reachability,
110112
SCNetworkReachabilityFlags flags,
111113
void *info) {
114+
GDTCORLogDebug("Reachability changed, new flags: %d", flags);
112115
[[GDTCORReachability sharedInstance] setCallbackFlags:flags];
113116
}
114117

GoogleDataTransport/GDTCORLibrary/GDTCORRegistrar.m

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@
1515
*/
1616

1717
#import "GDTCORLibrary/Public/GDTCORRegistrar.h"
18-
1918
#import "GDTCORLibrary/Private/GDTCORRegistrar_Private.h"
2019

20+
#import <GoogleDataTransport/GDTCORConsoleLogger.h>
21+
2122
@implementation GDTCORRegistrar {
2223
/** Backing ivar for targetToUploader property. */
2324
NSMutableDictionary<NSNumber *, id<GDTCORUploader>> *_targetToUploader;
@@ -50,6 +51,7 @@ - (void)registerUploader:(id<GDTCORUploader>)backend target:(GDTCORTarget)target
5051
dispatch_async(_registrarQueue, ^{
5152
GDTCORRegistrar *strongSelf = weakSelf;
5253
if (strongSelf) {
54+
GDTCORLogDebug("Registered an uploader: %@ for target:%ld", backend, (long)target);
5355
strongSelf->_targetToUploader[@(target)] = backend;
5456
}
5557
});
@@ -60,6 +62,7 @@ - (void)registerPrioritizer:(id<GDTCORPrioritizer>)prioritizer target:(GDTCORTar
6062
dispatch_async(_registrarQueue, ^{
6163
GDTCORRegistrar *strongSelf = weakSelf;
6264
if (strongSelf) {
65+
GDTCORLogDebug("Registered a prioritizer: %@ for target:%ld", prioritizer, (long)target);
6366
strongSelf->_targetToPrioritizer[@(target)] = prioritizer;
6467
}
6568
});

GoogleDataTransport/GDTCORLibrary/GDTCORStorage.m

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
NSString *cachePath =
3939
NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
4040
storagePath = [NSString stringWithFormat:@"%@/google-sdks-events", cachePath];
41+
GDTCORLogDebug("Events will be saved to %@", storagePath);
4142
});
4243
return storagePath;
4344
}
@@ -74,7 +75,9 @@ - (instancetype)init {
7475
}
7576

7677
- (void)storeEvent:(GDTCOREvent *)event {
78+
GDTCORLogDebug("Saving event: %@", event);
7779
if (event == nil) {
80+
GDTCORLogDebug("%@", @"The event was nil, so it was not saved.");
7881
return;
7982
}
8083

@@ -96,12 +99,14 @@ - (void)storeEvent:(GDTCOREvent *)event {
9699
// Check that a prioritizer is available for this target.
97100
id<GDTCORPrioritizer> prioritizer =
98101
[GDTCORRegistrar sharedInstance].targetToPrioritizer[@(target)];
99-
GDTCORAssert(prioritizer, @"There's no prioritizer registered for the given target.");
102+
GDTCORAssert(prioritizer, @"There's no prioritizer registered for the given target. Are you "
103+
@"sure you've added the support library for the backend you need?");
100104

101105
// Write the transport bytes to disk, get a filename.
102106
GDTCORAssert(event.dataObjectTransportBytes, @"The event should have been serialized to bytes");
103107
NSURL *eventFile = [self saveEventBytesToDisk:event.dataObjectTransportBytes
104108
eventHash:event.hash];
109+
GDTCORLogDebug("Event saved to disk: %@", eventFile);
105110
GDTCORDataFuture *dataFuture = [[GDTCORDataFuture alloc] initWithFileURL:eventFile];
106111
GDTCORStoredEvent *storedEvent = [event storedEventWithDataFuture:dataFuture];
107112

@@ -118,6 +123,7 @@ - (void)storeEvent:(GDTCOREvent *)event {
118123

119124
// Write state to disk if we're in the background.
120125
if ([[GDTCORApplication sharedApplication] isRunningInBackground]) {
126+
GDTCORLogDebug("%@", @"Saving storage state because the app is running in the background");
121127
if (@available(macOS 10.13, iOS 11.0, tvOS 11.0, *)) {
122128
NSError *error;
123129
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:self
@@ -134,6 +140,8 @@ - (void)storeEvent:(GDTCOREvent *)event {
134140
// Cancel or end the associated background task if it's still valid.
135141
[[GDTCORApplication sharedApplication] endBackgroundTask:bgID];
136142
bgID = GDTCORBackgroundIdentifierInvalid;
143+
GDTCORLogDebug("Event %@ is stored. There are %ld events stored on disk", event,
144+
(unsigned long)self->_storedEvents.count);
137145
});
138146
}
139147

@@ -147,6 +155,7 @@ - (void)removeEvents:(NSSet<GDTCORStoredEvent *> *)events {
147155
NSURL *fileURL = event.dataFuture.fileURL;
148156
[[NSFileManager defaultManager] removeItemAtURL:fileURL error:&error];
149157
GDTCORAssert(error == nil, @"There was an error removing an event file: %@", error);
158+
GDTCORLogDebug("Removed event from disk: %@", fileURL);
150159
}
151160

152161
// Remove from the tracking collections.

GoogleDataTransport/GDTCORLibrary/GDTCORTransformer.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
#import <GoogleDataTransport/GDTCORAssert.h>
2121
#import <GoogleDataTransport/GDTCORConsoleLogger.h>
22+
#import <GoogleDataTransport/GDTCOREvent.h>
2223
#import <GoogleDataTransport/GDTCOREventTransformer.h>
2324
#import <GoogleDataTransport/GDTCORLifecycle.h>
2425

@@ -60,6 +61,7 @@ - (void)transformEvent:(GDTCOREvent *)event
6061
GDTCOREvent *transformedEvent = event;
6162
for (id<GDTCOREventTransformer> transformer in transformers) {
6263
if ([transformer respondsToSelector:@selector(transform:)]) {
64+
GDTCORLogDebug("Applying a transformer to event %@", event);
6365
transformedEvent = [transformer transform:transformedEvent];
6466
if (!transformedEvent) {
6567
return;

GoogleDataTransport/GDTCORLibrary/GDTCORTransport.m

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ - (nullable instancetype)initWithMappingID:(NSString *)mappingID
4141
_target = target;
4242
_transformerInstance = [GDTCORTransformer sharedInstance];
4343
}
44+
GDTCORLogDebug("Transport object created. mappingID:%@ transformers:%@ target:%ld", _mappingID,
45+
_transformers, (long)_target);
4446
return self;
4547
}
4648

@@ -51,6 +53,7 @@ - (void)sendTelemetryEvent:(GDTCOREvent *)event {
5153
copiedEvent.qosTier = GDTCOREventQoSTelemetry;
5254
copiedEvent.clockSnapshot = [GDTCORClock snapshot];
5355
[self.transformerInstance transformEvent:copiedEvent withTransformers:_transformers];
56+
GDTCORLogDebug("Telemetry event sent: %@", event);
5457
}
5558

5659
- (void)sendDataEvent:(GDTCOREvent *)event {
@@ -60,6 +63,7 @@ - (void)sendDataEvent:(GDTCOREvent *)event {
6063
GDTCOREvent *copiedEvent = [event copy];
6164
copiedEvent.clockSnapshot = [GDTCORClock snapshot];
6265
[self.transformerInstance transformEvent:copiedEvent withTransformers:_transformers];
66+
GDTCORLogDebug("Data event sent: %@", event);
6367
}
6468

6569
- (GDTCOREvent *)eventForTransport {

GoogleDataTransport/GDTCORLibrary/GDTCORUploadCoordinator.m

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ - (instancetype)init {
5151

5252
- (void)forceUploadForTarget:(GDTCORTarget)target {
5353
dispatch_async(_coordinationQueue, ^{
54+
GDTCORLogDebug("Forcing an upload of target %ld", (long)target);
5455
GDTCORUploadConditions conditions = [self uploadConditions];
5556
conditions |= GDTCORUploadConditionHighPriority;
5657
[self uploadTargets:@[ @(target) ] conditions:conditions];
@@ -82,9 +83,11 @@ - (void)startTimer {
8283
dispatch_source_set_event_handler(self->_timer, ^{
8384
if (![[GDTCORApplication sharedApplication] isRunningInBackground]) {
8485
GDTCORUploadConditions conditions = [self uploadConditions];
86+
GDTCORLogDebug("%@", @"Upload timer fired");
8587
[self uploadTargets:[self.registrar.targetToUploader allKeys] conditions:conditions];
8688
}
8789
});
90+
GDTCORLogDebug("%@", @"Upload timer started");
8891
dispatch_resume(self->_timer);
8992
});
9093
}
@@ -109,6 +112,7 @@ - (void)uploadTargets:(NSArray<NSNumber *> *)targets conditions:(GDTCORUploadCon
109112
for (NSNumber *target in targets) {
110113
// Don't trigger uploads for targets that have an in-flight package already.
111114
if (self->_targetToInFlightPackages[target]) {
115+
GDTCORLogDebug("Target %@ will not upload, there's an upload in flight", target);
112116
continue;
113117
}
114118
// Ask the uploader if they can upload and do so, if it can.
@@ -118,11 +122,14 @@ - (void)uploadTargets:(NSArray<NSNumber *> *)targets conditions:(GDTCORUploadCon
118122
GDTCORUploadPackage *package = [prioritizer uploadPackageWithConditions:conditions];
119123
if (package.events.count) {
120124
self->_targetToInFlightPackages[target] = package;
125+
GDTCORLogDebug("Package of %ld events is being handed over to an uploader",
126+
(long)package.events.count);
121127
[uploader uploadPackage:package];
122128
} else {
123129
[package completeDelivery];
124130
}
125131
}
132+
GDTCORLogDebug("Target %@ is not ready to upload", target);
126133
}
127134
});
128135
}

0 commit comments

Comments
 (0)