SwiftLogMessage.swift 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // Software License Agreement (BSD License)
  2. //
  3. // Copyright (c) 2010-2025, Deusty, LLC
  4. // All rights reserved.
  5. //
  6. // Redistribution and use of this software in source and binary forms,
  7. // with or without modification, are permitted provided that the following conditions are met:
  8. //
  9. // * Redistributions of source code must retain the above copyright notice,
  10. // this list of conditions and the following disclaimer.
  11. //
  12. // * Neither the name of Deusty nor the names of its contributors may be used
  13. // to endorse or promote products derived from this software without specific
  14. // prior written permission of Deusty, LLC.
  15. public import CocoaLumberjack
  16. public import Logging
  17. extension Logging.Logger.Level {
  18. @inlinable
  19. var ddLogLevelAndFlag: (DDLogLevel, DDLogFlag) {
  20. switch self {
  21. case .trace: return (.verbose, .verbose)
  22. case .debug: return (.debug, .debug)
  23. case .info, .notice: return (.info, .info)
  24. case .warning: return (.warning, .warning)
  25. case .error, .critical: return (.error, .error)
  26. @unknown default: return (.error, .error) // better safe than sorry
  27. }
  28. }
  29. }
  30. /// This class (intentionally internal) is only an "encapsulation" layer above ``DDLogMessage``.
  31. /// It's basically an implementation detail of ``DDLogMessage/swiftLogInfo``.
  32. @usableFromInline
  33. final class SwiftLogMessage: DDLogMessage, @unchecked Sendable {
  34. @usableFromInline
  35. let _swiftLogInfo: SwiftLogInformation
  36. @usableFromInline
  37. init(loggerLabel: String,
  38. loggerMetadata: Logging.Logger.Metadata,
  39. loggerProvidedMetadata: Logging.Logger.Metadata?,
  40. message: Logging.Logger.Message,
  41. level: Logging.Logger.Level,
  42. metadata: Logging.Logger.Metadata?,
  43. source: String,
  44. file: String,
  45. function: String,
  46. line: UInt) {
  47. _swiftLogInfo = .init(logger: .init(label: loggerLabel,
  48. metadataSources: .init(logger: loggerMetadata,
  49. provider: loggerProvidedMetadata)),
  50. message: .init(message: message,
  51. level: level,
  52. metadata: metadata,
  53. source: source))
  54. let (ddLogLevel, ddLogFlag) = level.ddLogLevelAndFlag
  55. let msg = String(describing: message)
  56. super.init(format: msg,
  57. formatted: msg, // We have no chance in retrieving the original format here.
  58. level: ddLogLevel,
  59. flag: ddLogFlag,
  60. context: 0,
  61. file: file,
  62. function: function,
  63. line: line,
  64. tag: nil,
  65. options: .dontCopyMessage, // Swift will bridge to NSString. No need to make an additional copy.
  66. timestamp: nil) // Passing nil will make DDLogMessage create the timestamp which saves us the bridging between Date and NSDate.
  67. }
  68. // Not removed due to `@usableFromInline`.
  69. @usableFromInline
  70. @available(*, deprecated, renamed: "init(loggerLabel:loggerMetadata:loggerMetadata:message:level:metadata:source:file:function:line:)")
  71. convenience init(loggerLabel: String,
  72. loggerMetadata: Logging.Logger.Metadata,
  73. message: Logging.Logger.Message,
  74. level: Logging.Logger.Level,
  75. metadata: Logging.Logger.Metadata?,
  76. source: String,
  77. file: String,
  78. function: String,
  79. line: UInt) {
  80. self.init(loggerLabel: loggerLabel,
  81. loggerMetadata: loggerMetadata,
  82. loggerProvidedMetadata: nil,
  83. message: message,
  84. level: level,
  85. metadata: metadata,
  86. source: source,
  87. file: file,
  88. function: function,
  89. line: line)
  90. }
  91. override func isEqual(_ object: Any?) -> Bool {
  92. super.isEqual(object) && (object as? SwiftLogMessage)?._swiftLogInfo == _swiftLogInfo
  93. }
  94. }