Skip to content

Commit a166851

Browse files
authored
[Feature] Add _logChange implementation (#86)
1 parent 0fa2879 commit a166851

File tree

3 files changed

+81
-9
lines changed

3 files changed

+81
-9
lines changed

Docs/Version.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22

33
RELEASE_2021 - iOS 15.5 - macOS 12.7.1
44

5-
RELEASE_2023 - iOS 17.0 - macOS 14.0
5+
RELEASE_2023 - iOS 17.4 - macOS 14.4.1

Sources/OpenSwiftUI/Core/Log/Log.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@
77

88
#if OPENSWIFTUI_SWIFT_LOG
99
import Logging
10+
extension Logger {
11+
init(subsystem: String, category: String) {
12+
var logger = Logger(label: subsystem)
13+
logger[metadataKey: "category"] = MetadataValue.string(category)
14+
self = logger
15+
}
16+
}
1017
#else
1118
import os
1219
#if DEBUG

Sources/OpenSwiftUI/View/Debug/ChangedBodyProperty.swift

Lines changed: 73 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
internal import OpenGraphShims
1010
import Foundation
1111

12+
// MARK: - printChanges
13+
1214
extension View {
1315
public static func _printChanges() {
1416
printChangedBodyProperties(of: Self.self)
@@ -32,12 +34,75 @@ func printChangedBodyProperties<Body>(of type: Body.Type) {
3234
print(result)
3335
}
3436

37+
// MARK: - logChanges
38+
39+
// Audited for RELEASE_2023
40+
41+
#if OPENSWIFTUI_SWIFT_LOG
42+
import Logging
43+
44+
extension Logger {
45+
static let changeBodyPropertiesLogger = Logger(subsystem: "org.OpenSwiftUIProject.OpenSwiftUI", category: "Changed Body Properties")
46+
}
47+
#else
48+
import os.log
49+
50+
@available(iOS 14.0, macOS 11, *)
51+
extension Logger {
52+
static let changeBodyPropertiesLogger = Logger(subsystem: "org.OpenSwiftUIProject.OpenSwiftUI", category: "Changed Body Properties")
53+
}
54+
55+
extension OSLog {
56+
static let changeBodyPropertiesLogger = OSLog(subsystem: "org.OpenSwiftUIProject.OpenSwiftUI", category: "Changed Body Properties")
57+
}
58+
#endif
59+
60+
extension View {
61+
public static func _logChanges() {
62+
logChangedBodyProperties(of: Self.self)
63+
}
64+
}
65+
66+
extension ViewModifier {
67+
public static func _logChanges() {
68+
logChangedBodyProperties(of: Self.self)
69+
}
70+
}
71+
72+
func logChangedBodyProperties<Body>(of type: Body.Type) {
73+
let properties = changedBodyProperties(of: type)
74+
let result = OGTypeID(type).description
75+
if properties.isEmpty {
76+
#if OPENSWIFTUI_SWIFT_LOG
77+
Logger.changeBodyPropertiesLogger.info("\(result): unchanged.")
78+
#else
79+
if #available(iOS 14.0, macOS 11, *) {
80+
Logger.changeBodyPropertiesLogger.info("\(result, privacy: .public): unchanged.")
81+
} else {
82+
os_log("%{public}s: unchanged.", log: .changeBodyPropertiesLogger, type: .info, result)
83+
}
84+
#endif
85+
} else {
86+
#if OPENSWIFTUI_SWIFT_LOG
87+
Logger.changeBodyPropertiesLogger.info("\(result): \(properties.joined(separator: ", ")) changed.")
88+
#else
89+
if #available(iOS 14.0, macOS 11, *) {
90+
Logger.changeBodyPropertiesLogger.info("\(result, privacy: .public): \(properties.joined(separator: ", "), privacy: .public) changed.")
91+
} else {
92+
os_log("%{public}s: %{public}s changed.", log: .changeBodyPropertiesLogger, type: .info, result, properties.joined(separator: ", "))
93+
}
94+
#endif
95+
}
96+
}
97+
98+
// MARK: - changedBodyProperties
99+
35100
func changedBodyProperties<Body>(of type: Body.Type) -> [String] {
36101
var index = 0
37102
repeat {
38103
let options = [
39104
OGGraph.descriptionFormat.takeUnretainedValue(): "stack/frame",
40-
"frame_index": index
105+
"frame_index": index,
41106
] as NSDictionary
42107
guard let description = OGGraph.description(nil, options: options),
43108
let dict = description.takeUnretainedValue() as? [String: Any],
@@ -69,23 +134,23 @@ func changedBodyProperties<Body>(of type: Body.Type) -> [String] {
69134
let fields = DynamicPropertyCache.fields(of: Body.self)
70135
buffer.applyChanged { offset in
71136
switch fields.layout {
72-
case .product(let fields):
137+
case let .product(fields):
73138
guard let field = fields.first(where: { $0.offset == offset }),
74-
let name = field.name,
75-
let property = String(cString: name, encoding: .utf8) else {
139+
let name = field.name,
140+
let property = String(cString: name, encoding: .utf8)
141+
else {
76142
properties.append("@\(offset)")
77143
return
78144
}
79145
properties.append(property)
80-
break
81-
case .sum(_, _):
146+
case .sum:
82147
properties.append("@\(offset)")
83-
break
84148
}
85149
}
86150
}
87151
return properties
88-
} while (index != 32)
152+
} while index != 32
89153
return []
90154
}
155+
91156
#endif

0 commit comments

Comments
 (0)