CocoaLumberjack.swift 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. // Software License Agreement (BSD License)
  2. //
  3. // Copyright (c) 2010-2022, 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 import CocoaLumberjack
  16. #if SWIFT_PACKAGE
  17. import CocoaLumberjackSwiftSupport
  18. #endif
  19. extension DDLogFlag {
  20. public static func from(_ logLevel: DDLogLevel) -> DDLogFlag {
  21. DDLogFlag(rawValue: logLevel.rawValue)
  22. }
  23. public init(_ logLevel: DDLogLevel) {
  24. self = DDLogFlag(rawValue: logLevel.rawValue)
  25. }
  26. /// Returns the log level, or the lowest equivalent.
  27. public func toLogLevel() -> DDLogLevel {
  28. if let ourValid = DDLogLevel(rawValue: rawValue) {
  29. return ourValid
  30. } else {
  31. if contains(.verbose) {
  32. return .verbose
  33. } else if contains(.debug) {
  34. return .debug
  35. } else if contains(.info) {
  36. return .info
  37. } else if contains(.warning) {
  38. return .warning
  39. } else if contains(.error) {
  40. return .error
  41. } else {
  42. return .off
  43. }
  44. }
  45. }
  46. }
  47. /// 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`.
  48. public var dynamicLogLevel = DDLogLevel.all
  49. /// Resets the `dynamicLogLevel` to `.all`.
  50. /// - SeeAlso: `dynamicLogLevel`
  51. @inlinable
  52. public func resetDynamicLogLevel() {
  53. dynamicLogLevel = .all
  54. }
  55. @available(*, deprecated, message: "Please use dynamicLogLevel", renamed: "dynamicLogLevel")
  56. public var defaultDebugLevel: DDLogLevel {
  57. get {
  58. dynamicLogLevel
  59. }
  60. set {
  61. dynamicLogLevel = newValue
  62. }
  63. }
  64. @available(*, deprecated, message: "Please use resetDynamicLogLevel", renamed: "resetDynamicLogLevel")
  65. public func resetDefaultDebugLevel() {
  66. resetDynamicLogLevel()
  67. }
  68. /// If `true`, all logs (except errors) are logged asynchronously by default.
  69. public var asyncLoggingEnabled = true
  70. @inlinable
  71. public func _DDLogMessage(_ message: @autoclosure () -> Any,
  72. level: DDLogLevel,
  73. flag: DDLogFlag,
  74. context: Int,
  75. file: StaticString,
  76. function: StaticString,
  77. line: UInt,
  78. tag: Any?,
  79. asynchronous: Bool,
  80. ddlog: DDLog) {
  81. // The `dynamicLogLevel` will always be checked here (instead of being passed in).
  82. // We cannot "mix" it with the `DDDefaultLogLevel`, because otherwise the compiler won't strip strings that are not logged.
  83. if level.rawValue & flag.rawValue != 0 && dynamicLogLevel.rawValue & flag.rawValue != 0 {
  84. // Tell the DDLogMessage constructor to copy the C strings that get passed to it.
  85. let logMessage = DDLogMessage(message: String(describing: message()),
  86. level: level,
  87. flag: flag,
  88. context: context,
  89. file: String(describing: file),
  90. function: String(describing: function),
  91. line: line,
  92. tag: tag,
  93. options: [.copyFile, .copyFunction],
  94. timestamp: nil)
  95. ddlog.log(asynchronous: asynchronous, message: logMessage)
  96. }
  97. }
  98. @inlinable
  99. public func DDLogDebug(_ message: @autoclosure () -> Any,
  100. level: DDLogLevel = DDDefaultLogLevel,
  101. context: Int = 0,
  102. file: StaticString = #file,
  103. function: StaticString = #function,
  104. line: UInt = #line,
  105. tag: Any? = nil,
  106. asynchronous async: Bool = asyncLoggingEnabled,
  107. ddlog: DDLog = .sharedInstance) {
  108. _DDLogMessage(message(),
  109. level: level,
  110. flag: .debug,
  111. context: context,
  112. file: file,
  113. function: function,
  114. line: line,
  115. tag: tag,
  116. asynchronous: async,
  117. ddlog: ddlog)
  118. }
  119. @inlinable
  120. public func DDLogInfo(_ message: @autoclosure () -> Any,
  121. level: DDLogLevel = DDDefaultLogLevel,
  122. context: Int = 0,
  123. file: StaticString = #file,
  124. function: StaticString = #function,
  125. line: UInt = #line,
  126. tag: Any? = nil,
  127. asynchronous async: Bool = asyncLoggingEnabled,
  128. ddlog: DDLog = .sharedInstance) {
  129. _DDLogMessage(message(),
  130. level: level,
  131. flag: .info,
  132. context: context,
  133. file: file,
  134. function: function,
  135. line: line,
  136. tag: tag,
  137. asynchronous: async,
  138. ddlog: ddlog)
  139. }
  140. @inlinable
  141. public func DDLogWarn(_ message: @autoclosure () -> Any,
  142. level: DDLogLevel = DDDefaultLogLevel,
  143. context: Int = 0,
  144. file: StaticString = #file,
  145. function: StaticString = #function,
  146. line: UInt = #line,
  147. tag: Any? = nil,
  148. asynchronous async: Bool = asyncLoggingEnabled,
  149. ddlog: DDLog = .sharedInstance) {
  150. _DDLogMessage(message(),
  151. level: level,
  152. flag: .warning,
  153. context: context,
  154. file: file,
  155. function: function,
  156. line: line,
  157. tag: tag,
  158. asynchronous: async,
  159. ddlog: ddlog)
  160. }
  161. @inlinable
  162. public func DDLogVerbose(_ message: @autoclosure () -> Any,
  163. level: DDLogLevel = DDDefaultLogLevel,
  164. context: Int = 0,
  165. file: StaticString = #file,
  166. function: StaticString = #function,
  167. line: UInt = #line,
  168. tag: Any? = nil,
  169. asynchronous async: Bool = asyncLoggingEnabled,
  170. ddlog: DDLog = .sharedInstance) {
  171. _DDLogMessage(message(),
  172. level: level,
  173. flag: .verbose,
  174. context: context,
  175. file: file,
  176. function: function,
  177. line: line,
  178. tag: tag,
  179. asynchronous: async,
  180. ddlog: ddlog)
  181. }
  182. @inlinable
  183. public func DDLogError(_ message: @autoclosure () -> Any,
  184. level: DDLogLevel = DDDefaultLogLevel,
  185. context: Int = 0,
  186. file: StaticString = #file,
  187. function: StaticString = #function,
  188. line: UInt = #line,
  189. tag: Any? = nil,
  190. asynchronous async: Bool = false,
  191. ddlog: DDLog = .sharedInstance) {
  192. _DDLogMessage(message(),
  193. level: level,
  194. flag: .error,
  195. context: context,
  196. file: file,
  197. function: function,
  198. line: line,
  199. tag: tag,
  200. asynchronous: async,
  201. ddlog: ddlog)
  202. }
  203. /// Returns a String of the current filename, without full path or extension.
  204. /// Analogous to the C preprocessor macro `THIS_FILE`.
  205. public func currentFileName(_ fileName: StaticString = #file) -> String {
  206. var str = String(describing: fileName)
  207. if let idx = str.range(of: "/", options: .backwards)?.upperBound {
  208. str = String(str[idx...])
  209. }
  210. if let idx = str.range(of: ".", options: .backwards)?.lowerBound {
  211. str = String(str[..<idx])
  212. }
  213. return str
  214. }
  215. // swiftlint:disable identifier_name
  216. // swiftlint doesn't like func names that begin with a capital letter - deprecated
  217. @available(*, deprecated, message: "Please use currentFileName", renamed: "currentFileName")
  218. public func CurrentFileName(_ fileName: StaticString = #file) -> String {
  219. currentFileName(fileName)
  220. }