|
10 | 10 |
|
11 | 11 | import Foundation |
12 | 12 |
|
13 | | -public struct Tracing { |
14 | | - public enum EventType { |
15 | | - case asyncBegin |
16 | | - case asyncEnd |
| 13 | +public enum TracingEventType { |
| 14 | + case asyncBegin |
| 15 | + case asyncEnd |
| 16 | +} |
| 17 | + |
| 18 | +public protocol TracingEventProtocol { |
| 19 | + /// The category of the event. |
| 20 | + var cat: String { get } |
| 21 | + |
| 22 | + /// The name of the event. |
| 23 | + var name: String { get } |
| 24 | + |
| 25 | + /// The free form id of the event. |
| 26 | + var id: String { get } |
| 27 | + |
| 28 | + /// The phase of the event. |
| 29 | + var ph: TracingEventType { get } |
| 30 | + |
| 31 | + /// The process id of the process where the event occured. |
| 32 | + var pid: Int { get } |
| 33 | + |
| 34 | + /// The thread id of the process where the event occured. |
| 35 | + var tid: Int { get } |
| 36 | + |
| 37 | + /// The timestamp of the event. |
| 38 | + var ts: Int { get } |
| 39 | + |
| 40 | + /// The start time of the process where the event occured. |
| 41 | + var startTs: Int { get } |
| 42 | + init( |
| 43 | + cat: String, |
| 44 | + name: String, |
| 45 | + id: String, |
| 46 | + ph: TracingEventType, |
| 47 | + pid: Int, |
| 48 | + tid: Int, |
| 49 | + ts: Int, |
| 50 | + startTs: Int |
| 51 | + ) |
| 52 | +} |
| 53 | + |
| 54 | +public protocol TracingCollectionProtocol { |
| 55 | + var events: [TracingEventProtocol] { get set } |
| 56 | + init(_ events: [TracingEventProtocol]) |
| 57 | +} |
| 58 | + |
| 59 | +extension TracingCollectionProtocol { |
| 60 | + public mutating func append(_ tracingCollection: TracingCollectionProtocol) { |
| 61 | + self.events.append(contentsOf: tracingCollection.events) |
17 | 62 | } |
| 63 | +} |
18 | 64 |
|
19 | | - public struct Event { |
20 | | - /// The category of the event. |
21 | | - public let cat: String |
22 | | - |
23 | | - /// The name of the event. |
24 | | - public let name: String |
25 | | - |
26 | | - /// The free form id of the event. |
27 | | - public let id: String |
28 | | - |
29 | | - /// The phase of the event. |
30 | | - public let ph: EventType |
31 | | - |
32 | | - /// The process id of the process where the event occured. |
33 | | - public let pid: Int |
34 | | - |
35 | | - /// The thread id of the process where the event occured. |
36 | | - public let tid: Int |
37 | | - |
38 | | - /// The timestamp of the event. |
39 | | - public let ts: Int |
40 | | - |
41 | | - /// The start time of the process where the event occured. |
42 | | - public let startTs: Int |
43 | | - |
44 | | - #if canImport(Darwin) |
45 | | - public init( |
46 | | - cat: String, |
47 | | - name: String, |
48 | | - id: String, |
49 | | - ph: EventType, |
50 | | - pid: Int = Int(getpid()), |
51 | | - tid: Int = Int(pthread_mach_thread_np(pthread_self())), |
52 | | - ts: Int = Int(DispatchTime.now().uptimeNanoseconds), |
53 | | - startTs: Int = 0 |
54 | | - ) { |
55 | | - self.cat = cat |
56 | | - self.name = name |
57 | | - self.id = id |
58 | | - self.ph = ph |
59 | | - self.pid = pid |
60 | | - self.tid = tid |
61 | | - self.ts = ts |
62 | | - self.startTs = startTs |
63 | | - } |
64 | | - #elseif canImport(Glibc) |
65 | | - public init( |
66 | | - cat: String, |
67 | | - name: String, |
68 | | - id: String, |
69 | | - ph: EventType, |
70 | | - pid: Int = Int(getpid()), |
71 | | - tid: Int = 1, |
72 | | - ts: Int = Int(DispatchTime.now().uptimeNanoseconds), |
73 | | - startTs: Int = 0 |
74 | | - ) { |
75 | | - self.cat = cat |
76 | | - self.name = name |
77 | | - self.id = id |
78 | | - self.ph = ph |
79 | | - self.pid = pid |
80 | | - self.tid = tid |
81 | | - self.ts = ts |
82 | | - self.startTs = startTs |
83 | | - } |
84 | | - #else |
85 | | - public init( |
86 | | - cat: String, |
87 | | - name: String, |
88 | | - id: String, |
89 | | - ph: EventType, |
90 | | - pid: Int = 1, |
91 | | - tid: Int = 1, |
92 | | - ts: Int = Int(DispatchTime.now().uptimeNanoseconds), |
93 | | - startTs: Int = 0 |
94 | | - ) { |
95 | | - self.cat = cat |
96 | | - self.name = name |
97 | | - self.id = id |
98 | | - self.ph = ph |
99 | | - self.pid = pid |
100 | | - self.tid = tid |
101 | | - self.ts = ts |
102 | | - self.startTs = startTs |
103 | | - } |
104 | | - #endif |
| 65 | +public struct TracingEvent: TracingEventProtocol { |
| 66 | + public let cat: String |
| 67 | + public let name: String |
| 68 | + public let id: String |
| 69 | + public let ph: TracingEventType |
| 70 | + public let pid: Int |
| 71 | + public let tid: Int |
| 72 | + public let ts: Int |
| 73 | + public let startTs: Int |
| 74 | + |
| 75 | + #if canImport(Darwin) |
| 76 | + public init( |
| 77 | + cat: String, |
| 78 | + name: String, |
| 79 | + id: String, |
| 80 | + ph: TracingEventType, |
| 81 | + pid: Int = Int(getpid()), |
| 82 | + tid: Int = Int(pthread_mach_thread_np(pthread_self())), |
| 83 | + ts: Int = Int(DispatchTime.now().uptimeNanoseconds), |
| 84 | + startTs: Int = 0 |
| 85 | + ) { |
| 86 | + self.cat = cat |
| 87 | + self.name = name |
| 88 | + self.id = id |
| 89 | + self.ph = ph |
| 90 | + self.pid = pid |
| 91 | + self.tid = tid |
| 92 | + self.ts = ts |
| 93 | + self.startTs = startTs |
| 94 | + } |
| 95 | + #elseif canImport(Glibc) |
| 96 | + public init( |
| 97 | + cat: String, |
| 98 | + name: String, |
| 99 | + id: String, |
| 100 | + ph: TracingEventType, |
| 101 | + pid: Int = Int(getpid()), |
| 102 | + tid: Int = 1, |
| 103 | + ts: Int = Int(DispatchTime.now().uptimeNanoseconds), |
| 104 | + startTs: Int = 0 |
| 105 | + ) { |
| 106 | + self.cat = cat |
| 107 | + self.name = name |
| 108 | + self.id = id |
| 109 | + self.ph = ph |
| 110 | + self.pid = pid |
| 111 | + self.tid = tid |
| 112 | + self.ts = ts |
| 113 | + self.startTs = startTs |
| 114 | + } |
| 115 | + #else |
| 116 | + public init( |
| 117 | + cat: String, |
| 118 | + name: String, |
| 119 | + id: String, |
| 120 | + ph: TracingEventType, |
| 121 | + pid: Int = 1, |
| 122 | + tid: Int = 1, |
| 123 | + ts: Int = Int(DispatchTime.now().uptimeNanoseconds), |
| 124 | + startTs: Int = 0 |
| 125 | + ) { |
| 126 | + self.cat = cat |
| 127 | + self.name = name |
| 128 | + self.id = id |
| 129 | + self.ph = ph |
| 130 | + self.pid = pid |
| 131 | + self.tid = tid |
| 132 | + self.ts = ts |
| 133 | + self.startTs = startTs |
105 | 134 | } |
| 135 | + #endif |
| 136 | +} |
106 | 137 |
|
107 | | - public struct Collection { |
108 | | - public var events: [Event] = [] |
109 | | - public init(_ events: [Tracing.Event] = []) { |
110 | | - self.events = events |
111 | | - } |
| 138 | +public class TracingCollection: TracingCollectionProtocol { |
| 139 | + public var events: [TracingEventProtocol] = [] |
| 140 | + public required init(_ events: [TracingEventProtocol] = []) { |
| 141 | + self.events = events |
112 | 142 | } |
113 | 143 | } |
114 | 144 |
|
115 | 145 | extension Context { |
116 | | - public static func withTracing(_ collection: Tracing.Collection) -> Context { |
117 | | - return Context(dictionaryLiteral: (ObjectIdentifier(Tracing.Collection.self), collection as Any)) |
| 146 | + public static func withTracing(_ collection: TracingCollectionProtocol) -> Context { |
| 147 | + return Context(dictionaryLiteral: (ObjectIdentifier(TracingCollectionProtocol.self), collection as Any)) |
118 | 148 | } |
119 | 149 |
|
120 | | - public mutating func enrichWithTracing(_ collection: Tracing.Collection) -> Context { |
121 | | - self[ObjectIdentifier(Tracing.Collection.self)] = collection |
| 150 | + public mutating func enrichWithTracing(_ collection: TracingCollectionProtocol) -> Context { |
| 151 | + self[ObjectIdentifier(TracingCollectionProtocol.self)] = collection |
122 | 152 | return self |
123 | 153 | } |
124 | 154 |
|
125 | | - public var tracing: Tracing.Collection? { |
| 155 | + public var tracing: TracingCollectionProtocol? { |
126 | 156 | get { |
127 | | - guard let collection = self[ObjectIdentifier(Tracing.Collection.self)] as? Tracing.Collection else { |
| 157 | + guard let collection = self[ObjectIdentifier(TracingCollectionProtocol.self)] as? TracingCollectionProtocol else { |
128 | 158 | return nil |
129 | 159 | } |
130 | 160 | return collection |
131 | 161 | } |
132 | 162 | set { |
133 | | - self[ObjectIdentifier(Tracing.Collection.self)] = newValue |
| 163 | + self[ObjectIdentifier(TracingCollectionProtocol.self)] = newValue |
134 | 164 | } |
135 | 165 | } |
136 | 166 | } |
0 commit comments