Class: Pwrake::Writer

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/pwrake/worker/writer.rb

Instance Method Summary collapse

Constructor Details

#initializeWriter

Returns a new instance of Writer.



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/pwrake/worker/writer.rb', line 8

def initialize
  @out = $stdout
  @mutex = Mutex.new
  pipe_in, pipe_out = IO.pipe
  Thread.new(pipe_in,"log:") do |pin,pre|
    while s = pin.gets
      s.chomp!
      @out.puts pre+s
    end
  end
  $stderr = pipe_out
end

Instance Method Details

#add_logger(log) ⇒ Object



21
22
23
# File 'lib/pwrake/worker/writer.rb', line 21

def add_logger(log)
  @log = log
end

#dputs(s) ⇒ Object



54
55
56
# File 'lib/pwrake/worker/writer.rb', line 54

def dputs(s)
  puts(s) if $DEBUG
end

#flushObject



47
48
49
50
51
52
# File 'lib/pwrake/worker/writer.rb', line 47

def flush
  begin
    @out.flush
  rescue
  end
end


36
37
38
39
40
41
42
43
44
45
# File 'lib/pwrake/worker/writer.rb', line 36

def print(s)
  begin
    @mutex.synchronize do
      @out.print s
    end
    @out.flush
  rescue Errno::EPIPE
  end
  @log.info "<#{s}" if @log
end

#puts(s) ⇒ Object



25
26
27
28
29
30
31
32
33
34
# File 'lib/pwrake/worker/writer.rb', line 25

def puts(s)
  begin
    @mutex.synchronize do
      @out.print s+"\n"
    end
    @out.flush
  rescue Errno::EPIPE
  end
  @log.info "<#{s}" if @log
end