CocoaLumberjack.swift 5.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 from(_ 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: rawValue) {
  26. return ourValid
  27. } else {
  28. if contains(.verbose) {
  29. return .verbose
  30. } else if contains(.debug) {
  31. return .debug
  32. } else if contains(.info) {
  33. return .info
  34. } else if contains(.warning) {
  35. return .warning
  36. } else if contains(.error) {
  37. return .error
  38. } else {
  39. return .off
  40. }
  41. }
  42. }
  43. }
  44. public var defaultDebugLevel = DDLogLevel.verbose
  45. public func resetDefaultDebugLevel() {
  46. defaultDebugLevel = DDLogLevel.verbose
  47. }
  48. public func _DDLogMessage(_ message: @autoclosure () -> String, level: DDLogLevel, flag: DDLogFlag, context: Int, file: StaticString, function: StaticString, line: UInt, tag: Any?, asynchronous: Bool, ddlog: DDLog) {
  49. if level.rawValue & flag.rawValue != 0 {
  50. // Tell the DDLogMessage constructor to copy the C strings that get passed to it.
  51. let logMessage = DDLogMessage(message: message(), level: level, flag: flag, context: context, file: String(describing: file), function: String(describing: function), line: line, tag: tag, options: [.copyFile, .copyFunction], timestamp: nil)
  52. ddlog.log(asynchronous: asynchronous, message: logMessage)
  53. }
  54. }
  55. public func DDLogDebug(_ message: @autoclosure () -> String, level: DDLogLevel = defaultDebugLevel, context: Int = 0, file: StaticString = #file, function: StaticString = #function, line: UInt = #line, tag: Any? = nil, asynchronous async: Bool = true, ddlog: DDLog = DDLog.sharedInstance) {
  56. _DDLogMessage(message, level: level, flag: .debug, context: context, file: file, function: function, line: line, tag: tag, asynchronous: async, ddlog: ddlog)
  57. }
  58. public func DDLogInfo(_ message: @autoclosure () -> String, level: DDLogLevel = defaultDebugLevel, context: Int = 0, file: StaticString = #file, function: StaticString = #function, line: UInt = #line, tag: Any? = nil, asynchronous async: Bool = true, ddlog: DDLog = DDLog.sharedInstance) {
  59. _DDLogMessage(message, level: level, flag: .info, context: context, file: file, function: function, line: line, tag: tag, asynchronous: async, ddlog: ddlog)
  60. }
  61. public func DDLogWarn(_ message: @autoclosure () -> String, level: DDLogLevel = defaultDebugLevel, context: Int = 0, file: StaticString = #file, function: StaticString = #function, line: UInt = #line, tag: Any? = nil, asynchronous async: Bool = true, ddlog: DDLog = DDLog.sharedInstance) {
  62. _DDLogMessage(message, level: level, flag: .warning, context: context, file: file, function: function, line: line, tag: tag, asynchronous: async, ddlog: ddlog)
  63. }
  64. public func DDLogVerbose(_ message: @autoclosure () -> String, level: DDLogLevel = defaultDebugLevel, context: Int = 0, file: StaticString = #file, function: StaticString = #function, line: UInt = #line, tag: Any? = nil, asynchronous async: Bool = true, ddlog: DDLog = DDLog.sharedInstance) {
  65. _DDLogMessage(message, level: level, flag: .verbose, context: context, file: file, function: function, line: line, tag: tag, asynchronous: async, ddlog: ddlog)
  66. }
  67. public func DDLogError(_ message: @autoclosure () -> String, level: DDLogLevel = defaultDebugLevel, context: Int = 0, file: StaticString = #file, function: StaticString = #function, line: UInt = #line, tag: Any? = nil, asynchronous async: Bool = false, ddlog: DDLog = DDLog.sharedInstance) {
  68. _DDLogMessage(message, level: level, flag: .error, context: context, file: file, function: function, line: line, tag: tag, asynchronous: async, ddlog: ddlog)
  69. }
  70. /// Returns a String of the current filename, without full path or extension.
  71. ///
  72. /// Analogous to the C preprocessor macro `THIS_FILE`.
  73. public func CurrentFileName(_ fileName: StaticString = #file) -> String {
  74. var str = String(describing: fileName)
  75. if let idx = str.range(of: "/", options: .backwards)?.upperBound {
  76. str = String(str[idx...])
  77. }
  78. if let idx = str.range(of: ".", options: .backwards)?.lowerBound {
  79. str = String(str[..<idx])
  80. }
  81. return str
  82. }