Logging.swift 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 FirebaseSharedSwift
  15. import Foundation
  16. import OSLog
  17. extension FirebaseLogger {
  18. static let vertexAI = FirebaseLogger(category: "[FirebaseVertexAI]", categoryIdentifier: "VTX")
  19. }
  20. /// Enum of log messages.
  21. enum LogMessageID: Int {
  22. case loadRequestError = 1
  23. case loadRequestErrorResponse
  24. case loadRequestStreamError
  25. case loadRequestStreamErrorResponse
  26. case loadRequestStreamLine
  27. case appCheckTokenFetchError
  28. case nonHTTPResponseError
  29. case mlServiceDisabledError
  30. case jsonResponseError
  31. case jsonDecodingError
  32. case curlCommandForRequest
  33. }
  34. @available(iOS 15.0, macOS 11.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  35. struct Logging {
  36. /// Subsystem that should be used for all Loggers.
  37. static let subsystem = "com.google.firebase.vertex-ai"
  38. /// Default category used for most loggers, unless specialized.
  39. static let defaultCategory = ""
  40. /// The argument required to enable additional logging.
  41. static let enableArgumentKey = "-FIRDebugEnabled"
  42. /// The argument required to enable additional logging in the Google AI SDK; used for migration.
  43. ///
  44. /// To facilitate migration between the SDKs, this launch argument is also accepted to enable
  45. /// additional logging at this time, though it is expected to be removed in the future.
  46. static let migrationEnableArgumentKey = "-GoogleGenerativeAIDebugLogEnabled"
  47. // No initializer available.
  48. @available(*, unavailable)
  49. private init() {}
  50. /// The default logger that is visible for all users. Note: we shouldn't be using anything lower
  51. /// than `.notice`.
  52. static var `default` = Logger(subsystem: subsystem, category: defaultCategory)
  53. /// A non default
  54. static var network: Logger = {
  55. if additionalLoggingEnabled() {
  56. return Logger(subsystem: subsystem, category: "NetworkResponse")
  57. } else {
  58. // Return a valid logger that's using `OSLog.disabled` as the logger, hiding everything.
  59. return Logger(.disabled)
  60. }
  61. }()
  62. ///
  63. static var verbose: Logger = {
  64. if additionalLoggingEnabled() {
  65. return Logger(subsystem: subsystem, category: defaultCategory)
  66. } else {
  67. // Return a valid logger that's using `OSLog.disabled` as the logger, hiding everything.
  68. return Logger(.disabled)
  69. }
  70. }()
  71. /// Returns `true` if additional logging has been enabled via a launch argument.
  72. static func additionalLoggingEnabled() -> Bool {
  73. let arguments = ProcessInfo.processInfo.arguments
  74. if arguments.contains(enableArgumentKey) || arguments.contains(migrationEnableArgumentKey) {
  75. return true
  76. }
  77. return false
  78. }
  79. }