|
| 1 | +/* |
| 2 | + This source file is part of the Swift.org open source project |
| 3 | + |
| 4 | + Copyright (c) 2020 Apple Inc. and the Swift project authors |
| 5 | + Licensed under Apache License v2.0 with Runtime Library Exception |
| 6 | + |
| 7 | + See http://swift.org/LICENSE.txt for license information |
| 8 | + See http://swift.org/CONTRIBUTORS.txt for Swift project authors |
| 9 | + */ |
| 10 | + |
| 11 | +import Foundation |
| 12 | + |
| 13 | +public struct Tracing { |
| 14 | + public enum EventType { |
| 15 | + case asyncBegin |
| 16 | + case asyncEnd |
| 17 | + } |
| 18 | + |
| 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 |
| 105 | + } |
| 106 | + |
| 107 | + public struct Collection { |
| 108 | + public var events: [Event] = [] |
| 109 | + public init(_ events: [Tracing.Event] = []) { |
| 110 | + self.events = events |
| 111 | + } |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +extension Context { |
| 116 | + public static func withTracing(_ collection: Tracing.Collection) -> Context { |
| 117 | + return Context(dictionaryLiteral: (ObjectIdentifier(Tracing.Collection.self), collection as Any)) |
| 118 | + } |
| 119 | + |
| 120 | + public mutating func enrichWithTracing(_ collection: Tracing.Collection) -> Context { |
| 121 | + self[ObjectIdentifier(Tracing.Collection.self)] = collection |
| 122 | + return self |
| 123 | + } |
| 124 | + |
| 125 | + var tracing: Tracing.Collection? { |
| 126 | + get { |
| 127 | + guard let collection = self[ObjectIdentifier(Tracing.Collection.self)] as? Tracing.Collection else { |
| 128 | + return nil |
| 129 | + } |
| 130 | + return collection |
| 131 | + } |
| 132 | + set { |
| 133 | + self[ObjectIdentifier(Tracing.Collection.self)] = newValue |
| 134 | + } |
| 135 | + } |
| 136 | +} |
0 commit comments