|
| 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