CocoaLumberjack.swift 5.0 KB

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. 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. 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) {
  50. if level.rawValue & flg.rawValue != 0 {
  51. // Tell the DDLogMessage constructor to copy the C strings that get passed to it.
  52. // Using string interpolation to prevent integer overflow warning when using StaticString.stringValue
  53. let logMessage = DDLogMessage(message: string(), level: level, flag: flg, context: context, file: "\(file)", function: "\(function)", line: line, tag: tag, options: [.CopyFile, .CopyFunction], timestamp: nil)
  54. DDLog.log(isAsynchronous, message: logMessage)
  55. }
  56. }
  57. public func DDLogDebug(@autoclosure logText: () -> String, level: DDLogLevel = defaultDebugLevel, context: Int = 0, file: StaticString = __FILE__, function: StaticString = __FUNCTION__, line: UInt = __LINE__, tag: AnyObject? = nil, asynchronous async: Bool = true) {
  58. SwiftLogMacro(async, level: level, flag: .Debug, context: context, file: file, function: function, line: line, tag: tag, string: logText)
  59. }
  60. public func DDLogInfo(@autoclosure logText: () -> String, level: DDLogLevel = defaultDebugLevel, context: Int = 0, file: StaticString = __FILE__, function: StaticString = __FUNCTION__, line: UInt = __LINE__, tag: AnyObject? = nil, asynchronous async: Bool = true) {
  61. SwiftLogMacro(async, level: level, flag: .Info, context: context, file: file, function: function, line: line, tag: tag, string: logText)
  62. }
  63. public func DDLogWarn(@autoclosure logText: () -> String, level: DDLogLevel = defaultDebugLevel, context: Int = 0, file: StaticString = __FILE__, function: StaticString = __FUNCTION__, line: UInt = __LINE__, tag: AnyObject? = nil, asynchronous async: Bool = true) {
  64. SwiftLogMacro(async, level: level, flag: .Warning, context: context, file: file, function: function, line: line, tag: tag, string: logText)
  65. }
  66. public func DDLogVerbose(@autoclosure logText: () -> String, level: DDLogLevel = defaultDebugLevel, context: Int = 0, file: StaticString = __FILE__, function: StaticString = __FUNCTION__, line: UInt = __LINE__, tag: AnyObject? = nil, asynchronous async: Bool = true) {
  67. SwiftLogMacro(async, level: level, flag: .Verbose, context: context, file: file, function: function, line: line, tag: tag, string: logText)
  68. }
  69. public func DDLogError(@autoclosure logText: () -> String, level: DDLogLevel = defaultDebugLevel, context: Int = 0, file: StaticString = __FILE__, function: StaticString = __FUNCTION__, line: UInt = __LINE__, tag: AnyObject? = nil, asynchronous async: Bool = false) {
  70. SwiftLogMacro(async, level: level, flag: .Error, context: context, file: file, function: function, line: line, tag: tag, string: logText)
  71. }
  72. /// Analogous to the C preprocessor macro `THIS_FILE`.
  73. public func CurrentFileName(fileName: StaticString = __FILE__) -> String {
  74. // Using string interpolation to prevent integer overflow warning when using StaticString.stringValue
  75. // This double-casting to NSString is necessary as changes to how Swift handles NSPathUtilities requres the string to be an NSString
  76. return (("\(fileName)" as NSString).lastPathComponent as NSString).stringByDeletingPathExtension
  77. }