forked from nicklockwood/ShapeScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogging.swift
More file actions
251 lines (214 loc) · 6.37 KB
/
Logging.swift
File metadata and controls
251 lines (214 loc) · 6.37 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
//
// Logging.swift
// ShapeScript Lib
//
// Created by Nick Lockwood on 17/08/2021.
// Copyright © 2021 Nick Lockwood. All rights reserved.
//
import Euclid
import Foundation
public protocol Loggable {
/// Top-level log description
var logDescription: String { get }
/// Log description when nested inside an array or tuple
var nestedLogDescription: String { get }
}
extension String: Loggable {
public init(logDescriptionFor value: Any) {
self.init(describing: (value as? Loggable)?.logDescription ?? value)
}
public init(nestedLogDescriptionFor value: Any) {
self.init(describing: (value as? Loggable)?.nestedLogDescription ?? value)
}
public var logDescription: String {
self
}
public var nestedLogDescription: String {
"\"" + replacingOccurrences(of: "\\", with: "\\\\")
.replacingOccurrences(of: "\"", with: "\\\"")
.replacingOccurrences(of: "\n", with: "\\n") + "\""
}
}
extension TextValue: Loggable {
public var logDescription: String {
string
}
var nestedLogDescription: String {
string.nestedLogDescription
}
}
extension Double: Loggable {
private static let formatter: NumberFormatter = {
let formatter = NumberFormatter()
formatter.locale = Locale(identifier: "en_US")
formatter.numberStyle = .decimal
formatter.maximumFractionDigits = 4
formatter.usesGroupingSeparator = false
formatter.positiveInfinitySymbol = "∞"
formatter.negativeInfinitySymbol = "-∞"
formatter.notANumberSymbol = "NaN"
return formatter
}()
public var logDescription: String {
let result = Self.formatter.string(from: self as NSNumber) ?? "NaN"
return result == "-0" ? "0" : result
}
public var nestedLogDescription: String {
logDescription
}
}
extension Bool: Loggable {
public var logDescription: String {
"\(self)"
}
public var nestedLogDescription: String {
logDescription
}
}
extension Vector: Loggable {
public var logDescription: String {
"\(x.logDescription) \(y.logDescription) \(z.logDescription)"
}
public var nestedLogDescription: String {
"(\(logDescription))"
}
}
extension Angle: Loggable {
public var logDescription: String {
(radians / .pi).logDescription
}
public var nestedLogDescription: String {
logDescription
}
}
extension Rotation: Loggable {
public var logDescription: String {
"\(roll.logDescription) \(yaw.logDescription) \(pitch.logDescription)"
}
public var nestedLogDescription: String {
"(\(logDescription))"
}
}
extension Color: Loggable {
public var logDescription: String {
"\(r.logDescription) \(g.logDescription) \(b.logDescription) \(a.logDescription)"
}
public var nestedLogDescription: String {
"(\(logDescription))"
}
}
extension Texture: Loggable {
public var logDescription: String {
switch self {
case let .file(name: _, url: url):
return url.path.logDescription
case .data:
return "texture { #data }"
}
}
public var nestedLogDescription: String {
switch self {
case let .file(name: _, url: url):
return url.path.nestedLogDescription
case .data:
return logDescription
}
}
}
extension MaterialProperty: Loggable {
public var logDescription: String {
switch self {
case let .color(color):
return color.logDescription
case let .texture(texture):
return texture.logDescription
}
}
public var nestedLogDescription: String {
switch self {
case let .color(color):
return color.nestedLogDescription
case let .texture(texture):
return texture.nestedLogDescription
}
}
}
extension Path: Loggable {
public var logDescription: String {
if subpaths.count > 1 {
return "path { subpaths: \(subpaths.count) }"
}
return "path { points: \(points.count) }"
}
public var nestedLogDescription: String {
"path"
}
}
private extension GeometryType {
var logDescription: String {
switch self {
case .group: return "group"
case .cone: return "cone"
case .cylinder: return "cylinder"
case .sphere: return "sphere"
case .cube: return "cube"
case .extrude: return "extrusion"
case .lathe: return "lathe"
case .loft: return "loft"
case .fill: return "fill"
case .union: return "union"
case .difference: return "difference"
case .intersection: return "intersection"
case .xor: return "xor"
case .stencil: return "stencil"
case .path: return "path"
case .mesh: return "mesh"
case .camera: return "camera"
case .light: return "light"
}
}
}
extension Geometry: Loggable {
public var logDescription: String {
let fields = [
name.flatMap { $0.isEmpty ? nil : " name: \($0)" },
childCount == 0 ? nil : " children: \(childCount)",
" size: \(transform.scale.logDescription)",
" position: \(transform.offset.logDescription)",
" orientation: \(transform.rotation.logDescription)",
].compactMap { $0 }.joined(separator: "\n")
return """
\(type.logDescription) {
\(fields)
}
"""
}
public var nestedLogDescription: String {
type.logDescription
}
}
extension Optional: Loggable {
public var logDescription: String {
map { String(logDescriptionFor: $0) } ?? "nil"
}
public var nestedLogDescription: String {
map { String(nestedLogDescriptionFor: $0) } ?? "nil"
}
}
extension Array: Loggable {
public var logDescription: String {
map { String(nestedLogDescriptionFor: $0) }.joined(separator: " ")
}
public var nestedLogDescription: String {
"(\(logDescription))"
}
}
extension RangeValue: Loggable {
public var logDescription: String {
let stepText = (step == 1) ? "" : " step \(step.logDescription)"
return "\(start.logDescription) to \(end.logDescription)\(stepText)"
}
public var nestedLogDescription: String {
"(\(logDescription))"
}
}