EventGDTLogger.swift 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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,
  19. completion: @escaping @Sendable (Result<Void, Error>) -> Void)
  20. }
  21. ///
  22. /// EventGDTLogger is responsible for
  23. /// 1) Creating GDT Events and logging them to the GoogleDataTransport SDK
  24. /// 2) Handling debugging situations (eg. running in Simulator or printing the event to console)
  25. ///
  26. final class EventGDTLogger: EventGDTLoggerProtocol {
  27. let googleDataTransport: GoogleDataTransportProtocol
  28. let devEventConsoleLogger: EventGDTLoggerProtocol
  29. init(googleDataTransport: GoogleDataTransportProtocol,
  30. devEventConsoleLogger: EventGDTLoggerProtocol = DevEventConsoleLogger()) {
  31. self.googleDataTransport = googleDataTransport
  32. self.devEventConsoleLogger = devEventConsoleLogger
  33. }
  34. /// Logs the event to FireLog, taking into account debugging cases such as running
  35. /// in simulator.
  36. func logEvent(event: SessionStartEvent,
  37. completion: @escaping @Sendable (Result<Void, Error>) -> Void) {
  38. let gdtEvent = googleDataTransport.eventForTransport()
  39. gdtEvent.dataObject = event
  40. gdtEvent.qosTier = GDTCOREventQoS.qosDefault
  41. #if targetEnvironment(simulator)
  42. Logger.logDebug("Logging events using fast QOS due to running on a simulator")
  43. gdtEvent.qosTier = GDTCOREventQoS.qoSFast
  44. #endif // targetEnvironment(simulator)
  45. devEventConsoleLogger.logEvent(event: event) { _ in }
  46. googleDataTransport.logGDTEvent(event: gdtEvent, completion: completion)
  47. }
  48. }