Cleanup print + use OSLog

This commit is contained in:
Thomas Ricouard 2024-02-02 18:26:24 +01:00
parent 9bd967cddf
commit f67163e4b0
12 changed files with 40 additions and 66 deletions

View file

@ -24,16 +24,13 @@ extension NotificationService {
var _plaintext: Data?
do {
_plaintext = try AES.GCM.open(sealedBox, using: key)
} catch {
print(error)
}
} catch { }
guard let plaintext = _plaintext else {
return nil
}
let paddingLength = Int(plaintext[0]) * 256 + Int(plaintext[1])
guard plaintext.count >= 2 + paddingLength else {
print("1")
fatalError()
}
let unpadded = plaintext.suffix(from: paddingLength + 2)

View file

@ -37,9 +37,7 @@ public struct AccountDetailContextMenu: View {
Task {
do {
viewModel.relationship = try await client.post(endpoint: Accounts.unblock(id: account.id))
} catch {
print("Error while unblocking: \(error.localizedDescription)")
}
} catch { }
}
} label: {
Label("account.action.unblock", systemImage: "person.crop.circle.badge.exclamationmark")
@ -57,9 +55,7 @@ public struct AccountDetailContextMenu: View {
Task {
do {
viewModel.relationship = try await client.post(endpoint: Accounts.unmute(id: account.id))
} catch {
print("Error while unmuting: \(error.localizedDescription)")
}
} catch { }
}
} label: {
Label("account.action.unmute", systemImage: "speaker")
@ -71,9 +67,7 @@ public struct AccountDetailContextMenu: View {
Task {
do {
viewModel.relationship = try await client.post(endpoint: Accounts.mute(id: account.id, json: MuteData(duration: duration.rawValue)))
} catch {
print("Error while muting: \(error.localizedDescription)")
}
} catch { }
}
}
}
@ -92,9 +86,7 @@ public struct AccountDetailContextMenu: View {
viewModel.relationship = try await client.post(endpoint: Accounts.follow(id: account.id,
notify: false,
reblogs: relationship.showingReblogs))
} catch {
print("Error while disabling notifications: \(error.localizedDescription)")
}
} catch { }
}
} label: {
Label("account.action.notify-disable", systemImage: "bell.fill")
@ -106,9 +98,7 @@ public struct AccountDetailContextMenu: View {
viewModel.relationship = try await client.post(endpoint: Accounts.follow(id: account.id,
notify: true,
reblogs: relationship.showingReblogs))
} catch {
print("Error while enabling notifications: \(error.localizedDescription)")
}
} catch { }
}
} label: {
Label("account.action.notify-enable", systemImage: "bell")
@ -121,9 +111,7 @@ public struct AccountDetailContextMenu: View {
viewModel.relationship = try await client.post(endpoint: Accounts.follow(id: account.id,
notify: relationship.notifying,
reblogs: false))
} catch {
print("Error while disabling reboosts: \(error.localizedDescription)")
}
} catch { }
}
} label: {
Label("account.action.reboosts-hide", image: "Rocket.Fill")
@ -135,9 +123,7 @@ public struct AccountDetailContextMenu: View {
viewModel.relationship = try await client.post(endpoint: Accounts.follow(id: account.id,
notify: relationship.notifying,
reblogs: true))
} catch {
print("Error while enabling reboosts: \(error.localizedDescription)")
}
} catch { }
}
} label: {
Label("account.action.reboosts-show", image: "Rocket")

View file

@ -366,9 +366,7 @@ public struct AccountDetailView: View {
Task {
do {
viewModel.relationship = try await client.post(endpoint: Accounts.block(id: account.id))
} catch {
print("Error while blocking: \(error.localizedDescription)")
}
} catch { }
}
}
}

View file

@ -2,6 +2,7 @@ import Models
import Network
import Observation
import SwiftUI
import OSLog
public enum AccountsListMode {
case following(accountId: String), followers(accountId: String)
@ -126,7 +127,8 @@ public enum AccountsListMode {
relationships: relationships,
nextPageState: link?.maxId != nil ? .hasNextPage : .none)
} catch {
print(error)
let logger = Logger(subsystem: "com.icecubesapp", category: "UI")
logger.log(level: .info, "\(error.localizedDescription)")
}
}

