forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLoggingWrappers.swift.gyb
601 lines (509 loc) · 17 KB
/
LoggingWrappers.swift.gyb
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
//===--- LoggingWrappers.swift ---------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
public protocol WrapperType {
typealias Base
init(_: Base)
var base: Base {get set}
}
public protocol LoggingType : WrapperType {
typealias Log : AnyObject
}
extension LoggingType {
public var log: Log.Type {
return Log.self
}
public var selfType: Any.Type {
return self.dynamicType
}
}
public class GeneratorLog {
public static func dispatchTester<G: GeneratorType>(
g: G
) -> LoggingGenerator<LoggingGenerator<G>> {
return LoggingGenerator(LoggingGenerator(g))
}
public static var next = TypeIndexed(0)
}
public struct LoggingGenerator<Base: GeneratorType>
: GeneratorType, LoggingType {
public typealias Log = GeneratorLog
public init(_ base: Base) {
self.base = base
}
public mutating func next() -> Base.Element? {
++Log.next[selfType]
return base.next()
}
public var base: Base
}
/// Data container to keep track of how many times each `Base` type calls methods
/// of RangeReplaceableCollectionType.
///
/// Each static variable is a mapping of Type -> Number of calls.
public class RangeReplaceableCollectionLog {
public static var init_ = TypeIndexed(0)
public static var initWithSequence = TypeIndexed(0)
public static var append = TypeIndexed(0)
public static var appendContentsOf = TypeIndexed(0)
public static var insert = TypeIndexed(0)
public static var removeAll = TypeIndexed(0)
public static var removeAtIndex = TypeIndexed(0)
public static var _customRemoveLast = TypeIndexed(0)
public static var _customRemoveLastN = TypeIndexed(0)
public static var removeRange = TypeIndexed(0)
public static var removeFirst = TypeIndexed(0)
public static var removeFirstN = TypeIndexed(0)
public static var replaceRange = TypeIndexed(0)
public static var reserveCapacity = TypeIndexed(0)
public static var insertContentsOf = TypeIndexed(0)
public static func dispatchTester<C: RangeReplaceableCollectionType>(
rrc: C
) -> LoggingRangeReplaceableCollection<LoggingRangeReplaceableCollection<C>> {
return LoggingRangeReplaceableCollection(
LoggingRangeReplaceableCollection(rrc)
)
}
}
/// Interposes between RangeReplaceableCollectionType method calls to
/// increment each method's counter.
public struct LoggingRangeReplaceableCollection<
Base: RangeReplaceableCollectionType
> : RangeReplaceableCollectionType, LoggingType {
public typealias Index = Base.Index
public typealias Log = RangeReplaceableCollectionLog
public var base: Base
public init() {
self.base = Base()
++Log.init_[selfType]
}
public init<
S : SequenceType where S.Generator.Element == Generator.Element
>(_ elements: S) {
self.base = Base(elements)
++Log.initWithSequence[selfType]
}
public init(_ base: Base) {
self.base = base
}
public var count: Base.Index.Distance {
return base.count
}
public var first: Base.Generator.Element? {
return base.first
}
public var isEmpty: Bool {
return base.isEmpty
}
public var startIndex: Base.Index {
return base.startIndex
}
public var endIndex: Base.Index {
return base.endIndex
}
public subscript(position: Base.Index) -> Base.Generator.Element {
return base[position]
}
public mutating func replaceRange<
C : CollectionType where C.Generator.Element == Base.Generator.Element
>(
subRange: Range<Base.Index>, with newElements: C
) {
++Log.replaceRange[selfType]
base.replaceRange(subRange, with: newElements)
}
public mutating func append(newElement: Base.Generator.Element) {
++Log.append[selfType]
base.append(newElement)
}
public mutating func appendContentsOf<
S : SequenceType where S.Generator.Element == Base.Generator.Element
>(newElements: S) {
++Log.appendContentsOf[selfType]
base.appendContentsOf(newElements)
}
public mutating func insert(
newElement: Base.Generator.Element, atIndex i: Base.Index
) {
++Log.insert[selfType]
base.insert(newElement, atIndex: i)
}
public mutating func removeAtIndex(index: Base.Index) -> Base.Generator.Element {
++Log.removeAtIndex[selfType]
return base.removeAtIndex(index)
}
public mutating func _customRemoveLast() -> Base.Generator.Element? {
++Log._customRemoveLast[selfType]
return base._customRemoveLast()
}
public mutating func _customRemoveLast(n: Int) -> Bool {
++Log._customRemoveLastN[selfType]
return base._customRemoveLast(n)
}
public mutating func removeFirst() -> Base.Generator.Element {
++Log.removeFirst[selfType]
return base.removeFirst()
}
public mutating func removeFirst(n: Int) {
++Log.removeFirstN[selfType]
base.removeFirst(n)
}
public mutating func removeRange(subRange: Range<Base.Index>) {
++Log.removeRange[selfType]
base.removeRange(subRange)
}
public mutating func removeAll(keepCapacity keepCapacity: Bool) {
++Log.removeAll[selfType]
base.removeAll(keepCapacity: keepCapacity)
}
public mutating func reserveCapacity(n: Base.Index.Distance) {
++Log.reserveCapacity[selfType]
base.reserveCapacity(n)
}
public mutating func insertContentsOf<
C : CollectionType where C.Generator.Element == Base.Generator.Element
>(newElements: C, at i: Base.Index) {
++Log.insertContentsOf[selfType]
base.insertContentsOf(newElements, at: i)
}
@warn_unused_result
public func map<T>(
@noescape transform: (Base.Generator.Element) throws -> T
) rethrows -> [T] {
return try base.map(transform)
}
@warn_unused_result
public func filter(
@noescape includeElement: (Base.Generator.Element) throws -> Bool
) rethrows -> [Base.Generator.Element] {
return try base.filter(includeElement)
}
public func generate() -> Base.Generator {
return base.generate()
}
}
public class ForwardIndexLog {
public static var successor = TypeIndexed(0)
public static var advancedBy = TypeIndexed(0)
public static var advancedByWithLimit = TypeIndexed(0)
public static var distanceTo = TypeIndexed(0)
public class func dispatchTester<I : ForwardIndexType>(
i: I
) -> LoggingForwardIndex<LoggingForwardIndex<I>> {
return LoggingForwardIndex(LoggingForwardIndex(i))
}
}
public class BidirectionalIndexLog : ForwardIndexLog {
public static var predecessor = TypeIndexed(0)
public class func dispatchTester<I : BidirectionalIndexType>(
i: I
) -> LoggingBidirectionalIndex<LoggingBidirectionalIndex<I>> {
return LoggingBidirectionalIndex(LoggingBidirectionalIndex(i))
}
}
public class RandomAccessIndexLog : BidirectionalIndexLog {
public class func dispatchTester<I : RandomAccessIndexType>(
i: I
) -> LoggingRandomAccessIndex<LoggingRandomAccessIndex<I>> {
return LoggingRandomAccessIndex(LoggingRandomAccessIndex(i))
}
}
public class SequenceLog {
public static func dispatchTester<S: SequenceType>(
s: S
) -> LoggingSequence<LoggingSequence<S>> {
return LoggingSequence(LoggingSequence(s))
}
public static var forEach = TypeIndexed(0)
public static var generate = TypeIndexed(0)
public static var underestimateCount = TypeIndexed(0)
public static var map = TypeIndexed(0)
public static var filter = TypeIndexed(0)
public static var dropFirst = TypeIndexed(0)
public static var dropLast = TypeIndexed(0)
public static var prefix = TypeIndexed(0)
public static var suffix = TypeIndexed(0)
public static var split = TypeIndexed(0)
public static var _customContainsEquatableElement = TypeIndexed(0)
public static var _preprocessingPass = TypeIndexed(0)
public static var _copyToNativeArrayBuffer = TypeIndexed(0)
public static var _initializeTo = TypeIndexed(0)
}
public class CollectionLog : SequenceLog {
public class func dispatchTester<C : CollectionType>(
c: C
) -> LoggingCollection<LoggingCollection<C>> {
return LoggingCollection(LoggingCollection(c))
}
public static var startIndex = TypeIndexed(0)
public static var endIndex = TypeIndexed(0)
public static var prefixUpTo = TypeIndexed(0)
public static var prefixThrough = TypeIndexed(0)
public static var subscriptIndex = TypeIndexed(0)
public static var subscriptRange = TypeIndexed(0)
public static var suffixFrom = TypeIndexed(0)
public static var isEmpty = TypeIndexed(0)
public static var count = TypeIndexed(0)
public static var _customIndexOfEquatableElement = TypeIndexed(0)
public static var first = TypeIndexed(0)
}
public class MutableCollectionLog : CollectionLog {
public class func dispatchTester<C : MutableCollectionType>(
c: C
) -> LoggingMutableCollection<LoggingMutableCollection<C>> {
return LoggingMutableCollection(LoggingMutableCollection(c))
}
public static var subscriptIndexSet = TypeIndexed(0)
public static var subscriptRangeSet = TypeIndexed(0)
public static var _withUnsafeMutableBufferPointerIfSupported = TypeIndexed(0)
public static var _withUnsafeMutableBufferPointerIfSupportedNonNilReturns =
TypeIndexed(0)
}
% for Kind in ['ForwardIndex', 'BidirectionalIndex', 'RandomAccessIndex']:
public struct Logging${Kind}<
Base: ${Kind}Type
> : ${Kind}Type, LoggingType {
public typealias Log = ${Kind}Log
public typealias Distance = Base.Distance
public var base: Base
public init(_ base: Base) {
self.base = base
}
public func successor() -> Logging${Kind} {
++Log.successor[selfType]
return Logging${Kind}(base.successor())
}
% if Kind == 'BidirectionalIndex' or Kind == 'RandomAccessIndex':
public func predecessor() -> Logging${Kind}<Base> {
++Log.predecessor[selfType]
return Logging${Kind}(base.predecessor())
}
% end
public func distanceTo(end: Logging${Kind}<Base>) -> Base.Distance {
++Log.distanceTo[selfType]
return base.distanceTo(end.base)
}
public func advancedBy(n: Base.Distance) -> Logging${Kind}<Base> {
++Log.advancedBy[selfType]
return Logging${Kind}(base.advancedBy(n))
}
public func advancedBy(n: Base.Distance, limit: Logging${Kind}<Base>)
-> Logging${Kind} {
++Log.advancedByWithLimit[selfType]
return Logging${Kind}(base.advancedBy(n, limit: limit.base))
}
}
public func ==<Base>(lhs: Logging${Kind}<Base>, rhs: Logging${Kind}<Base>)
-> Bool {
return lhs.base == rhs.base
}
% end
% for Kind in 'Sequence', 'Collection', 'MutableCollection':
public struct Logging${Kind}<Base: ${Kind}Type> : ${Kind}Type, LoggingType {
public typealias Log = ${Kind}Log
public init(_ base: Base) {
self.base = base
}
% if Kind == 'Collection' or Kind == 'MutableCollection':
public var startIndex: Base.Index {
++Log.startIndex[selfType]
return base.startIndex
}
public var endIndex: Base.Index {
++Log.endIndex[selfType]
return base.endIndex
}
public subscript(position: Base.Index) -> Base.Generator.Element {
get {
++Log.subscriptIndex[selfType]
return base[position]
}
% if Kind == 'MutableCollection':
set {
++Log.subscriptIndexSet[selfType]
base[position] = newValue
}
% end
}
public subscript(bounds: Range<Base.Index>) -> Base.SubSequence {
get {
++Log.subscriptRange[selfType]
return base[bounds]
}
% if Kind == 'MutableCollection':
set {
++Log.subscriptRangeSet[selfType]
base[bounds] = newValue
}
% end
}
public var isEmpty: Bool {
++Log.isEmpty[selfType]
return base.isEmpty
}
public var count: Base.Index.Distance {
++Log.count[selfType]
return base.count
}
public func _customIndexOfEquatableElement(
element: Base.Generator.Element
) -> Base.Index?? {
++Log._customIndexOfEquatableElement[selfType]
return base._customIndexOfEquatableElement(element)
}
public var first: Base.Generator.Element? {
++Log.first[selfType]
return base.first
}
% end
% if Kind == 'MutableCollection':
public mutating func _withUnsafeMutableBufferPointerIfSupported<R>(
@noescape body: (UnsafeMutablePointer<Generator.Element>, Int) throws -> R
) rethrows -> R? {
++Log._withUnsafeMutableBufferPointerIfSupported[selfType]
let result = try base._withUnsafeMutableBufferPointerIfSupported(body)
if result != nil {
++Log._withUnsafeMutableBufferPointerIfSupportedNonNilReturns[selfType]
}
return result
}
% end
public func generate() -> LoggingGenerator<Base.Generator> {
++Log.generate[selfType]
return LoggingGenerator(base.generate())
}
public func underestimateCount() -> Int {
++Log.underestimateCount[selfType]
return base.underestimateCount()
}
public func forEach(
@noescape body: (Base.Generator.Element) throws -> Void
) rethrows {
++Log.forEach[selfType]
try base.forEach(body)
}
@warn_unused_result
public func map<T>(
@noescape transform: (Base.Generator.Element) throws -> T
) rethrows -> [T] {
++Log.map[selfType]
return try base.map(transform)
}
@warn_unused_result
public func filter(
@noescape includeElement: (Base.Generator.Element) throws -> Bool
) rethrows -> [Base.Generator.Element] {
++Log.filter[selfType]
return try base.filter(includeElement)
}
public func dropFirst(n: Int) -> Base.SubSequence {
++Log.dropFirst[selfType]
return base.dropFirst(n)
}
public func dropLast(n: Int) -> Base.SubSequence {
++Log.dropLast[selfType]
return base.dropLast(n)
}
public func prefix(maxLength: Int) -> Base.SubSequence {
++Log.prefix[selfType]
return base.prefix(maxLength)
}
public func suffix(maxLength: Int) -> Base.SubSequence {
++Log.suffix[selfType]
return base.suffix(maxLength)
}
public func split(
maxSplit: Int = Int.max,
allowEmptySlices: Bool = false,
@noescape isSeparator: (Base.Generator.Element) throws -> Bool
) rethrows -> [Base.SubSequence] {
++Log.split[selfType]
return try base.split(maxSplit, allowEmptySlices: allowEmptySlices,
isSeparator: isSeparator)
}
% if Kind == 'Collection' or Kind == 'MutableCollection':
public func prefixUpTo(end: Index) -> Base.SubSequence {
++Log.prefixUpTo[selfType]
return base.prefixUpTo(end)
}
public func suffixFrom(start: Index) -> Base.SubSequence {
++Log.suffixFrom[selfType]
return base.suffixFrom(start)
}
public func prefixThrough(position: Index) -> Base.SubSequence {
++Log.prefixThrough[selfType]
return base.prefixThrough(position)
}
% end
public func _customContainsEquatableElement(
element: Base.Generator.Element
) -> Bool? {
++Log._customContainsEquatableElement[selfType]
return base._customContainsEquatableElement(element)
}
/// If `self` is multi-pass (i.e., a `CollectionType`), invoke
/// `preprocess` on `self` and return its result. Otherwise, return
/// `nil`.
public func _preprocessingPass<R>(
preprocess: (Logging${Kind})->R
) -> R? {
++Log._preprocessingPass[selfType]
return base._preprocessingPass { _ in preprocess(self) }
}
/// Create a native array buffer containing the elements of `self`,
/// in the same order.
public func _copyToNativeArrayBuffer()
-> _ContiguousArrayBuffer<Base.Generator.Element> {
++Log._copyToNativeArrayBuffer[selfType]
return base._copyToNativeArrayBuffer()
}
/// Copy a Sequence into an array.
public func _initializeTo(ptr: UnsafeMutablePointer<Base.Generator.Element>)
-> UnsafeMutablePointer<Base.Generator.Element> {
++Log._initializeTo[selfType]
return base._initializeTo(ptr)
}
public var base: Base
}
% end
public func expectCustomizable<
T : WrapperType where
T : LoggingType,
T.Base : WrapperType, T.Base : LoggingType,
T.Log == T.Base.Log
>(_: T, _ counters: TypeIndexed<Int>,
//===--- TRACE boilerplate ----------------------------------------------===//
@autoclosure _ message: ()->String = "",
showFrame: Bool = true,
stackTrace: SourceLocStack = SourceLocStack(),
file: String = __FILE__, line: UInt = __LINE__
) {
let newTrace = stackTrace.pushIf(showFrame, file: file, line: line)
expectNotEqual(0, counters[T.self], message(), stackTrace: newTrace)
expectEqual(
counters[T.self], counters[T.Base.self], message(), stackTrace: newTrace)
}
public func expectNotCustomizable<
T : WrapperType where
T : LoggingType,
T.Base : WrapperType, T.Base : LoggingType,
T.Log == T.Base.Log
>(_: T, _ counters: TypeIndexed<Int>,
//===--- TRACE boilerplate ----------------------------------------------===//
@autoclosure _ message: ()->String = "",
showFrame: Bool = true,
stackTrace: SourceLocStack = SourceLocStack(),
file: String = __FILE__, line: UInt = __LINE__
) {
let newTrace = stackTrace.pushIf(showFrame, file: file, line: line)
expectNotEqual(0, counters[T.self], message(), stackTrace: newTrace)
expectEqual(0, counters[T.Base.self], message(), stackTrace: newTrace)
}