lib/multi_process/logger.rb in multi_process-0.5.2 vs lib/multi_process/logger.rb in multi_process-1.0.0
- old
+ new
@@ -14,10 +14,12 @@
def initialize(*args)
@opts = Hash === args.last ? args.pop : {}
@out = args[0] || $stdout
@err = args[1] || $stderr
+ @colwidth = 0
+
super()
end
protected
@@ -30,10 +32,14 @@
when :sys
output(process, line, delimiter: '$>') if @opts[:sys]
end
end
+ def connected(process, _)
+ @colwidth = [process.title.to_s.length, @colwidth].max
+ end
+
def read(pipe)
pipe.gets
end
def collapse?
@@ -41,29 +47,30 @@
end
private
def output(process, line, opts = {})
- @mutex.synchronize do
- opts[:delimiter] ||= ' |'
- name = if opts[:name]
- opts[:name].to_s.dup
+ opts[:delimiter] ||= ' |'
+ name = if opts[:name]
+ opts[:name].to_s.dup
+ else
+ if process
+ process.title.to_s.rjust(@colwidth, ' ')
else
- max = @readers.values.map { |h| h[:process] ? h[:process].title.length : 0 }.max
- process ? process.title.to_s.rjust(max, ' ') : (' ' * max)
- end
+ (' ' * @colwidth)
+ end
+ end
- io = opts[:io] || @out
- if @last_name == name && collapse?
- io.print " #{' ' * name.length} #{opts[:delimiter]} "
- else
- io.print " #{name} #{opts[:delimiter]} "
- end
- io.puts line
- io.flush
-
- @last_name = name
+ io = opts[:io] || @out
+ if @last_name == name && collapse?
+ io.print " #{' ' * name.length} #{opts[:delimiter]} "
+ else
+ io.print " #{name} #{opts[:delimiter]} "
end
+ io.puts line
+ io.flush
+
+ @last_name = name
end
class << self
def global
@global ||= new $stdout, $stderr