Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add _logChanges implementation #86

Merged
merged 1 commit into from
Apr 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading