Skip to content

Commit e5f1e4b

Browse files
authored
refactor: add debug logging (#186)
1 parent 6376107 commit e5f1e4b

File tree

4 files changed

+53
-3
lines changed

4 files changed

+53
-3
lines changed

Package.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,12 @@ let package = Package(
3131
.package(url: "https://github.com/pointfreeco/swift-concurrency-extras", from: "1.0.0"),
3232
],
3333
targets: [
34-
.target(name: "_Helpers"),
34+
.target(
35+
name: "_Helpers",
36+
dependencies: [
37+
.product(name: "ConcurrencyExtras", package: "swift-concurrency-extras"),
38+
]
39+
),
3540
.target(name: "Functions", dependencies: ["_Helpers"]),
3641
.testTarget(
3742
name: "FunctionsTests",

Sources/PostgREST/PostgrestBuilder.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,12 @@ public class PostgrestBuilder: @unchecked Sendable {
7171
}
7272

7373
return try await execute { [configuration] data in
74-
try configuration.decoder.decode(T.self, from: data)
74+
do {
75+
return try configuration.decoder.decode(T.self, from: data)
76+
} catch {
77+
debug("Fail to decode type '\(T.self) with error: \(error)")
78+
throw error
79+
}
7580
}
7681
}
7782

Sources/Supabase/Types.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@ public struct SupabaseClientOptions: Sendable {
1313
public let encoder: JSONEncoder
1414
public let decoder: JSONDecoder
1515

16-
public init(schema: String = "public", encoder: JSONEncoder = .postgrest, decoder: JSONDecoder = .postgrest) {
16+
public init(
17+
schema: String = "public",
18+
encoder: JSONEncoder = .postgrest,
19+
decoder: JSONDecoder = .postgrest
20+
) {
1721
self.schema = schema
1822
self.encoder = encoder
1923
self.decoder = decoder

Sources/_Helpers/Logger.swift

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//
2+
// Logger.swift
3+
//
4+
//
5+
// Created by Guilherme Souza on 11/12/23.
6+
//
7+
8+
import ConcurrencyExtras
9+
import Foundation
10+
11+
private let _debugLoggingEnabled = LockIsolated(false)
12+
@_spi(Internal) public var debugLoggingEnabled: Bool {
13+
get { _debugLoggingEnabled.value }
14+
set { _debugLoggingEnabled.setValue(newValue) }
15+
}
16+
17+
private let standardError = LockIsolated(FileHandle.standardError)
18+
@_spi(Internal) public func debug(
19+
_ message: @autoclosure () -> String,
20+
function: String = #function,
21+
file: String = #file,
22+
line: UInt = #line
23+
) {
24+
assert(
25+
{
26+
if debugLoggingEnabled {
27+
standardError.withValue {
28+
let logLine = "[\(function) \(file.split(separator: "/").last!):\(line)] \(message())\n"
29+
$0.write(Data(logLine.utf8))
30+
}
31+
}
32+
33+
return true
34+
}()
35+
)
36+
}

0 commit comments

Comments
 (0)