GDTCORAssert.h 4.7 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, format, ...) \
  41. do { \
  42. __PRAGMA_PUSH_NO_EXTRA_ARG_WARNINGS \
  43. if (__builtin_expect(!(condition), 0)) { \
  44. GDTCORAssertionBlock assertionBlock = GDTCORAssertionBlockToRunInstead(); \
  45. if (assertionBlock) { \
  46. assertionBlock(); \
  47. } else { \
  48. NSString *__assert_file__ = [NSString stringWithUTF8String:__FILE__]; \
  49. __assert_file__ = __assert_file__ ? __assert_file__ : @"<Unknown File>"; \
  50. GDTCORLogAssert(NO, __assert_file__, __LINE__, format, ##__VA_ARGS__); \
  51. __PRAGMA_POP_NO_EXTRA_ARG_WARNINGS \
  52. } \
  53. } \
  54. } while (0);
  55. /** Asserts by logging to the console and throwing an exception if NS_BLOCK_ASSERTIONS is not
  56. * defined.
  57. *
  58. * @param condition The condition you'd expect to be YES.
  59. */
  60. #define GDTCORFatalAssert(condition, format, ...) \
  61. do { \
  62. __PRAGMA_PUSH_NO_EXTRA_ARG_WARNINGS \
  63. if (__builtin_expect(!(condition), 0)) { \
  64. GDTCORAssertionBlock assertionBlock = GDTCORAssertionBlockToRunInstead(); \
  65. if (assertionBlock) { \
  66. assertionBlock(); \
  67. } else { \
  68. NSString *__assert_file__ = [NSString stringWithUTF8String:__FILE__]; \
  69. __assert_file__ = __assert_file__ ? __assert_file__ : @"<Unknown File>"; \
  70. GDTCORLogAssert(YES, __assert_file__, __LINE__, format, ##__VA_ARGS__); \
  71. [[NSAssertionHandler currentHandler] handleFailureInMethod:_cmd \
  72. object:self \
  73. file:__assert_file__ \
  74. lineNumber:__LINE__ \
  75. description:format, ##__VA_ARGS__]; \
  76. __PRAGMA_POP_NO_EXTRA_ARG_WARNINGS \
  77. } \
  78. } \
  79. } while (0);
  80. #endif // defined(NS_BLOCK_ASSERTIONS)