DDAssert.swift 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Software License Agreement (BSD License)
  2. //
  3. // Copyright (c) 2010-2022, 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 () -> String = "",
  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(), 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 () -> String = "",
  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(), file: file, line: line)
  80. }