From 81d663302f08246407ba899a642c10b207a5e1ef Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 17 Jul 2013 16:53:31 +0900 Subject: [PATCH] for Windows platform --- lib/fluent/log.rb | 7 +++- lib/fluent/plugin/in_tail.rb | 58 +++++++++++++++++++++++--- lib/fluent/supervisor.rb | 81 +++++++++++++++++++++++++----------- 3 files changed, 116 insertions(+), 30 deletions(-) diff --git a/lib/fluent/log.rb b/lib/fluent/log.rb index 9a1550797d..36dd9c7ec7 100644 --- a/lib/fluent/log.rb +++ b/lib/fluent/log.rb @@ -49,7 +49,12 @@ def initialize(out=STDERR, level=LEVEL_TRACE) @self_event = false @tag = 'fluent' @time_format = '%Y-%m-%d %H:%M:%S %z ' - enable_color out.tty? + if $platformwin == false + enable_color out.tty? + else + enable_color false + end + # TODO: This variable name is unclear so we should change to better name. @threads_exclude_events = [] end diff --git a/lib/fluent/plugin/in_tail.rb b/lib/fluent/plugin/in_tail.rb index 23fe445f69..2ffed8b6ab 100644 --- a/lib/fluent/plugin/in_tail.rb +++ b/lib/fluent/plugin/in_tail.rb @@ -159,7 +159,11 @@ def on_notify while @rotate_queue.first.ready? if io = @rotate_queue.first.io stat = io.stat + if $platformwin == false inode = stat.ino + else + inode = Win32File.getfileindex(io.path) + end if inode == @pe.read_inode # rotated file has the same inode number with the last file. # assuming following situation: @@ -188,7 +192,11 @@ def on_rotate(io) # first time stat = io.stat fsize = stat.size - inode = stat.ino + if $platformwin == false + inode = stat.ino + else + inode = Win32File.getfileindex(io.path) + end last_inode = @pe.read_inode if inode == last_inode @@ -301,10 +309,18 @@ def on_notify begin while true - if @buffer.empty? - @io.read_nonblock(2048, @buffer) + if $platformwin == false + if @buffer.empty? + @io.read_nonblock(2048, @buffer) + else + @buffer << @io.read_nonblock(2048, @iobuf) + end else - @buffer << @io.read_nonblock(2048, @iobuf) + if @buffer.empty? + @io.readpartial(2048, @buffer) + else + @buffer << @io.readpartial(2048, @buffer) + end end while line = @buffer.slice!(/.*?\n/m) lines << line @@ -363,7 +379,11 @@ def on_notify begin io = File.open(@path) stat = io.stat - inode = stat.ino + if $platformwin == false + inode = stat.ino + else + inode = Win32File.getfileindex(io.path) + end fsize = stat.size rescue Errno::ENOENT # moved or deleted @@ -492,4 +512,32 @@ def read_inode end end end + #temprary code + require 'Win32API' + class Win32File + def initialize + super + end + + def Win32File.getfileindex(path) + createfile = Win32API.new('kernel32', 'CreateFile', %w(p i i i i i i), 'i') + closehandle = Win32API.new('kernel32', 'CloseHandle', 'i', 'v') + getFileInformation = Win32API.new('kernel32', 'GetFileInformationByHandle', %w(i p), 'i') + + file_handle = createfile.call(path, 0, 0, 0, 3, 0x80, 0 ) + if file_handle == -1 + return 0 + end + + by_handle_file_information = '\0'*(4+8+8+8+4+4+4+4+4+4) #72bytes + ret = getFileInformation.call(file_handle, by_handle_file_information) + closehandle.call(file_handle) + if ret == 0 + return 0 + end + + return by_handle_file_information.unpack("I11Q1")[11] + end + end + end diff --git a/lib/fluent/supervisor.rb b/lib/fluent/supervisor.rb index fe9c6b5cc7..5dcb96ad8a 100644 --- a/lib/fluent/supervisor.rb +++ b/lib/fluent/supervisor.rb @@ -17,7 +17,7 @@ # module Fluent - +$usespawn = 0 class Supervisor class LoggerInitializer def initialize(path, level, chuser, chgroup) @@ -69,7 +69,12 @@ def initialize(opt) @inline_config = opt[:inline_config] @suppress_interval = opt[:suppress_interval] @dry_run = opt[:dry_run] + $usespawn = opt[:usespawn] + $platformwin = false + if RUBY_PLATFORM.downcase =~ /mswin(?!ce)|mingw|cygwin|bccwin/ + $platformwin = true + end @log = LoggerInitializer.new(@log_path, @log_level, @chuser, @chgroup) @finished = false @main_pid = nil @@ -168,9 +173,25 @@ def supervise(&block) start_time = Time.now $log.info "starting fluentd-#{Fluent::VERSION}" + $log.info "is windows platform : #{$platformwin}" + + if $platformwin == false @main_pid = fork do main_process(&block) end + else + if $usespawn == 0 then + flunetd_spawn_cmd = "fluentd " + $fluentdargv.each{|a| + flunetd_spawn_cmd << (a + " ") + } + flunetd_spawn_cmd << "-u" + @main_pid = Process.spawn(flunetd_spawn_cmd) + else + @main_pid = Process.pid + main_process(&block) + end + end if @daemonize && @wait_daemonize_pipe_w STDIN.reopen("/dev/null") @@ -180,7 +201,13 @@ def supervise(&block) @wait_daemonize_pipe_w = nil end - Process.waitpid(@main_pid) + if $platform == false + Process.waitpid(@main_pid) + else + if $usespawn == 0 + Process.waitpid(@main_pid) + end + end @main_pid = nil ecode = $?.to_i @@ -249,25 +276,28 @@ def install_supervisor_signal_handlers end end - trap :HUP do - $log.debug "fluentd supervisor process get SIGHUP" - $log.info "restarting" - if pid = @main_pid - Process.kill(:TERM, pid) - # don't resuce Erro::ESRSH here (invalid status) + if $platformwin == false + trap :HUP do + $log.debug "fluentd supervisor process get SIGHUP" + $log.info "restarting" + if pid = @main_pid + Process.kill(:TERM, pid) + # don't resuce Erro::ESRSH here (invalid status) + end end end - trap :USR1 do - $log.debug "fluentd supervisor process get SIGUSR1" - @log.reopen! - if pid = @main_pid - Process.kill(:USR1, pid) - # don't resuce Erro::ESRSH here (invalid status) + if $platformwin == false + trap :USR1 do + $log.debug "fluentd supervisor process get SIGUSR1" + @log.reopen! + if pid = @main_pid + Process.kill(:USR1, pid) + # don't resuce Erro::ESRSH here (invalid status) + end end end end - def read_config $log.info "reading config file", :path=>@config_path @config_fname = File.basename(@config_path) @@ -356,16 +386,19 @@ def install_main_process_signal_handlers end end - trap :HUP do - # TODO - $log.debug "fluentd main process get SIGHUP" + if $platformwin == false + trap :HUP do + # TODO + $log.debug "fluentd main process get SIGHUP" + end end - - trap :USR1 do - $log.debug "fluentd main process get SIGUSR1" - $log.info "force flushing buffered events" - @log.reopen! - Fluent::Engine.flush! + if $platformwin == false + trap :USR1 do + $log.debug "fluentd main process get SIGUSR1" + $log.info "force flushing buffered events" + @log.reopen! + Fluent::Engine.flush! + end end end