GDTCORAssert.h 4.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. #import <Foundation/Foundation.h>
  17. #import <GoogleDataTransport/GDTCORConsoleLogger.h>
  18. /** A block type that could be run instead of normal assertion logging. No return type, no params.
  19. */
  20. typedef void (^GDTCORAssertionBlock)(void);
  21. /** Returns the result of executing a soft-linked method present in unit tests that allows a block
  22. * to be run instead of normal assertion logging. This helps ameliorate issues with catching
  23. * exceptions that occur on a dispatch_queue.
  24. *
  25. * @return A block that can be run instead of normal assert printing.
  26. */
  27. FOUNDATION_EXPORT GDTCORAssertionBlock _Nullable GDTCORAssertionBlockToRunInstead(void);
  28. #if defined(NS_BLOCK_ASSERTIONS)
  29. #define GDTCORAssert(condition, ...) \
  30. do { \
  31. } while (0);
  32. #define GDTCORFatalAssert(condition, ...) \
  33. do { \
  34. } while (0);
  35. #else // defined(NS_BLOCK_ASSERTIONS)
  36. /** Asserts using a console log, unless a block was specified to be run instead.
  37. *
  38. * @param condition The condition you'd expect to be YES.
  39. */
  40. #define GDTCORAssert(condition, ...) \
  41. do { \
  42. if (__builtin_expect(!(condition), 0)) { \
  43. GDTCORAssertionBlock assertionBlock = GDTCORAssertionBlockToRunInstead(); \
  44. if (assertionBlock) { \
  45. assertionBlock(); \
  46. } else { \
  47. __PRAGMA_PUSH_NO_EXTRA_ARG_WARNINGS \
  48. NSString *__assert_file__ = [NSString stringWithUTF8String:__FILE__]; \
  49. __assert_file__ = __assert_file__ ? __assert_file__ : @"<Unknown File>"; \
  50. GDTCORLogError(GDTCORMCEGeneralError, @"Assertion failed (%@:%d): %@,", __assert_file__, \
  51. __LINE__, ##__VA_ARGS__); \
  52. __PRAGMA_POP_NO_EXTRA_ARG_WARNINGS \
  53. } \
  54. } \
  55. } while (0);
  56. /** Asserts by logging to the console and throwing an exception if NS_BLOCK_ASSERTIONS is not
  57. * defined.
  58. *
  59. * @param condition The condition you'd expect to be YES.
  60. */
  61. #define GDTCORFatalAssert(condition, ...) \
  62. do { \
  63. __PRAGMA_PUSH_NO_EXTRA_ARG_WARNINGS \
  64. if (__builtin_expect(!(condition), 0)) { \
  65. NSString *__assert_file__ = [NSString stringWithUTF8String:__FILE__]; \
  66. __assert_file__ = __assert_file__ ? __assert_file__ : @"<Unknown File>"; \
  67. GDTCORLogError(GDTCORMCEFatalAssertion, \
  68. @"Fatal assertion encountered, please open an issue at " \
  69. "https://github.com/firebase/firebase-ios-sdk/issues " \
  70. "(%@:%d): %@,", \
  71. __assert_file__, __LINE__, ##__VA_ARGS__); \
  72. [[NSAssertionHandler currentHandler] handleFailureInMethod:_cmd \
  73. object:self \
  74. file:__assert_file__ \
  75. lineNumber:__LINE__ \
  76. description:@"%@", ##__VA_ARGS__]; \
  77. } \
  78. __PRAGMA_POP_NO_EXTRA_ARG_WARNINGS \
  79. } while (0);
  80. #endif // defined(NS_BLOCK_ASSERTIONS)