Class: Rubysh::Plugin::SelfTee

Inherits:
Object
  • Object
show all
Defined in:
lib/rubysh/plugin/self_tee.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename, fd_map) ⇒ SelfTee

Returns a new instance of SelfTee.



32
33
34
35
36
37
38
39
40
# File 'lib/rubysh/plugin/self_tee.rb', line 32

def initialize(filename, fd_map)
  @logfile = File.open(filename, 'a')
  fd_map.each do |fd, (read, write)|
    write.close
  end

  @readers = {}
  fd_map.map {|fd, (read, write)| @readers[read] = fd}
end

Class Method Details

.child_actions(logfile, fd_map) ⇒ Object



27
28
29
30
# File 'lib/rubysh/plugin/self_tee.rb', line 27

def self.child_actions(logfile, fd_map)
  runner = self.new(logfile, fd_map)
  runner.run
end

.parent_actions(fd_map) ⇒ Object



20
21
22
23
24
25
# File 'lib/rubysh/plugin/self_tee.rb', line 20

def self.parent_actions(fd_map)
  fd_map.each do |fd, (read, write)|
    read.close
    Rubysh::Util.dup2(write, fd)
  end
end

.start(logfile, fd_nums) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/rubysh/plugin/self_tee.rb', line 8

def self.start(logfile, fd_nums)
  fd_map = {}
  fd_nums.each do |fd_num|
    fd = IO.new(fd_num)
    fd.autoclose = false
    fd_map[fd] = IO.pipe
  end

  fork {child_actions(logfile, fd_map)}
  parent_actions(fd_map)
end

Instance Method Details

#format_line(fd, line) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/rubysh/plugin/self_tee.rb', line 42

def format_line(fd, line)
  now = Time.now
  now_fmt = now.strftime("%Y-%m-%d %H:%M:%S")
  ms_fmt = sprintf("%06d", now.usec)

  output = "[#{now_fmt}.#{ms_fmt}] #{fd.fileno}: #{line.inspect}"
  output << "\n" unless output.end_with?("\n")
  output
end

#runObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/rubysh/plugin/self_tee.rb', line 52

def run
  parallel_io = Rubysh::Subprocess::ParallelIO.new(@readers, [])
  parallel_io.on_read do |fd, data|
    next if data == Rubysh::Subprocess::ParallelIO::EOF

    formatted = format_line(fd, data)
    @logfile.write(formatted)
    @logfile.flush

    fd.write(data)
    fd.flush
  end
  parallel_io.run
end