]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/commitdiff
fix: restart logger thread after fork
authorGiuseppe Bilotta <giuseppe.bilotta@gmail.com>
Sat, 29 May 2021 16:47:29 +0000 (18:47 +0200)
committerGiuseppe Bilotta <giuseppe.bilotta@gmail.com>
Sat, 29 May 2021 16:47:29 +0000 (18:47 +0200)
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).

bin/rbot
lib/rbot/ircbot.rb
lib/rbot/logger.rb

index 82ee9c9a4b9f28958369ad9c5dc78d662027e91a..8c65f42cadaa63f7ec7718392232181762f46e38 100755 (executable)
--- a/bin/rbot
+++ b/bin/rbot
@@ -111,14 +111,14 @@ if ($opts["help"])
   exit 0
 end
 
-if(bot = Irc::Bot.new(ARGV.shift, :argv => orig_opts))
-  # setup logger based on command line arguments
-  loglevel = $opts['loglevel'] ? $opts['loglevel'].to_i : nil
-  loglevel = $opts['debug'] ? 0 : loglevel
-  if loglevel
-    Irc::Bot::LoggerManager.instance.set_level(loglevel)
-  end
+# setup logger based on command line arguments
+loglevel = $opts['loglevel'] ? $opts['loglevel'].to_i : nil
+loglevel = $opts['debug'] ? 0 : loglevel
+if loglevel
+  Irc::Bot::LoggerManager.instance.set_level(loglevel)
+end
 
+if(bot = Irc::Bot.new(ARGV.shift, :argv => orig_opts))
   # just run the bot
   bot.mainloop
 end
index ecb48449dc284b477bd7cb9379f4eda58ae028bb..4eac68d610c1056e71910a35352fe80731a6d79e 100644 (file)
@@ -403,6 +403,8 @@ class Bot
       debug "Using `#{@logfile}' as debug log"
     end
 
+    LoggerManager.instance.flush
+
     # See http://blog.humlab.umu.se/samuel/archives/000107.html
     # for the backgrounding code
     if $daemonize
@@ -421,8 +423,11 @@ class Bot
       # File.umask 0000                # Ensure sensible umask. Adjust as needed.
     end
 
-    # setup logger based on bot configuration
-    LoggerManager.instance.set_level(@config['log.level'])
+    # setup logger based on bot configuration, if not set from the command line
+    loglevel_set = $opts.has_key?('debug') or $opts.has_key?('loglevel')
+    LoggerManager.instance.set_level(@config['log.level']) unless loglevel_set
+
+    # Set the logfile
     LoggerManager.instance.set_logfile(@logfile, @config['log.keep'], @config['log.max_size'])
 
     if $daemonize
index b5f615d7881ed061f948a28ec1db55cd65601e35..fd6d485fea3675b144a63e750f3c0ea7adefe713 100644 (file)
@@ -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|