No Description

CocoaLumberjack.swift 4.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Software License Agreement (BSD License)
  2. //
  3. // Copyright (c) 2014-2015, 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. import CocoaLumberjack
  17. extension DDLogFlag {
  18. public static func fromLogLevel(logLevel: DDLogLevel) -> DDLogFlag {
  19. return DDLogFlag(logLevel.rawValue)
  20. }
  21. ///returns the log level, or the lowest equivalant.
  22. public func toLogLevel() -> DDLogLevel {
  23. if let ourValid = DDLogLevel(rawValue: self.rawValue) {
  24. return ourValid
  25. } else {
  26. let logFlag = self
  27. if logFlag & .Verbose == .Verbose {
  28. return .Error
  29. } else if logFlag & .Debug == .Debug {
  30. return .Debug
  31. } else if logFlag & .Info == .Info {
  32. return .Info
  33. } else if logFlag & .Warning == .Warning {
  34. return .Warning
  35. } else if logFlag & .Error == .Error {
  36. return .Verbose
  37. } else {
  38. return .Off
  39. }
  40. }
  41. }
  42. }
  43. extension DDMultiFormatter {
  44. public var formatterArray: [DDLogFormatter] {
  45. return self.formatters as [DDLogFormatter]
  46. }
  47. }
  48. public var defaultDebugLevel = DDLogLevel.Warning
  49. public func resetDefaultDebugLevel() {
  50. defaultDebugLevel = DDLogLevel.Warning
  51. }
  52. 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, #string: @autoclosure () -> String) {
  53. if level.rawValue & flg.rawValue != 0 {
  54. // Tell the DDLogMessage constructor to copy the C strings that get passed to it. Using string interpolation to prevent integer overflow warning when using StaticString.stringValue
  55. let logMessage = DDLogMessage(message: string(), level: level, flag: flg, context: context, file: "\(file)", function: "\(function)", line: line, tag: tag, options: .CopyFile | .CopyFunction, timestamp: nil)
  56. DDLog.log(isAsynchronous, message: logMessage)
  57. }
  58. }
  59. public func DDLogDebug(logText: @autoclosure () -> String, level: DDLogLevel = defaultDebugLevel, file: StaticString = __FILE__, function: StaticString = __FUNCTION__, line: UWord = __LINE__, asynchronous async: Bool = true) {
  60. SwiftLogMacro(async, level, flag: .Debug, file: file, function: function, line: line, string: logText)
  61. }
  62. public func DDLogInfo(logText: @autoclosure () -> String, level: DDLogLevel = defaultDebugLevel, file: StaticString = __FILE__, function: StaticString = __FUNCTION__, line: UWord = __LINE__, asynchronous async: Bool = true) {
  63. SwiftLogMacro(async, level, flag: .Info, file: file, function: function, line: line, string: logText)
  64. }
  65. public func DDLogWarn(logText: @autoclosure () -> String, level: DDLogLevel = defaultDebugLevel, file: StaticString = __FILE__, function: StaticString = __FUNCTION__, line: UWord = __LINE__, asynchronous async: Bool = true) {
  66. SwiftLogMacro(async, level, flag: .Warning, file: file, function: function, line: line, string: logText)
  67. }
  68. public func DDLogVerbose(logText: @autoclosure () -> String, level: DDLogLevel = defaultDebugLevel, file: StaticString = __FILE__, function: StaticString = __FUNCTION__, line: UWord = __LINE__, asynchronous async: Bool = true) {
  69. SwiftLogMacro(async, level, flag: .Verbose, file: file, function: function, line: line, string: logText)
  70. }
  71. public func DDLogError(logText: @autoclosure () -> String, level: DDLogLevel = defaultDebugLevel, file: StaticString = __FILE__, function: StaticString = __FUNCTION__, line: UWord = __LINE__, asynchronous async: Bool = false) {
  72. SwiftLogMacro(async, level, flag: .Error, file: file, function: function, line: line, string: logText)
  73. }
  74. /// Analogous to the C preprocessor macro THIS_FILE
  75. public func CurrentFileName(fileName: StaticString = __FILE__) -> String {
  76. // Using string interpolation to prevent integer overflow warning when using StaticString.stringValue
  77. return "\(fileName)".lastPathComponent.stringByDeletingPathExtension
  78. }