Skip to content

Commit

Permalink
Merge pull request #11 from fummicc1/add-runtime-warning
Browse files Browse the repository at this point in the history
Fix method to get dso
  • Loading branch information
fummicc1 authored Dec 18, 2024
2 parents bdd4ee8 + 6a5a0e1 commit 25d4dcd
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 52 deletions.
46 changes: 17 additions & 29 deletions Sources/GeoHashFramework/GeoHash.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

import Foundation

internal let runtimeWarning = RuntimeWarning()

public struct GeoHash: Sendable, Hashable {
public private(set) var precision: GeoHashBitsPrecision
public private(set) var coordinate: GeoHashCoordinate2D
Expand All @@ -28,22 +26,18 @@ public struct GeoHash: Sendable, Hashable {
["0", "1"].contains($0)
})
if !isValidBinary {
Task {
await runtimeWarning.log(
message: "Binary string must contain only '0' or '1'."
)
}
RuntimeWarning.log(
message: "Binary string must contain only '0' or '1'."
)
return nil
}
if binary.count != precision.rawValue {
let isSameLength = binary.count == precision.rawValue
if !isSameLength {
Task {
await runtimeWarning.log(
message: "Binary length must be %d, but binary length is %d",
args: precision.rawValue, binary.count
)
}
RuntimeWarning.log(
message: "Binary length must be %d, but binary length is %d",
args: precision.rawValue, binary.count
)
}
}
self.binary = binary
Expand All @@ -60,19 +54,15 @@ public struct GeoHash: Sendable, Hashable {
precision: GeoHashBitsPrecision = .mid
) {
if latitude >= 90 || latitude <= -90 {
Task {
await runtimeWarning.log(
message: "Latitude must be between -90 and 90"
)
}
RuntimeWarning.log(
message: "Latitude must be between -90 and 90"
)
return nil
}
if longitude >= 180 || longitude <= -180 {
Task {
await runtimeWarning.log(
message: "Longitude must be between -180 and 180"
)
}
RuntimeWarning.log(
message: "Longitude must be between -180 and 180"
)
return nil
}

Expand Down Expand Up @@ -109,12 +99,10 @@ extension GeoHash {

for char in geoHash {
guard let index = Self.base32Chars.firstIndex(of: char) else {
Task {
await runtimeWarning.log(
message: "Invalid geohash character %s in geoHash: %s",
args: String(char), geoHash
)
}
RuntimeWarning.log(
message: "Invalid geohash character %s in geoHash: %s",
args: String(char), geoHash
)
continue
}

Expand Down
44 changes: 21 additions & 23 deletions Sources/GeoHashFramework/RuntimeWarning.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,29 @@
import Foundation
import OSLog

actor RuntimeWarning {
var info = Dl_info()

init() {
#if DEBUG
dladdr(
dlsym(
dlopen(
nil,
RTLD_LAZY
),
"""
$sxSg7SwiftUI8CommandsA2bCRzlAbCP4body4BodyQzvgTW
"""
),
&info
)
#endif
}

func log(message: StaticString, args: any CVarArg...) {
enum RuntimeWarning {
static func log(message: StaticString, args: any CVarArg...) {
#if DEBUG
var dso: UnsafeRawPointer?
// ref: https://github.com/pointfreeco/swift-issue-reporting/blob/a3f634d1a409c7979cabc0a71b3f26ffa9fc8af1/Sources/IssueReporting/IssueReporters/RuntimeWarningReporter.swift#L39-L55
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) {
dso = UnsafeRawPointer(header)
}
}
}
}
guard let dso = dso else {
assertionFailure("Failed to get DSO")
return
}
os_log(
.fault,
dso: info.dli_fbase,
dso: dso,
log: OSLog(
subsystem: "com.apple.runtime-issues",
category: "ReportRuntimeWarningSample"
Expand All @@ -42,4 +40,4 @@ actor RuntimeWarning {
)
#endif
}
}
}

0 comments on commit 25d4dcd

Please sign in to comment.