Class: Ferrum::Log::Annotated

Inherits:
Object
  • Object
show all
Includes:
Paint
Defined in:
lib/ferrum/log/annotated.rb,
sig/ferrum/log/annotated.rbs

Overview

Formats CDP messages one line each, the JSON whole after a header: the direction, the elapsed time, the command id and method. Received messages are indented under the commands sent. A response also names the method of its command and how long the answer took. Picked with FERRUM_LOG_FORMAT=annotated.

Constant Summary

Constants included from Paint

Paint::COLORS

Instance Method Summary collapse

Methods included from Paint

#color?

Constructor Details

#initialize(color: false) ⇒ Annotated

Returns a new instance of Annotated.

Parameters:

  • color: (Boolean) (defaults to: false)


17
18
19
# File 'lib/ferrum/log/annotated.rb', line 17

def initialize(color: false)
  @color = color
end

Instance Method Details

#received(message, raw, elapsed, command = nil) ⇒ String

Parameters:

  • message (Hash{String => Object}, nil)
  • raw (String)
  • elapsed (Float)
  • command (Array(String, Float), nil) (defaults to: nil) —

    The method of the command this message answers and how long the answer took, in seconds.

Returns:

  • (String)


42
43
44
45
46
47
48
49
50
51
# File 'lib/ferrum/log/annotated.rb', line 42

def received(message, raw, elapsed, command = nil)
  return line("   ", paint("◀", :magenta), time(elapsed), raw) unless message
  return event(message, elapsed) unless message.key?("id")

  method, duration = command
  error = message["error"]
  line("   ", paint("◀", error ? :red : :green), time(elapsed), "##{message['id']}", paint(method, :bold),
       duration && "#{(duration * 1000).round}ms", json(error || message["result"]),
       session(message["sessionId"]))
end

#sent(message, elapsed) ⇒ String

Parameters:

  • message (Hash{Symbol => Object})
  • elapsed (Float)

Returns:

  • (String)


26
27
28
29
# File 'lib/ferrum/log/annotated.rb', line 26

def sent(message, elapsed)
  line(paint("▶", :cyan), time(elapsed), "##{message[:id]}", paint(message[:method], :bold),
       json(message[:params]), session(message[:sessionId]))
end