Class: LogFile::Display

Inherits:
Object
  • Object
show all
Defined in:
lib/log_file.rb

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Display

Returns a new instance of Display.



5
6
7
# File 'lib/log_file.rb', line 5

def initialize(app)
  @app=app 
end

Instance Method Details

#call(env = {}) ⇒ Object



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
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/log_file.rb', line 9

def call(env={})
  if env["PATH_INFO"]=~/^\/log_file/
    @log_name=((Rails.env=="development")?("log/development.log"):("log/production.log"))

    @lines=`wc -l <"#{ @log_name }"`.to_i

    if(env["QUERY_STRING"]=~/direction/ )
      if(env["QUERY_STRING"]=="direction=Next")
        count=@lines-env['rack.session'][:line_count]
        env['rack.session'][:line_count]=@lines
        env['rack.session'][:displayed_lines] += count
        data=`tail -n "#{ count }" "#{ @log_name }"`
        [200,{"Content-Type"=>"text/html"},[view_append(data)]]         
                  elsif(env["QUERY_STRING"]=="direction=Previous")
                    lines_added=@lines-env['rack.session'][:line_count]
                    bottom_stack=env['rack.session'][:displayed_lines]+lines_added
                    previous_lines=(@lines-bottom_stack)
                    if previous_lines>=30
          env['rack.session'][:displayed_lines] += 30 
          data=`head -n "#{ previous_lines }" "#{ @log_name }" | tail -30`
          [200,{"Content-Type"=>"text/html"},[view_append(data)]]
        elsif((previous_lines<30)&&(previous_lines>0))
            env['rack.session'][:displayed_lines] += previous_lines 
          data=`head -"#{ previous_lines }" "#{ @log_name }"`
          [200,{"Content-Type"=>"text/html"},[view_append(data)]]
        else
          [200,{"Content-Type"=>"text/html"},[""]]   
        end 
      end  
    else
              env['rack.session'][:line_count]=@lines
              if @lines>=30
        env['rack.session'][:displayed_lines]=count=30
      else
        env['rack.session'][:displayed_lines]=count=@lines
      end    
      data=`tail -"#{ count }" "#{ @log_name }"`
      [200,{"Content-Type"=>"text/html"},[view_generate(data)]]
    end 
  else
    @app.call(env)
  end
end

#file_open(name) ⇒ Object



65
66
67
# File 'lib/log_file.rb', line 65

def file_open(name)
          File.open(File.expand_path(name,__FILE__),"r").read()
end

#view_append(data) ⇒ Object



59
60
61
62
63
# File 'lib/log_file.rb', line 59

def view_append(data)
  temp=file_open("../log_file/views/append.html.erb")
  temp=ERB.new(temp)
  return temp.result(binding)
end

#view_generate(data) ⇒ Object



53
54
55
56
57
# File 'lib/log_file.rb', line 53

def view_generate(data)
         temp=file_open("../log_file/views/display.html.erb")
         temp=ERB.new(temp)
         return temp.result(binding)
end