EventGDTLogger.swift 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. //
  2. // Copyright 2022 Google LLC
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. import Foundation
  16. internal import GoogleDataTransport
  17. protocol EventGDTLoggerProtocol: Sendable {
  18. func logEvent(event: SessionStartEvent, completion: @escaping (Result<Void, Error>) -> Void)
  19. }
  20. ///
  21. /// EventGDTLogger is responsible for
  22. /// 1) Creating GDT Events and logging them to the GoogleDataTransport SDK
  23. /// 2) Handling debugging situations (eg. running in Simulator or printing the event to console)
  24. ///
  25. final class EventGDTLogger: EventGDTLoggerProtocol {
  26. let googleDataTransport: GoogleDataTransportProtocol
  27. let devEventConsoleLogger: EventGDTLoggerProtocol
  28. init(googleDataTransport: GoogleDataTransportProtocol,
  29. devEventConsoleLogger: EventGDTLoggerProtocol = DevEventConsoleLogger()) {
  30. self.googleDataTransport = googleDataTransport
  31. self.devEventConsoleLogger = devEventConsoleLogger
  32. }
  33. /// Logs the event to FireLog, taking into account debugging cases such as running
  34. /// in simulator.
  35. func logEvent(event: SessionStartEvent, completion: @escaping (Result<Void, Error>) -> Void) {
  36. let gdtEvent = googleDataTransport.eventForTransport()
  37. gdtEvent.dataObject = event
  38. gdtEvent.qosTier = GDTCOREventQoS.qosDefault
  39. #if targetEnvironment(simulator)
  40. Logger.logDebug("Logging events using fast QOS due to running on a simulator")
  41. gdtEvent.qosTier = GDTCOREventQoS.qoSFast
  42. #endif // targetEnvironment(simulator)
  43. devEventConsoleLogger.logEvent(event: event) { _ in }
  44. googleDataTransport.logGDTEvent(event: gdtEvent, completion: completion)
  45. }
  46. }