@@ -16,6 +16,7 @@ import FirebaseAppCheckInterop
16
16
import FirebaseAuthInterop
17
17
import FirebaseCore
18
18
import Foundation
19
+ import os. log
19
20
20
21
@available ( iOS 15 . 0 , macOS 11 . 0 , macCatalyst 15 . 0 , tvOS 15 . 0 , watchOS 8 . 0 , * )
21
22
struct GenerativeAIService {
@@ -60,9 +61,15 @@ struct GenerativeAIService {
60
61
61
62
// Verify the status code is 200
62
63
guard response. statusCode == 200 else {
63
- Logging . network. error ( " [FirebaseVertexAI] The server responded with an error: \( response) " )
64
+ VertexLog . error (
65
+ code: . loadRequestResponseError,
66
+ " The server responded with an error: \( response) "
67
+ )
64
68
if let responseString = String ( data: data, encoding: . utf8) {
65
- Logging . default. error ( " [FirebaseVertexAI] Response payload: \( responseString) " )
69
+ VertexLog . error (
70
+ code: . loadRequestResponseErrorPayload,
71
+ " Response payload: \( responseString) "
72
+ )
66
73
}
67
74
68
75
throw parseError ( responseData: data)
@@ -108,14 +115,19 @@ struct GenerativeAIService {
108
115
109
116
// Verify the status code is 200
110
117
guard response. statusCode == 200 else {
111
- Logging . network
112
- . error ( " [FirebaseVertexAI] The server responded with an error: \( response) " )
118
+ VertexLog . error (
119
+ code: . loadRequestStreamResponseError,
120
+ " The server responded with an error: \( response) "
121
+ )
113
122
var responseBody = " "
114
123
for try await line in stream. lines {
115
124
responseBody += line + " \n "
116
125
}
117
126
118
- Logging . default. error ( " [FirebaseVertexAI] Response payload: \( responseBody) " )
127
+ VertexLog . error (
128
+ code: . loadRequestStreamResponseErrorPayload,
129
+ " Response payload: \( responseBody) "
130
+ )
119
131
continuation. finish ( throwing: parseError ( responseBody: responseBody) )
120
132
121
133
return
@@ -127,7 +139,7 @@ struct GenerativeAIService {
127
139
let decoder = JSONDecoder ( )
128
140
decoder. keyDecodingStrategy = . convertFromSnakeCase
129
141
for try await line in stream. lines {
130
- Logging . network . debug ( " [FirebaseVertexAI] Stream response: \( line) " )
142
+ VertexLog . debug ( code : . loadRequestStreamResponseLine , " Stream response: \( line) " )
131
143
132
144
if line. hasPrefix ( " data: " ) {
133
145
// We can assume 5 characters since it's utf-8 encoded, removing `data:`.
@@ -179,8 +191,10 @@ struct GenerativeAIService {
179
191
let tokenResult = await appCheck. getToken ( forcingRefresh: false )
180
192
urlRequest. setValue ( tokenResult. token, forHTTPHeaderField: " X-Firebase-AppCheck " )
181
193
if let error = tokenResult. error {
182
- Logging . default
183
- . debug ( " [FirebaseVertexAI] Failed to fetch AppCheck token. Error: \( error) " )
194
+ VertexLog . error (
195
+ code: . appCheckTokenFetchFailed,
196
+ " Failed to fetch AppCheck token. Error: \( error) "
197
+ )
184
198
}
185
199
}
186
200
@@ -202,10 +216,10 @@ struct GenerativeAIService {
202
216
private func httpResponse( urlResponse: URLResponse) throws -> HTTPURLResponse {
203
217
// Verify the status code is 200
204
218
guard let response = urlResponse as? HTTPURLResponse else {
205
- Logging . default
206
- . error (
207
- " [FirebaseVertexAI] Response wasn't an HTTP response, internal error \( urlResponse) "
208
- )
219
+ VertexLog . error (
220
+ code : . generativeAIServiceNonHTTPResponse ,
221
+ " Response wasn't an HTTP response, internal error \( urlResponse) "
222
+ )
209
223
throw NSError (
210
224
domain: " com.google.generative-ai " ,
211
225
code: - 1 ,
@@ -253,7 +267,7 @@ struct GenerativeAIService {
253
267
// These errors do not produce specific GenerateContentError or CountTokensError cases.
254
268
private func logRPCError( _ error: RPCError) {
255
269
if error. isFirebaseMLServiceDisabledError ( ) {
256
- Logging . default . error ( """
270
+ VertexLog . error ( code : . firebaseMLAPIDisabled , """
257
271
The Vertex AI for Firebase SDK requires the Firebase ML API `firebaseml.googleapis.com` to \
258
272
be enabled for your project. Get started in the Firebase Console \
259
273
(https://console.firebase.google.com/project/ \( projectID) /genai/vertex) or verify that the \
@@ -269,9 +283,12 @@ struct GenerativeAIService {
269
283
return try JSONDecoder ( ) . decode ( type, from: data)
270
284
} catch {
271
285
if let json = String ( data: data, encoding: . utf8) {
272
- Logging . network . error ( " [FirebaseVertexAI] JSON response: \( json) " )
286
+ VertexLog . error ( code : . loadRequestParseResponseFailedJSON , " JSON response: \( json) " )
273
287
}
274
- Logging . default. error ( " [FirebaseVertexAI] Error decoding server JSON: \( error) " )
288
+ VertexLog . error (
289
+ code: . loadRequestParseResponseFailedJSONError,
290
+ " Error decoding server JSON: \( error) "
291
+ )
275
292
throw error
276
293
}
277
294
}
@@ -297,9 +314,12 @@ struct GenerativeAIService {
297
314
}
298
315
299
316
private func printCURLCommand( from request: URLRequest ) {
317
+ guard VertexLog . additionalLoggingEnabled ( ) else {
318
+ return
319
+ }
300
320
let command = cURLCommand ( from: request)
301
- Logging . verbose . debug ( """
302
- [FirebaseVertexAI] Creating request with the equivalent cURL command:
321
+ os_log ( . debug, log : VertexLog . logObject , """
322
+ \( VertexLog . service ) Creating request with the equivalent cURL command:
303
323
----- cURL command -----
304
324
\( command, privacy: . private)
305
325
------------------------
0 commit comments