Logging.swift 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // Copyright 2023 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. import Foundation
  15. import OSLog
  16. @available(iOS 15.0, macOS 11.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  17. struct Logging {
  18. /// Subsystem that should be used for all Loggers.
  19. static let subsystem = "com.google.firebase.vertex-ai"
  20. /// Default category used for most loggers, unless specialized.
  21. static let defaultCategory = ""
  22. /// The argument required to enable additional logging.
  23. static let enableArgumentKey = "-FIRDebugEnabled"
  24. /// The argument required to enable additional logging in the Google AI SDK; used for migration.
  25. ///
  26. /// To facillitate migration between the SDKs, this launch argument is also accepted to enable
  27. /// additional logging at this time, though it is expected to be removed in the future.
  28. static let migrationEnableArgumentKey = "-GoogleGenerativeAIDebugLogEnabled"
  29. // No initializer available.
  30. @available(*, unavailable)
  31. private init() {}
  32. /// The default logger that is visible for all users. Note: we shouldn't be using anything lower
  33. /// than `.notice`.
  34. static var `default` = Logger(subsystem: subsystem, category: defaultCategory)
  35. /// A non default
  36. static var network: Logger = {
  37. if additionalLoggingEnabled() {
  38. return Logger(subsystem: subsystem, category: "NetworkResponse")
  39. } else {
  40. // Return a valid logger that's using `OSLog.disabled` as the logger, hiding everything.
  41. return Logger(.disabled)
  42. }
  43. }()
  44. ///
  45. static var verbose: Logger = {
  46. if additionalLoggingEnabled() {
  47. return Logger(subsystem: subsystem, category: defaultCategory)
  48. } else {
  49. // Return a valid logger that's using `OSLog.disabled` as the logger, hiding everything.
  50. return Logger(.disabled)
  51. }
  52. }()
  53. /// Returns `true` if additional logging has been enabled via a launch argument.
  54. static func additionalLoggingEnabled() -> Bool {
  55. let arguments = ProcessInfo.processInfo.arguments
  56. if arguments.contains(enableArgumentKey) || arguments.contains(migrationEnableArgumentKey) {
  57. return true
  58. }
  59. return false
  60. }
  61. }