OSDN Git Service

* [shogi-server] The server now provides more accurate time control.
[shogi-server/shogi-server.git] / shogi_server.rb
index c59dd78..587798a 100644 (file)
@@ -29,11 +29,17 @@ require 'webrick'
 require 'fileutils'
 require 'logger'
 
-require 'rubygems'
-require 'active_support'
-Dependencies.log_activity = true
-Dependencies.load_paths << File.dirname(__FILE__)
-RAILS_DEFAULT_LOGGER = Logger.new($stdout) if $DEBUG
+require 'shogi_server/board'
+require 'shogi_server/game'
+require 'shogi_server/league'
+require 'shogi_server/login'
+require 'shogi_server/piece'
+require 'shogi_server/player'
+require 'shogi_server/timeout_queue'
+require 'shogi_server/usi'
+require 'shogi_server/util'
+require 'shogi_server/command'
+require 'shogi_server/buoy'
 
 module ShogiServer # for a namespace
 
@@ -46,7 +52,55 @@ Login_Time = 300                # time for LOGIN
 Release  = "$Id$"
 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
+  RELOAD_FILES.each do |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
+    @datetime_format = "%Y-%m-%dT%H:%M:%S"
+  end
+
   def call(severity, time, progname, msg)
     %!%s [%s] %s\n! % [format_datetime(time), severity, msg2str(msg)]
   end