lib/stack_tracy.rb in stack_tracy-0.1.3 vs lib/stack_tracy.rb in stack_tracy-0.1.4

- old
+ new

@@ -16,11 +16,11 @@ PRESETS = { :core => "Array BasicObject Enumerable Fixnum Float Hash IO Integer Kernel Module Mutex Numeric Object Rational String Symbol Thread Time", :active_record => "ActiveRecord::Base", :data_mapper => "DataMapper::Resource" } - @options = Struct.new(:dump_dir, :only, :exclude).new(Dir::tmpdir) + @options = Struct.new(:dump_dir, :dump_source_location, :limit, :threshold, :only, :exclude).new(Dir::tmpdir, false, 7500, 0.001) class Error < StandardError; end def config yield @options @@ -65,30 +65,36 @@ line << event[:call] line << " <#{"%.6f" % event[:duration]}>" if event[:duration] } end - def dump(path = nil, *only) + def dump(path = nil, dump_source_location = nil, *only) unless path && path.match(/\.csv$/) path = File.join [path || @options.dump_dir, "stack_events-#{SecureRandom.hex(3)}.csv"].compact end File.expand_path(path).tap do |path| - keys = [:event, :file, :line, :singleton, :object, :method, :nsec, :time, :call, :depth, :duration] + bool = dump_source_location.nil? ? @options[:dump_source_location] : dump_source_location + keys = [:event, (:file if bool), (:line if bool), :singleton, :object, :method, :nsec, :time, :call, :depth, :duration] File.open(path, "w") do |file| file << keys.join(";") + "\n" select(only).each do |event| file << event.values_at(*keys).join(";") + "\n" end end + yield path if block_given? + stack_trace.clear end end - def open(path = nil, use_current_stack_trace = false) - unless use_current_stack_trace + def open(path = nil, use_current_stack_trace = false, threshold = nil, limit = nil) + if use_current_stack_trace + file = File.expand_path(path) if path + else unless path && path.match(/\.csv$/) - path = Dir[File.join(path || @options.dump_dir, "stack_events-*.csv")].sort_by{|f| File.mtime(f)}.last - path ||= Dir[File.join(path || Dir::tmpdir, "stack_events-*.csv")].sort_by{|f| File.mtime(f)}.last + path = Dir[File.join(path || @options.dump_dir, "stack_events-*.csv")].sort_by{|f| File.mtime(f)}.last + path ||= Dir[File.join(".", "stack_events-*.csv")].sort_by{|f| File.mtime(f)}.last + path ||= Dir[File.join(Dir::tmpdir, "stack_events-*.csv")].sort_by{|f| File.mtime(f)}.last end if path file = File.expand_path(path) else raise Error, "Could not locate StackTracy file" @@ -96,9 +102,11 @@ end index = ui("index.html") if use_current_stack_trace || (file && File.exists?(file)) + threshold = threshold.nil? ? @options[:threshold] : threshold + limit = limit.nil? ? @options[:limit] : limit events = use_current_stack_trace ? select : StackTracy::EventInfo.to_hashes(File.read(file)) erb = ERB.new File.new(ui("index.html.erb")).read File.open(index, "w"){|f| f.write erb.result(binding)} elsif path && path.match(/\.csv$/) raise Error, "Could not locate StackTracy file at #{file}" \ No newline at end of file