User Tools

Site Tools


Watch log

sinatra-app.rb
#!/usr/bin/env ruby
 
require 'sinatra/base'
require 'thin'
 
class Tail
 
    def initialize(file)
        @file = file
    end
 
    def first
        f = File.open(@file,"r")
        f.seek(-500, :END)
        f.readline
        res = String.new
        f.each do |line|
            line.chomp!
            line += '<br/>'
            line += "\n"
            res += line
        end
        @pos = f.tell
        f.close
        res
    end
 
    def last
        f = File.open(@file, "r")
        f.seek(@pos)
        res = String.new
        f.each do |line| 
            line.chomp!
            line += '<br/>'
            line += "\n"
            res += line
        end
        @pos = f.tell
        f.close
        res
    end
    def pos
        @pos
    end
    def pos=(pos)
        @pos = pos
    end
end
 
class App < Sinatra::Base
...
    get '/tail' do
        la = true unless params['next']
        la = false if params['next']
        erb :tail, :layout => la
    end
...
 
end

tail.erb

templ/tail.erb
<%# $Id$ %>
<% t = Tail.new("/var/log/debug.log") %>
 
<% unless params['next'] then %>
    <div class="callout">
        <div id="log">
            <%= t.first %>
            <% session['logpos'] = t.pos %>
        </div>
    </div>
 
    <div class="text-center">
        <div id="clear" class="button">Clear</div>
        <div id="pause" class="button">Pause</div>
    </div>
 
    <script>
        function aTail () { 
            $.get("", { next : "yes" },
                    function (data) {
                        $("#log").append(data);
                        $("#log").animate({
                            bottom : "-=100em",
                        });
            });
        };
 
        $("#clear").click(function() {
            $("#log").empty();
        });
 
        var tailRun = 1;
        var timerId = setInterval(function(){ aTail(); }, 1000);
 
        $("#pause").click(function() {
            if (tailRun == 1) {
                clearInterval(timerId);
                $("#pause").html("Pause");
                $("#pause").toggleClass("secondary");
                tailRun = 0;
            } else {
                timerId = setInterval(function() {aTail()}, 1000);
                $("#pause").html("Pause");
                $("#pause").toggleClass("secondary");
                tailRun = 1;
            }
        });
    </script>
<% end %>
 
<% if params['next'] then %>
    <% t.pos =  session['logpos'] %>
        <%= t.last %>
        <% session['logpos'] = t.pos %>
<% end %>
<%# EOF %>

First PagePrevious PageBack to overviewNext PageLast Page