OSDN Git Service

../shogi_server.rb, ../shogi_server/util.rb: Refactored mkdir_for to Mkdir.mkdir_for.
[shogi-server/shogi-server.git] / shogi_server.rb
index 3268222..56c778b 100644 (file)
@@ -33,13 +33,14 @@ require 'shogi_server/board'
 require 'shogi_server/game'
 require 'shogi_server/league'
 require 'shogi_server/login'
+require 'shogi_server/move'
 require 'shogi_server/piece'
 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,16 +56,73 @@ 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)
+        age_file = age_file_name(now)
+        move_age_file_in_the_way(age_file)
+        unless Mkdir.mkdir_for(age_file)
+          @dev.write("[ERROR] Could not create a directory: %s\n" % [File.dirname(age_file)])
+          return true
+        end
+
+        @dev.close
+        rename_file(@filename, age_file)
+        @dev = create_logfile(@filename)
+        return true
+      end
+
+      def age_file_name(time)
+        postfix = previous_period_end(time).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))
+        return age_file
+      end 
+
+      def age_file_exists?(age_file)
+        return FileTest.exist?(age_file)
+      end
+
+      def rename_file(old_file, new_file)
+        File.rename(old_file, new_file)
+      end
+
+      def move_age_file_in_the_way(age_file)
+        return unless age_file_exists?(age_file)
+        
+        now = Time.now
+        new_file = "%s.%s%06d"  % [age_file, now.strftime("%Y%m%d%H%M%S"), now.usec]
+        @dev.write("[WARN] An existing '#{age_file}' is beeing moved to '#{new_file}'\n")
+        rename_file(age_file, new_file)
+      end
+
+    end # @logdev
+  end
+
+end # class Logger
+
 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