GDTCORAssert.h 4.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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/GDTCORLibrary/Public/GoogleDataTransport/GDTCORConsoleLogger.h"
  18. NS_ASSUME_NONNULL_BEGIN
  19. /** A block type that could be run instead of normal assertion logging. No return type, no params.
  20. */
  21. typedef void (^GDTCORAssertionBlock)(void);
  22. /** Returns the result of executing a soft-linked method present in unit tests that allows a block
  23. * to be run instead of normal assertion logging. This helps ameliorate issues with catching
  24. * exceptions that occur on a dispatch_queue.
  25. *
  26. * @return A block that can be run instead of normal assert printing.
  27. */
  28. FOUNDATION_EXPORT GDTCORAssertionBlock _Nullable GDTCORAssertionBlockToRunInstead(void);
  29. #if defined(NS_BLOCK_ASSERTIONS)
  30. #define GDTCORAssert(condition, ...) \
  31. do { \
  32. } while (0);
  33. #define GDTCORFatalAssert(condition, ...) \
  34. do { \
  35. } while (0);
  36. #else // defined(NS_BLOCK_ASSERTIONS)
  37. /** Asserts using a console log, unless a block was specified to be run instead.
  38. *
  39. * @param condition The condition you'd expect to be YES.
  40. */
  41. #define GDTCORAssert(condition, format, ...) \
  42. do { \
  43. __PRAGMA_PUSH_NO_EXTRA_ARG_WARNINGS \
  44. if (__builtin_expect(!(condition), 0)) { \
  45. GDTCORAssertionBlock assertionBlock = GDTCORAssertionBlockToRunInstead(); \
  46. if (assertionBlock) { \
  47. assertionBlock(); \
  48. } else { \
  49. NSString *__assert_file__ = [NSString stringWithUTF8String:__FILE__]; \
  50. __assert_file__ = __assert_file__ ? __assert_file__ : @"<Unknown File>"; \
  51. GDTCORLogAssert(NO, __assert_file__, __LINE__, format, ##__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, format, ...) \
  62. do { \
  63. __PRAGMA_PUSH_NO_EXTRA_ARG_WARNINGS \
  64. if (__builtin_expect(!(condition), 0)) { \
  65. GDTCORAssertionBlock assertionBlock = GDTCORAssertionBlockToRunInstead(); \
  66. if (assertionBlock) { \
  67. assertionBlock(); \
  68. } else { \
  69. NSString *__assert_file__ = [NSString stringWithUTF8String:__FILE__]; \
  70. __assert_file__ = __assert_file__ ? __assert_file__ : @"<Unknown File>"; \
  71. GDTCORLogAssert(YES, __assert_file__, __LINE__, format, ##__VA_ARGS__); \
  72. [[NSAssertionHandler currentHandler] handleFailureInMethod:_cmd \
  73. object:self \
  74. file:__assert_file__ \
  75. lineNumber:__LINE__ \
  76. description:format, ##__VA_ARGS__]; \
  77. __PRAGMA_POP_NO_EXTRA_ARG_WARNINGS \
  78. } \
  79. } \
  80. } while (0);
  81. #endif // defined(NS_BLOCK_ASSERTIONS)
  82. NS_ASSUME_NONNULL_END