s2l: use logger

This commit is contained in:
Denis Knauf 2011-03-19 09:59:06 +01:00
parent 507951114e
commit 896f641856

View file

@ -1,5 +1,6 @@
#!/usr/bin/ruby #!/usr/bin/ruby
require 'logger'
require 'rubygems' require 'rubygems'
require 'sbdb' require 'sbdb'
require 'uuidtools' require 'uuidtools'
@ -7,6 +8,8 @@ require 'socket'
require 'select' require 'select'
require 'robustserver' require 'robustserver'
$logger = Logger.new $stderr
class S2L < Select::Server class S2L < Select::Server
attr_accessor :dbs attr_accessor :dbs
@ -16,22 +19,17 @@ class S2L < Select::Server
end end
def event_new_client a def event_new_client a
debug :connection => {:new => a}
{ :clientclass => S2L::Socket, :dbs => @dbs } { :clientclass => S2L::Socket, :dbs => @dbs }
end end
end end
module Kernel module Kernel
def debug( *p) logger :debug, *p end def debug( *p) $logger.debug *p end
def info( *p) logger :info, *p end def info( *p) $logger.info *p end
def warn( *p) logger :warn, *p end def warn( *p) $logger.warn *p end
def error( *p) logger :error, *p end def error( *p) $logger.error *p end
def fatal( *p) logger :fatal, *p end def fatal( *p) $logger.fatal *p end
def logger l, *p
p = p.first if p.length == 1
$stderr.puts [Time.now, l, p].inspect
end
private :logger
end end
class S2L::Socket < Select::Socket class S2L::Socket < Select::Socket
@ -41,6 +39,7 @@ class S2L::Socket < Select::Socket
end end
def event_line v def event_line v
debug :line => v
@dbs.emit v @dbs.emit v
end end
alias emit event_line alias emit event_line
@ -111,16 +110,22 @@ end
class Main < RobustServer class Main < RobustServer
def initialize conf def initialize conf
super super
@logger = $logger
@conf = conf @conf = conf
info :open => S2L info :open => S2L
@serv = S2L.new :sock => TCPServer.new( *@conf[:server]) @serv = S2L.new :sock => TCPServer.new( *@conf[:server])
info :create => {:home => @conf[:home]} info :create => {:home => @conf[:home]}
Dir.mkdir @conf[:home] rescue Errno::EEXIST Dir.mkdir @conf[:home] rescue Errno::EEXIST
@sigs[:INT] = @sigs[:TERM] = method(:shutdown) @sigs[:INT] = @sigs[:TERM] = method(:shutdown)
@sigs[:USR1] = method(:state)
end
def state s = nil
debug :server => @serv
end end
def shutdown s = nil def shutdown s = nil
$stderr.puts [:signal, s, Signal[s]].inspect info :shutdown => [s, Signal[s]]
@serv.close @serv.close
exit 0 exit 0
end end