View file

@ -5,6 +5,7 @@ import Models
import Network
import Observation
import SwiftUI
import OSLog
@MainActor
@Observable public class FollowButtonViewModel {
@ -32,7 +33,6 @@ import SwiftUI
relationship = try await client.post(endpoint: Accounts.follow(id: accountId, notify: false, reblogs: true))
relationshipUpdated(relationship)
} catch {
print("Error while following: \(error.localizedDescription)")
throw error
}
}
@ -43,7 +43,6 @@ import SwiftUI
relationship = try await client.post(endpoint: Accounts.unfollow(id: accountId))
relationshipUpdated(relationship)
} catch {
print("Error while unfollowing: \(error.localizedDescription)")
throw error
}
}
@ -56,7 +55,6 @@ import SwiftUI
reblogs: relationship.showingReblogs))
relationshipUpdated(relationship)
} catch {
print("Error while following: \(error.localizedDescription)")
throw error
}
}
@ -69,7 +67,6 @@ import SwiftUI
reblogs: !relationship.showingReblogs))
relationshipUpdated(relationship)
} catch {
print("Error while switching reboosts: \(error.localizedDescription)")
throw error
}
}

View file

@ -3,6 +3,7 @@ import Foundation
import Models
import Network
import Observation
import OSLog
@MainActor
@Observable public class StreamWatcher {
@ -26,6 +27,8 @@ import Observation
public var events: [any StreamEvent] = []
public var unreadNotificationsCount: Int = 0
public var latestEvent: (any StreamEvent)?
private let logger = Logger(subsystem: "com.icecubesapp", category: "stream")
public static let shared = StreamWatcher()
@ -89,10 +92,11 @@ import Observation
case let .string(string):
do {
guard let data = string.data(using: .utf8) else {
print("Error decoding streaming event string")
logger.error("Error decoding streaming event string")
return
}
let rawEvent = try decoder.decode(RawStreamEvent.self, from: data)
logger.info("Stream update: \(rawEvent.event)")
if let event = rawEventToEvent(rawEvent: rawEvent) {
Task { @MainActor in
self.events.append(event)
@ -103,7 +107,7 @@ import Observation
}
}
} catch {
print("Error decoding streaming event: \(error.localizedDescription)")
logger.error("Error decoding streaming event: \(error.localizedDescription)")
}
default:
@ -147,8 +151,8 @@ import Observation
return nil
}
} catch {
print("Error decoding streaming event to final event: \(error.localizedDescription)")
print("Raw data: \(rawEvent.payload)")
logger.error("Error decoding streaming event to final event: \(error.localizedDescription)")
logger.error("Raw data: \(rawEvent.payload)")
return nil
}
}

View file

