ConsoleDestination.swift 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. //
  2. // ConsoleDestination.swift
  3. // SwiftyBeaver
  4. //
  5. // Created by Sebastian Kreutzberger on 05.12.15.
  6. // Copyright © 2015 Sebastian Kreutzberger
  7. // Some rights reserved: http://opensource.org/licenses/MIT
  8. //
  9. import Foundation
  10. public class ConsoleDestination: BaseDestination {
  11. /// use NSLog instead of print, default is false
  12. public var useNSLog = false
  13. /// uses colors compatible to Terminal instead of Xcode, default is false
  14. public var useTerminalColors: Bool = false {
  15. didSet {
  16. if useTerminalColors {
  17. // use Terminal colors
  18. reset = "\u{001b}[0m"
  19. escape = "\u{001b}[38;5;"
  20. levelColor.verbose = "251m" // silver
  21. levelColor.debug = "35m" // green
  22. levelColor.info = "38m" // blue
  23. levelColor.warning = "178m" // yellow
  24. levelColor.error = "197m" // red
  25. } else {
  26. // use colored Emojis for better visual distinction
  27. // of log level for Xcode 8
  28. levelColor.verbose = "💜 " // silver
  29. levelColor.debug = "💚 " // green
  30. levelColor.info = "💙 " // blue
  31. levelColor.warning = "💛 " // yellow
  32. levelColor.error = "❤️ " // red
  33. }
  34. }
  35. }
  36. override public var defaultHashValue: Int { return 1 }
  37. public override init() {
  38. super.init()
  39. levelColor.verbose = "💜 " // silver
  40. levelColor.debug = "💚 " // green
  41. levelColor.info = "💙 " // blue
  42. levelColor.warning = "💛 " // yellow
  43. levelColor.error = "❤️ " // red
  44. }
  45. // print to Xcode Console. uses full base class functionality
  46. override public func send(_ level: SwiftyBeaver.Level, msg: String, thread: String,
  47. file: String, function: String, line: Int, context: Any? = nil) -> String? {
  48. let formattedString = super.send(level, msg: msg, thread: thread, file: file, function: function, line: line, context: context)
  49. if let str = formattedString {
  50. if useNSLog {
  51. #if os(Linux)
  52. print(str)
  53. #else
  54. NSLog("%@", str)
  55. #endif
  56. } else {
  57. print(str)
  58. }
  59. }
  60. return formattedString
  61. }
  62. }