Skip to content

Instantly share code, notes, and snippets.

@tkojitu
Created May 25, 2017 08:09
Show Gist options
  • Save tkojitu/fff9fc63ac546d53a8c2116ea44fab5e to your computer and use it in GitHub Desktop.
Save tkojitu/fff9fc63ac546d53a8c2116ea44fab5e to your computer and use it in GitHub Desktop.
class Mtail
def initialize(dirname)
@dirname = dirname
@filename = nil
@input = nil
end
def tail
Encoding.default_external = "ASCII-8BIT"
Encoding.default_internal = "ASCII-8BIT"
Dir.chdir(@dirname) do
while true
latest = find_latest_file
if latest != @filename
@input && @input.close
@filename = latest
@input = File.open(@filename)
end
while (ch = @input.getc)
$stdout.putc(ch)
end
sleep(1)
end
end
@input && @input.close
@input = nil
end
def find_latest_file
latest = nil
Dir.foreach(".") do |filename|
next if !File.file?(filename)
if !latest
latest = filename
next
end
if File.mtime(latest) < File.mtime(filename)
latest = filename
end
end
return latest
end
end
if $0 == __FILE__
Mtail.new(ARGV[0]).tail
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment