Class: Wip::DebugReporter

Inherits:
Object
  • Object
show all
Defined in:
lib/wip/debug_reporter.rb

Overview

Prints each step wip takes and how long it took, when debug mode is enabled.

Instance Method Summary collapse

Constructor Details

#initialize(enabled:, out: $stderr, log: nil) ⇒ DebugReporter

log: overrides where resource snapshots go, regardless of live:

nil  - automatic (see `step`'s `live:`)
'-'  - always print inline, even for interactive steps
path - always write to this file, even for non-interactive steps


12
13
14
15
16
# File 'lib/wip/debug_reporter.rb', line 12

def initialize(enabled:, out: $stderr, log: nil)
  @enabled = enabled
  @out = out
  @log = log
end

Instance Method Details

#step(label, live: true) ⇒ Object

live: false is for steps that hand the real TTY to the child process (e.g. wslc exec -it). The child owns raw-mode cursor control there, so writing periodic snapshots straight into that same terminal races with it and garbles the output; those snapshots go to a log file instead, unless overridden by log: in the constructor.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/wip/debug_reporter.rb', line 23

def step(label, live: true)
  return yield unless @enabled

  started = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  @out.puts "wip: [debug] #{label}"
  file = log_file(live)
  monitor = ResourceMonitor.new(out: file || @out)
  monitor.start(label)
  begin
    yield
  ensure
    monitor.stop
    file&.close
    elapsed = Process.clock_gettime(Process::CLOCK_MONOTONIC) - started
    @out.puts format('wip: [debug] done in %<elapsed>.2fs: %<label>s', elapsed: elapsed, label: label)
  end
end