]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - lib/rbot/ircbot.rb
bot: reorder fork sequence
[user/henk/code/ruby/rbot.git] / lib / rbot / ircbot.rb
index ecb48449dc284b477bd7cb9379f4eda58ae028bb..058e7bbeaf5e1add2a77808bf46fd46b3dcc580e 100644 (file)
@@ -403,30 +403,18 @@ class Bot
       debug "Using `#{@logfile}' as debug log"
     end
 
-    # See http://blog.humlab.umu.se/samuel/archives/000107.html
-    # for the backgrounding code
-    if $daemonize
-      begin
-        exit if fork
-        Process.setsid
-        exit if fork
-      rescue NotImplementedError
-        warning "Could not background, fork not supported"
-      rescue SystemExit
-        exit 0
-      rescue Exception => e
-        warning "Could not background. #{e.pretty_inspect}"
-      end
-      Dir.chdir botclass
-      # File.umask 0000                # Ensure sensible umask. Adjust as needed.
-    end
+    # 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
 
-    # setup logger based on bot configuration
-    LoggerManager.instance.set_level(@config['log.level'])
+    # Set the logfile
     LoggerManager.instance.set_logfile(@logfile, @config['log.keep'], @config['log.max_size'])
 
     if $daemonize
-      log "Redirecting standard input/output/error"
+      log "Redirecting standard input/output/error, console logger disabled"
+      LoggerManager.instance.flush
+      LoggerManager.instance.disable_console_logger
+
       [$stdin, $stdout, $stderr].each do |fd|
         begin
           fd.reopen "/dev/null"
@@ -436,20 +424,40 @@ class Bot
         end
       end
 
-      def $stdout.write(str=nil)
+      def $stdout.write(*args)
+        str = args.map { |s| s.to_s }.join("")
         log str, 2
-        return str.to_s.size
+        return str.bytesize
       end
-      def $stdout.write(str=nil)
+      def $stderr.write(*args)
+        str = args.map { |s| s.to_s }.join("")
         if str.to_s.match(/:\d+: warning:/)
           warning str, 2
         else
           error str, 2
         end
-        return str.to_s.size
+        return str.bytesize
       end
+
+      # See http://blog.humlab.umu.se/samuel/archives/000107.html
+      # for the backgrounding code
+      begin
+        exit if fork
+        Process.setsid
+        exit if fork
+      rescue NotImplementedError
+        warning "Could not background, fork not supported"
+      rescue SystemExit
+        exit 0
+      rescue Exception => e
+        warning "Could not background. #{e.pretty_inspect}"
+      end
+      Dir.chdir botclass
+      # File.umask 0000                # Ensure sensible umask. Adjust as needed.
     end
 
+    LoggerManager.instance.log_session_start
+
     File.open($opts['pidfile'] || File.join(@botclass, 'rbot.pid'), 'w') do |pf|
       pf << "#{$$}\n"
     end
@@ -919,7 +927,8 @@ class Bot
 
   # begin event handling loop
   def mainloop
-    while true
+    @keep_looping = true
+    while @keep_looping
       too_fast = 0
       quit_msg = nil
       valid_recv = false # did we receive anything (valid) from the server yet?
@@ -949,7 +958,8 @@ class Bot
       # exceptions that ARENT SocketError's. How am I supposed to handle
       # that?
       rescue SystemExit
-        exit 0
+        @keep_looping = false
+        break
       rescue Errno::ETIMEDOUT, Errno::ECONNABORTED, TimeoutError, SocketError => e
         error "network exception: #{e.pretty_inspect}"
         quit_msg = e.to_s
@@ -1246,7 +1256,7 @@ class Bot
     begin
       shutdown(message)
     ensure
-      exit 0
+      @keep_looping = false
     end
   end
 
@@ -1256,6 +1266,10 @@ class Bot
       :wait => @config['server.reconnect_wait']
     } if (!message || message.empty?)
     shutdown(message)
+
+    Irc::Bot::LoggerManager.instance.flush
+    Irc::Bot::LoggerManager.instance.log_session_end
+
     sleep @config['server.reconnect_wait']
     begin
       # now we re-exec