You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

67 lines
1.9 KiB

  1. module Semantic
  2. # Abstract colorized formatter
  3. class AbstractFormatter < SemanticLogger::Formatters::Color
  4. include AnsiColors
  5. TAG_NONE = ''.freeze
  6. CENTER_SIZE = 20
  7. TAG_FIXED_LENGTH = Rails.application.config.x.action_controller.main_session_tag_fixed_length || 10
  8. private
  9. def exception
  10. return unless log.exception
  11. if log.exception.is_a?(Semantic::TagWrapError)
  12. exc_wrapper = log.exception
  13. # puts "TAG_WRAP detected #{exc_wrapper.tag}"
  14. exc = exc_wrapper.error
  15. log.tags = Array.wrap(exc_wrapper.tag)
  16. else
  17. exc = log.exception
  18. end
  19. clazz = colorize("#{exc.class}\n", color_map[:fatal])
  20. message = colorize(exc.message.chomp(''), color_map[:error])
  21. # TODO: backtrace_cleaner might be optionally disable from Live::Constant
  22. backtrace = stackisize(Rails.backtrace_cleaner.clean(exc.backtrace))
  23. "#{clazz}#{message}#{backtrace}"
  24. end
  25. def origin
  26. true_class = log.name == log.name.upcase_first
  27. taint = true_class ? TEXT_CYAN : TEXT_GRAY_400
  28. colorize(centerize(log.name), taint)
  29. end
  30. def ansi_trace(trace, symbol)
  31. match = trace.match(/(↳ )?(.*:\d+)(:in `)?(.*'?)/) # only m2(=file) and m4(=optional function) are useful
  32. return trace unless match
  33. _, m2, _, m4 = match.captures
  34. "#{symbol} #{m2} #{BOLD}#{m4.chop}#{CLEAR}"
  35. end
  36. def stackisize(items)
  37. return '' if items.empty?
  38. traces = items.map { |item| ansi_trace(item, '➟') }
  39. "\n#{traces.join("\n")}"
  40. end
  41. def tags
  42. first_tag, taint = log.tags.empty? ? [TAG_NONE, CLEAR] : [log.tags.first, BG_GRAY + TEXT_YELLOW]
  43. colorize(centerize(first_tag, TAG_FIXED_LENGTH), taint)
  44. end
  45. def named_tags
  46. return if log.named_tags.empty?
  47. log.named_tags
  48. end
  49. def centerize(text, max_length = CENTER_SIZE) = text.reverse.truncate(max_length).reverse.center(max_length)
  50. end
  51. end