Skip to content

Commit 24d0dd8

Browse files
committed
Add Tracing.libraryName
1 parent 6dab9b6 commit 24d0dd8

File tree

6 files changed

+103
-29
lines changed

6 files changed

+103
-29
lines changed

Package.resolved

Lines changed: 19 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Sources/OpenSwiftUI/Core/Util/Tracing.swift renamed to Sources/OpenSwiftUI/Core/Util/DescriptiveDynamicProperty.swift

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,9 @@
11
//
2-
// Tracing.swift
2+
// DescriptiveDynamicProperty.swift
33
// OpenSwiftUI
44
//
5-
// Audited for RELEASE_2021
6-
// Status: WIP
7-
// ID: D59B7A281FFF29619A43A3D8F551CCE1
8-
9-
// MARK: - Tracing
10-
11-
enum Tracing {
12-
static func libraryName(defining _: Any.Type) -> String {
13-
// TODO:
14-
""
15-
}
16-
// private static moduleLookupCache: ThreadSpecific<[UnsafeRawPointer : String]>
17-
}
5+
// Created by Kyle on 2024/9/22.
6+
//
187

198
// MARK: - DescriptiveDynamicProperty
209

Sources/OpenSwiftUICore/Log/Logging.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ package enum Log {
4747
#if OPENSWIFTUI_SWIFT_LOG
4848
internalErrorsLog.debug("\(message()) \(file) \(line)")
4949
#else
50-
#if DEBUG
50+
#if DEBUG && OPENSWIFTUI_SUPPORT_2022_API
5151
os_log(.default, log: internalErrorsLog, "%s %s: %s", message(), file.description, line.description)
5252
#endif
5353
#endif
@@ -82,8 +82,8 @@ package enum Log {
8282
#else
8383
#if OPENSWIFTUI_SUPPORT_2022_API
8484
os_log(.fault, log: internalErrorsLog, "%s %s: %s", message(), file.description, line.description)
85-
print("\(message()) - \(file): - please file a bug report")
8685
#endif
86+
print("\(message()) - \(file): - please file a bug report")
8787
#endif
8888
}
8989

Sources/OpenSwiftUI/Core/Log/Signpost.swift renamed to Sources/OpenSwiftUICore/Log/Signpost.swift

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,23 @@
77
// ID: 34756F646CF7AC3DBE2A8E0B344C962F
88

99
internal import OpenGraphShims
10-
#if canImport(os)
10+
#if canImport(Darwin)
1111
import os.signpost
1212
#endif
1313

14-
struct Signpost {
14+
package struct Signpost {
15+
#if canImport(Darwin)
16+
package static let archiving = OSSignposter(logger: Log.archiving)
17+
package static let metaExtraction = OSSignposter(logger: Log.metadataExtraction)
18+
#endif
19+
20+
package static let moduleName: String = Tracing.libraryName(defining: Signpost.self)
21+
1522
private let style: Style
1623
private let stability: Stability
1724

1825
// TODO
19-
var isEnabled: Bool {
26+
package var isEnabled: Bool {
2027
switch stability {
2128
case .disabled, .verbose, .debug:
2229
return false
@@ -25,14 +32,14 @@ struct Signpost {
2532
}
2633
}
2734

28-
static let render = Signpost(style: .kdebug(0), stability: .published)
29-
static let renderUpdate = Signpost(style: .kdebug(0), stability: .published)
30-
static let viewHost = Signpost(style: .kdebug(9), stability: .published)
31-
static let bodyInvoke = Signpost(style: .kdebug(5), stability: .published)
35+
package static let render = Signpost(style: .kdebug(0), stability: .published)
36+
package static let renderUpdate = Signpost(style: .kdebug(0), stability: .published)
37+
package static let viewHost = Signpost(style: .kdebug(9), stability: .published)
38+
package static let bodyInvoke = Signpost(style: .kdebug(5), stability: .published)
3239

3340
@_transparent
3441
@inline(__always)
35-
func traceInterval<R>(
42+
package func traceInterval<R>(
3643
object: AnyObject? = nil,
3744
_ message: StaticString?,
3845
closure: () -> R
@@ -46,7 +53,7 @@ struct Signpost {
4653

4754
@_transparent
4855
@inline(__always)
49-
func traceInterval<R>(
56+
package func traceInterval<R>(
5057
object: AnyObject? = nil,
5158
_ message: StaticString?,
5259
_ arguments: @autoclosure () -> [CVarArg],
@@ -77,7 +84,7 @@ extension Signpost {
7784
@_transparent
7885
@inline(__always)
7986
// FIXME
80-
func traceRuleBody<R>(_ type: Any.Type, body: () -> R) -> R {
87+
package func traceRuleBody<R>(_ type: Any.Type, body: () -> R) -> R {
8188
Signpost.bodyInvoke.traceInterval(
8289
"%{public}@.body [in %{public}@]",
8390
[OGTypeID(type).description, Tracing.libraryName(defining: type)]

Sources/OpenSwiftUICore/Util/ThreadUtils.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// Audited for RELEASE_2024
66
// Status: Complete
77

8-
import Foundation
8+
@preconcurrency import Foundation
99

1010
final package class ThreadSpecific<T> {
1111
var key: pthread_key_t
@@ -15,6 +15,9 @@ final package class ThreadSpecific<T> {
1515
key = 0
1616
self.defaultValue = defaultValue
1717
pthread_key_create(&key) { pointer in
18+
#if !canImport(Darwin)
19+
guard let pointer else { return }
20+
#endif
1821
pointer.withMemoryRebound(to: Any.self, capacity: 1) { ptr in
1922
ptr.deinitialize(count: 1)
2023
ptr.deallocate()
@@ -56,7 +59,10 @@ package func onMainThread(do body: @escaping () -> Void) {
5659
if Thread.isMainThread {
5760
body()
5861
} else {
59-
RunLoop.main.perform(inModes: [.common], block: body)
62+
RunLoop.main.perform(inModes: [.common]) {
63+
// Workaround the @Senable warning
64+
body()
65+
}
6066
}
6167
#endif
6268
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
//
2+
// Tracing.swift
3+
// OpenSwiftUICore
4+
//
5+
// Audited for RELEASE_2024
6+
// Status: WIP
7+
// ID: D59B7A281FFF29619A43A3D8F551CCE1 (RELEASE_2021)
8+
// ID: 56D4CED87D5B226E2B40FB60C47D6F49 (RELEASE_2024)
9+
10+
#if canImport(Darwin)
11+
import Darwin
12+
#elseif canImport(Glibc)
13+
import Glibc
14+
#elseif canImport(Musl)
15+
import Musl
16+
#elseif os(WASI)
17+
import WASILibc
18+
#endif
19+
internal import OpenGraphShims
20+
import Foundation
21+
22+
enum Tracing {
23+
// RELEASE_2021 ID: D59B7A281FFF29619A43A3D8F551CCE1
24+
// RELEASE_2024 ID: 56D4CED87D5B226E2B40FB60C47D6F49
25+
private static var moduleLookupCache = ThreadSpecific<[UnsafeRawPointer : String]>([:])
26+
27+
static func libraryName(defining type: Any.Type) -> String {
28+
let unknown = "ModuleUnknown"
29+
guard let nominalDescriptor = OGTypeID(type).nominalDescriptor else {
30+
return unknown
31+
}
32+
if let cachedName = moduleLookupCache.value[nominalDescriptor] {
33+
return cachedName
34+
} else {
35+
#if canImport(Darwin)
36+
var info = Dl_info()
37+
guard dladdr(nominalDescriptor, &info) == 0 else {
38+
return unknown
39+
}
40+
let name = (String(cString: info.dli_fname) as NSString).lastPathComponent
41+
moduleLookupCache.value[nominalDescriptor] = name
42+
return unknown
43+
#else
44+
// TODO: [Easy] Add a C layer to import dladdr on non-Darwin Swift platform
45+
// See https://forums.swift.org/t/dladdr-and-the-clang-importer/26379/11
46+
return unknown
47+
#endif
48+
}
49+
}
50+
51+
static func nominalTypeName(_ type: Any.Type) -> String {
52+
OGTypeID(type).description
53+
}
54+
}

0 commit comments

Comments
 (0)