CocoaLumberjack.swift 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  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. // swiftlint:disable file_length
  16. #if canImport(Synchronization)
  17. import Synchronization
  18. #endif
  19. @_exported import CocoaLumberjack
  20. #if SWIFT_PACKAGE
  21. import CocoaLumberjackSwiftSupport
  22. #endif
  23. extension DDLogFlag {
  24. public static func from(_ logLevel: DDLogLevel) -> DDLogFlag {
  25. DDLogFlag(rawValue: logLevel.rawValue)
  26. }
  27. public init(_ logLevel: DDLogLevel) {
  28. self = DDLogFlag(rawValue: logLevel.rawValue)
  29. }
  30. /// Returns the log level, or the lowest equivalent.
  31. public func toLogLevel() -> DDLogLevel {
  32. if let ourValid = DDLogLevel(rawValue: rawValue) {
  33. return ourValid
  34. } else {
  35. if contains(.verbose) {
  36. return .verbose
  37. } else if contains(.debug) {
  38. return .debug
  39. } else if contains(.info) {
  40. return .info
  41. } else if contains(.warning) {
  42. return .warning
  43. } else if contains(.error) {
  44. return .error
  45. } else {
  46. return .off
  47. }
  48. }
  49. }
  50. }
  51. #if canImport(Synchronization)
  52. #if compiler(>=6.0)
  53. @available(macOS 15, iOS 18, tvOS 18, watchOS 11, visionOS 2, *)
  54. extension DDLogLevel: @retroactive AtomicRepresentable {}
  55. #else
  56. extension DDLogLevel: AtomicRepresentable {}
  57. #endif
  58. @available(macOS 15, iOS 18, tvOS 18, watchOS 11, visionOS 2, *)
  59. private let _dynamicLogLevel = Atomic(DDLogLevel.all)
  60. #endif
  61. private let _dynamicLogLevelLock = NSLock()
  62. #if swift(>=5.9)
  63. nonisolated(unsafe) private var _dynamicLogLevelStorage = DDLogLevel.all
  64. #else
  65. private var _dynamicLogLevelStorage = DDLogLevel.all
  66. #endif
  67. /// The log level that can dynamically limit log messages (vs. the static ``DDDefaultLogLevel``). This log level will only be checked, if the message passes the ``DDDefaultLogLevel``.
  68. public var dynamicLogLevel: DDLogLevel {
  69. get {
  70. #if canImport(Synchronization)
  71. if #available(macOS 15, iOS 18, tvOS 18, watchOS 11, visionOS 2, *) {
  72. return _dynamicLogLevel.load(ordering: .relaxed)
  73. }
  74. #endif
  75. _dynamicLogLevelLock.lock()
  76. defer { _dynamicLogLevelLock.unlock() }
  77. return _dynamicLogLevelStorage
  78. }
  79. set {
  80. #if canImport(Synchronization)
  81. if #available(macOS 15, iOS 18, tvOS 18, watchOS 11, visionOS 2, *) {
  82. _dynamicLogLevel.store(newValue, ordering: .relaxed)
  83. return
  84. }
  85. #endif
  86. _dynamicLogLevelLock.lock()
  87. defer { _dynamicLogLevelLock.unlock() }
  88. _dynamicLogLevelStorage = newValue
  89. }
  90. }
  91. /// Resets the ``dynamicLogLevel`` to ``DDLogLevel/all``.
  92. /// - SeeAlso: ``dynamicLogLevel``
  93. @inlinable
  94. public func resetDynamicLogLevel() {
  95. dynamicLogLevel = .all
  96. }
  97. @available(*, deprecated, message: "Please use dynamicLogLevel", renamed: "dynamicLogLevel")
  98. public var defaultDebugLevel: DDLogLevel {
  99. get {
  100. dynamicLogLevel
  101. }
  102. set {
  103. dynamicLogLevel = newValue
  104. }
  105. }
  106. @available(*, deprecated, message: "Please use resetDynamicLogLevel", renamed: "resetDynamicLogLevel")
  107. public func resetDefaultDebugLevel() {
  108. resetDynamicLogLevel()
  109. }
  110. #if canImport(Synchronization)
  111. @available(macOS 15, iOS 18, tvOS 18, watchOS 11, visionOS 2, *)
  112. private let _asyncLoggingEnabled = Atomic(true)
  113. #endif
  114. private let _asyncLoggingEnabledLock = NSLock()
  115. #if swift(>=5.9)
  116. nonisolated(unsafe) private var _asyncLoggingEnabledStorage = true
  117. #else
  118. private var _asyncLoggingEnabledStorage = true
  119. #endif
  120. /// If `true`, all logs (except errors) are logged asynchronously by default.
  121. public var asyncLoggingEnabled: Bool {
  122. get {
  123. #if canImport(Synchronization)
  124. if #available(macOS 15, iOS 18, tvOS 18, watchOS 11, visionOS 2, *) {
  125. return _asyncLoggingEnabled.load(ordering: .relaxed)
  126. }
  127. #endif
  128. _asyncLoggingEnabledLock.lock()
  129. defer { _asyncLoggingEnabledLock.unlock() }
  130. return _asyncLoggingEnabledStorage
  131. }
  132. set {
  133. #if canImport(Synchronization)
  134. if #available(macOS 15, iOS 18, tvOS 18, watchOS 11, visionOS 2, *) {
  135. _asyncLoggingEnabled.store(newValue, ordering: .relaxed)
  136. return
  137. }
  138. #endif
  139. _asyncLoggingEnabledLock.lock()
  140. defer { _asyncLoggingEnabledLock.unlock() }
  141. _asyncLoggingEnabledStorage = newValue
  142. }
  143. }
  144. @frozen
  145. public struct DDLogMessageFormat: ExpressibleByStringInterpolation {
  146. public typealias StringLiteralType = String
  147. @usableFromInline
  148. struct Storage {
  149. #if swift(>=5.6)
  150. @usableFromInline
  151. typealias VArg = any CVarArg
  152. #else
  153. @usableFromInline
  154. typealias VArg = CVarArg
  155. #endif
  156. @usableFromInline
  157. let requiresArgumentParsing: Bool
  158. @usableFromInline
  159. var format: String
  160. @usableFromInline
  161. var args: Array<VArg> {
  162. willSet {
  163. // We only assert here to let the compiler optimize it away.
  164. // The setter will be used repeatedly during string interpolation, thus should stay fast.
  165. assert(requiresArgumentParsing || newValue.isEmpty, "Non-empty arguments always require argument parsing!")
  166. }
  167. }
  168. @usableFromInline
  169. init(requiresArgumentParsing: Bool, format: String, args: Array<VArg>) {
  170. precondition(requiresArgumentParsing || args.isEmpty, "Non-empty arguments always require argument parsing!")
  171. self.requiresArgumentParsing = requiresArgumentParsing
  172. self.format = format
  173. self.args = args
  174. }
  175. @available(*, deprecated, message: "Use initializer specifying the need for argument parsing: init(requiresArgumentParsing:format:args:)")
  176. @usableFromInline
  177. init(format: String, args: Array<VArg>) {
  178. self.init(requiresArgumentParsing: !args.isEmpty, format: format, args: args)
  179. }
  180. @usableFromInline
  181. mutating func addString(_ string: String) {
  182. format.append(string.replacingOccurrences(of: "%", with: "%%"))
  183. }
  184. @inlinable
  185. mutating func addValue(_ arg: VArg, withSpecifier specifier: String) {
  186. format.append(specifier)
  187. args.append(arg)
  188. }
  189. }
  190. @frozen
  191. public struct StringInterpolation: StringInterpolationProtocol {
  192. @usableFromInline
  193. var storage: Storage
  194. @inlinable
  195. public init(literalCapacity: Int, interpolationCount: Int) {
  196. var format = String()
  197. format.reserveCapacity(literalCapacity)
  198. var args = Array<Storage.VArg>()
  199. args.reserveCapacity(interpolationCount)
  200. storage = .init(requiresArgumentParsing: true, format: format, args: args)
  201. }
  202. @inlinable
  203. public mutating func appendLiteral(_ literal: StringLiteralType) {
  204. storage.addString(literal)
  205. }
  206. @inlinable
  207. public mutating func appendInterpolation<S: StringProtocol>(_ string: S) {
  208. storage.addValue(String(string), withSpecifier: "%@")
  209. }
  210. @inlinable
  211. public mutating func appendInterpolation(_ int: Int8) {
  212. storage.addValue(int, withSpecifier: "%c")
  213. }
  214. @inlinable
  215. public mutating func appendInterpolation(_ int: UInt8) {
  216. storage.addValue(int, withSpecifier: "%c")
  217. }
  218. @inlinable
  219. public mutating func appendInterpolation(_ int: Int16) {
  220. storage.addValue(int, withSpecifier: "%i")
  221. }
  222. @inlinable
  223. public mutating func appendInterpolation(_ int: UInt16) {
  224. storage.addValue(int, withSpecifier: "%u")
  225. }
  226. @inlinable
  227. public mutating func appendInterpolation(_ int: Int32) {
  228. storage.addValue(int, withSpecifier: "%li")
  229. }
  230. @inlinable
  231. public mutating func appendInterpolation(_ int: UInt32) {
  232. storage.addValue(int, withSpecifier: "%lu")
  233. }
  234. @inlinable
  235. public mutating func appendInterpolation(_ int: Int64) {
  236. storage.addValue(int, withSpecifier: "%lli")
  237. }
  238. @inlinable
  239. public mutating func appendInterpolation(_ int: UInt64) {
  240. storage.addValue(int, withSpecifier: "%llu")
  241. }
  242. @inlinable
  243. public mutating func appendInterpolation(_ int: Int) {
  244. #if arch(arm64) || arch(x86_64)
  245. storage.addValue(int, withSpecifier: "%lli")
  246. #else
  247. storage.addValue(int, withSpecifier: "%li")
  248. #endif
  249. }
  250. @inlinable
  251. public mutating func appendInterpolation(_ int: UInt) {
  252. #if arch(arm64) || arch(x86_64)
  253. storage.addValue(int, withSpecifier: "%llu")
  254. #else
  255. storage.addValue(int, withSpecifier: "%lu")
  256. #endif
  257. }
  258. @inlinable
  259. public mutating func appendInterpolation(_ flt: Float) {
  260. storage.addValue(flt, withSpecifier: "%f")
  261. }
  262. @inlinable
  263. public mutating func appendInterpolation(_ dbl: Double) {
  264. storage.addValue(dbl, withSpecifier: "%lf")
  265. }
  266. @inlinable
  267. public mutating func appendInterpolation(_ bool: Bool) {
  268. storage.addValue(bool, withSpecifier: "%i") // bools are printed as ints
  269. }
  270. @inlinable
  271. public mutating func appendInterpolation<Convertible: ReferenceConvertible>(_ convertible: Convertible) {
  272. if convertible is Storage.VArg {
  273. print("""
  274. [WARNING]: CocoaLumberjackSwift is creating a \(DDLogMessageFormat.self) with an interpolation conforming to `CVarArg` \
  275. using the overload for `ReferenceConvertible` interpolations!
  276. Please report this as a bug, including the following snippet:
  277. ```
  278. Convertible: \(Convertible.self), ReferenceType: \(Convertible.ReferenceType.self), type(of: convertible): \(type(of: convertible))
  279. ```
  280. """)
  281. }
  282. // This should be safe, sine the compiler should convert it to the reference.
  283. // swiftlint:disable:next force_cast
  284. storage.addValue(convertible as? Storage.VArg ?? convertible as! Convertible.ReferenceType, withSpecifier: "%@")
  285. }
  286. @inlinable
  287. public mutating func appendInterpolation<Obj: NSObject>(_ object: Obj) {
  288. storage.addValue(object, withSpecifier: "%@")
  289. }
  290. @_disfavoredOverload
  291. public mutating func appendInterpolation(_ any: Any) {
  292. appendInterpolation(String(describing: any))
  293. }
  294. }
  295. @usableFromInline
  296. let storage: Storage
  297. @inlinable
  298. var format: String { storage.format }
  299. @inlinable
  300. var args: Array<Storage.VArg> { storage.args }
  301. @inlinable
  302. var formatted: String {
  303. guard storage.requiresArgumentParsing else { return storage.format }
  304. return String(format: storage.format, arguments: storage.args)
  305. }
  306. @inlinable
  307. public init(stringLiteral value: StringLiteralType) {
  308. storage = .init(requiresArgumentParsing: false, format: value, args: [])
  309. }
  310. @inlinable
  311. public init(stringInterpolation: StringInterpolation) {
  312. storage = stringInterpolation.storage
  313. }
  314. @inlinable
  315. internal init(_formattedMessage: String) {
  316. storage = .init(requiresArgumentParsing: false, format: _formattedMessage, args: [])
  317. }
  318. }
  319. extension DDLogMessage {
  320. @inlinable
  321. public convenience init(_ format: DDLogMessageFormat,
  322. level: DDLogLevel,
  323. flag: DDLogFlag,
  324. context: Int = 0,
  325. file: StaticString = #file,
  326. function: StaticString = #function,
  327. line: UInt = #line,
  328. tag: Any? = nil,
  329. timestamp: Date? = nil) {
  330. self.init(format: format.format,
  331. formatted: format.formatted,
  332. level: level,
  333. flag: flag,
  334. context: context,
  335. file: String(describing: file),
  336. function: String(describing: function),
  337. line: line,
  338. tag: tag,
  339. options: [.dontCopyMessage],
  340. timestamp: timestamp)
  341. }
  342. }
  343. @inlinable
  344. public func _DDLogMessage(_ messageFormat: @autoclosure () -> DDLogMessageFormat,
  345. level: DDLogLevel,
  346. flag: DDLogFlag,
  347. context: Int,
  348. file: StaticString,
  349. function: StaticString,
  350. line: UInt,
  351. tag: Any?,
  352. asynchronous: Bool?,
  353. ddlog: DDLog) {
  354. // The `dynamicLogLevel` will always be checked here (instead of being passed in).
  355. // We cannot "mix" it with the `DDDefaultLogLevel`, because otherwise the compiler won't strip strings that are not logged.
  356. if level.rawValue & flag.rawValue != 0 && dynamicLogLevel.rawValue & flag.rawValue != 0 {
  357. let logMessage = DDLogMessage(messageFormat(),
  358. level: level,
  359. flag: flag,
  360. context: context,
  361. file: file,
  362. function: function,
  363. line: line,
  364. tag: tag)
  365. ddlog.log(asynchronous: asynchronous ?? asyncLoggingEnabled, message: logMessage)
  366. }
  367. }
  368. @inlinable
  369. public func DDLogDebug(_ message: @autoclosure () -> DDLogMessageFormat,
  370. level: DDLogLevel = DDDefaultLogLevel,
  371. context: Int = 0,
  372. file: StaticString = #file,
  373. function: StaticString = #function,
  374. line: UInt = #line,
  375. tag: Any? = nil,
  376. asynchronous: Bool? = nil,
  377. ddlog: DDLog = .sharedInstance) {
  378. _DDLogMessage(message(),
  379. level: level,
  380. flag: .debug,
  381. context: context,
  382. file: file,
  383. function: function,
  384. line: line,
  385. tag: tag,
  386. asynchronous: asynchronous,
  387. ddlog: ddlog)
  388. }
  389. @inlinable
  390. public func DDLogInfo(_ message: @autoclosure () -> DDLogMessageFormat,
  391. level: DDLogLevel = DDDefaultLogLevel,
  392. context: Int = 0,
  393. file: StaticString = #file,
  394. function: StaticString = #function,
  395. line: UInt = #line,
  396. tag: Any? = nil,
  397. asynchronous: Bool? = nil,
  398. ddlog: DDLog = .sharedInstance) {
  399. _DDLogMessage(message(),
  400. level: level,
  401. flag: .info,
  402. context: context,
  403. file: file,
  404. function: function,
  405. line: line,
  406. tag: tag,
  407. asynchronous: asynchronous,
  408. ddlog: ddlog)
  409. }
  410. @inlinable
  411. public func DDLogWarn(_ message: @autoclosure () -> DDLogMessageFormat,
  412. level: DDLogLevel = DDDefaultLogLevel,
  413. context: Int = 0,
  414. file: StaticString = #file,
  415. function: StaticString = #function,
  416. line: UInt = #line,
  417. tag: Any? = nil,
  418. asynchronous: Bool? = nil,
  419. ddlog: DDLog = .sharedInstance) {
  420. _DDLogMessage(message(),
  421. level: level,
  422. flag: .warning,
  423. context: context,
  424. file: file,
  425. function: function,
  426. line: line,
  427. tag: tag,
  428. asynchronous: asynchronous,
  429. ddlog: ddlog)
  430. }
  431. @inlinable
  432. public func DDLogVerbose(_ message: @autoclosure () -> DDLogMessageFormat,
  433. level: DDLogLevel = DDDefaultLogLevel,
  434. context: Int = 0,
  435. file: StaticString = #file,
  436. function: StaticString = #function,
  437. line: UInt = #line,
  438. tag: Any? = nil,
  439. asynchronous: Bool? = nil,
  440. ddlog: DDLog = .sharedInstance) {
  441. _DDLogMessage(message(),
  442. level: level,
  443. flag: .verbose,
  444. context: context,
  445. file: file,
  446. function: function,
  447. line: line,
  448. tag: tag,
  449. asynchronous: asynchronous,
  450. ddlog: ddlog)
  451. }
  452. @inlinable
  453. public func DDLogError(_ message: @autoclosure () -> DDLogMessageFormat,
  454. level: DDLogLevel = DDDefaultLogLevel,
  455. context: Int = 0,
  456. file: StaticString = #file,
  457. function: StaticString = #function,
  458. line: UInt = #line,
  459. tag: Any? = nil,
  460. asynchronous: Bool? = nil,
  461. ddlog: DDLog = .sharedInstance) {
  462. _DDLogMessage(message(),
  463. level: level,
  464. flag: .error,
  465. context: context,
  466. file: file,
  467. function: function,
  468. line: line,
  469. tag: tag,
  470. asynchronous: asynchronous ?? false,
  471. ddlog: ddlog)
  472. }
  473. @available(*, deprecated, message: "Use an interpolated DDLogMessageFormat instead")
  474. @inlinable
  475. @_disfavoredOverload
  476. public func _DDLogMessage(_ message: @autoclosure () -> Any,
  477. level: DDLogLevel,
  478. flag: DDLogFlag,
  479. context: Int,
  480. file: StaticString,
  481. function: StaticString,
  482. line: UInt,
  483. tag: Any?,
  484. asynchronous: Bool?,
  485. ddlog: DDLog) {
  486. // This will lead to `messageFormat` and `message` being equal on DDLogMessage,
  487. // which is what the legacy initializer of DDLogMessage does as well.
  488. _DDLogMessage(.init(_formattedMessage: String(describing: message())),
  489. level: level,
  490. flag: flag,
  491. context: context,
  492. file: file,
  493. function: function,
  494. line: line,
  495. tag: tag,
  496. asynchronous: asynchronous,
  497. ddlog: ddlog)
  498. }
  499. @available(*, deprecated, message: "Use an interpolated DDLogMessageFormat instead")
  500. @inlinable
  501. @_disfavoredOverload
  502. public func DDLogDebug(_ message: @autoclosure () -> Any,
  503. level: DDLogLevel = DDDefaultLogLevel,
  504. context: Int = 0,
  505. file: StaticString = #file,
  506. function: StaticString = #function,
  507. line: UInt = #line,
  508. tag: Any? = nil,
  509. asynchronous async: Bool? = nil,
  510. ddlog: DDLog = .sharedInstance) {
  511. _DDLogMessage(message(),
  512. level: level,
  513. flag: .debug,
  514. context: context,
  515. file: file,
  516. function: function,
  517. line: line,
  518. tag: tag,
  519. asynchronous: async,
  520. ddlog: ddlog)
  521. }
  522. @available(*, deprecated, message: "Use an interpolated DDLogMessageFormat instead")
  523. @inlinable
  524. @_disfavoredOverload
  525. public func DDLogInfo(_ message: @autoclosure () -> Any,
  526. level: DDLogLevel = DDDefaultLogLevel,
  527. context: Int = 0,
  528. file: StaticString = #file,
  529. function: StaticString = #function,
  530. line: UInt = #line,
  531. tag: Any? = nil,
  532. asynchronous async: Bool? = nil,
  533. ddlog: DDLog = .sharedInstance) {
  534. _DDLogMessage(message(),
  535. level: level,
  536. flag: .info,
  537. context: context,
  538. file: file,
  539. function: function,
  540. line: line,
  541. tag: tag,
  542. asynchronous: async,
  543. ddlog: ddlog)
  544. }
  545. @available(*, deprecated, message: "Use an interpolated DDLogMessageFormat instead")
  546. @inlinable
  547. @_disfavoredOverload
  548. public func DDLogWarn(_ message: @autoclosure () -> Any,
  549. level: DDLogLevel = DDDefaultLogLevel,
  550. context: Int = 0,
  551. file: StaticString = #file,
  552. function: StaticString = #function,
  553. line: UInt = #line,
  554. tag: Any? = nil,
  555. asynchronous async: Bool? = nil,
  556. ddlog: DDLog = .sharedInstance) {
  557. _DDLogMessage(message(),
  558. level: level,
  559. flag: .warning,
  560. context: context,
  561. file: file,
  562. function: function,
  563. line: line,
  564. tag: tag,
  565. asynchronous: async,
  566. ddlog: ddlog)
  567. }
  568. @available(*, deprecated, message: "Use an interpolated DDLogMessageFormat instead")
  569. @inlinable
  570. @_disfavoredOverload
  571. public func DDLogVerbose(_ message: @autoclosure () -> Any,
  572. level: DDLogLevel = DDDefaultLogLevel,
  573. context: Int = 0,
  574. file: StaticString = #file,
  575. function: StaticString = #function,
  576. line: UInt = #line,
  577. tag: Any? = nil,
  578. asynchronous async: Bool? = nil,
  579. ddlog: DDLog = .sharedInstance) {
  580. _DDLogMessage(message(),
  581. level: level,
  582. flag: .verbose,
  583. context: context,
  584. file: file,
  585. function: function,
  586. line: line,
  587. tag: tag,
  588. asynchronous: async,
  589. ddlog: ddlog)
  590. }
  591. @available(*, deprecated, message: "Use an interpolated DDLogMessageFormat instead")
  592. @inlinable
  593. @_disfavoredOverload
  594. public func DDLogError(_ message: @autoclosure () -> Any,
  595. level: DDLogLevel = DDDefaultLogLevel,
  596. context: Int = 0,
  597. file: StaticString = #file,
  598. function: StaticString = #function,
  599. line: UInt = #line,
  600. tag: Any? = nil,
  601. asynchronous async: Bool? = nil,
  602. ddlog: DDLog = .sharedInstance) {
  603. _DDLogMessage(message(),
  604. level: level,
  605. flag: .error,
  606. context: context,
  607. file: file,
  608. function: function,
  609. line: line,
  610. tag: tag,
  611. asynchronous: async ?? false,
  612. ddlog: ddlog)
  613. }
  614. /// Returns a String of the current filename, without full path or extension.
  615. /// Analogous to the C preprocessor macro `THIS_FILE`.
  616. public func currentFileName(_ fileName: StaticString = #file) -> String {
  617. var str = String(describing: fileName)
  618. if let idx = str.range(of: "/", options: .backwards)?.upperBound {
  619. str = String(str[idx...])
  620. }
  621. if let idx = str.range(of: ".", options: .backwards)?.lowerBound {
  622. str = String(str[..<idx])
  623. }
  624. return str
  625. }
  626. // swiftlint:disable identifier_name
  627. // swiftlint doesn't like func names that begin with a capital letter - deprecated
  628. @available(*, deprecated, message: "Please use currentFileName", renamed: "currentFileName")
  629. public func CurrentFileName(_ fileName: StaticString = #file) -> String {
  630. currentFileName(fileName)
  631. }
  632. // swiftlint:enable identifier_name
  633. // swiftlint:enable file_length