diff options
author | dmitry kim <jason@nichego.net> | 2008-04-15 22:54:55 +0400 |
---|---|---|
committer | dmitry kim <jason@nichego.net> | 2008-04-15 22:54:55 +0400 |
commit | c3c62ed2f838cdd79a2f26571cbd18642398905c (patch) | |
tree | 6af55dfa3bdbc76a50b300df4ef6609a4172b222 /lib | |
parent | 56eacc7b8dc2a58949ff8211b982493e21f7ab33 (diff) |
* ircbot logging: $log_queue / logger thread
Diffstat (limited to 'lib')
-rw-r--r-- | lib/rbot/ircbot.rb | 50 |
1 files changed, 28 insertions, 22 deletions
diff --git a/lib/rbot/ircbot.rb b/lib/rbot/ircbot.rb index cd073b32..7def98b5 100644 --- a/lib/rbot/ircbot.rb +++ b/lib/rbot/ircbot.rb @@ -18,6 +18,14 @@ $logger.datetime_format = $dateformat $logger.level = $cl_loglevel if defined? $cl_loglevel $logger.level = 0 if $debug +$log_queue = Queue.new +Thread.new do + l = nil + while l = $log_queue.pop + $logger.add(*l) + end +end + require 'pp' unless Kernel.instance_methods.include?("pretty_inspect") @@ -39,29 +47,26 @@ class Exception end def rawlog(level, message=nil, who_pos=1) - begin - call_stack = caller - if call_stack.length > who_pos - who = call_stack[who_pos].sub(%r{(?:.+)/([^/]+):(\d+)(:in .*)?}) { "#{$1}:#{$2}#{$3}" } - else - who = "(unknown)" - end - # Output each line. To distinguish between separate messages and multi-line - # messages originating at the same time, we blank #{who} after the first message - # is output. - # Also, we output strings as-is but for other objects we use pretty_inspect - case message - when String - str = message - else - str = message.pretty_inspect - end - str.each_line { |l| - $logger.add(level, l.chomp, who) - who.gsub!(/./," ") - } - rescue SecurityError + call_stack = caller + if call_stack.length > who_pos + who = call_stack[who_pos].sub(%r{(?:.+)/([^/]+):(\d+)(:in .*)?}) { "#{$1}:#{$2}#{$3}" } + else + who = "(unknown)" + end + # Output each line. To distinguish between separate messages and multi-line + # messages originating at the same time, we blank #{who} after the first message + # is output. + # Also, we output strings as-is but for other objects we use pretty_inspect + case message + when String + str = message + else + str = message.pretty_inspect end + str.each_line { |l| + $log_queue.push [level, l.chomp, who] + who = ' ' * who.size + } end def log_session_start @@ -70,6 +75,7 @@ end def log_session_end $logger << "\n\n=== #{botclass} session ended on #{Time.now.strftime($dateformat)} ===\n\n" + $log_queue << nil end def debug(message=nil, who_pos=1) |