diff options
author | dmitry kim <jason@nichego.net> | 2008-04-15 05:14:25 +0400 |
---|---|---|
committer | dmitry kim <jason@nichego.net> | 2008-04-15 05:14:25 +0400 |
commit | 86e2386cce5df6bf45c2069140e752dc810cc011 (patch) | |
tree | 0a470c6e42d109a6dbe3ea2e5ee9b5c824e656ac | |
parent | ec5a16fa448c1eeba69697617183bda8ec0c863a (diff) |
+ core/irclog: expiration for the open logfiles cache
-rw-r--r-- | lib/rbot/core/irclog.rb | 26 |
1 files changed, 22 insertions, 4 deletions
diff --git a/lib/rbot/core/irclog.rb b/lib/rbot/core/irclog.rb index fcaf1b75..0c024ddd 100644 --- a/lib/rbot/core/irclog.rb +++ b/lib/rbot/core/irclog.rb @@ -8,6 +8,8 @@ # License:: GPL v2 class IrcLogModule < CoreBotModule + + MAX_OPEN_FILES = 20 # XXX: maybe add a config value def initialize super @@ -15,23 +17,39 @@ class IrcLogModule < CoreBotModule Dir.mkdir("#{@bot.botclass}/logs") unless File.exist?("#{@bot.botclass}/logs") end + def logfile_close(where_str, reason = 'unknown reason') + f = @logs.delete(where_str) or return + stamp = Time.now.strftime '%Y/%m/%d %H:%M:%S' + f[1].puts "[#{stamp}] @ Log closed by #{@bot.myself.nick} (#{reason})" + f[1].close + end + # log IRC-related message +message+ to a file determined by +where+. # +where+ can be a channel name, or a nick for private message logging def irclog(message, where="server") message = message.chomp - stamp = Time.now.strftime("%Y/%m/%d %H:%M:%S") + now = Time.now + stamp = now.strftime("%Y/%m/%d %H:%M:%S") if where.class <= Server where_str = "server" else where_str = where.downcase.gsub(/[:!?$*()\/\\<>|"']/, "_") end - unless(@logs.has_key?(where_str)) + unless @logs.has_key? where_str + if @logs.size > MAX_OPEN_FILES + @logs.keys.sort do |a, b| + @logs[a][0] <=> @logs[b][0] + end.slice(0, @logs.size - MAX_OPEN_FILES).each do |w| + logfile_close w, "idle since #{@logs[w][0]}" + end + end f = File.new("#{@bot.botclass}/logs/#{where_str}", "a") f.sync = true f.puts "[#{stamp}] @ Log started by #{@bot.myself.nick}" - @logs[where_str] = f + @logs[where_str] = [now, f] end - @logs[where_str].puts "[#{stamp}] #{message}" + @logs[where_str][1].puts "[#{stamp}] #{message}" + @logs[where_str][0] = now #debug "[#{stamp}] <#{where}> #{message}" end |