VertexLog.swift 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Copyright 2024 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 os.log
  16. @_implementationOnly import FirebaseCoreExtension
  17. enum VertexLog {
  18. /// Log message codes for the Vertex AI SDK
  19. ///
  20. /// These codes should ideally not be re-used in order to facilitate matching error codes in
  21. /// support requests to lines in the SDK. These codes should range between 0 and 999999 to avoid
  22. /// being truncated in log messages.
  23. enum MessageCode: Int {
  24. // Logging Configuration
  25. case verboseLoggingDisabled = 100
  26. case verboseLoggingEnabled = 101
  27. // API Enablement Errors
  28. case vertexAIInFirebaseAPIDisabled = 200
  29. // Generative Model Configuration
  30. case generativeModelInitialized = 1000
  31. // Imagen Model Configuration
  32. case imagenInvalidJPEGCompressionQuality = 1201
  33. // Network Errors
  34. case generativeAIServiceNonHTTPResponse = 2000
  35. case loadRequestResponseError = 2001
  36. case loadRequestResponseErrorPayload = 2002
  37. case loadRequestStreamResponseError = 2003
  38. case loadRequestStreamResponseErrorPayload = 2004
  39. // Parsing Errors
  40. case loadRequestParseResponseFailedJSON = 3000
  41. case loadRequestParseResponseFailedJSONError = 3001
  42. case generateContentResponseUnrecognizedFinishReason = 3002
  43. case generateContentResponseUnrecognizedBlockReason = 3003
  44. case generateContentResponseUnrecognizedBlockThreshold = 3004
  45. case generateContentResponseUnrecognizedHarmProbability = 3005
  46. case generateContentResponseUnrecognizedHarmCategory = 3006
  47. case generateContentResponseUnrecognizedHarmSeverity = 3007
  48. case decodedInvalidProtoDateYear = 3008
  49. case decodedInvalidProtoDateMonth = 3009
  50. case decodedInvalidProtoDateDay = 3010
  51. case decodedInvalidCitationPublicationDate = 3011
  52. case generateContentResponseUnrecognizedContentModality = 3012
  53. case decodedUnsupportedImagenPredictionType = 3013
  54. // SDK State Errors
  55. case generateContentResponseNoCandidates = 4000
  56. case generateContentResponseNoText = 4001
  57. case appCheckTokenFetchFailed = 4002
  58. // SDK Debugging
  59. case loadRequestStreamResponseLine = 5000
  60. }
  61. /// Subsystem that should be used for all Loggers.
  62. static let subsystem = "com.google.firebase"
  63. /// Log identifier for the Vertex AI SDK.
  64. ///
  65. /// > Note: This corresponds to the `category` in `OSLog`.
  66. static let service = "[FirebaseVertexAI]"
  67. /// The raw `OSLog` log object.
  68. ///
  69. /// > Important: This is only needed for direct `os_log` usage.
  70. static let logObject = OSLog(subsystem: subsystem, category: service)
  71. /// The argument required to enable additional logging.
  72. static let enableArgumentKey = "-FIRDebugEnabled"
  73. static func log(level: FirebaseLoggerLevel, code: MessageCode, _ message: String) {
  74. let messageCode = String(format: "I-VTX%06d", code.rawValue)
  75. FirebaseLogger.log(
  76. level: level,
  77. service: VertexLog.service,
  78. code: messageCode,
  79. message: message
  80. )
  81. }
  82. static func error(code: MessageCode, _ message: String) {
  83. log(level: .error, code: code, message)
  84. }
  85. static func warning(code: MessageCode, _ message: String) {
  86. log(level: .warning, code: code, message)
  87. }
  88. static func notice(code: MessageCode, _ message: String) {
  89. log(level: .notice, code: code, message)
  90. }
  91. static func info(code: MessageCode, _ message: String) {
  92. log(level: .info, code: code, message)
  93. }
  94. static func debug(code: MessageCode, _ message: String) {
  95. log(level: .debug, code: code, message)
  96. }
  97. /// Returns `true` if additional logging has been enabled via a launch argument.
  98. static func additionalLoggingEnabled() -> Bool {
  99. return ProcessInfo.processInfo.arguments.contains(enableArgumentKey)
  100. }
  101. }