Skip to content

Commit 3bd5060

Browse files
committed
Update traceInternal and traceBody
1 parent 70817fd commit 3bd5060

File tree

3 files changed

+119
-46
lines changed

3 files changed

+119
-46
lines changed

Package.resolved

+1-19
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,6 @@
11
{
2-
"originHash" : "5ab08033fa312aa04f0cf9d4751edd41156f13745ec663a3da48d38ca11c507b",
2+
"originHash" : "093c1851811a8423bb946386b7901adf094aa28258f12ae335ef9ee3a59018c7",
33
"pins" : [
4-
{
5-
"identity" : "opencombine",
6-
"kind" : "remoteSourceControl",
7-
"location" : "https://github.com/OpenSwiftUIProject/OpenCombine.git",
8-
"state" : {
9-
"revision" : "63aef318cb3a853bcb8d774cce15f4dcb1ccdfe4",
10-
"version" : "0.15.1"
11-
}
12-
},
134
{
145
"identity" : "opengraph",
156
"kind" : "remoteSourceControl",
@@ -18,15 +9,6 @@
189
"branch" : "main",
1910
"revision" : "7f22fb5948bb6bc77603f5496a9881622041d3bb"
2011
}
21-
},
22-
{
23-
"identity" : "swift-log",
24-
"kind" : "remoteSourceControl",
25-
"location" : "https://github.com/apple/swift-log",
26-
"state" : {
27-
"revision" : "9cb486020ebf03bfa5b5df985387a14a98744537",
28-
"version" : "1.6.1"
29-
}
3012
}
3113
],
3214
"version" : 3

Sources/OpenSwiftUICore/Log/Signpost.swift

