OSDN Git Service

Merge branch 'logger'
authordaigo <beatles@users.sourceforge.jp>
Sun, 25 Jul 2010 06:17:43 +0000 (15:17 +0900)
committerDaigo Moriwaki <daigo@debian.org>
Sun, 25 Jul 2010 06:17:43 +0000 (15:17 +0900)
Conflicts:
changelog

changelog
shogi_server.rb
test/TC_ALL.rb
test/TC_logger.rb [new file with mode: 0644]

index b088f4c..7c72303 100644 (file)
--- a/changelog
+++ b/changelog
@@ -5,6 +5,8 @@
            + For an unknown error command log, an empty line is no longer logged.
            + Commands specific to 81Dojo, starting with '%%%', are just
              ignored instead of unknown command errors.
+         - shogi_server.rb: Refactoring. Added test/TC_logger.rb to test
+           ShogiServer::Logger class.
 
 2010-07-23  Daigo Moriwaki <daigo at debian dot org>
 
index 05bc2d5..7467f6f 100644 (file)
@@ -70,31 +70,54 @@ class Logger < ::Logger
     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
+        age_file = age_file_name(now)
+        move_age_file_in_the_way(age_file)
+
         unless FileTest.directory?(File.dirname(age_file))
           begin
             FileUtils.mkdir_p File.dirname(age_file)
           rescue
+            @dev.write("[ERROR] Could not create a directory: %s\n" % [File.dirname(age_file)])
             raise RuntimeError.new("Could not create a directory: %s" % [File.dirname(age_file)])
           end
         end
         @dev.close
-        File.rename("#{@filename}", age_file)
+        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
   end
-end
+
+end # class Logger
 
 class Formatter < ::Logger::Formatter
   def initialize
index ff314f0..1c28109 100644 (file)
@@ -15,6 +15,7 @@ require 'TC_game_result'
 require 'TC_handicapped_boards'
 require 'TC_jishogi_kachi'
 require 'TC_league'
+require 'TC_logger'
 require 'TC_login'
 require 'TC_move'
 require 'TC_not_sennichite'
diff --git a/test/TC_logger.rb b/test/TC_logger.rb
new file mode 100644 (file)
index 0000000..89355bc
--- /dev/null
@@ -0,0 +1,80 @@
+$:.unshift File.join(File.dirname(__FILE__), "..")
+$topdir = File.expand_path File.dirname(__FILE__)
+require 'test/unit'
+require 'shogi_server'
+
+class TestableLogger < ShogiServer::Logger
+  def initialize(logdev, shift_age = 0, shift_size = 1048576)
+    super
+    class << @logdev
+      attr_accessor :return_age_file_exists
+      def age_file_exists?(age_file)
+        return @return_age_file_exists || false
+      end
+
+      attr_reader :result_rename_file
+      def rename_file(old_file, new_file)
+        @result_rename_file ||= []
+        @result_rename_file << [old_file, new_file]
+      end
+    end
+  end
+  attr_reader :logdev
+
+end
+
+class TestLogger < Test::Unit::TestCase
+  def setup
+    filename = File.join($topdir, "TC_logger_test.log")
+    @logger = TestableLogger.new(filename, "daily")
+    @logger.formatter = ShogiServer::Formatter.new
+    @logger.level = TestableLogger::DEBUG
+    @logger.datetime_format = "%Y-%m-%d %H:%M:%S"
+  end
+
+  def test_dummy
+    assert true
+  end
+
+  def test_age_file_name
+    time =  Time.mktime(2010, 7, 25, 23, 59, 59)
+    assert_equal File.expand_path(File.join($topdir, "2010", "07", "24", "TC_logger_test.log")), 
+                 @logger.logdev.age_file_name(time)
+  end
+
+  def test_age_file_exists
+    assert !@logger.logdev.age_file_exists?(nil)
+    @logger.logdev.return_age_file_exists = true
+    assert @logger.logdev.age_file_exists?(nil)
+  end
+
+  def test_rename_file
+    @logger.logdev.rename_file("old", "new")
+    assert_equal [["old", "new"]], @logger.logdev.result_rename_file
+  end
+
+  def test_move_age_file_in_the_way
+    assert !@logger.logdev.age_file_exists?(nil)
+    @logger.logdev.move_age_file_in_the_way("hoge.log")
+    assert_nil @logger.logdev.result_rename_file
+
+    @logger.logdev.return_age_file_exists = true
+    @logger.logdev.move_age_file_in_the_way("hoge.log")
+    assert_equal 1, @logger.logdev.result_rename_file.size
+    assert_equal "hoge.log", @logger.logdev.result_rename_file.first.first
+  end
+
+  def test_log_info
+    @logger.info("test_log_info")
+    assert true
+  end
+
+  def test_shift_log_period
+    @logger.info("test_shift_log_period")
+    now =  Time.mktime(2010, 7, 25, 23, 59, 59)
+    @logger.logdev.shift_log_period(now)
+    assert_equal [["/home/daigo/rubyprojects/shogi-server/test/TC_logger_test.log",
+        "/home/daigo/rubyprojects/shogi-server/test/2010/07/24/TC_logger_test.log"]], 
+        @logger.logdev.result_rename_file
+  end
+end