CocoaLumberjack.swift 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // Software License Agreement (BSD License)
  2. //
  3. // Copyright (c) 2014-2016, 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. import Foundation
  16. extension DDLogFlag {
  17. public static func fromLogLevel(logLevel: DDLogLevel) -> DDLogFlag {
  18. return DDLogFlag(rawValue: logLevel.rawValue)
  19. }
  20. public init(_ logLevel: DDLogLevel) {
  21. self = DDLogFlag(rawValue: logLevel.rawValue)
  22. }
  23. ///returns the log level, or the lowest equivalant.
  24. public func toLogLevel() -> DDLogLevel {
  25. if let ourValid = DDLogLevel(rawValue: self.rawValue) {
  26. return ourValid
  27. } else {
  28. let logFlag:DDLogFlag = self
  29. if logFlag.contains(.Verbose) {
  30. return .Verbose
  31. } else if logFlag.contains(.Debug) {
  32. return .Debug
  33. } else if logFlag.contains(.Info) {
  34. return .Info
  35. } else if logFlag.contains(.Warning) {
  36. return .Warning
  37. } else if logFlag.contains(.Error) {
  38. return .Error
  39. } else {
  40. return .Off
  41. }
  42. }
  43. }
  44. }
  45. public var defaultDebugLevel = DDLogLevel.Verbose
  46. public func resetDefaultDebugLevel() {
  47. defaultDebugLevel = DDLogLevel.Verbose
  48. }
  49. @available(*, deprecated, message="Use one of the DDLog*() functions if appropriate or call _DDLogMessage()")
  50. public func SwiftLogMacro(isAsynchronous: Bool, level: DDLogLevel, flag flg: DDLogFlag, context: Int = 0, file: StaticString = #file, function: StaticString = #function, line: UInt = #line, tag: AnyObject? = nil, @autoclosure string: () -> String, ddlog: DDLog = DDLog.sharedInstance()) {
  51. _DDLogMessage(string, level: level, flag: flg, context: context, file: file, function: function, line: line, tag: tag, asynchronous: isAsynchronous, ddlog: ddlog)
  52. }
  53. public func _DDLogMessage(@autoclosure message: () -> String, level: DDLogLevel, flag: DDLogFlag, context: Int, file: StaticString, function: StaticString, line: UInt, tag: AnyObject?, asynchronous: Bool, ddlog: DDLog) {
  54. if level.rawValue & flag.rawValue != 0 {
  55. // Tell the DDLogMessage constructor to copy the C strings that get passed to it.
  56. let logMessage = DDLogMessage(message: message(), level: level, flag: flag, context: context, file: file.stringValue, function: function.stringValue, line: line, tag: tag, options: [.CopyFile, .CopyFunction], timestamp: nil)
  57. ddlog.log(asynchronous, message: logMessage)
  58. }
  59. }
  60. public func DDLogDebug(@autoclosure message: () -> String, level: DDLogLevel = defaultDebugLevel, context: Int = 0, file: StaticString = #file, function: StaticString = #function, line: UInt = #line, tag: AnyObject? = nil, asynchronous async: Bool = true, ddlog: DDLog = DDLog.sharedInstance()) {
  61. _DDLogMessage(message, level: level, flag: .Debug, context: context, file: file, function: function, line: line, tag: tag, asynchronous: async, ddlog: ddlog)
  62. }
  63. public func DDLogInfo(@autoclosure message: () -> String, level: DDLogLevel = defaultDebugLevel, context: Int = 0, file: StaticString = #file, function: StaticString = #function, line: UInt = #line, tag: AnyObject? = nil, asynchronous async: Bool = true, ddlog: DDLog = DDLog.sharedInstance()) {
  64. _DDLogMessage(message, level: level, flag: .Info, context: context, file: file, function: function, line: line, tag: tag, asynchronous: async, ddlog: ddlog)
  65. }
  66. public func DDLogWarn(@autoclosure message: () -> String, level: DDLogLevel = defaultDebugLevel, context: Int = 0, file: StaticString = #file, function: StaticString = #function, line: UInt = #line, tag: AnyObject? = nil, asynchronous async: Bool = true, ddlog: DDLog = DDLog.sharedInstance()) {
  67. _DDLogMessage(message, level: level, flag: .Warning, context: context, file: file, function: function, line: line, tag: tag, asynchronous: async, ddlog: ddlog)
  68. }
  69. public func DDLogVerbose(@autoclosure message: () -> String, level: DDLogLevel = defaultDebugLevel, context: Int = 0, file: StaticString = #file, function: StaticString = #function, line: UInt = #line, tag: AnyObject? = nil, asynchronous async: Bool = true, ddlog: DDLog = DDLog.sharedInstance()) {
  70. _DDLogMessage(message, level: level, flag: .Verbose, context: context, file: file, function: function, line: line, tag: tag, asynchronous: async, ddlog: ddlog)
  71. }
  72. public func DDLogError(@autoclosure message: () -> String, level: DDLogLevel = defaultDebugLevel, context: Int = 0, file: StaticString = #file, function: StaticString = #function, line: UInt = #line, tag: AnyObject? = nil, asynchronous async: Bool = false, ddlog: DDLog = DDLog.sharedInstance()) {
  73. _DDLogMessage(message, level: level, flag: .Error, context: context, file: file, function: function, line: line, tag: tag, asynchronous: async, ddlog: ddlog)
  74. }
  75. /// Returns a String of the current filename, without full path or extension.
  76. ///
  77. /// Analogous to the C preprocessor macro `THIS_FILE`.
  78. public func CurrentFileName(fileName: StaticString = #file) -> String {
  79. var str = fileName.stringValue
  80. if let idx = str.rangeOfString("/", options: .BackwardsSearch)?.endIndex {
  81. str = str.substringFromIndex(idx)
  82. }
  83. if let idx = str.rangeOfString(".", options: .BackwardsSearch)?.startIndex {
  84. str = str.substringToIndex(idx)
  85. }
  86. return str
  87. }