-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6e99619
commit dcb3b4a
Showing
4 changed files
with
15 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,86 +1,4 @@ | ||
|
||
|
||
import Foundation | ||
|
||
public extension Notification.Name { | ||
static let foundationExtensionsRuntimeWarning: Self = .init( | ||
rawValue: "FoundationExtensions.runtimeWarning" | ||
) | ||
} | ||
|
||
@_transparent | ||
@inlinable | ||
@inline(__always) | ||
func runtimeWarn( | ||
_ message: @autoclosure () -> String, | ||
category: String? = "FoundationExtensions", | ||
notificationName: Notification.Name? = .foundationExtensionsRuntimeWarning | ||
) { | ||
#if DEBUG | ||
let message = message() | ||
notificationName.map { notificationName in | ||
NotificationCenter.default.post( | ||
name: notificationName, | ||
object: nil, | ||
userInfo: ["message": message] | ||
) | ||
} | ||
|
||
let category = category ?? "Runtime Warning" | ||
|
||
if _XCTIsTesting { | ||
XCTFail(message) | ||
} else { | ||
#if canImport(os) | ||
os_log( | ||
.fault, | ||
dso: dso, | ||
log: OSLog(subsystem: "com.apple.runtime-issues", category: category), | ||
"%@", | ||
message | ||
) | ||
#else | ||
fputs("\(formatter.string(from: Date())) [\(category)] \(message)\n", stderr) | ||
#endif | ||
} | ||
#endif | ||
} | ||
|
||
#if DEBUG | ||
|
||
import XCTestDynamicOverlay | ||
|
||
#if canImport(os) | ||
import os | ||
|
||
// NB: Xcode runtime warnings offer a much better experience than traditional assertions and | ||
// breakpoints, but Apple provides no means of creating custom runtime warnings ourselves. | ||
// To work around this, we hook into SwiftUI's runtime issue delivery mechanism, instead. | ||
// | ||
// Feedback filed: https://gist.github.com/stephencelis/a8d06383ed6ccde3e5ef5d1b3ad52bbc | ||
@usableFromInline | ||
let dso = { () -> UnsafeMutableRawPointer in | ||
let count = _dyld_image_count() | ||
for i in 0..<count { | ||
if let name = _dyld_get_image_name(i) { | ||
let swiftString = String(cString: name) | ||
if swiftString.hasSuffix("/SwiftUI") { | ||
if let header = _dyld_get_image_header(i) { | ||
return UnsafeMutableRawPointer(mutating: UnsafeRawPointer(header)) | ||
} | ||
} | ||
} | ||
} | ||
return UnsafeMutableRawPointer(mutating: #dsohandle) | ||
}() | ||
#else | ||
import Foundation | ||
|
||
@usableFromInline | ||
let formatter: DateFormatter = { | ||
let formatter = DateFormatter() | ||
formatter.dateFormat = "yyyy-MM-dd HH:MM:SS.sssZ" | ||
return formatter | ||
}() | ||
#endif | ||
#endif | ||
import IssueReporting |