Skip to content

Commit

Permalink
Merge pull request #27 from k-kohey/fix/bug
Browse files Browse the repository at this point in the history
fix
  • Loading branch information
k-kohey authored May 20, 2023
2 parents 9592f2d + 5a4a6c1 commit 474a743
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 38 deletions.
7 changes: 3 additions & 4 deletions Demo.swiftpm/Sources/AppModule/Demo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ struct MyLogger: LoggerComponent {
static let id: LoggerComponentID = .my

func send(_ log: [LoggerSendable]) async -> Bool {
print("🚀 Send \(log.count) events\n \(log.reduce("", { $0 + "\($1)\n" }))")
try? await Task.sleep(nanoseconds: 1000_000)
return true
}
Expand All @@ -31,13 +30,13 @@ extension TrackingEvent {
}

let logger = LoggerBundler.make(
components: [MyLogger()],
bufferFlowController: DefaultBufferFlowController(pollingInterval: 5)
components: [MyLogger(), DebugLogger()],
bufferFlowController: DefaultBufferFlowController(pollingInterval: 5, delayInputLimit: 5)
)

@main
struct ExampleAppApp: App {
@State @Tracked(name: "count", with: logger) var count: Int = 0
@State var count: Int = 0
@State @Tracked(name: "text", with: logger, scope: \.description) var text = ""

var body: some Scene {
Expand Down
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import PackageDescription

let package = Package(
name: "Parchment",
platforms: [.iOS(.v13), .macOS(.v11)],
platforms: [.iOS(.v14), .macOS(.v11)],
products: [
.library(
name: "Parchment",
Expand Down
48 changes: 28 additions & 20 deletions Sources/Parchment/LoggerBundler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,13 @@ public final actor LoggerBundler {
case .immediately:
await self.upload([payload], with: logger)
case .bufferingFirst:
try? await self.bufferFlowController.input(
[payload], with: self.buffer
)
do {
try await self.bufferFlowController.input(
[payload], with: self.buffer
)
} catch {
osLogger.error("The following error occurred when saving Log to Buffer\n\(error)")
}
}
}
}
Expand All @@ -74,33 +78,37 @@ public final actor LoggerBundler {

private func upload(_ payloads: [Payload], with logger: any LoggerComponent) async {
let isSucceeded = await logger.send(payloads)
let shouldBuffering = !isSucceeded && (configMap[logger.id]?.allowBuffering != .some(false))
let shouldBuffering = !isSucceeded
if shouldBuffering {
try? await self.bufferFlowController.input(
payloads, with: self.buffer
)
} else if !isSucceeded {
// console()?.log("""
// ⚠ The logger(id=\(logger.id.value)) failed to log an event.
// However, buffering is skiped because it is not allowed in the configuration.
// """)
do {
try await self.bufferFlowController.input(
payloads, with: self.buffer
)
} catch {
osLogger.error("The following error occurred when saving Log to Buffer\n\(error)")
}
}
}

@discardableResult
public func startLogging() -> Task<Void, Error> {
Task {
try await withThrowingTaskGroup(of: Void.self) { group in
for try await payloads in await bufferFlowController.output(with: buffer) {
group.addTask {
let payloadEachLogger = Dictionary(grouping: payloads) {
$0.destination
}
for (destination, payloads) in payloadEachLogger {
await self.upload(payloads, with: self.components[.init(destination)])
do {
try await withThrowingTaskGroup(of: Void.self) { group in
for try await payloads in await bufferFlowController.output(with: buffer) {
group.addTask {
let payloadEachLogger = Dictionary(grouping: payloads) {
$0.destination
}
for (destination, payloads) in payloadEachLogger {
await self.upload(payloads, with: self.components[.init(destination)])
}
}
}
}
} catch {
osLogger.error("The following error occurred when polling Log to Buffer\n\(error)")
throw error
}
}
}
Expand Down
30 changes: 30 additions & 0 deletions Sources/Parchment/osLogger.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//
// osLogger.swift
//
//
// Created by Kohei Kawaguchi on 2023/05/20.
//

import os
import Foundation

@_spi(Parchment) public let osLogger = Logger(subsystem: "com.k-kohey.parchment", category: "parchment")

public extension LoggerComponentID {
static var debug: LoggerComponentID {
.init("debug")
}
}

public struct DebugLogger: LoggerComponent {
public static let id: LoggerComponentID = .debug

public init() {}

public func send(_ log: [LoggerSendable]) async -> Bool {
osLogger.debug(
"🚀 Send \(log.count) events\n\(log.reduce("", { $0 + "\($1)\n" }))"
)
return true
}
}
28 changes: 16 additions & 12 deletions Sources/ParchmentDefault/DefaultBufferFlowController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ public final class DefaultBufferFlowController: BufferFlowController, Sendable {
let maxBufferSize: Int
let inputAccumulationLimit: Int
let delayInputLimit: TimeInterval
private var inputAccumulationPayloads: [Payload] = []

@MainActor private var inputAccumulationPayloads: [Payload] = []

private var bufferTask: Task<Void, Error>? = nil

Expand All @@ -32,24 +33,27 @@ public final class DefaultBufferFlowController: BufferFlowController, Sendable {
public func input<T: LogBuffer>(
_ events: [Payload], with buffer: T
) async throws {
@Sendable func save() async throws {
@Sendable @MainActor func save() async throws {
try await buffer.save(inputAccumulationPayloads)
inputAccumulationPayloads = []
}

bufferTask?.cancel()
inputAccumulationPayloads += events
Task { @MainActor in
inputAccumulationPayloads += events
if inputAccumulationLimit < inputAccumulationPayloads.count {
try await save()
}
else {
bufferTask = Task {
try await Task.sleep(nanoseconds: UInt64(delayInputLimit) * 1000_000_000)

if inputAccumulationLimit < inputAccumulationPayloads.count {
try await save()
} else {
bufferTask = Task {
if Task.isCancelled {
return
}
if Task.isCancelled {
return
}

try await Task.sleep(nanoseconds: UInt64(delayInputLimit) * 1000_000_000)
try await save()
try await save()
}
}
}
}
Expand Down
6 changes: 5 additions & 1 deletion Sources/ParchmentDefault/SQLiteBuffer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// Created by k-kohey on 2021/10/27.
//

import Parchment
@_spi(Parchment) import Parchment
import SQLite
import Foundation

Expand Down Expand Up @@ -52,6 +52,10 @@ public final actor SQLiteBuffer: LogBuffer {
}
)
)

osLogger.debug(
"🎁 Buffer \(e.count) events\n\(e.reduce("", { $0 + "\($1)\n" }))"
)
}

public func load(limit: Int?) throws -> [Payload] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ class RegularlyPollingSchedulerTests: XCTestCase {
), count: 3),
with: buffer
)

await Task.yield()
XCTAssertEqual(buffer.count(), 4)
}

Expand Down

0 comments on commit 474a743

Please sign in to comment.