Created
April 29, 2010 09:31
-
-
Save jordandobson/383370 to your computer and use it in GitHub Desktop.
Logger for UW Ruby Class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'thread' | |
require 'resolv' | |
class Logger | |
def initialize( infile = 'old-log.txt' , outfile = 'new-log.txt', max_threads = 3 ) | |
@cache = {} | |
@queue = Queue.new | |
@list = [] | |
@threads = [] | |
@new = File.open( infile ) | |
@mutex = Mutex.new | |
@max_threads = max_threads | |
#@old = File.open( outfile, 'w' ) | |
end | |
def run | |
create_queue_and_list | |
load_threads_queue | |
write_new_log | |
end | |
def load_threads_queue | |
until @queue.empty? | |
@max_threads.times do | |
unless @queue.empty? | |
@threads << Thread.new do | |
index = @queue.pop | |
@list[index][0] = cache(@list[index][0]) | |
end | |
else | |
break | |
end | |
end | |
@threads.each{ |t| t.join } | |
puts ">> #{@queue.size} left" | |
end | |
end | |
def write_new_log | |
@list.each do |line| | |
puts line.join | |
# Write to @old instead | |
end | |
end | |
def create_queue_and_list | |
@new.each_line do |line| | |
sp = line.gsub(/(^\d\S+)(.+)/) do | |
@list << [$1, $2] | |
@queue << @new.lineno - 1 | |
end | |
end | |
puts "> QUEUE has #{@queue.size} items" | |
end | |
def cache( ip ) | |
@mutex.synchronize{ | |
return "#{@cache[ip]} CACHED!" if @cache[ip] | |
begin | |
@cache[ip] = Resolv.getname( ip ) | |
rescue Resolv::ResolvError | |
@cache[ip] = ip | |
end | |
return @cache[ip] | |
} | |
end | |
end | |
Logger.new.run |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
208.77.188.166 - #1 - [29/Apr/2009:16:07:38 -0700] "GET / HTTP/1.1" 200 1342 | |
75.119.201.189 - #2 - [29/Apr/2009:16:07:44 -0700] "GET /favicon.ico HTTP/1.1" 200 1406 | |
75.119.201.189 - #3 - [29/Apr/2009:16:09:53 -0700] "GET / HTTP/1.1" 200 1340 | |
208.77.188.166 - #4 - [29/Apr/2009:16:11:51 -0700] "GET / HTTP/1.1" 304 - | |
75.119.201.189 - #5 - [29/Apr/2009:16:13:15 -0700] "GET / HTTP/1.1" 304 - | |
208.77.188.166 - #6 - [29/Apr/2009:16:13:15 -0700] "GET / HTTP/1.1" 304 - | |
75.119.201.189 - #7 - [29/Apr/2009:16:13:17 -0700] "GET / HTTP/1.1" 304 - | |
74.125.67.100 - #8 - [29/Apr/2009:16:13:55 -0700] "GET /stylesheets/home.css?1240264242 HTTP/1.1" 200 7829 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment