about summary refs log tree commit homepage
path: root/lib/unicorn/util.rb
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2009-05-04 02:01:32 -0700
committerEric Wong <normalperson@yhbt.net>2009-05-04 02:22:21 -0700
commit79d4a02e343e9fdf92535e2689f10d4a311cc88d (patch)
treea4d45692eae2bef032c85b25e1bd4745a9cd0beb /lib/unicorn/util.rb
parent17dc99cc5c3a3fc59145724059f36c5c907f6c3f (diff)
downloadunicorn-79d4a02e343e9fdf92535e2689f10d4a311cc88d.tar.gz
Ensure we preserve both internal and external encodings
when reopening logs.
Diffstat (limited to 'lib/unicorn/util.rb')
-rw-r--r--lib/unicorn/util.rb15
1 files changed, 10 insertions, 5 deletions
diff --git a/lib/unicorn/util.rb b/lib/unicorn/util.rb
index 0400fd0..2d3f827 100644
--- a/lib/unicorn/util.rb
+++ b/lib/unicorn/util.rb
@@ -4,6 +4,8 @@ module Unicorn
   class Util
     class << self
 
+      APPEND_FLAGS = File::WRONLY | File::APPEND
+
       # this reopens logs that have been rotated (using logrotate(8) or
       # similar).  It is recommended that you install
       # A +File+ object is considered for reopening if it is:
@@ -17,17 +19,20 @@ module Unicorn
         ObjectSpace.each_object(File) do |fp|
           next if fp.closed?
           next unless (fp.sync && fp.path[0..0] == "/")
-
-          flags = fp.fcntl(Fcntl::F_GETFL)
-          open_flags = File::WRONLY | File::APPEND
-          next unless (flags & open_flags) == open_flags
+          next unless (fp.fcntl(Fcntl::F_GETFL) & APPEND_FLAGS) == APPEND_FLAGS
 
           begin
             a, b = fp.stat, File.stat(fp.path)
             next if a.ino == b.ino && a.dev == b.dev
           rescue Errno::ENOENT
           end
-          fp.reopen(fp.path, "a")
+
+          open_arg = 'a'
+          if fp.respond_to?(:external_encoding) && enc = fp.external_encoding
+            open_arg << ":#{enc.to_s}"
+            enc = fp.internal_encoding and open_arg << ":#{enc.to_s}"
+          end
+          fp.reopen(fp.path, open_arg)
           fp.sync = true
           nr += 1
         end # each_object