From fb987eb4611386a6d5bae4858098a5d247ed405f Mon Sep 17 00:00:00 2001 From: Denis Knauf Date: Mon, 29 Mar 2010 22:36:57 +0200 Subject: [PATCH] the fileparser-problem --- lib/logan/cache.rb | 9 +++++---- lib/logan/inc/main.rb | 19 +++++++++++++++---- lib/logan/inc/server.rb | 3 +-- 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/lib/logan/cache.rb b/lib/logan/cache.rb index 8c6087b..de71614 100644 --- a/lib/logan/cache.rb +++ b/lib/logan/cache.rb @@ -42,7 +42,7 @@ class LogAn::Cache def write_cache= type @type &= ~ (type ? 0 : 2) - define_singleton_method :[], method( type ? :oset : :dset) + define_singleton_method :[]=, method( type ? :oset : :dset) end #include Enumerable @@ -56,6 +56,7 @@ end class LogAn::AutoValueConvertHash include Enumerable + attr_reader :decode, :encode def initialize obj, encode = nil, each = nil, &decode @object, @encoder = obj, decode.nil? ? encode || Marshal.method( :dump) : nil, @@ -64,16 +65,16 @@ class LogAn::AutoValueConvertHash end def [] k - decode.call @object[k] + @decode.call @object[k] end def []= k, v - @object[k] = encode.call v + @object[k] = @encode.call v end def each *paras @each.call *paras do |k, v| - yield k, decode( v) + yield k, @decode.call( v) end end end diff --git a/lib/logan/inc/main.rb b/lib/logan/inc/main.rb index fe11de3..6d29a3e 100644 --- a/lib/logan/inc/main.rb +++ b/lib/logan/inc/main.rb @@ -10,11 +10,11 @@ require 'logan/cache' module LogAn::Inc class Main < RobustServer # Open Config. - def config env, db, type = nil, flags = nil + def config env, db, type = nil, flags = nil, &e $stderr.puts "Open Database \"sids.cnf\" #{db.inspect} (#{type.inspect})" type ||= 1+4 ret = env[ 'sids.cnf', db, :flags => flags || SBDB::RDONLY] - ret = LogAn::AutoValueConvertHash.new ret if type&4 > 0 + ret = LogAn::AutoValueConvertHash.new ret, &e if type&4 > 0 or e ret = LogAn::Cache.new ret, type&3 if type&3 > 0 ret end @@ -31,27 +31,38 @@ module LogAn::Inc def initialize conf super @conf = {} + # Copy config - changes possible conf.each {|key, val| @conf[key]= val } + # Default directories %w[logs etc].each {|key| @conf[key.to_sym] = key } + # Open Loglines-databases @logs = LogAn::Loglines.new @conf[:logs] + # Open config-databases Dir.mkdir @conf[:etc] rescue Errno::EEXIST @etc = SBDB::Env.new( @conf[:etc], log_config: SBDB::Env::LOG_IN_MEMORY | SBDB::Env::LOG_AUTO_REMOVE, flags: SBDB::CREATE | SBDB::Env::INIT_TXN | Bdb::DB_INIT_MPOOL) + # Set inc-config - stored in etc/inc.cnf - @conf[:inc] = {} - %w[hosts files fileparser].each {|key| @conf[:inc][key.to_sym] = config( @etc, key) } + begin + inc = @conf[:inc] = {} + %w[hosts files fileparser].each {|key| @conf[:inc][key.to_sym] = config( @etc, key) } + inc[:fileparser] = config( @etc, 'fileparser') {|val| Safebox.run "[#{val}]"} + end @store = LogAn::Cache.new LogAn::AutoValueConvertHash.new( @etc[ 'sids.store', 'seeks', SBDB::Recno, SBDB::CREATE | SBDB::AUTO_COMMIT]), 3 + # Prepare Inc-server - create server LogAn::Inc::FileParser::Base.logdb = @logs LogAn::Inc::FileParser::Base.store = @store @serv = LogAn::Inc::Server.new :sock => TCPServer.new( *@conf[:server]), :config => @conf[:inc] + # Shutdown on signals @sigs[:INT] = @sigs[:TERM] = method( :shutdown) + rescue Object # It's better to close everything, because BDB doesn't like unexpected exits self.at_exit diff --git a/lib/logan/inc/server.rb b/lib/logan/inc/server.rb index 0106e19..a0c2e70 100644 --- a/lib/logan/inc/server.rb +++ b/lib/logan/inc/server.rb @@ -97,8 +97,7 @@ class LogAn::Inc::Server < ::Select::Server def event_cmd cmd sid, line = cmd.unpack 'Na*' - fps = @config[:fileparser] - fp = fps[sid] + fps = @config[:fileparser][sid] fp.event_line line, self if fileparser end end