Ahhh. Thats better :). Much easier to understand
now.
I didn't realise you could use puts to write plain
html to a file, much quicker and easier than cgi.
I can actually cut this down to this much now, not
sure if i'm losing anything here, still seems to
output the same.
======
#!/usr/bin/ruby
My_Title = "Testing Log Read"
Log_File = "/var/log/apache2/error.log"
puts <<EOF
Content-type: text/html
<html>
<head>
<title>#{My_Title}</title>
</head>
<body bgcolor=\"#efefef\">
<table width=\"95%\">
<tr><td colspan=\"2\">Apache Log Records</td></tr>
EOF
if File.exists? Log_File
lines = File.open(Log_File).readlines
lines.each_with_index do |line, index|
index % 2 == 0 ? color = "#dddddd" :
color="#cccccc"
# where index == 5
puts "<tr bgcolor=\"#{color}\">\n
<td>#{index}</td>\n <td>#{line}</td>"
puts "</tr>"
end
else
puts "<tr><td>No File Found:<br /> Check path:
#{Log_File}</td></tr>\n"
end
puts "</body>\n</html>"
======
Thanks again for your help:).
Tristan
--- pat eyler <pat.eyler / gmail.com> wrote:
> Here's a working version that I've made some changes
> to (some of the
> changes were merely to show a different way of doing
> things -- but I
> got rid of the cgi module since you weren't really
> making enough use
> of it to justify the extra load time):
>
> #!/usr/bin/ruby
>
> My_Title = "Testing Log Read"
> Log_File = "/var/log/apache2/error.log"
>
> puts <<EOF
> Content-type: text/html
>
> <html>
> <head>
> <title>#{My_Title}</title>
> </head>
> <body bgcolor=\"#efefef\">
> <table width=\"80%\">
> <tr><td>Call records</td></tr>
> EOF
>
> if File.exists? Log_File
> lines = File.open(Log_File).readlines
> lines.each_with_index do |line, index|
> index % 2 == 0 ? color = "#dddddd" :
> color="#cccccc"
> puts " <tr bgcolor=\"#{color}\">\n
> <td>#{index}</td>"
>
> entries = line.chop.split(/\n/)
> entries.each do |field|
> puts "<td>#{field}</td>"
> end
> puts "\n</tr>\n"
> end
> else
> puts "No File Found\n"
> end
> puts "</body>\n</html>"
>
>
>
> On 7/14/05, Tristan Knowles <cydonia_1 / yahoo.com>
> wrote:
> > Hi,
> >
> > After reading up on Ruby I thought it sounded good
> and
> > decided to learn it so I can manipulate my files
> on my
> > linux server such as log, csv, conf etc. Anyway,
> i'm
> > a beginner when it comes to programming, so bear
> with
> > me.
> >
> > I use the Asterisk PBX and came across this script
> > here which reads a csv file and puts each record
> into
> > an html table:
> >
>
http://rubyforge.org/snippet/detail.php?type=snippet&id=76
> >
> >
> > Anyway, I thought I would then apply this basic
> > concept to a log file, but I cannot get it to
> display
> > through a web browser. Here is my script:
> >
> >
> > ===========
> > #!/usr/bin/ruby
> >
> > My_Title = "Testing Log Read"
> > Log_File = "/var/log/apache2/error.log"
> >
> > require 'cgi'
> >
> > cgi = CGI.new
> > printf cgi.header("Content-type" =>
> "text/html\n\n")
> >
> >
> > printf "<html>\n<head>\n"
> > printf "<title>#{My_Title}</title>\n"
> > printf "</head>\n<body bgcolor=\"#efefef\">\n"
> > printf "<table width=\"80%\">\n<tr><td>Call
> > records</td></tr>\n"
> > if File.exists? Log_File
> > lines = File.open(Log_File).readlines
> > lines.each_with_index do |line, index|
> > printf " <tr%s>\n <td>%d</td>",
> > index % 2 == 0 ? ' bgcolor="#dddddd"' : '
> > bgcolor="#cccccc"', index + 1
> > entries = line.chop.split(/\n/)
> > entries.each_with_index do |field, index|
> > printf "<td>%s</td>", entries
> > end
> > printf "\n</tr>\n"
> > end
> > else
> > printf "No File Found\n"
> > end
> > printf "</body>\n</html>"
> > ===========
> >
> >
> > If I comment out all the printf commands related
> to
> > web and cgi, and run it through the shell, it
> seems to
> > be taking each line and putting it into <tr><td>
> > brackets, but I can't get it to display through a
> > browser, it stops at the third printf command.
> >
> >
> > I have tried heaps of different things, but feel
> like
> > i'm running around in circles now.
> >
> > Any advise with this one?
> >
> >
> > Cheers.
> > Tristan
> >
> >
> >
> >
> >
> >
> >
>
___________________________________________________________
> > Yahoo! Messenger - NEW crystal clear PC to PC
> calling worldwide with voicemail
> http://uk.messenger.yahoo.com
> >
> >
>
>
> --
> thanks,
> -pate
> -------------------------
> We are often unable to tell people what they need to
> know, because
> they want to know something else, and would
> therefore only
> misunderstand what we said
> - the Raven (George MacDonald, Lilith)
>
>
___________________________________________________________
How much free photo storage do you get? Store your holiday
snaps for FREE with Yahoo! Photos http://uk.photos.yahoo.com