summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authordmitry kim <jason@nichego.net>2008-06-18 13:24:48 +0400
committerdmitry kim <jason@nichego.net>2008-06-18 14:39:27 +0400
commit58d7ea0fe4491225ad5856a49b1a965f2e5ee40c (patch)
treed35339a4e7abeb6a9ed31eb2e44c693090276fb5 /lib
parentb74f6944594ec5e998bb023a4bb6184794d02298 (diff)
+ core/irclog: dedicated loggers thread
Diffstat (limited to 'lib')
-rw-r--r--lib/rbot/core/irclog.rb68
1 files changed, 42 insertions, 26 deletions
diff --git a/lib/rbot/core/irclog.rb b/lib/rbot/core/irclog.rb
index 507cdbe2..39d0a1fb 100644
--- a/lib/rbot/core/irclog.rb
+++ b/lib/rbot/core/irclog.rb
@@ -24,6 +24,8 @@ class IrcLogModule < CoreBotModule
attr :nolog_rx, :dolog_rx
def initialize
super
+ @queue = Queue.new
+ @thread = Thread.new { loggers_thread }
@logs = Hash.new
Dir.mkdir("#{@bot.botclass}/logs") unless File.exist?("#{@bot.botclass}/logs")
event_irclog_list_changed(@bot.config['irclog.no_log'], @bot.config['irclog.do_log'])
@@ -56,35 +58,13 @@ class IrcLogModule < CoreBotModule
# 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
- 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
- return unless can_log_on(where_str)
- unless @logs.has_key? where_str
- if @logs.size > @bot.config['irclog.max_open_files']
- @logs.keys.sort do |a, b|
- @logs[a][0] <=> @logs[b][0]
- end.slice(0, @logs.size - @bot.config['irclog.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] = [now, f]
- end
- @logs[where_str][1].puts "[#{stamp}] #{message}"
- @logs[where_str][0] = now
- #debug "[#{stamp}] <#{where}> #{message}"
+ @queue.push [message, where]
end
def cleanup
- @logs.keys.each { |w| logfile_close(w, 'rescan or shutdown') }
+ @queue << nil
+ @thread.join
+ @thread = nil
end
def sent(m)
@@ -251,6 +231,42 @@ class IrcLogModule < CoreBotModule
def unknown_message(m)
irclog m.logmessage, ".unknown"
end
+
+ protected
+ def loggers_thread
+ ls = nil
+ debug 'loggers_thread starting'
+ while ls = @queue.pop
+ message, where = ls
+ message = message.chomp
+ 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
+ return unless can_log_on(where_str)
+ unless @logs.has_key? where_str
+ if @logs.size > @bot.config['irclog.max_open_files']
+ @logs.keys.sort do |a, b|
+ @logs[a][0] <=> @logs[b][0]
+ end.slice(0, @logs.size - @bot.config['irclog.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] = [now, f]
+ end
+ @logs[where_str][1].puts "[#{stamp}] #{message}"
+ @logs[where_str][0] = now
+ #debug "[#{stamp}] <#{where}> #{message}"
+ end
+ @logs.keys.each { |w| logfile_close(w, 'rescan or shutdown') }
+ debug 'loggers_thread terminating'
+ end
end
ilm = IrcLogModule.new