CocoaLumberjack.swift 7.9 KB

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