Skip to content

Commit 5a2e88d

Browse files
authored
Remove asserts and replace with GDTLogs (#3535)
* Remove asserts and replace with GDTLogs Should infrequently log to the console in !NDEBUG builds instead of asserting. * Remove tests that check for assertions.
1 parent f3cc48a commit 5a2e88d

File tree

14 files changed

+74
-51
lines changed

14 files changed

+74
-51
lines changed

Firebase/CoreDiagnostics/FIRCDLibrary/FIRCoreDiagnostics.m

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#import <objc/runtime.h>
1818
#include <sys/utsname.h>
1919

20+
#import <GoogleDataTransport/GDTConsoleLogger.h>
2021
#import <GoogleDataTransport/GDTEvent.h>
2122
#import <GoogleDataTransport/GDTTargets.h>
2223
#import <GoogleDataTransport/GDTTransport.h>
@@ -116,15 +117,17 @@ - (NSData *)transportBytes {
116117

117118
// Encode 1 time to determine the size.
118119
if (!pb_encode(&sizestream, logs_proto_mobilesdk_ios_ICoreConfiguration_fields, &_config)) {
119-
NSCAssert(NO, @"Error in nanopb encoding for size: %s", PB_GET_ERROR(&sizestream));
120+
GDTLogError(GDTMCETransportBytesError, @"Error in nanopb encoding for size: %s",
121+
PB_GET_ERROR(&sizestream));
120122
}
121123

122124
// Encode a 2nd time to actually get the bytes from it.
123125
size_t bufferSize = sizestream.bytes_written;
124126
CFMutableDataRef dataRef = CFDataCreateMutable(CFAllocatorGetDefault(), bufferSize);
125127
pb_ostream_t ostream = pb_ostream_from_buffer((void *)CFDataGetBytePtr(dataRef), bufferSize);
126128
if (!pb_encode(&ostream, logs_proto_mobilesdk_ios_ICoreConfiguration_fields, &_config)) {
127-
NSCAssert(NO, @"Error in nanopb encoding for bytes: %s", PB_GET_ERROR(&ostream));
129+
GDTLogError(GDTMCETransportBytesError, @"Error in nanopb encoding for bytes: %s",
130+
PB_GET_ERROR(&ostream));
128131
}
129132
CFDataSetLength(dataRef, ostream.bytes_written);
130133

@@ -370,8 +373,9 @@ void FIRPopulateProtoWithCommonInfoFromApp(logs_proto_mobilesdk_ios_ICoreConfigu
370373
config->pod_name = logs_proto_mobilesdk_ios_ICoreConfiguration_PodName_FIREBASE;
371374
config->has_pod_name = 1;
372375

373-
NSCAssert(diagnosticObjects[kFIRCDllAppsCountKey],
374-
@"App count is a required value in the data dict.");
376+
if (!diagnosticObjects[kFIRCDllAppsCountKey]) {
377+
GDTLogError(GDTMCEGeneralError, @"%@", @"App count is a required value in the data dict.");
378+
}
375379
config->app_count = (int32_t)[diagnosticObjects[kFIRCDllAppsCountKey] integerValue];
376380
config->has_app_count = 1;
377381

GoogleDataTransport/GDTLibrary/GDTConsoleLogger.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
#import "GDTLibrary/Private/GDTConsoleLogger.h"
17+
#import "GDTLibrary/Public/GDTConsoleLogger.h"
1818

1919
/** The console logger prefix. */
2020
static NSString *kGDTConsoleLogger = @"[GoogleDataTransport]";

GoogleDataTransport/GDTLibrary/GDTPlatform.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ @implementation GDTApplication
3939

4040
+ (void)load {
4141
#if TARGET_OS_IOS || TARGET_OS_TV
42+
// If this asserts, please file a bug at https://github.com/firebase/firebase-ios-sdk/issues.
4243
NSAssert(GDTBackgroundIdentifierInvalid == UIBackgroundTaskInvalid,
4344
@"GDTBackgroundIdentifierInvalid and UIBackgroundTaskInvalid should be the same.");
4445
#endif

GoogleDataTransport/GDTLibrary/GDTReachability.m

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#import "GDTLibrary/Private/GDTReachability.h"
1818
#import "GDTLibrary/Private/GDTReachability_Private.h"
1919

20+
#import <GoogleDataTransport/GDTConsoleLogger.h>
21+
2022
#import <netinet/in.h>
2123

2224
/** Sets the _callbackFlag ivar whenever the network changes.
@@ -74,9 +76,13 @@ - (instancetype)init {
7476
_reachabilityRef = SCNetworkReachabilityCreateWithAddress(
7577
kCFAllocatorDefault, (const struct sockaddr *)&zeroAddress);
7678
Boolean success = SCNetworkReachabilitySetDispatchQueue(_reachabilityRef, _reachabilityQueue);
77-
NSAssert(success, @"The reachability queue wasn't set.");
79+
if (!success) {
80+
GDTLogWarning(GDTMCWReachabilityFailed, @"%@", @"The reachability queue wasn't set.");
81+
}
7882
success = SCNetworkReachabilitySetCallback(_reachabilityRef, GDTReachabilityCallback, NULL);
79-
NSAssert(success, @"The reachability callback wasn't set.");
83+
if (!success) {
84+
GDTLogWarning(GDTMCWReachabilityFailed, @"%@", @"The reachability callback wasn't set.");
85+
}
8086

8187
// Get the initial set of flags.
8288
dispatch_async(_reachabilityQueue, ^{

GoogleDataTransport/GDTLibrary/GDTStorage.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@
1717
#import "GDTLibrary/Private/GDTStorage.h"
1818
#import "GDTLibrary/Private/GDTStorage_Private.h"
1919

20+
#import <GoogleDataTransport/GDTConsoleLogger.h>
2021
#import <GoogleDataTransport/GDTLifecycle.h>
2122
#import <GoogleDataTransport/GDTPrioritizer.h>
2223
#import <GoogleDataTransport/GDTStoredEvent.h>
2324

2425
#import "GDTLibrary/Private/GDTAssert.h"
25-
#import "GDTLibrary/Private/GDTConsoleLogger.h"
2626
#import "GDTLibrary/Private/GDTEvent_Private.h"
2727
#import "GDTLibrary/Private/GDTRegistrar_Private.h"
2828
#import "GDTLibrary/Private/GDTUploadCoordinator.h"

GoogleDataTransport/GDTLibrary/GDTTransformer.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@
1717
#import "GDTLibrary/Private/GDTTransformer.h"
1818
#import "GDTLibrary/Private/GDTTransformer_Private.h"
1919

20+
#import <GoogleDataTransport/GDTConsoleLogger.h>
2021
#import <GoogleDataTransport/GDTEventTransformer.h>
2122
#import <GoogleDataTransport/GDTLifecycle.h>
2223

2324
#import "GDTLibrary/Private/GDTAssert.h"
24-
#import "GDTLibrary/Private/GDTConsoleLogger.h"
2525
#import "GDTLibrary/Private/GDTStorage.h"
2626

2727
@implementation GDTTransformer

GoogleDataTransport/GDTLibrary/GDTUploadCoordinator.m

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
#import "GDTLibrary/Private/GDTUploadCoordinator.h"
1818

1919
#import <GoogleDataTransport/GDTClock.h>
20+
#import <GoogleDataTransport/GDTConsoleLogger.h>
2021

2122
#import "GDTLibrary/Private/GDTAssert.h"
22-
#import "GDTLibrary/Private/GDTConsoleLogger.h"
2323
#import "GDTLibrary/Private/GDTReachability.h"
2424
#import "GDTLibrary/Private/GDTRegistrar_Private.h"
2525
#import "GDTLibrary/Private/GDTStorage.h"
@@ -212,7 +212,10 @@ - (void)packageDelivered:(GDTUploadPackage *)package successful:(BOOL)successful
212212
NSNumber *targetNumber = @(package.target);
213213
[self->_targetToInFlightPackages removeObjectForKey:targetNumber];
214214
id<GDTPrioritizer> prioritizer = self->_registrar.targetToPrioritizer[targetNumber];
215-
NSAssert(prioritizer, @"A prioritizer should be registered for this target: %@", targetNumber);
215+
if (!prioritizer) {
216+
GDTLogError(GDTMCEPrioritizerError, @"A prioritizer should be registered for this target: %@",
217+
targetNumber);
218+
}
216219
if ([prioritizer respondsToSelector:@selector(packageDelivered:successful:)]) {
217220
[prioritizer packageDelivered:package successful:successful];
218221
}

GoogleDataTransport/GDTLibrary/GDTUploadPackage.m

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#import <GoogleDataTransport/GDTUploadPackage.h>
1818

1919
#import <GoogleDataTransport/GDTClock.h>
20+
#import <GoogleDataTransport/GDTConsoleLogger.h>
2021

2122
#import "GDTLibrary/Private/GDTStorage_Private.h"
2223
#import "GDTLibrary/Private/GDTUploadCoordinator.h"
@@ -74,7 +75,10 @@ - (void)setStorage:(GDTStorage *)storage {
7475
}
7576

7677
- (void)completeDelivery {
77-
NSAssert(_isDelivered == NO, @"It's an API violation to call -completeDelivery twice.");
78+
if (_isDelivered) {
79+
GDTLogError(GDTMCEDeliverTwice, @"%@",
80+
@"It's an API violation to call -completeDelivery twice.");
81+
}
7882
_isDelivered = YES;
7983
if (!_isHandled && _handler &&
8084
[_handler respondsToSelector:@selector(packageDelivered:successful:)]) {

GoogleDataTransport/GDTLibrary/Private/GDTConsoleLogger.h renamed to GoogleDataTransport/GDTLibrary/Public/GDTConsoleLogger.h

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
#import "GDTLibrary/Private/GDTAssert.h"
17+
#import <Foundation/Foundation.h>
1818

1919
/** A list of message codes to print in the logger that help to correspond printed messages with
2020
* code locations.
@@ -34,14 +34,29 @@ typedef NS_ENUM(NSInteger, GDTMessageCode) {
3434
/** For warning messages concerning a forced event upload. */
3535
GDTMCWForcedUpload = 3,
3636

37+
/** For warning messages concerning a failed reachability call. */
38+
GDTMCWReachabilityFailed = 4,
39+
3740
/** For error messages concerning transform: not being implemented by an event transformer. */
3841
GDTMCETransformerDoesntImplementTransform = 1000,
3942

4043
/** For error messages concerning the creation of a directory failing. */
4144
GDTMCEDirectoryCreationError = 1001,
4245

4346
/** For error messages concerning the writing of a event file. */
44-
GDTMCEFileWriteError = 1002
47+
GDTMCEFileWriteError = 1002,
48+
49+
/** For error messages concerning the lack of a prioritizer for a given backend. */
50+
GDTMCEPrioritizerError = 1003,
51+
52+
/** For error messages concerning a package delivery API violation. */
53+
GDTMCEDeliverTwice = 1004,
54+
55+
/** For error messages concerning an error in an implementation of -transportBytes. */
56+
GDTMCETransportBytesError = 1005,
57+
58+
/** For general purpose error messages in a dependency. */
59+
GDTMCEGeneralError = 1006
4560
};
4661

4762
/** */
@@ -61,5 +76,4 @@ FOUNDATION_EXPORT NSString *_Nonnull GDTMessageCodeEnumToString(GDTMessageCode c
6176

6277
// A define to wrap GULLogError with slightly more convenient usage and a failing assert.
6378
#define GDTLogError(MESSAGE_CODE, MESSAGE_FORMAT, ...) \
64-
GDTLog(MESSAGE_CODE, MESSAGE_FORMAT, __VA_ARGS__); \
65-
GDTAssert(NO, MESSAGE_FORMAT, __VA_ARGS__);
79+
GDTLog(MESSAGE_CODE, MESSAGE_FORMAT, __VA_ARGS__);

GoogleDataTransport/GDTLibrary/Public/GoogleDataTransport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616

1717
#import "GDTClock.h"
18+
#import "GDTConsoleLogger.h"
1819
#import "GDTDataFuture.h"
1920
#import "GDTEvent.h"
2021
#import "GDTEventDataObject.h"

0 commit comments

Comments
 (0)