Skip to content

Commit a5a662b

Browse files
author
Luc Dion
committed
Improve warning log output
1 parent 818632b commit a5a662b

File tree

9 files changed

+67
-41
lines changed

9 files changed

+67
-41
lines changed

Example/PinLayoutSample/UI/Examples/AutoAdjustingSizeView/AutoAdjustingSizeView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ class AutoAdjustingSizeView: BaseView {
8282
row1Item2.pin.right(of: row1Item1, aligned: .top).bottomRight().margin(0, 2, 2, 2)
8383

8484
row2.pin.below(of: row1, aligned: .left).size(of: row1).marginTop(10)
85-
row2Item1.pin.top().right().bottom().width(150).width(25%).margin(2)
85+
row2Item1.pin.top().bottom().right().width(150).margin(2)
8686
row2Item2.pin.left(of: row2Item1, aligned: .top).left().bottom().margin(0, 2, 2, 2)
8787

8888
row3.pin.below(of: row2, aligned: .left).size(of: row1).marginTop(10)

Example/PinLayoutSample/UI/Examples/Intro/IntroView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class IntroView: BaseView {
6262
let containerInsets = safeArea.minInsets(UIEdgeInsets(top: 10, left: 10, bottom: 0, right: 10))
6363
contentView.pin.all().margin(containerInsets)
6464

65-
logo.pin.top().left().size(100).aspectRatio().marginTop(10)
65+
logo.pin.top().left().width(100).aspectRatio().marginTop(10)
6666
segmented.pin.after(of: logo, aligned: .top).right().marginLeft(10)
6767
textLabel.pin.below(of: segmented, aligned: .left).width(of: segmented).pinEdges().marginTop(10).sizeToFit(.width)
6868
separatorView.pin.below(of: [logo, textLabel], aligned: .left).right(to: segmented.edge.right).marginTop(10)

Example/PinLayoutSample/UI/Examples/IntroObjectiveC/IntroObjectiveCView.m

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ @implementation IntroObjectiveCView {
3232

3333
- (id)initWithFrame:(CGRect)frame {
3434
if ((self = [super initWithFrame:frame])) {
35+
Pin.logMissingLayoutCalls = true;
36+
3537
topLayoutGuide = 0;
3638
self.backgroundColor = UIColor.whiteColor;
3739

@@ -56,6 +58,10 @@ - (id)initWithFrame:(CGRect)frame {
5658
return self;
5759
}
5860

61+
- (void)dealloc {
62+
Pin.logMissingLayoutCalls = false;
63+
}
64+
5965
- (void) layoutSubviews {
6066
[super layoutSubviews];
6167

Sources/Impl/PinLayoutImpl+Warning.swift

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,19 +52,25 @@ extension PinLayoutImpl {
5252

5353
internal func warnPropertyAlreadySet(_ propertyName: String, propertyValue: CGFloat, _ context: Context) {
5454
guard Pin.logWarnings else { return }
55-
displayWarning("PinLayout Conflict: \(context()) won't be applied since it value has already been set to \(propertyValue).")
55+
displayWarning("PinLayout Conflict: \(context()) won't be applied since it value has already been set to \(propertyValue.description).")
5656
}
5757

5858
internal func warnPropertyAlreadySet(_ propertyName: String, propertyValue: CGSize, _ context: Context) {
5959
guard Pin.logWarnings else { return }
60-
displayWarning("PinLayout Conflict: \(context()) won't be applied since it value has already been set to CGSize(width: \(propertyValue.width), height: \(propertyValue.height)).")
60+
displayWarning("PinLayout Conflict: \(context()) won't be applied since it value has already been set to CGSize(width: \(propertyValue.width.description), height: \(propertyValue.height.description)).")
6161
}
6262

6363
internal func warnConflict(_ context: Context, _ properties: [String: Any]) {
6464
guard Pin.logWarnings else { return }
6565
var warning = "PinLayout Conflict: \(context()) won't be applied since it conflicts with the following already set properties:"
6666
properties.forEach { (property) in
67-
warning += "\n \(property.key): \(property.value)"
67+
warning += "\n \(property.key): "
68+
69+
if let floatValue = property.value as? CGFloat {
70+
warning += "\(floatValue.description)"
71+
} else {
72+
warning += "\(property.value)"
73+
}
6874
}
6975

7076
displayWarning(warning)

Sources/Impl/PinLayoutImpl.swift

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ class PinLayoutImpl: PinLayout {
6969
}
7070

7171
deinit {
72-
if !isLayouted && Pin.warnMissingLayoutCalls {
73-
warn("PinLayout commands have been issued without calling the 'layout()' method to complete the layout. (These warnings can be disabled by setting Pin.warnMissingLayoutCalls to false)")
72+
if !isLayouted && Pin.logMissingLayoutCalls {
73+
warn("PinLayout commands have been issued without calling the 'layout()' method to complete the layout. (These warnings can be disabled by setting Pin.logMissingLayoutCalls to false)")
7474
}
7575
apply()
7676
}
@@ -91,7 +91,7 @@ class PinLayoutImpl: PinLayout {
9191

9292
@discardableResult
9393
func top(_ percent: Percent) -> PinLayout {
94-
func context() -> String { return "top(\(percent))" }
94+
func context() -> String { return "top(\(percent.description))" }
9595
guard let layoutSuperview = layoutSuperview(context) else { return self }
9696
setTop(percent.of(layoutSuperview.frame.height), context)
9797
return self
@@ -109,7 +109,7 @@ class PinLayoutImpl: PinLayout {
109109

110110
@discardableResult
111111
func left(_ percent: Percent) -> PinLayout {
112-
return left(percent, { return "left(\(percent))" })
112+
return left(percent, { return "left(\(percent.description))" })
113113
}
114114

115115
@discardableResult
@@ -126,7 +126,7 @@ class PinLayoutImpl: PinLayout {
126126

127127
@discardableResult
128128
func start(_ percent: Percent) -> PinLayout {
129-
func context() -> String { return "start(\(percent))" }
129+
func context() -> String { return "start(\(percent.description))" }
130130
return isLTR() ? left(percent, context) : right(percent, context)
131131
}
132132

@@ -141,7 +141,7 @@ class PinLayoutImpl: PinLayout {
141141

142142
@discardableResult
143143
func bottom(_ percent: Percent) -> PinLayout {
144-
func context() -> String { return "bottom(\(percent))" }
144+
func context() -> String { return "bottom(\(percent.description))" }
145145
guard let layoutSuperview = layoutSuperview(context) else { return self }
146146
bottom(percent.of(layoutSuperview.frame.height), context)
147147
return self
@@ -158,7 +158,7 @@ class PinLayoutImpl: PinLayout {
158158

159159
@discardableResult
160160
func right(_ percent: Percent) -> PinLayout {
161-
return right(percent, { return "right(\(percent))" })
161+
return right(percent, { return "right(\(percent.description))" })
162162
}
163163

164164
@discardableResult func end() -> PinLayout {
@@ -174,7 +174,7 @@ class PinLayoutImpl: PinLayout {
174174

175175
@discardableResult
176176
func end(_ percent: Percent) -> PinLayout {
177-
func context() -> String { return "end(\(percent))" }
177+
func context() -> String { return "end(\(percent.description))" }
178178
return isLTR() ? right(percent, context) : left(percent, context)
179179
}
180180

@@ -196,7 +196,7 @@ class PinLayoutImpl: PinLayout {
196196

197197
@discardableResult
198198
func hCenter(_ percent: Percent) -> PinLayout {
199-
func context() -> String { return "hCenter(\(percent))" }
199+
func context() -> String { return "hCenter(\(percent.description))" }
200200
guard let layoutSuperview = layoutSuperview(context) else { return self }
201201
setHorizontalCenter((layoutSuperview.frame.width / 2) + percent.of(layoutSuperview.frame.width), context)
202202
return self
@@ -220,7 +220,7 @@ class PinLayoutImpl: PinLayout {
220220

221221
@discardableResult
222222
func vCenter(_ percent: Percent) -> PinLayout {
223-
func context() -> String { return "vCenter(\(percent))" }
223+
func context() -> String { return "vCenter(\(percent.description))" }
224224
guard let layoutSuperview = layoutSuperview(context) else { return self }
225225
setVerticalCenter((layoutSuperview.frame.height / 2) + percent.of(layoutSuperview.frame.height), context)
226226
return self
@@ -607,7 +607,7 @@ class PinLayoutImpl: PinLayout {
607607

608608
@discardableResult
609609
func width(_ percent: Percent) -> PinLayout {
610-
func context() -> String { return "width(\(percent))" }
610+
func context() -> String { return "width(\(percent.description))" }
611611
guard let layoutSuperview = layoutSuperview(context) else { return self }
612612
return setWidth(percent.of(layoutSuperview.frame.width), context)
613613
}
@@ -625,7 +625,7 @@ class PinLayoutImpl: PinLayout {
625625

626626
@discardableResult
627627
func minWidth(_ percent: Percent) -> PinLayout {
628-
func context() -> String { return "minWidth(\(percent))" }
628+
func context() -> String { return "minWidth(\(percent.description))" }
629629
guard let layoutSuperview = layoutSuperview(context) else { return self }
630630
return setMinWidth(percent.of(layoutSuperview.frame.width), context)
631631
}
@@ -638,7 +638,7 @@ class PinLayoutImpl: PinLayout {
638638

639639
@discardableResult
640640
func maxWidth(_ percent: Percent) -> PinLayout {
641-
func context() -> String { return "maxWidth(\(percent))" }
641+
func context() -> String { return "maxWidth(\(percent.description))" }
642642
guard let layoutSuperview = layoutSuperview(context) else { return self }
643643
return setMaxWidth(percent.of(layoutSuperview.frame.width), context)
644644
}
@@ -650,7 +650,7 @@ class PinLayoutImpl: PinLayout {
650650

651651
@discardableResult
652652
func height(_ percent: Percent) -> PinLayout {
653-
func context() -> String { return "height(\(percent))" }
653+
func context() -> String { return "height(\(percent.description))" }
654654
guard let layoutSuperview = layoutSuperview(context) else { return self }
655655
return setHeight(percent.of(layoutSuperview.frame.height), context)
656656
}
@@ -668,7 +668,7 @@ class PinLayoutImpl: PinLayout {
668668

669669
@discardableResult
670670
func minHeight(_ percent: Percent) -> PinLayout {
671-
func context() -> String { return "minHeight(\(percent))" }
671+
func context() -> String { return "minHeight(\(percent.description))" }
672672
guard let layoutSuperview = layoutSuperview(context) else { return self }
673673
return setMinHeight(percent.of(layoutSuperview.frame.height), context)
674674
}
@@ -681,7 +681,7 @@ class PinLayoutImpl: PinLayout {
681681

682682
@discardableResult
683683
func maxHeight(_ percent: Percent) -> PinLayout {
684-
func context() -> String { return "maxHeight(\(percent))" }
684+
func context() -> String { return "maxHeight(\(percent.description))" }
685685
guard let layoutSuperview = layoutSuperview(context) else { return self }
686686
return setMaxHeight(percent.of(layoutSuperview.frame.height), context)
687687
}
@@ -701,7 +701,7 @@ class PinLayoutImpl: PinLayout {
701701

702702
@discardableResult
703703
func size(_ percent: Percent) -> PinLayout {
704-
func context() -> String { return "size(\(percent))" }
704+
func context() -> String { return "size(\(percent.description))" }
705705
guard let layoutSuperview = layoutSuperview(context) else { return self }
706706
let size = CGSize(width: percent.of(layoutSuperview.frame.width), height: percent.of(layoutSuperview.frame.height))
707707
return setSize(size, context)

Sources/Impl/TypesImpl.swift

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,21 @@ extension Percent {
182182
return rhs * value / 100
183183
}
184184
public var description: String {
185-
return "\(value)%"
185+
if value.truncatingRemainder(dividingBy: 1) == 0.0 {
186+
return "\(Int(value))%"
187+
} else {
188+
return "\(value)%"
189+
}
190+
}
191+
}
192+
193+
extension CGFloat {
194+
public var description: String {
195+
if self.truncatingRemainder(dividingBy: 1) == 0.0 {
196+
return "\(Int(self))"
197+
} else {
198+
return "\(self)"
199+
}
186200
}
187201
}
188202

Tests/LayoutMethodSpec.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class LayoutMethodSpec: QuickSpec {
3939

4040
beforeEach {
4141
Pin.lastWarningText = nil
42-
Pin.warnMissingLayoutCalls = false
42+
Pin.logMissingLayoutCalls = false
4343

4444
viewController = UIViewController()
4545

@@ -53,7 +53,7 @@ class LayoutMethodSpec: QuickSpec {
5353
}
5454

5555
afterEach {
56-
Pin.warnMissingLayoutCalls = false
56+
Pin.logMissingLayoutCalls = false
5757
}
5858

5959
//
@@ -71,8 +71,8 @@ class LayoutMethodSpec: QuickSpec {
7171
expect(aView.frame).to(equal(CGRect(x: 0.0, y: 100.0, width: 400.0, height: 60.0)))
7272
}
7373

74-
it("should warn if layout() is not called when Pin.warnMissingLayoutCalls is set to true") {
75-
Pin.warnMissingLayoutCalls = true
74+
it("should warn if layout() is not called when Pin.logMissingLayoutCalls is set to true") {
75+
Pin.logMissingLayoutCalls = true
7676

7777
aView.pin.left().right()
7878
expect(Pin.lastWarningText).to(contain(["PinLayout commands have been issued without calling the 'layout()' method"]))

Tests/ObjectiveCSpec.m

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
__block UIView* aView = nil;
2323

2424
beforeEach(^{
25-
Pin.warnMissingLayoutCalls = false;
25+
Pin.logMissingLayoutCalls = false;
2626
Pin.lastWarningText = nil;
2727

2828
viewController = [[UIViewController alloc] initWithNibName:nil bundle:nil];
@@ -35,7 +35,7 @@
3535
});
3636

3737
afterEach(^{
38-
Pin.warnMissingLayoutCalls = false;
38+
Pin.logMissingLayoutCalls = false;
3939
});
4040

4141
describe(@"its click", ^{
@@ -44,15 +44,15 @@
4444
expect(@(aView.frame)).to(equal(@(CGRectMake(40, 10, 100, 60))));
4545
});
4646

47-
it(@"using Pin.warnMissingLayoutCalls", ^{
48-
Pin.warnMissingLayoutCalls = true;
47+
it(@"using Pin.logMissingLayoutCalls", ^{
48+
Pin.logMissingLayoutCalls = true;
4949
[[aView pinObjc] top:10];
5050
expect(@(aView.frame)).to(equal(@(CGRectMake(40, 10, 100, 60))));
5151
expect(Pin.lastWarningText).to(contain(@"PinLayout commands have been issued without calling the 'layout()' method to complete the layout"));
5252
});
5353

54-
it(@"using Pin.warnMissingLayoutCalls set to false", ^{
55-
Pin.warnMissingLayoutCalls = false;
54+
it(@"using Pin.logMissingLayoutCalls set to false", ^{
55+
Pin.logMissingLayoutCalls = false;
5656
[[[aView pinObjc] top:10] layout];
5757
expect(@(aView.frame)).to(equal(@(CGRectMake(40, 10, 100, 60))));
5858
expect(Pin.lastWarningText).to(beNil());

Tests/PinEdgesSpec.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -482,20 +482,20 @@ class PinEdgesSpec: QuickSpec {
482482

483483
it("should warn") {
484484
aView.pin.top(20).all()
485-
expect(Pin.lastWarningText).to(contain(["all() top coordinate", "won't be applied", "already been set to 20.0"]))
485+
expect(Pin.lastWarningText).to(contain(["all() top coordinate", "won't be applied", "already been set to 20"]))
486486
}
487487
it("should warn") {
488488
aView.pin.left(20).all()
489-
expect(Pin.lastWarningText).to(contain(["all() left coordinate", "won't be applied", "already been set to 20.0"]))
489+
expect(Pin.lastWarningText).to(contain(["all() left coordinate", "won't be applied", "already been set to 20"]))
490490
}
491491
it("should warn") {
492492
aView.pin.right(20).all()
493-
expect(Pin.lastWarningText).to(contain(["all() right coordinate", "won't be applied", "already been set to 20.0"]))
493+
expect(Pin.lastWarningText).to(contain(["all() right coordinate", "won't be applied", "already been set to 20"]))
494494
}
495495

496496
it("should warn") {
497497
aView.pin.bottom(20).all()
498-
expect(Pin.lastWarningText).to(contain(["all() bottom coordinate", "won't be applied", "already been set to 20.0"]))
498+
expect(Pin.lastWarningText).to(contain(["all() bottom coordinate", "won't be applied", "already been set to 20"]))
499499
}
500500
}
501501

@@ -525,11 +525,11 @@ class PinEdgesSpec: QuickSpec {
525525

526526
it("should warn") {
527527
aView.pin.left(20).horizontally()
528-
expect(Pin.lastWarningText).to(contain(["horizontally() left coordinate", "won't be applied", "already been set to 20.0"]))
528+
expect(Pin.lastWarningText).to(contain(["horizontally() left coordinate", "won't be applied", "already been set to 20"]))
529529
}
530530
it("should warn") {
531531
aView.pin.right(20).horizontally()
532-
expect(Pin.lastWarningText).to(contain(["horizontally() right coordinate", "won't be applied", "already been set to 20.0"]))
532+
expect(Pin.lastWarningText).to(contain(["horizontally() right coordinate", "won't be applied", "already been set to 20"]))
533533
}
534534
}
535535

@@ -559,11 +559,11 @@ class PinEdgesSpec: QuickSpec {
559559

560560
it("should warn") {
561561
aView.pin.top(20).vertically()
562-
expect(Pin.lastWarningText).to(contain(["vertically() top coordinate", "won't be applied", "already been set to 20.0"]))
562+
expect(Pin.lastWarningText).to(contain(["vertically() top coordinate", "won't be applied", "already been set to 20"]))
563563
}
564564
it("should warn") {
565565
aView.pin.bottom(20).vertically()
566-
expect(Pin.lastWarningText).to(contain(["vertically() bottom coordinate", "won't be applied", "already been set to 20.0"]))
566+
expect(Pin.lastWarningText).to(contain(["vertically() bottom coordinate", "won't be applied", "already been set to 20"]))
567567
}
568568
}
569569
}

0 commit comments

Comments
 (0)