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.

45 lines
1.2 KiB

  1. module Semantic
  2. # Abstract colorized formatter
  3. class AbstractFormatter < SemanticLogger::Formatters::Color
  4. include AnsiColors
  5. private
  6. def exception
  7. return unless log.exception
  8. if log.exception.is_a?(Semantic::TagWrapError)
  9. exc_wrapper = log.exception
  10. # puts "TAG_WRAP detected #{exc_wrapper.tag}"
  11. exc = exc_wrapper.error
  12. log.tags = Array.wrap(exc_wrapper.tag)
  13. else
  14. exc = log.exception
  15. end
  16. clazz = colorize("#{exc.class}\n", color_map[:fatal])
  17. message = colorize(exc.message.chomp(''), color_map[:error])
  18. # TODO: backtrace_cleaner might be optionally disable from Live::Constant
  19. backtrace = stackisize(Rails.backtrace_cleaner.clean(exc.backtrace))
  20. "#{clazz}#{message}#{backtrace}"
  21. end
  22. def ansi_trace(trace, symbol)
  23. match = trace.match(/(↳ )?(.*:\d+)(:in `)?(.*'?)/) # only m2(=file) and m4(=optional function) are useful
  24. return trace unless match
  25. _, m2, _, m4 = match.captures
  26. "#{symbol} #{m2} #{BOLD}#{m4.chop}#{CLEAR}"
  27. end
  28. def stackisize(items)
  29. return '' if items.empty?
  30. traces = items.map { |item| ansi_trace(item, '➟') }
  31. "\n#{traces.join("\n")}"
  32. end
  33. end
  34. end