@ -4,8 +4,9 @@ import Models
import Observation
import os
import SwiftUI
import OSLog
@Observable public final class Client: Equatable, Identifiable, Hashable {
@Observable public final class Client: Equatable, Identifiable, Hashable, @unchecked Sendable {
public static func == (lhs: Client, rhs: Client) -> Bool {
let lhsToken = lhs.critical.withLock { $0.oauthToken }
let rhsToken = rhs.critical.withLock { $0.oauthToken }
@ -43,6 +44,8 @@ import SwiftUI
public let version: Version
private let urlSession: URLSession
private let decoder = JSONDecoder()
private let logger = Logger(subsystem: "com.icecubesapp", category: "networking")
// Putting all mutable state inside an `OSAllocatedUnfairLock` makes `Client`
// provably `Sendable`. The lock is a struct, but it uses a `ManagedBuffer`
@ -125,7 +128,7 @@ import SwiftUI
request.httpBody = jsonData
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
} catch {
print("Client Error encoding JSON: \(error.localizedDescription)")
logger.error("Error encoding JSON: \(error.localizedDescription)")
}
}
return request
@ -141,7 +144,8 @@ import SwiftUI
}
public func getWithLink<Entity: Decodable>(endpoint: Endpoint) async throws -> (Entity, LinkHandler?) {
let (data, httpResponse) = try await urlSession.data(for: makeGet(endpoint: endpoint))
let request = try makeGet(endpoint: endpoint)
let (data, httpResponse) = try await urlSession.data(for: request)
var linkHandler: LinkHandler?
if let response = httpResponse as? HTTPURLResponse,
let link = response.allHeaderFields["Link"] as? String
@ -149,6 +153,7 @@ import SwiftUI
linkHandler = .init(rawLink: link)
}
logResponseOnError(httpResponse: httpResponse, data: data)
logger.log(level: .info, "\(request)")
return try (decoder.decode(Entity.self, from: data), linkHandler)
}
@ -188,6 +193,7 @@ import SwiftUI
let url = try makeURL(endpoint: endpoint, forceVersion: forceVersion)
let request = makeURLRequest(url: url, endpoint: endpoint, httpMethod: method)
let (data, httpResponse) = try await urlSession.data(for: request)
logger.log(level: .info, "\(request)")
logResponseOnError(httpResponse: httpResponse, data: data)
do {
return try decoder.decode(Entity.self, from: data)
@ -298,10 +304,8 @@ import SwiftUI
private func logResponseOnError(httpResponse: URLResponse, data: Data) {
if let httpResponse = httpResponse as? HTTPURLResponse, httpResponse.statusCode > 299 {
print(httpResponse)
print(String(data: data, encoding: .utf8) ?? "")
let error = "HTTP Response error: \(httpResponse.statusCode), response: \(httpResponse), data: \(String(data: data, encoding: .utf8) ?? "")"
logger.error("\(error)")
}
}
}
extension Client: Sendable {}

View file

@ -835,9 +835,7 @@ extension StatusEditor {
error: nil
)
}
} catch {
print(error.localizedDescription)
}
} catch { }
}
try? await Task.sleep(for: .seconds(5))
} while !Task.isCancelled
@ -858,7 +856,7 @@ extension StatusEditor {
mediaAttachment: media,
error: nil
)
} catch { print(error) }
} catch { }
}
}

View file

@ -56,9 +56,7 @@ public struct StatusEditHistoryView: View {
.task {
do {
history = try await client.get(endpoint: Statuses.history(id: statusId))
} catch {
print(error)
}
} catch { }
}
.listStyle(.plain)
.scrollContentBackground(.hidden)

View file

@ -35,9 +35,7 @@ import SwiftUI
votes = poll.ownVotes ?? []
showResults = true
}
} catch {
print(error)
}
} catch { }
}
public func handleSelection(_ pollIndex: Int) {

View file

@ -201,9 +201,7 @@ public struct StatusRowView: View {
do {
let operationAccount = viewModel.status.reblog?.account ?? viewModel.status.account
viewModel.authorRelationship = try await client.post(endpoint: Accounts.block(id: operationAccount.id))
} catch {
print("Error while blocking: \(error.localizedDescription)")
}
} catch { }
}
}
}

View file

@ -204,9 +204,7 @@ struct StatusRowContextMenu: View {
do {
let operationAccount = viewModel.status.reblog?.account ?? viewModel.status.account
viewModel.authorRelationship = try await client.post(endpoint: Accounts.unmute(id: operationAccount.id))
} catch {
print("Error while unmuting: \(error.localizedDescription)")
}
} catch { }
}
} label: {
Label("account.action.unmute", systemImage: "speaker")
@ -219,9 +217,7 @@ struct StatusRowContextMenu: View {
do {
let operationAccount = viewModel.status.reblog?.account ?? viewModel.status.account
viewModel.authorRelationship = try await client.post(endpoint: Accounts.mute(id: operationAccount.id, json: MuteData(duration: duration.rawValue)))
} catch {
print("Error while muting: \(error.localizedDescription)")
}
} catch { }
}
}
}
@ -275,9 +271,7 @@ struct StatusRowContextMenu: View {
do {
let operationAccount = viewModel.status.reblog?.account ?? viewModel.status.account
viewModel.authorRelationship = try await client.post(endpoint: Accounts.unblock(id: operationAccount.id))
} catch {
print("Error while unblocking: \(error.localizedDescription)")
}
} catch { }
}
} label: {
Label("account.action.unblock", systemImage: "person.crop.circle.badge.exclamationmark")