Logging.swift 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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, *)
  17. struct Logging {
  18. /// Subsystem that should be used for all Loggers.
  19. static let subsystem = "com.google.generative-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 = "-GoogleGenerativeAIDebugLogEnabled"
  24. // No initializer available.
  25. @available(*, unavailable)
  26. private init() {}
  27. /// The default logger that is visible for all users. Note: we shouldn't be using anything lower
  28. /// than `.notice`.
  29. static var `default` = Logger(subsystem: subsystem, category: defaultCategory)
  30. /// A non default
  31. static var network: Logger = {
  32. if ProcessInfo.processInfo.arguments.contains(enableArgumentKey) {
  33. return Logger(subsystem: subsystem, category: "NetworkResponse")
  34. } else {
  35. // Return a valid logger that's using `OSLog.disabled` as the logger, hiding everything.
  36. return Logger(.disabled)
  37. }
  38. }()
  39. ///
  40. static var verbose: Logger = {
  41. if ProcessInfo.processInfo.arguments.contains(enableArgumentKey) {
  42. return Logger(subsystem: subsystem, category: defaultCategory)
  43. } else {
  44. // Return a valid logger that's using `OSLog.disabled` as the logger, hiding everything.
  45. return Logger(.disabled)
  46. }
  47. }()
  48. }