|
| 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