-
Notifications
You must be signed in to change notification settings - Fork 17
/
tail.rb
38 lines (32 loc) · 693 Bytes
/
tail.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/usr/bin/env ruby
#
# Simple 'tail -f' example.
# Usage example:
# tail.rb /var/log/messages
require "rubygems"
require "eventmachine"
require "eventmachine-tail"
class Reader < EventMachine::FileTail
def initialize(path, startpos=-1)
super(path, startpos)
puts "Tailing #{path}"
@buffer = BufferedTokenizer.new
end
def receive_data(data)
@buffer.extract(data).each do |line|
puts "#{path}: #{line}"
end
end
end
def main(args)
if args.length == 0
puts "Usage: #{$0} <path> [path2] [...]"
return 1
end
EventMachine.run do
args.each do |path|
EventMachine::file_tail(path, Reader)
end
end
end # def main
exit(main(ARGV))