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
# 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")
				count=env['rack.session'][:displayed_lines]+10+(@lines-env['rack.session'][:line_count])
				env['rack.session'][:displayed_lines] += 10 
				data=`tail -n "#{ count }" "#{ @log_name }" | head -10`
				[200,{"Content-Type"=>"text/html"},[view_append(data)]]
			end	
		else
             	env['rack.session'][:line_count]=@lines
			env['rack.session'][:displayed_lines]=30
			data=`tail -30 "#{ @log_name }"`
			[200,{"Content-Type"=>"text/html"},[view_generate(data)]]
		end	
	else
		@app.call(env)
	end
end

#file_open(name) ⇒ Object



51
52
53
# File 'lib/log_file.rb', line 51

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

#view_append(data) ⇒ Object



45
46
47
48
49
# File 'lib/log_file.rb', line 45

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



39
40
41
42
43
# File 'lib/log_file.rb', line 39

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