Forum: Ruby Log file doesn't show message

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
7138c70b5f9288678057ec7ae0087cd5?d=identicon&s=25 Tom Bombadil (Guest)
on 2007-06-15 08:34
(Received via mailing list)
Hello,

I'd like to have a logging function which either tells me that the text
files have been created successfully or that provides me an error
message
should there have been a fatal error. In doing so I coded the following.
Note, however, that a logfile is created but neither of the comments
mentioned above are shown. Why?


# loads the library modules into the Ruby program
require 'rubygems'
require 'hpricot'
require 'open-uri'
require 'logger'

# create a log file
def do_at_exit(message,exit_code,logger)
  at_exit { logger.fatal(message) if exit_code == 1 }
  Process.exit(exit_code)
end

logger = Logger.new('logfile.log')

# logger formats: DEBUG < INFO < WARN < ERROR < FATAL < UNKNOWN
logger.level = Logger::INFO
do_at_exit("Text files successfully created.\n",1,logger) if Process.uid
!=
0

# creates array object and iterates over the collection passing elements
one
at a time to the "{ |e| ... }" block
%w(rubygems hpricot open-uri).each { |e| require e }

# check new entries

# Hpricot() method takes an object and loads the contents into a
document
object
(1..3).each do |id|
  doc = Hpricot(open("http://www.securityfocus.com/bid/#{id}"))

# opens a txt-file for each id according to aModeString (w:= write only,
truncates existing file to zero
# length or creates a new file for writing) and returns a new File
object
  File.open("#{id}.txt", "w+") do |f|
    f << (doc/'#vulnerability').inner_text
  end

end
Ee469623eb1b8e6e35d192822b9c4aa2?d=identicon&s=25 Florian Aßmann (Guest)
on 2007-06-15 11:01
(Received via mailing list)
Hello Tom,

I'm not sure what this code should do when it's finished but I
rewrote the src, hope this helps:

# Initialize Logger
require 'logger'
root_logger = Logger.new 'root.log'
root_logger.level = Logger::INFO

# Setup at_exit hook
at_exit { root_logger.info 'done.' }

%w{ rubygems hpricot open-uri }.each { |lib| require lib }
URL_MASK = 'http://www.securityfocus.com/bid/%i'.freeze

tgroup = ThreadGroup.new

root_logger.info 'Parsing SecurityFocus...'
( 1 .. 3 ).each do |id|
   root_logger.info "  open #{ URL_MASK % id }"
   thread = Thread.new do
     begin
       Hpricot open( URL_MASK % id )
     rescue => error
       root_logger.error error.inspect
     end
   end
   tgroup.add thread
end

tgroup.list.each_with_index do |thread, index|
   if doc = thread.value
     File.open( "#{ index }.txt", 'w+' ) do |file|
       file << ( doc/'#vulnerability' ).inner_text
     end
   end
end

Sincerely
Florian

Am 15.06.2007 um 08:34 schrieb Tom Bombadil:

>
>  Process.exit(exit_code)
This would exit the Process (your programm) immediatly after calling
do_at_exit...

> # creates array object and iterates over the collection passing
> elements one
> at a time to the "{ |e| ... }" block
> %w(rubygems hpricot open-uri).each { |e| require e }
You already requiered these (?)
7138c70b5f9288678057ec7ae0087cd5?d=identicon&s=25 Tom Bombadil (Guest)
on 2007-06-15 11:27
(Received via mailing list)
Great, thanks a lot! However, I just encountered a little bug -> I'd
like to
have the bid on www.securityfocus.com as file name. Thus, for bugtraq id
1
-> 1.txt, for bugtraq id 2 = 2.txt, etc... How can I do that?

Thanks!
Ee469623eb1b8e6e35d192822b9c4aa2?d=identicon&s=25 Florian Aßmann (Guest)
on 2007-06-15 11:49
(Received via mailing list)
Add 1 to index when you choose the filename...

Am 15.06.2007 um 11:26 schrieb Tom Bombadil:

> On 6/15/07, Florian Aßmann <florian.assmann@email.de> wrote:
>>
>>    root_logger.info "  open #{ URL_MASK % id }"
>> tgroup.list.each_with_index do |thread, index|
>>    if doc = thread.value
>> #     File.open( "#{ index }.txt", 'w+' ) do |file|
File.open( "#{ index + 1 }.txt", 'w+' ) do |file| # EASY ISN'T IT?
7138c70b5f9288678057ec7ae0087cd5?d=identicon&s=25 Tom Bombadil (Guest)
on 2007-06-15 13:13
(Received via mailing list)
Not that easy I'd say as I now have Bugtraq ID: 5, but 1.txt :-)
7138c70b5f9288678057ec7ae0087cd5?d=identicon&s=25 Tom Bombadil (Guest)
on 2007-06-15 13:39
(Received via mailing list)
Florian,

What if I'd only like to get the latest entries on
www.securityfocus.com/bid?
I'd then need a check if #{ URL_MASK % id } == 0 or so, yes?

So long,
Tom

--
Ee469623eb1b8e6e35d192822b9c4aa2?d=identicon&s=25 Florian Aßmann (Guest)
on 2007-06-15 14:05
(Received via mailing list)
Tom Bombadil schrieb:
> What if I'd only like to get the latest entries on
> www.securityfocus.com/bid?
I'd subscribe the maillist or use a webservice if it exists, otherwise
you'd have to parse the html source of www.securityfocus.com/bid and
find the first field with the bid...
Fetching the site is very expensive, takes much time and ressources.

> I'd then need a check if #{ URL_MASK % id } == 0 or so, yes?
No, 'URL_MASK % id' is like 'sprintf URL_MASK, id', see above for
another solution.

Regards
Florian
7138c70b5f9288678057ec7ae0087cd5?d=identicon&s=25 Tom Bombadil (Guest)
on 2007-06-15 14:22
(Received via mailing list)
All right. Lastly, it pretty much seems like it's not that easy as I now
get
1.txt for the Bugtraq ID: 5 :-) It should be 1-1, 2-2, etc.

Have a great weekend,
-Tom


--
This topic is locked and can not be replied to.