+89-26
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@ package struct Signpost {
101101
#if canImport(Darwin)
102102
switch style {
103103
case let .kdebug(code):
104-
return kdebug_is_enabled(UInt32(code))
105-
case let .os_log(name):
104+
return kdebug_is_enabled(UInt32(OSSignpostType.event.rawValue & 0xfc) | (UInt32(code) << 2) & 0x3fc | 0x14110000)
105+
case .os_log:
106106
guard kdebug_is_enabled(UInt32(OSSignpostType.event.rawValue & 0xfc) | 0x14110000) else {
107107
return false
108108
}
@@ -115,44 +115,107 @@ package struct Signpost {
115115

116116
// TODO
117117
@_transparent
118-
@inline(__always)
119-
package func traceInterval<R>(
120-
object: AnyObject? = nil,
118+
package func traceInterval<T>(
119+
object: AnyObject?,
121120
_ message: StaticString?,
122-
closure: () -> R
123-
) -> R {
121+
closure: () -> T
122+
) -> T {
124123
guard isEnabled else {
125124
return closure()
126125
}
127-
// TODO
128-
return closure()
126+
let id = OSSignpostID.makeExclusiveID(object)
127+
switch style {
128+
case let .kdebug(code):
129+
kdebug_trace(UInt32(code) << 2 | (UInt32(OSSignpostType.begin.rawValue & 0xfc) | 0x14110000), id.rawValue, 0, 0, 0)
130+
defer { kdebug_trace(UInt32(code) << 2 | (UInt32(OSSignpostType.end.rawValue & 0xfc) | 0x14110000), id.rawValue, 0, 0, 0) }
131+
return closure()
132+
case let .os_log(name):
133+
if let message {
134+
os_signpost(.begin, log: _signpostLog, name: name, signpostID: id, message, [])
135+
} else {
136+
os_signpost(.begin, log: _signpostLog, name: name, signpostID: id)
137+
}
138+
defer { os_signpost(.end, log: _signpostLog, name: name, signpostID: id) }
139+
return closure()
140+
}
129141
}
130142

131-
// TODO
132143
@_transparent
133-
@inline(__always)
134-
package func traceInterval<R>(
144+
package func traceInterval<T>(
135145
object: AnyObject? = nil,
136-
_ message: StaticString?,
137-
_ arguments: @autoclosure () -> [CVarArg],
138-
closure: () -> R
139-
) -> R {
146+
_ message: StaticString,
147+
_ args: @autoclosure () -> [any CVarArg],
148+
closure: () -> T
149+
) -> T {
140150
guard isEnabled else {
141151
return closure()
142152
}
153+
let id = OSSignpostID.makeExclusiveID(object)
154+
switch style {
155+
case let .kdebug(code):
156+
// FIXME: _primitive
157+
print(code)
158+
return closure()
159+
case let .os_log(name):
160+
os_signpost(.begin, log: _signpostLog, name: name, signpostID: id, message, args())
161+
defer { os_signpost(.end, log: _signpostLog, name: name, signpostID: id) }
162+
return closure()
163+
}
164+
}
165+
166+
@_transparent
167+
package func traceEvent(
168+
type: OSSignpostType,
169+
object: AnyObject?,
170+
_ message: StaticString,
171+
_ args: @autoclosure () -> [any CVarArg]
172+
) {
173+
guard isEnabled else {
174+
return
175+
}
176+
#if canImport(Darwin)
177+
let id = OSSignpostID.makeExclusiveID(object)
178+
let args = args()
179+
180+
switch style {
181+
case let .kdebug(code):
182+
// FIXME: _primitive
183+
print(code)
184+
return
185+
case let .os_log(name):
186+
os_signpost(type, log: _signpostLog, name: name, signpostID: id, message, args)
187+
}
188+
#endif
189+
}
190+
191+
#if canImport(Darwin)
192+
private func _primitive(
193+
_ type: OSSignpostType,
194+
log: OSLog,
195+
signpostID: OSSignpostID,
196+
_ message: StaticString?,
197+
_ arguments: [any CVarArg]?
198+
) {
143199
// TODO
144-
return closure()
145200
}
201+
#endif
146202
}
147203

148-
// TODO
149-
@_transparent
150-
@inline(__always)
151-
package func traceRuleBody<R>(_ type: Any.Type, body: () -> R) -> R {
152-
Signpost.bodyInvoke.traceInterval(
153-
"%{public}@.body [in %{public}@]",
154-
[OGTypeID(type).description, Tracing.libraryName(defining: type)]
155-
) {
156-
body()
204+
#if canImport(Darwin)
205+
extension OSSignpostID {
206+
private static let continuation = OSSignpostID(0x0ea89ce2)
207+
208+
@inline(__always)
209+
static func makeExclusiveID(_ object: AnyObject?) -> OSSignpostID {
210+
if let object {
211+
OSSignpostID(log: _signpostLog, object: object)
212+
} else {
213+
.exclusive
214+
}
157215
}
158216
}
217+
#endif
218+
219+
private func withKDebugValues(_ code: UInt32, _ args: [(any CVarArg)?], closure: (([UInt64]) -> Void)) {
220+
// TODO
221+
}

Sources/OpenSwiftUICore/Util/Tracing.swift

+29-1
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,13 @@ import WASILibc
1818
#endif
1919
internal import OpenGraphShims
2020
import Foundation
21+
internal import COpenSwiftUICore
22+
#if canImport(Darwin)
23+
import os.log
24+
#endif
2125

2226
enum Tracing {
23-
private static var moduleLookupCache = ThreadSpecific<[UnsafeRawPointer : String]>([:])
27+
private static var moduleLookupCache = ThreadSpecific<[UnsafeRawPointer: String]>([:])
2428

2529
static func libraryName(defining type: Any.Type) -> String {
2630
let unknown = "ModuleUnknown"
@@ -50,3 +54,27 @@ enum Tracing {
5054
OGTypeID(type).description
5155
}
5256
}
57+
58+
@_transparent
59+
package func traceBody<Body>(_ v: any Any.Type, body: () -> Body) -> Body {
60+
#if canImport(Darwin)
61+
// Signpost.bodyInvoke.traceInterval
62+
guard kdebug_is_enabled(UInt32(OSSignpostType.event.rawValue) & 0xF8 | 0x1411_0014) else {
63+
return body()
64+
}
65+
// TODO: OGTypeID(type).description, Tracing.libraryName(defining: v)
66+
return body()
67+
#else
68+
body()
69+
#endif
70+
}
71+
72+
@_transparent
73+
package func traceRuleBody<Body>(_ v: any Any.Type, body: () -> Body) -> Body {
74+
#if canImport(Darwin)
75+
// TODO:
76+
return body()
77+
#else
78+
body()
79+
#endif
80+
}

0 commit comments

Comments
 (0)