Debugging.swift 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. //
  2. // SnapKit
  3. //
  4. // Copyright (c) 2011-Present SnapKit Team - https://github.com/SnapKit
  5. //
  6. // Permission is hereby granted, free of charge, to any person obtaining a copy
  7. // of this software and associated documentation files (the "Software"), to deal
  8. // in the Software without restriction, including without limitation the rights
  9. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. // copies of the Software, and to permit persons to whom the Software is
  11. // furnished to do so, subject to the following conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be included in
  14. // all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. // THE SOFTWARE.
  23. #if os(iOS) || os(tvOS)
  24. import UIKit
  25. #else
  26. import AppKit
  27. #endif
  28. public extension LayoutConstraint {
  29. override var description: String {
  30. var description = "<"
  31. description += descriptionForObject(self)
  32. if let firstItem = conditionalOptional(from: self.firstItem) {
  33. description += " \(descriptionForObject(firstItem))"
  34. }
  35. if self.firstAttribute != .notAnAttribute {
  36. description += ".\(descriptionForAttribute(self.firstAttribute))"
  37. }
  38. description += " \(descriptionForRelation(self.relation))"
  39. if let secondItem = self.secondItem {
  40. description += " \(descriptionForObject(secondItem))"
  41. }
  42. if self.secondAttribute != .notAnAttribute {
  43. description += ".\(descriptionForAttribute(self.secondAttribute))"
  44. }
  45. if self.multiplier != 1.0 {
  46. description += " * \(self.multiplier)"
  47. }
  48. if self.secondAttribute == .notAnAttribute {
  49. description += " \(self.constant)"
  50. } else {
  51. if self.constant > 0.0 {
  52. description += " + \(self.constant)"
  53. } else if self.constant < 0.0 {
  54. description += " - \(abs(self.constant))"
  55. }
  56. }
  57. if self.priority.rawValue != 1000.0 {
  58. description += " ^\(self.priority)"
  59. }
  60. description += ">"
  61. return description
  62. }
  63. }
  64. private func descriptionForRelation(_ relation: LayoutRelation) -> String {
  65. switch relation {
  66. case .equal: return "=="
  67. case .greaterThanOrEqual: return ">="
  68. case .lessThanOrEqual: return "<="
  69. #if swift(>=5.0)
  70. @unknown default: return "unknown"
  71. #endif
  72. }
  73. }
  74. private func descriptionForAttribute(_ attribute: LayoutAttribute) -> String {
  75. #if os(iOS) || os(tvOS)
  76. switch attribute {
  77. case .notAnAttribute: return "notAnAttribute"
  78. case .top: return "top"
  79. case .left: return "left"
  80. case .bottom: return "bottom"
  81. case .right: return "right"
  82. case .leading: return "leading"
  83. case .trailing: return "trailing"
  84. case .width: return "width"
  85. case .height: return "height"
  86. case .centerX: return "centerX"
  87. case .centerY: return "centerY"
  88. case .lastBaseline: return "lastBaseline"
  89. case .firstBaseline: return "firstBaseline"
  90. case .topMargin: return "topMargin"
  91. case .leftMargin: return "leftMargin"
  92. case .bottomMargin: return "bottomMargin"
  93. case .rightMargin: return "rightMargin"
  94. case .leadingMargin: return "leadingMargin"
  95. case .trailingMargin: return "trailingMargin"
  96. case .centerXWithinMargins: return "centerXWithinMargins"
  97. case .centerYWithinMargins: return "centerYWithinMargins"
  98. #if swift(>=5.0)
  99. @unknown default: return "unknown"
  100. #endif
  101. }
  102. #else
  103. switch attribute {
  104. case .notAnAttribute: return "notAnAttribute"
  105. case .top: return "top"
  106. case .left: return "left"
  107. case .bottom: return "bottom"
  108. case .right: return "right"
  109. case .leading: return "leading"
  110. case .trailing: return "trailing"
  111. case .width: return "width"
  112. case .height: return "height"
  113. case .centerX: return "centerX"
  114. case .centerY: return "centerY"
  115. case .lastBaseline: return "lastBaseline"
  116. case .firstBaseline: return "firstBaseline"
  117. #if swift(>=5.0)
  118. @unknown default: return "unknown"
  119. #endif
  120. }
  121. #endif
  122. }
  123. private func conditionalOptional<T>(from object: Optional<T>) -> Optional<T> {
  124. return object
  125. }
  126. private func conditionalOptional<T>(from object: T) -> Optional<T> {
  127. return Optional.some(object)
  128. }
  129. private func descriptionForObject(_ object: AnyObject) -> String {
  130. let pointerDescription = String(format: "%p", UInt(bitPattern: ObjectIdentifier(object)))
  131. var desc = ""
  132. desc += type(of: object).description()
  133. if let object = object as? ConstraintView {
  134. desc += ":\(object.snp.label() ?? pointerDescription)"
  135. } else if let object = object as? LayoutConstraint {
  136. desc += ":\(object.label ?? pointerDescription)"
  137. } else {
  138. desc += ":\(pointerDescription)"
  139. }
  140. if let object = object as? LayoutConstraint, let file = object.constraint?.sourceLocation.0, let line = object.constraint?.sourceLocation.1 {
  141. desc += "@\((file as NSString).lastPathComponent)#\(line)"
  142. }
  143. desc += ""
  144. return desc
  145. }