summaryrefslogtreecommitdiff
path: root/lib/rbot/logger.rb
diff options
context:
space:
mode:
authorGiuseppe Bilotta <giuseppe.bilotta@gmail.com>2021-05-29 18:47:29 +0200
committerGiuseppe Bilotta <giuseppe.bilotta@gmail.com>2021-05-29 18:47:29 +0200
commit98446561cf3c7126dd93b34c9be361fa4aa8da36 (patch)
tree1d6030d5db6d5d40c0be9b07bdae4cf09031dd49 /lib/rbot/logger.rb
parent0368e8c09385a6c49e0d59cd6162675057e5b339 (diff)
fix: restart logger thread after fork
Logging was broken when daemonizing, due to the logger thread being dead after the fork. This can be solved by restarting the thread, if necessary when setting the log file (which we conveniently do right after the fork).
Diffstat (limited to 'lib/rbot/logger.rb')
-rw-r--r--lib/rbot/logger.rb14
1 files changed, 12 insertions, 2 deletions
diff --git a/lib/rbot/logger.rb b/lib/rbot/logger.rb
index b5f615d7..fd6d485f 100644
--- a/lib/rbot/logger.rb
+++ b/lib/rbot/logger.rb
@@ -23,13 +23,16 @@ class Bot
@file_logger = nil
@queue = Queue.new
- @thread = start_thread
+ start_thread
end
def set_logfile(filename, keep, max_size)
@file_logger = Logger.new(filename, keep, max_size*1024*1024)
@file_logger.datetime_format = @dateformat
@file_logger.level = @logger.level
+ # make sure the thread is running, which might be false after a fork
+ # (conveniently, we call set_logfile right after the fork)
+ start_thread
end
def set_level(level)
@@ -90,10 +93,17 @@ class Bot
end
end
+ def flush
+ while @queue.size > 0
+ next
+ end
+ end
+
private
def start_thread
- Thread.new do
+ return if @thread and @thread.alive?
+ @thread = Thread.new do
lines = nil
while lines = @queue.pop
lines.each { |line|