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

refactor: add debug logging #186

Merged
merged 2 commits into from
Dec 11, 2023
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
7 changes: 6 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@ let package = Package(
.package(url: "https://github.com/pointfreeco/swift-concurrency-extras", from: "1.0.0"),
],
targets: [
.target(name: "_Helpers"),
.target(
name: "_Helpers",
dependencies: [
.product(name: "ConcurrencyExtras", package: "swift-concurrency-extras"),
]
),
.target(name: "Functions", dependencies: ["_Helpers"]),
.testTarget(
name: "FunctionsTests",
Expand Down
7 changes: 6 additions & 1 deletion Sources/PostgREST/PostgrestBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,12 @@ public class PostgrestBuilder: @unchecked Sendable {
}

return try await execute { [configuration] data in
try configuration.decoder.decode(T.self, from: data)
do {
return try configuration.decoder.decode(T.self, from: data)
} catch {
debug("Fail to decode type '\(T.self) with error: \(error)")
throw error
}
}
}

Expand Down
6 changes: 5 additions & 1 deletion Sources/Supabase/Types.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ public struct SupabaseClientOptions: Sendable {
public let encoder: JSONEncoder
public let decoder: JSONDecoder

public init(schema: String = "public", encoder: JSONEncoder = .postgrest, decoder: JSONDecoder = .postgrest) {
public init(
schema: String = "public",
encoder: JSONEncoder = .postgrest,
decoder: JSONDecoder = .postgrest
) {
self.schema = schema
self.encoder = encoder
self.decoder = decoder
Expand Down
36 changes: 36 additions & 0 deletions Sources/_Helpers/Logger.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//
// Logger.swift
//
//
// Created by Guilherme Souza on 11/12/23.
//

import ConcurrencyExtras
import Foundation

private let _debugLoggingEnabled = LockIsolated(false)
@_spi(Internal) public var debugLoggingEnabled: Bool {
get { _debugLoggingEnabled.value }
set { _debugLoggingEnabled.setValue(newValue) }
}

private let standardError = LockIsolated(FileHandle.standardError)
@_spi(Internal) public func debug(
_ message: @autoclosure () -> String,
function: String = #function,
file: String = #file,
line: UInt = #line
) {
assert(
{
if debugLoggingEnabled {
standardError.withValue {
let logLine = "[\(function) \(file.split(separator: "/").last!):\(line)] \(message())\n"
$0.write(Data(logLine.utf8))
}
}

return true
}()
)
}