-
Notifications
You must be signed in to change notification settings - Fork 100
/
VCLLoggingViewController.swift
112 lines (95 loc) · 3.7 KB
/
VCLLoggingViewController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
//
// VCLLoggingViewController.swift
//
// Created by CS193p Instructor.
// Copyright © 2015-17 Stanford University. All rights reserved.
//
import UIKit
class VCLLoggingViewController : UIViewController
{
private struct LogGlobals {
var prefix = ""
var instanceCounts = [String:Int]()
var lastLogTime = Date()
var indentationInterval: TimeInterval = 1
var indentationString = "__"
}
private static var logGlobals = LogGlobals()
private static func logPrefix(for loggingName: String) -> String {
if logGlobals.lastLogTime.timeIntervalSinceNow < -logGlobals.indentationInterval {
logGlobals.prefix += logGlobals.indentationString
print("")
}
logGlobals.lastLogTime = Date()
return logGlobals.prefix + loggingName
}
private static func bumpInstanceCount(for loggingName: String) -> Int {
logGlobals.instanceCounts[loggingName] = (logGlobals.instanceCounts[loggingName] ?? 0) + 1
return logGlobals.instanceCounts[loggingName]!
}
private var instanceCount: Int!
var vclLoggingName: String {
return String(describing: type(of: self))
}
private func logVCL(_ msg: String) {
if instanceCount == nil {
instanceCount = VCLLoggingViewController.bumpInstanceCount(for: vclLoggingName)
}
print("\(VCLLoggingViewController.logPrefix(for: vclLoggingName))(\(instanceCount!)) \(msg)")
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
logVCL("init(coder:) - created via InterfaceBuilder ")
}
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
logVCL("init(nibName:bundle:) - create in code")
}
deinit {
logVCL("left the heap")
}
override func awakeFromNib() {
logVCL("awakeFromNib()")
}
override func viewDidLoad() {
super.viewDidLoad()
logVCL("viewDidLoad()")
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
logVCL("viewWillAppear(animated = \(animated))")
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
logVCL("viewDidAppear(animated = \(animated))")
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
logVCL("viewWillDisappear(animated = \(animated))")
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
logVCL("viewDidDisappear(animated = \(animated))")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
logVCL("didReceiveMemoryWarning()")
}
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
logVCL("viewWillLayoutSubviews() bounds.size = \(view.bounds.size)")
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
logVCL("viewDidLayoutSubviews() bounds.size = \(view.bounds.size)")
}
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
logVCL("viewWillTransition(to: \(size), with: coordinator)")
coordinator.animate(alongsideTransition: { (context: UIViewControllerTransitionCoordinatorContext!) -> Void in
self.logVCL("begin animate(alongsideTransition:completion:)")
}, completion: { context -> Void in
self.logVCL("end animate(alongsideTransition:completion:)")
})
}
}