DDAssert.swift 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // Software License Agreement (BSD License)
  2. //
  3. // Copyright (c) 2010-2024, Deusty, LLC
  4. // All rights reserved.
  5. //
  6. // Redistribution and use of this software in source and binary forms,
  7. // with or without modification, are permitted provided that the following conditions are met:
  8. //
  9. // * Redistributions of source code must retain the above copyright notice,
  10. // this list of conditions and the following disclaimer.
  11. //
  12. // * Neither the name of Deusty nor the names of its contributors may be used
  13. // to endorse or promote products derived from this software without specific
  14. // prior written permission of Deusty, LLC.
  15. #if SWIFT_PACKAGE
  16. import CocoaLumberjack
  17. import CocoaLumberjackSwiftSupport
  18. #endif
  19. /**
  20. * Replacement for Swift's `assert` function that will output a log message even when assertions
  21. * are disabled.
  22. *
  23. * - Parameters:
  24. * - condition: The condition to test. Unlike `Swift.assert`, `condition` is always evaluated,
  25. * even when assertions are disabled.
  26. * - message: A string to log (using `DDLogError`) if `condition` evaluates to `false`.
  27. * The default is an empty string.
  28. */
  29. @inlinable
  30. public func DDAssert(_ condition: @autoclosure () -> Bool,
  31. _ message: @autoclosure () -> DDLogMessageFormat = "",
  32. level: DDLogLevel = DDDefaultLogLevel,
  33. context: Int = 0,
  34. file: StaticString = #file,
  35. function: StaticString = #function,
  36. line: UInt = #line,
  37. tag: Any? = nil,
  38. asynchronous async: Bool = false,
  39. ddlog: DDLog = DDLog.sharedInstance) {
  40. if !condition() {
  41. DDLogError(message(),
  42. level: level,
  43. context: context,
  44. file: file,
  45. function: function,
  46. line: line,
  47. tag: tag,
  48. asynchronous: async,
  49. ddlog: ddlog)
  50. Swift.assertionFailure(message().formatted, file: file, line: line)
  51. }
  52. }
  53. /**
  54. * Replacement for Swift's `assertionFailure` function that will output a log message even
  55. * when assertions are disabled.
  56. *
  57. * - Parameters:
  58. * - message: A string to log (using `DDLogError`). The default is an empty string.
  59. */
  60. @inlinable
  61. public func DDAssertionFailure(_ message: @autoclosure () -> DDLogMessageFormat = "",
  62. level: DDLogLevel = DDDefaultLogLevel,
  63. context: Int = 0,
  64. file: StaticString = #file,
  65. function: StaticString = #function,
  66. line: UInt = #line,
  67. tag: Any? = nil,
  68. asynchronous async: Bool = false,
  69. ddlog: DDLog = DDLog.sharedInstance) {
  70. DDLogError(message(),
  71. level: level,
  72. context: context,
  73. file: file,
  74. function: function,
  75. line: line,
  76. tag: tag,
  77. asynchronous: async,
  78. ddlog: ddlog)
  79. Swift.assertionFailure(message().formatted, file: file, line: line)
  80. }
  81. /**
  82. * Replacement for Swift's `assert` function that will output a log message even when assertions
  83. * are disabled.
  84. *
  85. * - Parameters:
  86. * - condition: The condition to test. Unlike `Swift.assert`, `condition` is always evaluated,
  87. * even when assertions are disabled.
  88. * - message: A string to log (using `DDLogError`) if `condition` evaluates to `false`.
  89. * The default is an empty string.
  90. */
  91. @inlinable
  92. @available(*, deprecated, message: "Use an interpolated message.")
  93. public func DDAssert(_ condition: @autoclosure () -> Bool,
  94. _ message: @autoclosure () -> String = "",
  95. level: DDLogLevel = DDDefaultLogLevel,
  96. context: Int = 0,
  97. file: StaticString = #file,
  98. function: StaticString = #function,
  99. line: UInt = #line,
  100. tag: Any? = nil,
  101. asynchronous async: Bool = false,
  102. ddlog: DDLog = DDLog.sharedInstance) {
  103. if !condition() {
  104. DDLogError(message(),
  105. level: level,
  106. context: context,
  107. file: file,
  108. function: function,
  109. line: line,
  110. tag: tag,
  111. asynchronous: async,
  112. ddlog: ddlog)
  113. Swift.assertionFailure(message(), file: file, line: line)
  114. }
  115. }
  116. /**
  117. * Replacement for Swift's `assertionFailure` function that will output a log message even
  118. * when assertions are disabled.
  119. *
  120. * - Parameters:
  121. * - message: A string to log (using `DDLogError`). The default is an empty string.
  122. */
  123. @inlinable
  124. @available(*, deprecated, message: "Use an interpolated message.")
  125. public func DDAssertionFailure(_ message: @autoclosure () -> String = "",
  126. level: DDLogLevel = DDDefaultLogLevel,
  127. context: Int = 0,
  128. file: StaticString = #file,
  129. function: StaticString = #function,
  130. line: UInt = #line,
  131. tag: Any? = nil,
  132. asynchronous async: Bool = false,
  133. ddlog: DDLog = DDLog.sharedInstance) {
  134. DDLogError(message(),
  135. level: level,
  136. context: context,
  137. file: file,
  138. function: function,
  139. line: line,
  140. tag: tag,
  141. asynchronous: async,
  142. ddlog: ddlog)
  143. Swift.assertionFailure(message(), file: file, line: line)
  144. }