Changeset 58d7ea0fe4491225ad5856a49b1a965f2e5ee40c
- Timestamp:
- 06/18/08 14:39:27
(7 months ago)
- Author:
- dmitry kim <jason@nichego.net>
- git-committer:
- dmitry kim <jason@nichego.net> 1213785567 +0400
- git-parent:
[b74f6944594ec5e998bb023a4bb6184794d02298]
- git-author:
- dmitry kim <jason@nichego.net> 1213781088 +0400
- Message:
+ core/irclog: dedicated loggers thread
-
Files:
-
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
| reea0523 |
r58d7ea0 |
|
| 25 | 25 | def initialize |
|---|
| 26 | 26 | super |
|---|
| | 27 | @queue = Queue.new |
|---|
| | 28 | @thread = Thread.new { loggers_thread } |
|---|
| 27 | 29 | @logs = Hash.new |
|---|
| 28 | 30 | Dir.mkdir("#{@bot.botclass}/logs") unless File.exist?("#{@bot.botclass}/logs") |
|---|
| … | … | |
| 57 | 59 | # +where+ can be a channel name, or a nick for private message logging |
|---|
| 58 | 60 | def irclog(message, where="server") |
|---|
| 59 | | message = message.chomp |
|---|
| 60 | | now = Time.now |
|---|
| 61 | | stamp = now.strftime("%Y/%m/%d %H:%M:%S") |
|---|
| 62 | | if where.class <= Server |
|---|
| 63 | | where_str = "server" |
|---|
| 64 | | else |
|---|
| 65 | | where_str = where.downcase.gsub(/[:!?$*()\/\\<>|"']/, "_") |
|---|
| 66 | | end |
|---|
| 67 | | return unless can_log_on(where_str) |
|---|
| 68 | | unless @logs.has_key? where_str |
|---|
| 69 | | if @logs.size > @bot.config['irclog.max_open_files'] |
|---|
| 70 | | @logs.keys.sort do |a, b| |
|---|
| 71 | | @logs[a][0] <=> @logs[b][0] |
|---|
| 72 | | end.slice(0, @logs.size - @bot.config['irclog.max_open_files']).each do |w| |
|---|
| 73 | | logfile_close w, "idle since #{@logs[w][0]}" |
|---|
| 74 | | end |
|---|
| 75 | | end |
|---|
| 76 | | f = File.new("#{@bot.botclass}/logs/#{where_str}", "a") |
|---|
| 77 | | f.sync = true |
|---|
| 78 | | f.puts "[#{stamp}] @ Log started by #{@bot.myself.nick}" |
|---|
| 79 | | @logs[where_str] = [now, f] |
|---|
| 80 | | end |
|---|
| 81 | | @logs[where_str][1].puts "[#{stamp}] #{message}" |
|---|
| 82 | | @logs[where_str][0] = now |
|---|
| 83 | | #debug "[#{stamp}] <#{where}> #{message}" |
|---|
| | 61 | @queue.push [message, where] |
|---|
| 84 | 62 | end |
|---|
| 85 | 63 | |
|---|
| 86 | 64 | def cleanup |
|---|
| 87 | | @logs.keys.each { |w| logfile_close(w, 'rescan or shutdown') } |
|---|
| | 65 | @queue << nil |
|---|
| | 66 | @thread.join |
|---|
| | 67 | @thread = nil |
|---|
| 88 | 68 | end |
|---|
| 89 | 69 | |
|---|
| … | … | |
| 252 | 232 | irclog m.logmessage, ".unknown" |
|---|
| 253 | 233 | end |
|---|
| | 234 | |
|---|
| | 235 | protected |
|---|
| | 236 | def loggers_thread |
|---|
| | 237 | ls = nil |
|---|
| | 238 | debug 'loggers_thread starting' |
|---|
| | 239 | while ls = @queue.pop |
|---|
| | 240 | message, where = ls |
|---|
| | 241 | message = message.chomp |
|---|
| | 242 | now = Time.now |
|---|
| | 243 | stamp = now.strftime("%Y/%m/%d %H:%M:%S") |
|---|
| | 244 | if where.class <= Server |
|---|
| | 245 | where_str = "server" |
|---|
| | 246 | else |
|---|
| | 247 | where_str = where.downcase.gsub(/[:!?$*()\/\\<>|"']/, "_") |
|---|
| | 248 | end |
|---|
| | 249 | return unless can_log_on(where_str) |
|---|
| | 250 | unless @logs.has_key? where_str |
|---|
| | 251 | if @logs.size > @bot.config['irclog.max_open_files'] |
|---|
| | 252 | @logs.keys.sort do |a, b| |
|---|
| | 253 | @logs[a][0] <=> @logs[b][0] |
|---|
| | 254 | end.slice(0, @logs.size - @bot.config['irclog.max_open_files']).each do |w| |
|---|
| | 255 | logfile_close w, "idle since #{@logs[w][0]}" |
|---|
| | 256 | end |
|---|
| | 257 | end |
|---|
| | 258 | f = File.new("#{@bot.botclass}/logs/#{where_str}", "a") |
|---|
| | 259 | f.sync = true |
|---|
| | 260 | f.puts "[#{stamp}] @ Log started by #{@bot.myself.nick}" |
|---|
| | 261 | @logs[where_str] = [now, f] |
|---|
| | 262 | end |
|---|
| | 263 | @logs[where_str][1].puts "[#{stamp}] #{message}" |
|---|
| | 264 | @logs[where_str][0] = now |
|---|
| | 265 | #debug "[#{stamp}] <#{where}> #{message}" |
|---|
| | 266 | end |
|---|
| | 267 | @logs.keys.each { |w| logfile_close(w, 'rescan or shutdown') } |
|---|
| | 268 | debug 'loggers_thread terminating' |
|---|
| | 269 | end |
|---|
| 254 | 270 | end |
|---|
| 255 | 271 | |
|---|