Module: Polyrun::Log

Defined in:
lib/polyrun/log.rb

Overview

Swappable sinks for CLI and library output. Defaults match Kernel#warn (stderr) and puts/print (stdout).

Assign an IO, StringIO, Ruby Logger, or any object responding to puts, write, or warn (Logger).

Polyrun::Log.stderr = Logger.new($stderr)
Polyrun::Log.stdout = StringIO.new

Orchestration (orchestration_warn): worker timeout and SIGINT lines use the same sink as warn unless POLYRUN_ORCHESTRATION_STDERR=1 and stderr is not process $stderr (then the summary is copied to $stderr).

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.stderrObject



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

def stderr
  @stderr || $stderr
end

.stdoutObject



21
22
23
# File 'lib/polyrun/log.rb', line 21

def stdout
  @stdout || $stdout
end

Class Method Details

.orchestration_warn(msg) ⇒ Object

Like #warn, and when POLYRUN_ORCHESTRATION_STDERR=1 and #stderr is not the process $stderr, also writes one line to $stderr so timeout/interrupt attribution survives custom/null Log sinks.



33
34
35
36
37
38
39
40
41
42
# File 'lib/polyrun/log.rb', line 33

def orchestration_warn(msg)
  warn(msg)
  return unless %w[1 true yes].include?(ENV["POLYRUN_ORCHESTRATION_STDERR"]&.downcase)
  return if stderr.equal?($stderr)

  # Intentionally the real stderr stream (+Kernel#warn+ routes through +Log.stderr+).
  # rubocop:disable Style/StderrPuts
  $stderr.puts(msg.to_s.chomp)
  # rubocop:enable Style/StderrPuts
end


52
53
54
55
56
57
58
59
# File 'lib/polyrun/log.rb', line 52

def print(msg = "")
  io = stdout
  if io.respond_to?(:write)
    io.write(msg.to_s)
  elsif io.respond_to?(:print)
    io.print(msg.to_s)
  end
end

.puts(msg = "") ⇒ Object



44
45
46
47
48
49
50
# File 'lib/polyrun/log.rb', line 44

def puts(msg = "")
  if msg.nil?
    stdout.write("\n")
  else
    emit_line(stdout, msg)
  end
end

.reset_io!Object

Clears custom sinks so stderr / stdout resolve to the current global $stderr / $stdout (e.g. after tests).



62
63
64
65
# File 'lib/polyrun/log.rb', line 62

def reset_io!
  @stderr = nil
  @stdout = nil
end

.warn(msg = nil) ⇒ Object



25
26
27
28
29
# File 'lib/polyrun/log.rb', line 25

def warn(msg = nil)
  return if msg.nil?

  emit_line(stderr, msg)
end