class Inprovise::Logger
Attributes
node[RW]
task[R]
Public Class Methods
new(node, task)
click to toggle source
# File lib/inprovise/logger.rb, line 21 def initialize(node, task) @node = node set_task(task) end
streams()
click to toggle source
# File lib/inprovise/logger.rb, line 13 def streams @streams ||= ::Hash.new.extend(::MonitorMixin).merge!({ :stdout => { :ios => $stdout, :buffer => [{col: nil, ln: '', cr: false}] }, :stderr => { :ios => $stderr, :buffer => [{col: nil, ln: '', cr: false}] } }) end
Public Instance Methods
cached(cmd)
click to toggle source
# File lib/inprovise/logger.rb, line 54 def cached(cmd) execute(cmd) end
clone_for_node(node)
click to toggle source
# File lib/inprovise/logger.rb, line 26 def clone_for_node(node) copy = self.dup copy.node = node copy end
command(msg)
click to toggle source
# File lib/inprovise/logger.rb, line 38 def command(msg) say(msg, :yellow) end
execute(cmd)
click to toggle source
# File lib/inprovise/logger.rb, line 46 def execute(cmd) say(cmd, :cyan) end
flush(stream=:stdout)
click to toggle source
# File lib/inprovise/logger.rb, line 144 def flush(stream=:stdout) synchronize do do_flush(stream) end self end
flush_all()
click to toggle source
# File lib/inprovise/logger.rb, line 151 def flush_all synchronize do [:stderr, :stdout].each { |stream| do_flush(stream) } end self end
local(cmd)
click to toggle source
# File lib/inprovise/logger.rb, line 42 def local(cmd) say(cmd, :bold) end
log(msg, color=nil)
click to toggle source
# File lib/inprovise/logger.rb, line 62 def log(msg, color=nil) say(msg, color) end
mock_execute(cmd)
click to toggle source
# File lib/inprovise/logger.rb, line 50 def mock_execute(cmd) execute(cmd) end
print(msg, color=nil, stream=:stdout)
click to toggle source
# File lib/inprovise/logger.rb, line 158 def print(msg, color=nil, stream=:stdout) synchronize do put(msg, color, stream) do_print(stream) end self end
println(msg, color=nil, stream=:stdout)
click to toggle source
# File lib/inprovise/logger.rb, line 166 def println(msg, color=nil, stream=:stdout) synchronize do puts(msg, color, stream) do_print(stream) end self end
remote(cmd)
click to toggle source
# File lib/inprovise/logger.rb, line 58 def remote(cmd) say(cmd, :blue) end
say(msg, color=nil, stream=:stdout)
click to toggle source
# File lib/inprovise/logger.rb, line 198 def say(msg, color=nil, stream=:stdout) synchronize do [:stderr, :stdout].each { |stream| do_flush(stream) } streambuf = buffer(stream) msg.to_s.scan(/([^\n]*)(\n\r|\n)?/) do |txt,sep| puts(txt) break unless sep end do_print(stream) end self end
set_task(task)
click to toggle source
# File lib/inprovise/logger.rb, line 32 def set_task(task) oldtask = @task @task = task.to_s oldtask end
stderr(msg, force=false)
click to toggle source
# File lib/inprovise/logger.rb, line 194 def stderr(msg, force=false) redirect(msg, :red, :stderr) if force || Inprovise.verbosity>0 end
stdout(msg, force=false)
click to toggle source
# File lib/inprovise/logger.rb, line 190 def stdout(msg, force=false) redirect(msg, :green, :stdout) if force || Inprovise.verbosity>0 end
Private Instance Methods
buffer(stream=:stdout)
click to toggle source
# File lib/inprovise/logger.rb, line 78 def buffer(stream=:stdout) self.class.streams[stream][:buffer] end
do_flush(stream)
click to toggle source
# File lib/inprovise/logger.rb, line 134 def do_flush(stream) lnbuf = buffer(stream).last unless lnbuf[:ln].empty? && !lnbuf[:cr] # add an empty line buffer to force output of current buffered contents buffer(stream) << {col:nil, ln:'',cr:false} do_print(stream) end end
do_print(stream=:stdout)
click to toggle source
# File lib/inprovise/logger.rb, line 107 def do_print(stream=:stdout) until buffer(stream).empty? col, ln, cr_at_start = next_line(stream) ln.scan(/([^\r]*)(\r)?/) do |txt, cr| nl = buffer(stream).size > 0 if cr || nl unless txt.empty? out = col ? txt.to_s.send(col) : txt ios(stream).print "\r".to_eol if cr_at_start ios(stream).print "#{@node} [#{@task.bold}] #{out}" end if cr ios(stream).flush else ios(stream).puts unless txt.empty? && !cr_at_start end cr_at_start = cr else # cache for later push_line(stream, txt.empty? ? nil : col, txt, cr_at_start) return # done printing for now end end end end
ios(stream=:stdout)
click to toggle source
# File lib/inprovise/logger.rb, line 73 def ios(stream=:stdout) self.class.streams[stream][:ios] end
next_line(stream)
click to toggle source
# File lib/inprovise/logger.rb, line 83 def next_line(stream) lnbuf = buffer(stream).shift || {} [lnbuf[:col], lnbuf[:ln], lnbuf[:cr]] end
push_line(stream, col, ln, cr)
click to toggle source
# File lib/inprovise/logger.rb, line 89 def push_line(stream, col, ln, cr) buffer(stream) << {col: col, ln: ln, cr: cr ? true : false} end
put(msg, color=nil, stream=:stdout)
click to toggle source
# File lib/inprovise/logger.rb, line 94 def put(msg, color=nil, stream=:stdout) streambuf = buffer(stream) streambuf.last[:col] ||= color streambuf.last[:ln] << msg streambuf end
puts(msg, color=nil, stream=:stdout)
click to toggle source
# File lib/inprovise/logger.rb, line 102 def puts(msg, color=nil, stream=:stdout) put(msg, color, stream) << {col:nil, ln:'',cr:false} end
redirect(msg, color, stream)
click to toggle source
# File lib/inprovise/logger.rb, line 174 def redirect(msg, color, stream) synchronize do msg.to_s.scan(/([^\n]*)(\n\r|\n)?/) do |txt,sep| if sep puts(txt, color, stream) else put(txt, color, stream) end break unless sep end do_print(stream) end self end
synchronize(&block)
click to toggle source
# File lib/inprovise/logger.rb, line 66 def synchronize(&block) self.class.streams.synchronize do block.call end if block_given? end