OSDN Git Service

* [shogi-server] The server now provides more accurate time control.
[shogi-server/shogi-server.git] / shogi_server.rb
index 800656f..587798a 100644 (file)
@@ -38,8 +38,8 @@ require 'shogi_server/player'
 require 'shogi_server/timeout_queue'
 require 'shogi_server/usi'
 require 'shogi_server/util'
-
-require 'rubygems'
+require 'shogi_server/command'
+require 'shogi_server/buoy'
 
 module ShogiServer # for a namespace
 
@@ -55,15 +55,46 @@ Revision = (r = /Revision: (\d+)/.match("$Revision$") ? r[1] : 0)
 RELOAD_FILES = ["shogi_server/league/floodgate.rb",
                 "shogi_server/league/persistent.rb",
                 "shogi_server/pairing.rb"]
+BASE_DIR = File.expand_path(File.dirname(__FILE__))
 
 def reload
-  here = TOP_DIR || File.dirname(__FILE__)
   RELOAD_FILES.each do |f|
-    load File.join(here, f)
+    load File.join(BASE_DIR, f)
   end
 end
 module_function :reload
 
+class Logger < ::Logger
+  def initialize(logdev, shift_age = 0, shift_size = 1048576)
+    super
+    class << @logdev
+      def shift_log_period(now)
+        postfix = previous_period_end(now).strftime("%Y%m%d")  # YYYYMMDD
+        age_file = File.join(
+                     File.dirname(@filename),
+                     postfix[0..3], # YYYY
+                     postfix[4..5], # MM
+                     postfix[6..7], # DD
+                     File.basename(@filename))
+        if FileTest.exist?(age_file)
+          raise RuntimeError.new("'#{ age_file }' already exists.")
+        end
+        unless FileTest.directory?(File.dirname(age_file))
+          begin
+            FileUtils.mkdir_p File.dirname(age_file)
+          rescue
+            raise RuntimeError.new("Could not create a directory: %s" % [File.dirname(age_file)])
+          end
+        end
+        @dev.close
+        File.rename("#{@filename}", age_file)
+        @dev = create_logfile(@filename)
+        return true
+      end
+    end
+  end
+end
+
 class Formatter < ::Logger::Formatter
   def initialize
     super