Module: OMQ::CLI::Term

Defined in:
lib/omq/cli/term.rb

Overview

Stateless terminal formatting and stderr writing helpers shared by every code path that emits verbose-driven log lines (event monitor callbacks in BaseRunner / PipeRunner, SocketSetup attach helpers, parallel/pipe Ractor workers).

Pure module functions: no state, no instance, safe to call from any thread or Ractor. Errors and abort messages do not go through this module — they aren’t logs.

Class Method Summary collapse

Class Method Details

.format_attach(kind, url, timestamps) ⇒ String

Formats an “attached endpoint” log line (bound to / connecting to).

Parameters:

  • kind (:bind, :connect)
  • url (String)
  • timestamps (Symbol, nil)

Returns:

  • (String)


90
91
92
93
# File 'lib/omq/cli/term.rb', line 90

def format_attach(kind, url, timestamps)
  verb = kind == :bind ? "bound to" : "connecting to"
  "#{log_prefix(timestamps)}omq: #{verb} #{url}"
end

.format_event(event, timestamps) ⇒ String

Formats one OMQ::MonitorEvent into a single log line (no trailing newline).

Parameters:

  • event (OMQ::MonitorEvent)
  • timestamps (Symbol, nil)

Returns:

  • (String)


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/omq/cli/term.rb', line 39

def format_event(event, timestamps)
  prefix = log_prefix(timestamps)
  case event.type
  when :message_sent
    "#{prefix}omq: >> #{Formatter.preview(event.detail[:parts], wire_size: event.detail[:wire_size])}"
  when :message_received
    "#{prefix}omq: << #{Formatter.preview(event.detail[:parts], wire_size: event.detail[:wire_size])}"
  when :zdict_sent
    "#{prefix}omq: >> ZDICT (#{event.detail[:size]}B)"
  when :zdict_received
    "#{prefix}omq: << ZDICT (#{event.detail[:size]}B)"
  else
    ep     = event.endpoint ? " #{event.endpoint}" : ""
    detail = format_event_detail(event.detail)
    "#{prefix}omq: #{event.type}#{ep}#{detail}"
  end
end

.format_event_detail(detail) ⇒ String

Renders MonitorEvent#detail as a “ (…)” suffix for log lines. Rewrites plain peer-close exceptions (EOFError) to “closed by peer” — the io-stream library reports these as “Stream finished before reading enough data!”, which is confusing noise for what is just a normal disconnect.

Parameters:

  • detail (Hash, Object, nil)

Returns:

  • (String)


66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/omq/cli/term.rb', line 66

def format_event_detail(detail)
  return "" if detail.nil?
  return " #{detail}" unless detail.is_a?(Hash)

  error = detail[:error]
  reason = detail[:reason]

  case error
  when nil
    reason ? " (#{reason})" : ""
  when EOFError
    " (closed by peer)"
  else
    " (#{reason || error.message})"
  end
end

.log_prefix(timestamps) ⇒ String

Returns a stderr log line prefix with a UTC ISO8601 timestamp at the requested precision (:s/:ms/:us), or “” when nil.

Parameters:

  • timestamps (Symbol, nil)

    :s, :ms, :us, or nil (disabled)

Returns:

  • (String)


23
24
25
26
27
28
29
30
# File 'lib/omq/cli/term.rb', line 23

def log_prefix(timestamps)
  case timestamps
  when nil then ""
  when :s  then "#{Time.now.utc.strftime("%FT%T")}Z "
  when :ms then "#{Time.now.utc.strftime("%FT%T.%3N")}Z "
  when :us then "#{Time.now.utc.strftime("%FT%T.%6N")}Z "
  end
end

.write_attach(kind, url, timestamps, io: $stderr) ⇒ void

This method returns an undefined value.

Writes one “bound to / connecting to” line to io (default $stderr).

Parameters:

  • kind (:bind, :connect)
  • url (String)
  • timestamps (Symbol, nil)
  • io (#write) (defaults to: $stderr)


115
116
117
# File 'lib/omq/cli/term.rb', line 115

def write_attach(kind, url, timestamps, io: $stderr)
  io.write("#{format_attach(kind, url, timestamps)}\n")
end

.write_event(event, timestamps, io: $stderr) ⇒ void

This method returns an undefined value.

Writes one formatted event line to io (default $stderr).

Parameters:

  • event (OMQ::MonitorEvent)
  • timestamps (Symbol, nil)
  • io (#write) (defaults to: $stderr)

    writable sink, default $stderr



102
103
104
# File 'lib/omq/cli/term.rb', line 102

def write_event(event, timestamps, io: $stderr)
  io.write("#{format_event(event, timestamps)}\n")
end