lib/rotoscope.rb in rotoscope-0.3.0.pre.6 vs lib/rotoscope.rb in rotoscope-0.3.0.pre.7
- old
+ new
@@ -1,57 +1,56 @@
# frozen_string_literal: true
require 'rotoscope/rotoscope'
-require 'fileutils'
-require 'tempfile'
require 'csv'
class Rotoscope
class << self
- def new(output_path, blacklist: [], flatten: false)
- super(output_path, blacklist, flatten)
- end
-
- def trace(dest, blacklist: [], flatten: false, &block)
- config = { blacklist: blacklist, flatten: flatten }
- if dest.is_a?(String)
- event_trace(dest, config, &block)
+ def new(output, blacklist: [])
+ if output.is_a?(String)
+ io = File.open(output, 'w')
+ prevent_flush_from_finalizer_in_fork(io)
+ obj = super(io, blacklist)
+ obj.log_path = output
+ obj
else
- io_event_trace(dest, config, &block)
+ super(output, blacklist)
end
end
- private
-
- def with_temp_file(name)
- temp_file = Tempfile.new(name)
- yield temp_file
+ def trace(dest, blacklist: [])
+ rs = new(dest, blacklist: blacklist)
+ rs.trace { yield rs }
+ rs
ensure
- temp_file.close! if temp_file
+ rs.close if rs && dest.is_a?(String)
end
- def temp_event_trace(config, block)
- with_temp_file("rotoscope_output") do |temp_file|
- rs = event_trace(temp_file.path, config, &block)
- yield rs
- rs
- end
- end
+ private
- def io_event_trace(dest_io, config, &block)
- temp_event_trace(config, block) do |rs|
- File.open(rs.log_path) do |rs_file|
- IO.copy_stream(rs_file, dest_io)
- end
+ def prevent_flush_from_finalizer_in_fork(io)
+ pid = Process.pid
+ finalizer = lambda do |_|
+ next if Process.pid == pid
+ # close the file descriptor from another IO object so
+ # buffered writes aren't flushed
+ IO.for_fd(io.fileno).close
end
+ ObjectSpace.define_finalizer(io, finalizer)
end
+ end
- def event_trace(dest_path, config)
- rs = Rotoscope.new(dest_path, blacklist: config[:blacklist], flatten: config[:flatten])
- rs.trace { yield rs }
- rs
- ensure
- rs.close if rs
+ attr_accessor :log_path
+
+ def mark(message = "")
+ state = self.state
+ if state == :tracing
+ # stop tracing to avoid logging these io method calls
+ stop_trace
end
+ io.write("--- ")
+ io.puts(message)
+ ensure
+ start_trace if state == :tracing
end
def closed?
state == :closed
end