CocoaLumberjack.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. @_exported public import CocoaLumberjack
  16. #if SWIFT_PACKAGE
  17. public import CocoaLumberjackSwiftSupport
  18. #endif
  19. @inlinable
  20. public func _DDLogMessage(_ messageFormat: @autoclosure () -> DDLogMessageFormat,
  21. level: DDLogLevel,
  22. flag: DDLogFlag,
  23. context: Int,
  24. file: StaticString,
  25. function: StaticString,
  26. line: UInt,
  27. tag: Any?,
  28. asynchronous: Bool?,
  29. ddlog: DDLog) {
  30. // The `dynamicLogLevel` will always be checked here (instead of being passed in).
  31. // We cannot "mix" it with the `DDDefaultLogLevel`, because otherwise the compiler won't strip strings that are not logged.
  32. if level.rawValue & flag.rawValue != 0 && dynamicLogLevel.rawValue & flag.rawValue != 0 {
  33. let logMessage = DDLogMessage(messageFormat(),
  34. level: level,
  35. flag: flag,
  36. context: context,
  37. file: file,
  38. function: function,
  39. line: line,
  40. tag: tag)
  41. ddlog.log(asynchronous: asynchronous ?? asyncLoggingEnabled, message: logMessage)
  42. }
  43. }
  44. @inlinable
  45. public func DDLogDebug(_ message: @autoclosure () -> DDLogMessageFormat,
  46. level: DDLogLevel = DDDefaultLogLevel,
  47. context: Int = 0,
  48. file: StaticString = #file,
  49. function: StaticString = #function,
  50. line: UInt = #line,
  51. tag: Any? = nil,
  52. asynchronous: Bool? = nil,
  53. ddlog: DDLog = .sharedInstance) {
  54. _DDLogMessage(message(),
  55. level: level,
  56. flag: .debug,
  57. context: context,
  58. file: file,
  59. function: function,
  60. line: line,
  61. tag: tag,
  62. asynchronous: asynchronous,
  63. ddlog: ddlog)
  64. }
  65. @inlinable
  66. public func DDLogInfo(_ message: @autoclosure () -> DDLogMessageFormat,
  67. level: DDLogLevel = DDDefaultLogLevel,
  68. context: Int = 0,
  69. file: StaticString = #file,
  70. function: StaticString = #function,
  71. line: UInt = #line,
  72. tag: Any? = nil,
  73. asynchronous: Bool? = nil,
  74. ddlog: DDLog = .sharedInstance) {
  75. _DDLogMessage(message(),
  76. level: level,
  77. flag: .info,
  78. context: context,
  79. file: file,
  80. function: function,
  81. line: line,
  82. tag: tag,
  83. asynchronous: asynchronous,
  84. ddlog: ddlog)
  85. }
  86. @inlinable
  87. public func DDLogWarn(_ message: @autoclosure () -> DDLogMessageFormat,
  88. level: DDLogLevel = DDDefaultLogLevel,
  89. context: Int = 0,
  90. file: StaticString = #file,
  91. function: StaticString = #function,
  92. line: UInt = #line,
  93. tag: Any? = nil,
  94. asynchronous: Bool? = nil,
  95. ddlog: DDLog = .sharedInstance) {
  96. _DDLogMessage(message(),
  97. level: level,
  98. flag: .warning,
  99. context: context,
  100. file: file,
  101. function: function,
  102. line: line,
  103. tag: tag,
  104. asynchronous: asynchronous,
  105. ddlog: ddlog)
  106. }
  107. @inlinable
  108. public func DDLogVerbose(_ message: @autoclosure () -> DDLogMessageFormat,
  109. level: DDLogLevel = DDDefaultLogLevel,
  110. context: Int = 0,
  111. file: StaticString = #file,
  112. function: StaticString = #function,
  113. line: UInt = #line,
  114. tag: Any? = nil,
  115. asynchronous: Bool? = nil,
  116. ddlog: DDLog = .sharedInstance) {
  117. _DDLogMessage(message(),
  118. level: level,
  119. flag: .verbose,
  120. context: context,
  121. file: file,
  122. function: function,
  123. line: line,
  124. tag: tag,
  125. asynchronous: asynchronous,
  126. ddlog: ddlog)
  127. }
  128. @inlinable
  129. public func DDLogError(_ message: @autoclosure () -> DDLogMessageFormat,
  130. level: DDLogLevel = DDDefaultLogLevel,
  131. context: Int = 0,
  132. file: StaticString = #file,
  133. function: StaticString = #function,
  134. line: UInt = #line,
  135. tag: Any? = nil,
  136. asynchronous: Bool? = nil,
  137. ddlog: DDLog = .sharedInstance) {
  138. _DDLogMessage(message(),
  139. level: level,
  140. flag: .error,
  141. context: context,
  142. file: file,
  143. function: function,
  144. line: line,
  145. tag: tag,
  146. asynchronous: asynchronous ?? false,
  147. ddlog: ddlog)
  148. }
  149. @available(*, deprecated, message: "Use an interpolated DDLogMessageFormat instead")
  150. @inlinable
  151. @_disfavoredOverload
  152. public func _DDLogMessage(_ message: @autoclosure () -> Any,
  153. level: DDLogLevel,
  154. flag: DDLogFlag,
  155. context: Int,
  156. file: StaticString,
  157. function: StaticString,
  158. line: UInt,
  159. tag: Any?,
  160. asynchronous: Bool?,
  161. ddlog: DDLog) {
  162. // This will lead to `messageFormat` and `message` being equal on DDLogMessage,
  163. // which is what the legacy initializer of DDLogMessage does as well.
  164. _DDLogMessage(.init(_formattedMessage: String(describing: message())),
  165. level: level,
  166. flag: flag,
  167. context: context,
  168. file: file,
  169. function: function,
  170. line: line,
  171. tag: tag,
  172. asynchronous: asynchronous,
  173. ddlog: ddlog)
  174. }
  175. @available(*, deprecated, message: "Use an interpolated DDLogMessageFormat instead")
  176. @inlinable
  177. @_disfavoredOverload
  178. public func DDLogDebug(_ message: @autoclosure () -> Any,
  179. level: DDLogLevel = DDDefaultLogLevel,
  180. context: Int = 0,
  181. file: StaticString = #file,
  182. function: StaticString = #function,
  183. line: UInt = #line,
  184. tag: Any? = nil,
  185. asynchronous async: Bool? = nil,
  186. ddlog: DDLog = .sharedInstance) {
  187. _DDLogMessage(message(),
  188. level: level,
  189. flag: .debug,
  190. context: context,
  191. file: file,
  192. function: function,
  193. line: line,
  194. tag: tag,
  195. asynchronous: async,
  196. ddlog: ddlog)
  197. }
  198. @available(*, deprecated, message: "Use an interpolated DDLogMessageFormat instead")
  199. @inlinable
  200. @_disfavoredOverload
  201. public func DDLogInfo(_ message: @autoclosure () -> Any,
  202. level: DDLogLevel = DDDefaultLogLevel,
  203. context: Int = 0,
  204. file: StaticString = #file,
  205. function: StaticString = #function,
  206. line: UInt = #line,
  207. tag: Any? = nil,
  208. asynchronous async: Bool? = nil,
  209. ddlog: DDLog = .sharedInstance) {
  210. _DDLogMessage(message(),
  211. level: level,
  212. flag: .info,
  213. context: context,
  214. file: file,
  215. function: function,
  216. line: line,
  217. tag: tag,
  218. asynchronous: async,
  219. ddlog: ddlog)
  220. }
  221. @available(*, deprecated, message: "Use an interpolated DDLogMessageFormat instead")
  222. @inlinable
  223. @_disfavoredOverload
  224. public func DDLogWarn(_ message: @autoclosure () -> Any,
  225. level: DDLogLevel = DDDefaultLogLevel,
  226. context: Int = 0,
  227. file: StaticString = #file,
  228. function: StaticString = #function,
  229. line: UInt = #line,
  230. tag: Any? = nil,
  231. asynchronous async: Bool? = nil,
  232. ddlog: DDLog = .sharedInstance) {
  233. _DDLogMessage(message(),
  234. level: level,
  235. flag: .warning,
  236. context: context,
  237. file: file,
  238. function: function,
  239. line: line,
  240. tag: tag,
  241. asynchronous: async,
  242. ddlog: ddlog)
  243. }
  244. @available(*, deprecated, message: "Use an interpolated DDLogMessageFormat instead")
  245. @inlinable
  246. @_disfavoredOverload
  247. public func DDLogVerbose(_ message: @autoclosure () -> Any,
  248. level: DDLogLevel = DDDefaultLogLevel,
  249. context: Int = 0,
  250. file: StaticString = #file,
  251. function: StaticString = #function,
  252. line: UInt = #line,
  253. tag: Any? = nil,
  254. asynchronous async: Bool? = nil,
  255. ddlog: DDLog = .sharedInstance) {
  256. _DDLogMessage(message(),
  257. level: level,
  258. flag: .verbose,
  259. context: context,
  260. file: file,
  261. function: function,
  262. line: line,
  263. tag: tag,
  264. asynchronous: async,
  265. ddlog: ddlog)
  266. }
  267. @available(*, deprecated, message: "Use an interpolated DDLogMessageFormat instead")
  268. @inlinable
  269. @_disfavoredOverload
  270. public func DDLogError(_ message: @autoclosure () -> Any,
  271. level: DDLogLevel = DDDefaultLogLevel,
  272. context: Int = 0,
  273. file: StaticString = #file,
  274. function: StaticString = #function,
  275. line: UInt = #line,
  276. tag: Any? = nil,
  277. asynchronous async: Bool? = nil,
  278. ddlog: DDLog = .sharedInstance) {
  279. _DDLogMessage(message(),
  280. level: level,
  281. flag: .error,
  282. context: context,
  283. file: file,
  284. function: function,
  285. line: line,
  286. tag: tag,
  287. asynchronous: async ?? false,
  288. ddlog: ddlog)
  289. }