Module: NNQ::CLI::Term
- Defined in:
- lib/nnq/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.
Class Method Summary collapse
-
.format_attach(kind, url, timestamps) ⇒ String
Formats an “attached endpoint” log line (Bound to / Connecting to).
-
.format_event(event, timestamps) ⇒ String
Formats one NNQ::MonitorEvent into a single log line (no trailing newline).
-
.format_event_detail(detail) ⇒ String
Renders MonitorEvent#detail as a suffix for log lines.
-
.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.
-
.write_attach(kind, url, timestamps, io: $stderr) ⇒ void
Writes one “Bound to / Connecting to” line to
io(default $stderr). -
.write_event(event, timestamps, io: $stderr) ⇒ void
Writes one formatted event line to
io(default $stderr).
Class Method Details
.format_attach(kind, url, timestamps) ⇒ String
Formats an “attached endpoint” log line (Bound to / Connecting to).
83 84 85 86 |
# File 'lib/nnq/cli/term.rb', line 83 def format_attach(kind, url, ) verb = kind == :bind ? "Bound to" : "Connecting to" "#{log_prefix()}nnq: #{verb} #{url}" end |
.format_event(event, timestamps) ⇒ String
Formats one NNQ::MonitorEvent into a single log line (no trailing newline).
38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/nnq/cli/term.rb', line 38 def format_event(event, ) prefix = log_prefix() case event.type when :message_sent "#{prefix}nnq: >> #{Formatter.preview(event.detail[:body])}" when :message_received "#{prefix}nnq: << #{Formatter.preview(event.detail[:body])}" else ep = event.endpoint ? " #{event.endpoint}" : "" detail = format_event_detail(event.detail) "#{prefix}nnq: #{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”.
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/nnq/cli/term.rb', line 59 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.})" 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.
22 23 24 25 26 27 28 29 |
# File 'lib/nnq/cli/term.rb', line 22 def log_prefix() case 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).
108 109 110 |
# File 'lib/nnq/cli/term.rb', line 108 def write_attach(kind, url, , io: $stderr) io.write("#{format_attach(kind, url, )}\n") end |
.write_event(event, timestamps, io: $stderr) ⇒ void
This method returns an undefined value.
Writes one formatted event line to io (default $stderr).
95 96 97 |
# File 'lib/nnq/cli/term.rb', line 95 def write_event(event, , io: $stderr) io.write("#{format_event(event, )}\n") end |