Class: Bidi2pdf::Bidi::NetworkEventFormatters::NetworkEventConsoleFormatter

Inherits:
Object
  • Object
show all
Includes:
NetworkEventFormatterUtils
Defined in:
lib/bidi2pdf/bidi/network_event_formatters/network_event_console_formatter.rb

Overview

rubocop: disable Metrics/AbcSize

Constant Summary collapse

RESET =

ANSI styles

"\e[0m"
BOLD =
"\e[1m"
DIM =
"\e[2m"
RED =
"\e[31m"
GREEN =
"\e[32m"
YELLOW =
"\e[33m"
CYAN =
"\e[36m"
GRAY =
"\e[90m"

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from NetworkEventFormatterUtils

#format_bytes, #format_timestamp, #parse_timing, #shorten_url

Constructor Details

#initialize(color: true, logger: Bidi2pdf.network_events_logger) ⇒ NetworkEventConsoleFormatter

Returns a new instance of NetworkEventConsoleFormatter.



22
23
24
25
# File 'lib/bidi2pdf/bidi/network_event_formatters/network_event_console_formatter.rb', line 22

def initialize(color: true, logger: Bidi2pdf.network_events_logger)
  @color_enabled = color
  @logger = logger
end

Instance Attribute Details

#color_enabledObject (readonly)

Returns the value of attribute color_enabled.



10
11
12
# File 'lib/bidi2pdf/bidi/network_event_formatters/network_event_console_formatter.rb', line 10

def color_enabled
  @color_enabled
end

#loggerObject (readonly)

Returns the value of attribute logger.



10
11
12
# File 'lib/bidi2pdf/bidi/network_event_formatters/network_event_console_formatter.rb', line 10

def logger
  @logger
end

Instance Method Details

#log(events) ⇒ Object



27
28
29
# File 'lib/bidi2pdf/bidi/network_event_formatters/network_event_console_formatter.rb', line 27

def log(events)
  events.each { |event| pretty_log(event).each_line { |line| logger.info(line.chomp) } }
end

#pretty_log(event) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/bidi2pdf/bidi/network_event_formatters/network_event_console_formatter.rb', line 31

def pretty_log(event)
  status = event.http_status_code ? "HTTP #{event.http_status_code}" : "pending"
  status_color = color_for_status(event.http_status_code)
  start = event.format_timestamp(event.start_timestamp)
  finish = event.end_timestamp ? event.format_timestamp(event.end_timestamp) : dim("...")
  duration = event.duration_seconds ? cyan("#{event.duration_seconds}s") : dim("in progress")
  timing_details = format_timing(event)
  bytes = event.bytes_received ? format_bytes(event.bytes_received) : dim("N/A")
  displayed_url = shorten_url(event.url)

  <<~LOG.strip
    #{bold("┌─ Network Event ──────────────────────────────────────")}
    #{bold("│ Request: ")}#{event.http_method || "?"} #{displayed_url}#{"          "}
    #{bold("│ State:   ")}#{event.state}
    #{bold("│ Status:  ")}#{status_color}#{status}#{reset}
    #{bold("│ Started: ")}#{start}
    #{bold("│ Ended:   ")}#{finish}
    #{bold("│ Duration:")} #{duration}
    #{bold("│ Received:")} #{bytes}
    #{timing_details}
    #{bold("└──────────────────────────────────────────────────────")}
  LOG
end