Module: Whoop::Main

Defined in:
lib/whoop.rb

Constant Summary collapse

COLORS =
i[black red green yellow blue magenta cyan white light_black light_red light_green light_yellow light_blue light_magenta light_cyan light_white default].freeze
FORMATS =
i[plain pretty json sql].freeze
PATTERN =
"-"
COUNT =
80
INDENT =
"┆"
TOP_LINE_CHAR =
"┏"
BOTTOM_LINE_CHAR =
"┗"

Instance Method Summary collapse

Instance Method Details

#whoop(label = nil, pattern: PATTERN, count: COUNT, color: :default, format: :pretty, caller_depth: 0, explain: false, context: nil) ⇒ Object

Log the message to the logger

Parameters:

  • label (String) (defaults to: nil)

    (optional) - the label or object to log

  • pattern (String) (defaults to: PATTERN)
    • the pattern to use for the line (e.g. ‘-’)

  • count (Integer) (defaults to: COUNT)
    • the number of times to repeat the pattern per line (e.g. 80)

  • color (Symbol) (defaults to: :default)
    • the color to use for the line (e.g. :red)

  • format (Symbol) (defaults to: :pretty)
    • the format to use for the message (one of :json, :sql, :plain, :pretty (default))

  • caller_depth (Integer) (defaults to: 0)
    • the depth of the caller to use for the source (default: 0)

  • explain (Boolean) (defaults to: false)
    • whether to explain the SQL query (default: false)

  • context (Hash) (defaults to: nil)
    • Any additional context you’d like to include in the log



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/whoop.rb', line 47

def whoop(
  label = nil,
  pattern: PATTERN,
  count: COUNT,
  color: :default,
  format: :pretty,
  caller_depth: 0,
  explain: false,
  context: nil
)
  logger_method = detect_logger_method
  color_method = detect_color_method(color)
  formatter_method = detect_formatter_method(format, colorize: color.present?, explain: explain)

  line = pattern * count
  caller_path = clean_caller_path(caller[caller_depth])
  caller_path_line = [color_method.call(INDENT), "source:".colorize(:light_black).underline, caller_path].join(" ")
  timestamp_line = [color_method.call(INDENT), "timestamp:".colorize(:light_black).underline, Time.now].join(" ")

  context_lines =
    if context.is_a?(Hash) && context.keys.length > 0
      context.map do |k, v|
        [color_method.call(INDENT), "#{k}:".colorize(:light_black).underline, v].join(" ")
      end
    else
      []
    end

  if block_given?
    result = yield
    top_line =
      if label.present? && label.is_a?(String)
        wrapped_line(label.to_s, pattern: pattern, count: count)
      else
        pattern * count
      end
    result.tap do
      logger_method.call color_method.call "\n\n#{TOP_LINE_CHAR}#{top_line}"
      logger_method.call timestamp_line
      logger_method.call caller_path_line
      context_lines.each { |l| logger_method.call l }
      logger_method.call ""
      logger_method.call formatter_method.call(result)
      logger_method.call ""
      display_invalid_format_message(format, color_method, logger_method)
      logger_method.call color_method.call "#{BOTTOM_LINE_CHAR}#{line}\n\n"
    end
  else
    tap do
      logger_method.call color_method.call "\n\n#{TOP_LINE_CHAR}#{line}"
      logger_method.call timestamp_line
      logger_method.call caller_path_line
      context_lines.each { |l| logger_method.call l }
      logger_method.call ""
      logger_method.call formatter_method.call(label)
      logger_method.call ""
      display_invalid_format_message(format, color_method, logger_method)
      logger_method.call color_method.call "#{BOTTOM_LINE_CHAR}#{line}\n\n"
    end
  end
end