class SyncWrap::Formatter

Attributes

colorize[RW]
io[R]
lock[R]

Public Class Methods

new( io = $stdout ) click to toggle source
# File lib/syncwrap/formatter.rb, line 29
def initialize( io = $stdout )
  @io = io
  @lock = Mutex.new
  @colorize = true
  @newlined = true
  @backtraces = {}
end

Public Instance Methods

flush() click to toggle source
# File lib/syncwrap/formatter.rb, line 139
def flush
  io.flush
end
output_terminate() click to toggle source
# File lib/syncwrap/formatter.rb, line 93
def output_terminate
  unless @newlined
    io.puts
    @newlined = true
  end
end
short_cn( cls ) click to toggle source
# File lib/syncwrap/formatter.rb, line 143
def short_cn( cls )
  cls.name.sub(/^SyncWrap::/,'')
end
sync( &block ) click to toggle source
# File lib/syncwrap/formatter.rb, line 37
def sync( &block )
  @lock.synchronize( &block )
end
write_command_output( stream, buff, color = true ) click to toggle source
# File lib/syncwrap/formatter.rb, line 100
def write_command_output( stream, buff, color = true )
  unless buff.empty?
    if stream == :err && colorize && color
      io << red << buff << clear
    else
      io << buff
    end
    @newlined = ( buff[-1] == "\n" )
  end
end
write_command_outputs( outputs, color = true ) click to toggle source
# File lib/syncwrap/formatter.rb, line 85
def write_command_outputs( outputs, color = true )
  outputs.each do |stream, buff|
    write_command_output( stream, buff, color )
  end
  output_terminate
  flush
end
write_component( host, comp, mth, state ) click to toggle source
# File lib/syncwrap/formatter.rb, line 41
def write_component( host, comp, mth, state )
  io << yellow if colorize
  io << '== ' << host.name << ' ' << comp.class << '#' << mth
  io << ': ' << state
  io << clear if colorize
  io << "\n"
  flush
end
write_error( host, error, comp = nil, mth = nil ) click to toggle source
# File lib/syncwrap/formatter.rb, line 111
def write_error( host, error, comp = nil, mth = nil )
  bt = error.backtrace
  bt_num = @backtraces[ bt ]
  if bt_num
    bt = nil
  else
    @backtraces[ bt ] = bt_num = @backtraces.length + 1
  end

  io << yellow if colorize
  io << '== ' << host.name << ' '
  io << comp.class << '#' << mth << ' ' if comp && mth
  io << "error"
  io << ", same stack as" unless bt
  io << " [" << bt_num << "]:\n"

  io << red if colorize
  io << short_cn( error.class ) << ': ' << error.message << "\n"
  if bt
    bt.each do |line|
      break if line =~ /execute_component'$/
      io.puts line
    end
  end
  io << clear if colorize
  flush
end
write_header( host, mode, opts, live = false ) click to toggle source
# File lib/syncwrap/formatter.rb, line 50
def write_header( host, mode, opts, live = false )
  olist = []
  olist << "-#{opts[:sh_verbose]}" if opts[:sh_verbose] && mode != :rsync
  olist << 'coalesce' if opts[:coalesce]
  olist << 'dryrun' if opts[:dryrun]
  olist << "accept:#{opts[:accept].join ','}" if opts[:accept]
  olist << "user:#{opts[:user]}" if opts[:user]
  olist << 'live' if live

  io << yellow if colorize
  io << '<-- ' << mode << ' ' << host.name
  first = true
  olist.each do |li|
    if first
      io << ' ('
      first = false
    else
      io << ' '
    end
    io << li
  end
  io << ')' unless olist.empty?
  io << clear if colorize
  io << "\n"
  flush
end
write_result( result ) click to toggle source
# File lib/syncwrap/formatter.rb, line 77
def write_result( result )
  io << yellow if colorize
  io << '--> ' << result
  io << clear if colorize
  io << "\n"
  flush
end