Skip to content

Commit f153d37

Browse files
committed
Update Logging implementation
1 parent 99479b1 commit f153d37

File tree

3 files changed

+192
-84
lines changed

3 files changed

+192
-84
lines changed

Sources/OpenSwiftUI/Core/Log/Log.swift

Lines changed: 0 additions & 79 deletions
This file was deleted.

Sources/OpenSwiftUI/Data/Model/DynamicProperty/DynamicPropertyBuffer.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public struct _DynamicPropertyBuffer {
9595
}
9696

9797
func destroy() {
98-
precondition(_count >= 0)
98+
Swift.precondition(_count >= 0)
9999
var count = _count
100100
var pointer = buf
101101
while count > 0 {
@@ -112,7 +112,7 @@ public struct _DynamicPropertyBuffer {
112112
}
113113

114114
func reset() {
115-
precondition(_count >= 0)
115+
Swift.precondition(_count >= 0)
116116
var count = _count
117117
var pointer = buf
118118
while count > 0 {
@@ -125,7 +125,7 @@ public struct _DynamicPropertyBuffer {
125125
}
126126

127127
func getState<Value>(type: Value.Type) -> Binding<Value>? {
128-
precondition(_count >= 0)
128+
Swift.precondition(_count >= 0)
129129
var count = _count
130130
var pointer = buf
131131
while count > 0 {
@@ -141,7 +141,7 @@ public struct _DynamicPropertyBuffer {
141141
}
142142

143143
func update(container: UnsafeMutableRawPointer, phase: _GraphInputs.Phase) -> Bool {
144-
precondition(_count >= 0)
144+
Swift.precondition(_count >= 0)
145145
var changed = false
146146
var count = _count
147147
var pointer = buf
@@ -163,7 +163,7 @@ public struct _DynamicPropertyBuffer {
163163
}
164164

165165
private mutating func allocate(bytes: Int) -> UnsafeMutableRawPointer {
166-
precondition(_count >= 0)
166+
Swift.precondition(_count >= 0)
167167
var count = _count
168168
var pointer = buf
169169
while count > 0 {
Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
//
2+
// Logging.swift
3+
// OpenSwiftUI
4+
//
5+
// Audited for RELEASE_2024
6+
// Status: Complete
7+
8+
import Foundation
9+
#if OPENSWIFTUI_SWIFT_LOG
10+
import Logging
11+
extension Logger {
12+
package init(subsystem: String, category: String) {
13+
var logger = Logger(label: subsystem)
14+
logger[metadataKey: "category"] = MetadataValue.string(category)
15+
self = logger
16+
}
17+
}
18+
#else
19+
import os.log
20+
21+
#if DEBUG
22+
package let dso = { () -> UnsafeMutableRawPointer in
23+
let count = _dyld_image_count()
24+
for i in 0 ..< count {
25+
if let name = _dyld_get_image_name(i) {
26+
let swiftString = String(cString: name)
27+
if swiftString.hasSuffix("/SwiftUI") {
28+
if let header = _dyld_get_image_header(i) {
29+
return UnsafeMutableRawPointer(mutating: UnsafeRawPointer(header))
30+
}
31+
}
32+
}
33+
}
34+
return UnsafeMutableRawPointer(mutating: #dsohandle)
35+
}()
36+
#endif
37+
38+
#endif
39+
40+
@usableFromInline
41+
package enum Log {
42+
package static let subsystem: String = "org.OpenSwiftUIProject.OpenSwiftUI"
43+
44+
@inline(__always)
45+
package static func log(_ message: @autoclosure () -> String, unless condition: @autoclosure () -> Bool, file: StaticString, line: UInt) {
46+
guard !condition() else { return }
47+
#if OPENSWIFTUI_SWIFT_LOG
48+
internalErrorsLog.debug("\(message()) \(file) \(line)")
49+
#else
50+
#if DEBUG
51+
os_log(.default, log: internalErrorsLog, "%s %s: %s", message(), file.description, line.description)
52+
#endif
53+
#endif
54+
}
55+
56+
@inline(__always)
57+
package static func log(_ message: @autoclosure () -> String, unless condition: @autoclosure () -> Bool, file: StaticString = #fileID) {
58+
log(message(), unless: condition(), file: file, line: #line)
59+
}
60+
61+
@inline(__always)
62+
package static func log(_ message: @autoclosure () -> String, unless condition: @autoclosure () -> Bool) {
63+
log(message(), unless: condition(), file: #fileID)
64+
}
65+
66+
@inline(__always)
67+
package static func log(_ message: @autoclosure () -> String) {
68+
log(message(), unless: false)
69+
}
70+
71+
package static func internalWarning(_ message: @autoclosure () -> String, file: StaticString, line: UInt) {
72+
print("\(message()) - \(file): - please file a bug report")
73+
}
74+
75+
package static func internalWarning(_ message: @autoclosure () -> String) {
76+
internalWarning(message(), file: #fileID, line: #line)
77+
}
78+
79+
package static func internalError(_ message: @autoclosure () -> String, file: StaticString = #fileID, line: UInt = #line) {
80+
#if OPENSWIFTUI_SWIFT_LOG
81+
internalErrorsLog.log(level: .error, "\(message()) - \(file): - please file a bug report")
82+
#else
83+
#if OPENSWIFTUI_SUPPORT_2022_API
84+
os_log(.fault, log: internalErrorsLog, "%s %s: %s", message(), file.description, line.description)
85+
print("\(message()) - \(file): - please file a bug report")
86+
#endif
87+
#endif
88+
}
89+
90+
package static func internalError(_ message: @autoclosure () -> String) {
91+
internalError(message(), file: #fileID, line: #line)
92+
}
93+
94+
package static func externalWarning(_ message: String) {
95+
#if OPENSWIFTUI_SWIFT_LOG
96+
unlocatedIssuesLog.log(level: .critical, "\(message)")
97+
#else
98+
unlocatedIssuesLog.fault("\(message)")
99+
#endif
100+
}
101+
102+
package static func eventDebug(_ message: String) {
103+
#if !OPENSWIFTUI_SWIFT_LOG
104+
os_log(log: eventDebuggingLog, "\(message)")
105+
#endif
106+
}
107+
108+
#if OPENSWIFTUI_SWIFT_LOG
109+
@usableFromInline
110+
package static var runtimeIssuesLog = Logger(subsystem: "com.apple.runtime-issues", category: "OpenSwiftUI")
111+
112+
@_transparent
113+
package static func runtimeIssues(
114+
_ message: @autoclosure () -> StaticString,
115+
_ args: @autoclosure () -> [CVarArg] = []
116+
) {
117+
runtimeIssuesLog.log(level: .critical, "\(message())")
118+
}
119+
#else
120+
@usableFromInline
121+
package static var runtimeIssuesLog: OSLog = OSLog(subsystem: "com.apple.runtime-issues", category: "OpenSwiftUI")
122+
123+
@_transparent
124+
package static func runtimeIssues(
125+
_ message: @autoclosure () -> StaticString,
126+
_ args: @autoclosure () -> [CVarArg] = []
127+
) {
128+
#if DEBUG
129+
unsafeBitCast(
130+
os_log as (OSLogType, UnsafeRawPointer, OSLog, StaticString, CVarArg...) -> Void,
131+
to: ((OSLogType, UnsafeRawPointer, OSLog, StaticString, [CVarArg]) -> Void).self
132+
)(.fault, dso, runtimeIssuesLog, message(), args())
133+
#else
134+
os_log(.fault, log: runtimeIssuesLog, message(), args())
135+
#endif
136+
}
137+
138+
#endif
139+
package static let propertyChangeLog: Logger = Logger(subsystem: subsystem, category: "Changed Body Properties")
140+
package static var unlocatedIssuesLog: Logger = Logger(subsystem: subsystem, category: "Invalid Configuration")
141+
142+
#if OPENSWIFTUI_SWIFT_LOG
143+
@usableFromInline
144+
package static var internalErrorsLog: Logger = Logger(subsystem: subsystem, category: "OpenSwiftUI")
145+
#else
146+
#if OPENSWIFTUI_SUPPORT_2022_API
147+
@usableFromInline
148+
package static var internalErrorsLog: OSLog = OSLog(subsystem: subsystem, category: "OpenSwiftUI")
149+
#endif
150+
151+
@usableFromInline
152+
package static var eventDebuggingLog: OSLog = OSLog(subsystem: "com.apple.diagnostics.events", category: "OpenSwiftUI")
153+
#endif
154+
155+
package static let archiving: Logger = Logger(subsystem: subsystem, category: "Archiving")
156+
package static let archivedToggle: Logger = Logger(subsystem: subsystem, category: "ArchivedToggle")
157+
package static let archivedButton: Logger = Logger(subsystem: subsystem, category: "ArchivedButton")
158+
package static let archivedPlaybackButton: Logger = Logger(subsystem: subsystem, category: "ArchivedPlaybackButton")
159+
package static let metadataExtraction: Logger = Logger(subsystem: subsystem, category: "MetadataExtraction")
160+
}
161+
162+
@available(*, unavailable)
163+
extension Log: Sendable {}
164+
165+
@_transparent
166+
package func precondition(_ condition: @autoclosure () -> Bool, _ message: @autoclosure () -> String, file: StaticString = #fileID, line: UInt = #line) {
167+
guard condition() else {
168+
Swift.preconditionFailure(message(), file: file, line: line)
169+
}
170+
}
171+
172+
@_transparent
173+
package func preconditionFailure(_ message: @autoclosure () -> String, file: StaticString, line: UInt) -> Never {
174+
Swift.preconditionFailure(message(), file: file, line: line)
175+
}
176+
177+
@_transparent
178+
package func preconditionFailure(_ message: @autoclosure () -> String) -> Never {
179+
preconditionFailure(message(), file: #fileID, line: #line)
180+
}
181+
182+
#if !OPENSWIFTUI_SWIFT_LOG
183+
extension os.OSLog {
184+
@usableFromInline
185+
static var runtimeIssuesLog: os.OSLog = OSLog(subsystem: "com.apple.runtime-issues", category: "OpenSwiftUI")
186+
}
187+
#endif

0 commit comments

Comments
 (0)