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

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.stderrObject



14
15
16
# File 'lib/polyrun/log.rb', line 14

def stderr
  @stderr || $stderr
end

.stdoutObject



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

def stdout
  @stdout || $stdout
end

Class Method Details



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

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



28
29
30
31
32
33
34
# File 'lib/polyrun/log.rb', line 28

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).



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

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

.warn(msg = nil) ⇒ Object



22
23
24
25
26
# File 'lib/polyrun/log.rb', line 22

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

  emit_line(stderr, msg)
end