Skip to content

Commit

Permalink
[Feature] Add _logChange implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyle-Ye committed Apr 27, 2024
1 parent 0fa2879 commit 5e95b2d
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Docs/Version.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

RELEASE_2021 - iOS 15.5 - macOS 12.7.1

RELEASE_2023 - iOS 17.0 - macOS 14.0
RELEASE_2023 - iOS 17.4 - macOS 14.4.1
7 changes: 7 additions & 0 deletions Sources/OpenSwiftUI/Core/Log/Log.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@

#if OPENSWIFTUI_SWIFT_LOG
import Logging
extension Logger {
init(subsystem: String, category: String) {
var logger = Logger(label: subsystem)
logger[metadataKey: "category"] = MetadataValue.string(category)
self = logger
}
}
#else
import os
#if DEBUG
Expand Down
81 changes: 73 additions & 8 deletions Sources/OpenSwiftUI/View/Debug/ChangedBodyProperty.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
internal import OpenGraphShims
import Foundation

// MARK: - printChanges

extension View {
public static func _printChanges() {
printChangedBodyProperties(of: Self.self)
Expand All @@ -32,12 +34,75 @@ func printChangedBodyProperties<Body>(of type: Body.Type) {
print(result)
}

// MARK: - logChanges

// Audited for RELEASE_2023

#if OPENSWIFTUI_SWIFT_LOG
import Logging

extension Logger {
static let changeBodyPropertiesLogger = Logger(subsystem: "org.OpenSwiftUIProject.OpenSwiftUI", category: "Changed Body Properties")
}
#else
import os.log

@available(iOS 14.0, macOS 11, *)
extension Logger {
static let changeBodyPropertiesLogger = Logger(subsystem: "org.OpenSwiftUIProject.OpenSwiftUI", category: "Changed Body Properties")
}

extension OSLog {
static let changeBodyPropertiesLogger = OSLog(subsystem: "org.OpenSwiftUIProject.OpenSwiftUI", category: "Changed Body Properties")
}
#endif

extension View {
public static func _logChanges() {
logChangedBodyProperties(of: Self.self)
}
}

extension ViewModifier {
public static func _logChanges() {
logChangedBodyProperties(of: Self.self)
}
}

func logChangedBodyProperties<Body>(of type: Body.Type) {
let properties = changedBodyProperties(of: type)
let result = OGTypeID(type).description
if properties.isEmpty {
#if OPENSWIFTUI_SWIFT_LOG
Logger.changeBodyPropertiesLogger.info("\(result): unchanged.")
#else
if #available(iOS 14.0, macOS 11, *) {
Logger.changeBodyPropertiesLogger.info("\(result, privacy: .public): unchanged.")
} else {
os_log("%{public}s: unchanged.", log: .changeBodyPropertiesLogger, type: .info, result)
}
#endif
} else {
#if OPENSWIFTUI_SWIFT_LOG
Logger.changeBodyPropertiesLogger.info("\(result): \(properties.joined(separator: ", ")) changed.")
#else
if #available(iOS 14.0, macOS 11, *) {
Logger.changeBodyPropertiesLogger.info("\(result, privacy: .public): \(properties.joined(separator: ", "), privacy: .public) changed.")
} else {
os_log("%{public}s: %{public}s changed.", log: .changeBodyPropertiesLogger, type: .info, result, properties.joined(separator: ", "))
}
#endif
}
}

// MARK: - changedBodyProperties

func changedBodyProperties<Body>(of type: Body.Type) -> [String] {
var index = 0
repeat {
let options = [
OGGraph.descriptionFormat.takeUnretainedValue(): "stack/frame",
"frame_index": index
"frame_index": index,
] as NSDictionary
guard let description = OGGraph.description(nil, options: options),
let dict = description.takeUnretainedValue() as? [String: Any],
Expand Down Expand Up @@ -69,23 +134,23 @@ func changedBodyProperties<Body>(of type: Body.Type) -> [String] {
let fields = DynamicPropertyCache.fields(of: Body.self)
buffer.applyChanged { offset in
switch fields.layout {
case .product(let fields):
case let .product(fields):
guard let field = fields.first(where: { $0.offset == offset }),
let name = field.name,
let property = String(cString: name, encoding: .utf8) else {
let name = field.name,
let property = String(cString: name, encoding: .utf8)
else {
properties.append("@\(offset)")
return
}
properties.append(property)
break
case .sum(_, _):
case .sum:
properties.append("@\(offset)")
break
}
}
}
return properties
} while (index != 32)
} while index != 32
return []
}

#endif

0 comments on commit 5e95b2d

Please sign in to comment.