Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 

91 rinda
4.8 KiB

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