Skip to content

Commit 0609592

Browse files
authored
Transition from NSAssert to GDTAssert in all GDT libraries (#3768)
* Git rid of all NSAsserts in GDT and GDTCCT - Moves GDTAssert to be a public API - Always logs to console - Differentiates between fatal and non-fatal assertions - Messaging of assertions encourages creating an issue - Tests assertion use with NS_BLOCK_ASSERTIONS defined and undefined * Update CHANGELOGs and remove one last NSCAssert * Remove a test because xcconfigs aren't respected * Address nit
1 parent 0d0f905 commit 0609592

File tree

19 files changed

+203
-94
lines changed

19 files changed

+203
-94
lines changed

GoogleDataTransport/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# v1.2.0
2+
- Removes all NSAsserts in favor of custom asserts. (#3747)
3+
14
# v1.1.3
25
- Wrap decoding in GDTUploadCoordinator in a try catch. (#3676)
36

GoogleDataTransport/GDTLibrary/GDTAssert.m

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

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

19-
GDTAssertionBlock GDTAssertionBlockToRunInsteadOfNSAssert(void) {
19+
GDTAssertionBlock GDTAssertionBlockToRunInstead(void) {
2020
// This class is only compiled in by unit tests, and this should fail quickly in optimized builds.
2121
Class GDTAssertClass = NSClassFromString(@"GDTAssertHelper");
2222
if (__builtin_expect(!!GDTAssertClass, 0)) {

GoogleDataTransport/GDTLibrary/GDTEvent.m

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,19 @@
1616

1717
#import <GoogleDataTransport/GDTEvent.h>
1818

19+
#import <GoogleDataTransport/GDTAssert.h>
1920
#import <GoogleDataTransport/GDTStoredEvent.h>
2021

21-
#import "GDTLibrary/Private/GDTAssert.h"
2222
#import "GDTLibrary/Private/GDTEvent_Private.h"
2323

2424
@implementation GDTEvent
2525

2626
- (instancetype)initWithMappingID:(NSString *)mappingID target:(NSInteger)target {
2727
GDTAssert(mappingID.length > 0, @"Please give a valid mapping ID");
2828
GDTAssert(target > 0, @"A target cannot be negative or 0");
29+
if (mappingID == nil || mappingID.length == 0 || target <= 0) {
30+
return nil;
31+
}
2932
self = [super init];
3033
if (self) {
3134
_mappingID = mappingID;

GoogleDataTransport/GDTLibrary/GDTPlatform.m

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
#import <GoogleDataTransport/GDTPlatform.h>
1818

19+
#import <GoogleDataTransport/GDTAssert.h>
20+
1921
const GDTBackgroundIdentifier GDTBackgroundIdentifierInvalid = 0;
2022

2123
NSString *const kGDTApplicationDidEnterBackgroundNotification =
@@ -40,8 +42,8 @@ @implementation GDTApplication
4042
+ (void)load {
4143
#if TARGET_OS_IOS || TARGET_OS_TV
4244
// If this asserts, please file a bug at https://github.com/firebase/firebase-ios-sdk/issues.
43-
NSAssert(GDTBackgroundIdentifierInvalid == UIBackgroundTaskInvalid,
44-
@"GDTBackgroundIdentifierInvalid and UIBackgroundTaskInvalid should be the same.");
45+
GDTFatalAssert(GDTBackgroundIdentifierInvalid == UIBackgroundTaskInvalid,
46+
@"GDTBackgroundIdentifierInvalid and UIBackgroundTaskInvalid should be the same.");
4547
#endif
4648
[self sharedApplication];
4749
}

GoogleDataTransport/GDTLibrary/GDTStorage.m

Lines changed: 5 additions & 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/GDTAssert.h>
2021
#import <GoogleDataTransport/GDTConsoleLogger.h>
2122
#import <GoogleDataTransport/GDTLifecycle.h>
2223
#import <GoogleDataTransport/GDTPrioritizer.h>
2324
#import <GoogleDataTransport/GDTStoredEvent.h>
2425

25-
#import "GDTLibrary/Private/GDTAssert.h"
2626
#import "GDTLibrary/Private/GDTEvent_Private.h"
2727
#import "GDTLibrary/Private/GDTRegistrar_Private.h"
2828
#import "GDTLibrary/Private/GDTUploadCoordinator.h"
@@ -74,6 +74,10 @@ - (instancetype)init {
7474
}
7575

7676
- (void)storeEvent:(GDTEvent *)event {
77+
if (event == nil) {
78+
return;
79+
}
80+
7781
[self createEventDirectoryIfNotExists];
7882

7983
__block GDTBackgroundIdentifier bgID = GDTBackgroundIdentifierInvalid;

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/GDTAssert.h>
2021
#import <GoogleDataTransport/GDTConsoleLogger.h>
2122
#import <GoogleDataTransport/GDTEventTransformer.h>
2223
#import <GoogleDataTransport/GDTLifecycle.h>
2324

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

2727
@implementation GDTTransformer

GoogleDataTransport/GDTLibrary/GDTTransport.m

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,24 @@
1717
#import <GoogleDataTransport/GDTTransport.h>
1818
#import "GDTLibrary/Private/GDTTransport_Private.h"
1919

20+
#import <GoogleDataTransport/GDTAssert.h>
2021
#import <GoogleDataTransport/GDTClock.h>
2122
#import <GoogleDataTransport/GDTEvent.h>
2223

23-
#import "GDTLibrary/Private/GDTAssert.h"
2424
#import "GDTLibrary/Private/GDTTransformer.h"
2525

2626
@implementation GDTTransport
2727

2828
- (instancetype)initWithMappingID:(NSString *)mappingID
2929
transformers:(nullable NSArray<id<GDTEventTransformer>> *)transformers
3030
target:(NSInteger)target {
31+
GDTAssert(mappingID.length > 0, @"A mapping ID cannot be nil or empty");
32+
GDTAssert(target > 0, @"A target cannot be negative or 0");
33+
if (mappingID == nil || mappingID.length == 0 || target <= 0) {
34+
return nil;
35+
}
3136
self = [super init];
3237
if (self) {
33-
GDTAssert(mappingID.length > 0, @"A mapping ID cannot be nil or empty");
34-
GDTAssert(target > 0, @"A target cannot be negative or 0");
3538
_mappingID = mappingID;
3639
_transformers = transformers;
3740
_target = target;

GoogleDataTransport/GDTLibrary/GDTUploadCoordinator.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616

1717
#import "GDTLibrary/Private/GDTUploadCoordinator.h"
1818

19+
#import <GoogleDataTransport/GDTAssert.h>
1920
#import <GoogleDataTransport/GDTClock.h>
2021
#import <GoogleDataTransport/GDTConsoleLogger.h>
2122

22-
#import "GDTLibrary/Private/GDTAssert.h"
2323
#import "GDTLibrary/Private/GDTReachability.h"
2424
#import "GDTLibrary/Private/GDTRegistrar_Private.h"
2525
#import "GDTLibrary/Private/GDTStorage.h"

GoogleDataTransport/GDTLibrary/Private/GDTAssert.h

Lines changed: 0 additions & 54 deletions
This file was deleted.
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* Copyright 2019 Google
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#import <Foundation/Foundation.h>
18+
19+
#import <GoogleDataTransport/GDTConsoleLogger.h>
20+
21+
/** A block type that could be run instead of normal assertion logging. No return type, no params.
22+
*/
23+
typedef void (^GDTAssertionBlock)(void);
24+
25+
/** Returns the result of executing a soft-linked method present in unit tests that allows a block
26+
* to be run instead of normal assertion logging. This helps ameliorate issues with catching
27+
* exceptions that occur on a dispatch_queue.
28+
*
29+
* @return A block that can be run instead of normal assert printing.
30+
*/
31+
FOUNDATION_EXPORT GDTAssertionBlock _Nullable GDTAssertionBlockToRunInstead(void);
32+
33+
#if defined(NS_BLOCK_ASSERTIONS)
34+
35+
#define GDTAssert(condition, ...) \
36+
do { \
37+
} while (0);
38+
39+
#define GDTFatalAssert(condition, ...) \
40+
do { \
41+
} while (0);
42+
43+
#else // defined(NS_BLOCK_ASSERTIONS)
44+
45+
/** Asserts using a console log, unless a block was specified to be run instead.
46+
*
47+
* @param condition The condition you'd expect to be YES.
48+
*/
49+
#define GDTAssert(condition, ...) \
50+
do { \
51+
if (__builtin_expect(!(condition), 0)) { \
52+
GDTAssertionBlock assertionBlock = GDTAssertionBlockToRunInstead(); \
53+
if (assertionBlock) { \
54+
assertionBlock(); \
55+
} else { \
56+
__PRAGMA_PUSH_NO_EXTRA_ARG_WARNINGS \
57+
NSString *__assert_file__ = [NSString stringWithUTF8String:__FILE__]; \
58+
__assert_file__ = __assert_file__ ? __assert_file__ : @"<Unknown File>"; \
59+
GDTLogError(GDTMCEGeneralError, @"Assertion failed (%@:%d): %s,", __assert_file__, \
60+
__LINE__, ##__VA_ARGS__); \
61+
__PRAGMA_POP_NO_EXTRA_ARG_WARNINGS \
62+
} \
63+
} \
64+
} while (0);
65+
66+
/** Asserts by logging to the console and throwing an exception if NS_BLOCK_ASSERTIONS is not
67+
* defined.
68+
*
69+
* @param condition The condition you'd expect to be YES.
70+
*/
71+
#define GDTFatalAssert(condition, ...) \
72+
do { \
73+
__PRAGMA_PUSH_NO_EXTRA_ARG_WARNINGS \
74+
if (__builtin_expect(!(condition), 0)) { \
75+
NSString *__assert_file__ = [NSString stringWithUTF8String:__FILE__]; \
76+
__assert_file__ = __assert_file__ ? __assert_file__ : @"<Unknown File>"; \
77+
GDTLogError(GDTMCEFatalAssertion, \
78+
@"Fatal assertion encountered, please open an issue at " \
79+
"https://github.com/firebase/firebase-ios-sdk/issues " \
80+
"(%@:%d): %s,", \
81+
__assert_file__, __LINE__, ##__VA_ARGS__); \
82+
[[NSAssertionHandler currentHandler] handleFailureInMethod:_cmd \
83+
object:self \
84+
file:__assert_file__ \
85+
lineNumber:__LINE__ \
86+
description:@"%@", ##__VA_ARGS__]; \
87+
} \
88+
__PRAGMA_POP_NO_EXTRA_ARG_WARNINGS \
89+
} while (0);
90+
91+
#endif // defined(NS_BLOCK_ASSERTIONS)

0 commit comments

Comments
 (0)