DeviceLogger.swift 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // Copyright 2021 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. #if SWIFT_PACKAGE
  16. import GoogleUtilities_Logger
  17. #else
  18. import GoogleUtilities
  19. #endif
  20. /// Enum of debug messages.
  21. // TODO: Create list of all possible messages with code - according to format.
  22. enum LoggerMessageCode: Int {
  23. case modelDownloaded = 1
  24. case downloadedModelFileSaved
  25. case downloadedModelInfoSaved
  26. case downloaderInstanceCreated
  27. case downloaderInstanceRetrieved
  28. case downloaderInstanceDeleted
  29. case localModelFound
  30. case backgroundModelDownloaded
  31. case modelNameParseError
  32. case noLocalModelInfo
  33. case outdatedModelPathError
  34. case allLocalModelsFound
  35. case listModelsError
  36. case modelNotFound
  37. case modelDeleted
  38. case invalidOptions
  39. case retryDownload
  40. case anotherDownloadInProgressError
  41. case invalidModelName
  42. case permissionDenied
  43. case notEnoughSpace
  44. case validHTTPResponse
  45. case invalidModelInfoFetchURL
  46. case modelInfoRetrievalError
  47. case validAuthToken
  48. case hostnameError
  49. case invalidDownloadSessionError
  50. case invalidHTTPResponse
  51. case missingModelHash
  52. case invalidModelInfoJSON
  53. case modelInfoDeleted
  54. case modelInfoDownloaded
  55. case modelInfoUnmodified
  56. case mergeRequests
  57. case authTokenError
  58. case expiredModelInfo
  59. case modelDownloadError
  60. case downloadedModelSaveError
  61. case modelHashMismatchError
  62. case noModelHash
  63. case analyticsEventEncodeError
  64. case telemetryInitError
  65. case backgroundDownloadError
  66. case testError
  67. }
  68. /// On-device logger.
  69. class DeviceLogger {
  70. static let service = "[Firebase/MLModelDownloader]"
  71. static func logEvent(level: GoogleLoggerLevel, message: String, messageCode: LoggerMessageCode) {
  72. let code = String(format: "I-MLM%06d", messageCode.rawValue)
  73. let args: [CVarArg] = []
  74. GULLoggerWrapper.log(
  75. with: level,
  76. withService: DeviceLogger.service,
  77. withCode: code,
  78. withMessage: message,
  79. withArgs: getVaList(args)
  80. )
  81. }
  82. }