From 8bfa19661541ffa49aeceb1d9770d0413b9ec1da Mon Sep 17 00:00:00 2001 From: Tom Preston-Werner Date: Wed, 14 Oct 2009 14:27:47 -0700 Subject: [PATCH] use Logger for timestamped log messages and add connection accept logging --- bin/proxymachine | 6 +++--- examples/git.rb | 2 +- lib/proxymachine.rb | 15 +++++++++------ lib/proxymachine/client_connection.rb | 17 +++++++++-------- 4 files changed, 22 insertions(+), 18 deletions(-) diff --git a/bin/proxymachine b/bin/proxymachine index fc4abfa..906d96c 100755 --- a/bin/proxymachine +++ b/bin/proxymachine @@ -38,8 +38,8 @@ rescue Exception => e if e.instance_of?(SystemExit) raise else - puts 'Uncaught exception' - puts e.message - puts e.backtrace.join("\n") + LOGGER.info 'Uncaught exception' + LOGGER.info e.message + LOGGER.info e.backtrace.join("\n") end end \ No newline at end of file diff --git a/examples/git.rb b/examples/git.rb index e95a2a3..86a62cf 100644 --- a/examples/git.rb +++ b/examples/git.rb @@ -6,7 +6,7 @@ class GitRouter # Look at the routing table and return the correct address for +name+ # Returns ":" e.g. "ae8f31c.example.com:9418" def self.lookup(name) - puts "Proxying for user #{name}" + LOGGER.info "Proxying for user #{name}" "localhost:9418" end end diff --git a/lib/proxymachine.rb b/lib/proxymachine.rb index 4210473..02a4fe5 100644 --- a/lib/proxymachine.rb +++ b/lib/proxymachine.rb @@ -1,9 +1,12 @@ require 'rubygems' require 'eventmachine' +require 'logger' require 'proxymachine/client_connection' require 'proxymachine/server_connection' +LOGGER = Logger.new(STDOUT) + class ProxyMachine MAX_FAST_SHUTDOWN_SECONDS = 10 @@ -30,7 +33,7 @@ def self.incr def self.decr @@counter -= 1 if $server.nil? - puts "Waiting for #{@@counter} connections to finish." + LOGGER.info "Waiting for #{@@counter} connections to finish." end self.update_procline EM.stop if $server.nil? and @@counter == 0 @@ -47,17 +50,17 @@ def self.router def self.graceful_shutdown(signal) EM.stop_server($server) if $server - puts "Received #{signal} signal. No longer accepting new connections." - puts "Waiting for #{ProxyMachine.count} connections to finish." + LOGGER.info "Received #{signal} signal. No longer accepting new connections." + LOGGER.info "Waiting for #{ProxyMachine.count} connections to finish." $server = nil EM.stop if ProxyMachine.count == 0 end def self.fast_shutdown(signal) EM.stop_server($server) if $server - puts "Received #{signal} signal. No longer accepting new connections." - puts "Maximum time to wait for connections is #{MAX_FAST_SHUTDOWN_SECONDS} seconds." - puts "Waiting for #{ProxyMachine.count} connections to finish." + LOGGER.info "Received #{signal} signal. No longer accepting new connections." + LOGGER.info "Maximum time to wait for connections is #{MAX_FAST_SHUTDOWN_SECONDS} seconds." + LOGGER.info "Waiting for #{ProxyMachine.count} connections to finish." $server = nil EM.stop if ProxyMachine.count == 0 Thread.new do diff --git a/lib/proxymachine/client_connection.rb b/lib/proxymachine/client_connection.rb index 93aa9e9..6355853 100644 --- a/lib/proxymachine/client_connection.rb +++ b/lib/proxymachine/client_connection.rb @@ -3,12 +3,13 @@ module Protocols class ClientConnection < Connection def self.start(host, port) $server = EM.start_server(host, port, self) - puts "Listening on #{host}:#{port}" - puts "Send QUIT to quit after waiting for all connections to finish." - puts "Send TERM or INT to quit after waiting for up to 10 seconds for connections to finish." + LOGGER.info "Listening on #{host}:#{port}" + LOGGER.info "Send QUIT to quit after waiting for all connections to finish." + LOGGER.info "Send TERM or INT to quit after waiting for up to 10 seconds for connections to finish." end def post_init + LOGGER.info "Accepted #{peer}" @buffer = [] @tries = 0 ProxyMachine.incr @@ -29,14 +30,14 @@ def receive_data(data) end rescue => e close_connection - puts "#{e.class} - #{e.message}" + LOGGER.info "#{e.class} - #{e.message}" end def ensure_server_side_connection @timer.cancel if @timer unless @server_side commands = ProxyMachine.router.call(@buffer.join) - puts "#{peer} #{commands.inspect}" + LOGGER.info "#{peer} #{commands.inspect}" close_connection unless commands.instance_of?(Hash) if remote = commands[:remote] m, host, port = *remote.match(/^(.+):(.+)$/) @@ -64,18 +65,18 @@ def ensure_server_side_connection def try_server_connect(host, port) @server_side = ServerConnection.request(host, port, self) proxy_incoming_to(@server_side, 10240) - puts "Successful connection to #{host}:#{port}." + LOGGER.info "Successful connection to #{host}:#{port}." true rescue => e if @tries < 10 @tries += 1 - puts "Failed on server connect attempt #{@tries}. Trying again..." + LOGGER.info "Failed on server connect attempt #{@tries}. Trying again..." @timer.cancel if @timer @timer = EventMachine::Timer.new(0.1) do self.ensure_server_side_connection end else - puts "Failed after ten connection attempts." + LOGGER.info "Failed after ten connection attempts." end false end