Debugging.swift 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 public var description: String {
  30. var description = "<"
  31. description += descriptionForObject(self)
  32. description += " \(descriptionForObject(self.firstItem))"
  33. if self.firstAttribute != .NotAnAttribute {
  34. description += ".\(descriptionForAttribute(self.firstAttribute))"
  35. }
  36. description += " \(descriptionForRelation(self.relation))"
  37. if let secondItem: AnyObject = self.secondItem {
  38. description += " \(descriptionForObject(secondItem))"
  39. }
  40. if self.secondAttribute != .NotAnAttribute {
  41. description += ".\(descriptionForAttribute(self.secondAttribute))"
  42. }
  43. if self.multiplier != 1.0 {
  44. description += " * \(self.multiplier)"
  45. }
  46. if self.secondAttribute == .NotAnAttribute {
  47. description += " \(self.constant)"
  48. } else {
  49. if self.constant > 0.0 {
  50. description += " + \(self.constant)"
  51. } else if self.constant < 0.0 {
  52. description += " - \(CGFloat.abs(self.constant))"
  53. }
  54. }
  55. if self.priority != 1000.0 {
  56. description += " ^\(self.priority)"
  57. }
  58. description += ">"
  59. return description
  60. }
  61. }
  62. private func descriptionForRelation(relation: NSLayoutRelation) -> String {
  63. switch relation {
  64. case .Equal: return "=="
  65. case .GreaterThanOrEqual: return ">="
  66. case .LessThanOrEqual: return "<="
  67. }
  68. }
  69. private func descriptionForAttribute(attribute: NSLayoutAttribute) -> String {
  70. #if os(iOS) || os(tvOS)
  71. switch attribute {
  72. case .NotAnAttribute: return "notAnAttribute"
  73. case .Top: return "top"
  74. case .Left: return "left"
  75. case .Bottom: return "bottom"
  76. case .Right: return "right"
  77. case .Leading: return "leading"
  78. case .Trailing: return "trailing"
  79. case .Width: return "width"
  80. case .Height: return "height"
  81. case .CenterX: return "centerX"
  82. case .CenterY: return "centerY"
  83. case .Baseline: return "baseline"
  84. case .FirstBaseline: return "firstBaseline"
  85. case .TopMargin: return "topMargin"
  86. case .LeftMargin: return "leftMargin"
  87. case .BottomMargin: return "bottomMargin"
  88. case .RightMargin: return "rightMargin"
  89. case .LeadingMargin: return "leadingMargin"
  90. case .TrailingMargin: return "trailingMargin"
  91. case .CenterXWithinMargins: return "centerXWithinMargins"
  92. case .CenterYWithinMargins: return "centerYWithinMargins"
  93. }
  94. #else
  95. switch attribute {
  96. case .NotAnAttribute: return "notAnAttribute"
  97. case .Top: return "top"
  98. case .Left: return "left"
  99. case .Bottom: return "bottom"
  100. case .Right: return "right"
  101. case .Leading: return "leading"
  102. case .Trailing: return "trailing"
  103. case .Width: return "width"
  104. case .Height: return "height"
  105. case .CenterX: return "centerX"
  106. case .CenterY: return "centerY"
  107. case .Baseline: return "baseline"
  108. default: return "default"
  109. }
  110. #endif
  111. }
  112. private func descriptionForObject(object: AnyObject) -> String {
  113. let pointerDescription = NSString(format: "%p", ObjectIdentifier(object).uintValue)
  114. var desc = ""
  115. desc += object.dynamicType.description()
  116. if let object = object as? ConstraintView {
  117. desc += ":\(object.snp.label ?? pointerDescription)"
  118. } else if let object = object as? LayoutConstraint {
  119. desc += ":\(object.label ?? pointerDescription)"
  120. } else {
  121. desc += ":\(pointerDescription)"
  122. }
  123. if let object = object as? LayoutConstraint, let file = object.constraint?.sourceLocation.0, let line = object.constraint?.sourceLocation.1 {
  124. desc += "@\((file as NSString).lastPathComponent)#\(line)"
  125. }
  126. desc += ""
  127. return desc
  128. }