lib/request_log_analyzer/controller.rb in request-log-analyzer-1.9.9 vs lib/request_log_analyzer/controller.rb in request-log-analyzer-1.9.10

- old
+ new

@@ -18,53 +18,53 @@ attr_reader :source, :filters, :aggregators, :output, :options # Builds a RequestLogAnalyzer::Controller given parsed command line arguments # <tt>arguments<tt> A CommandLine::Arguments hash containing parsed commandline parameters. def self.build_from_arguments(arguments) - + require 'mixins/gets_memory_protection' if arguments[:gets_memory_protection] - + options = {} - + # Copy fields options[:database] = arguments[:database] options[:reset_database] = arguments[:reset_database] options[:debug] = arguments[:debug] options[:yaml] = arguments[:yaml] || arguments[:dump] options[:mail] = arguments[:mail] options[:no_progress] = arguments[:no_progress] options[:format] = arguments[:format] - options[:output] = arguments[:output].downcase + options[:output] = arguments[:output] options[:file] = arguments[:file] options[:after] = arguments[:after] options[:before] = arguments[:before] options[:reject] = arguments[:reject] options[:select] = arguments[:select] options[:boring] = arguments[:boring] options[:aggregator] = arguments[:aggregator] options[:report_width] = arguments[:report_width] options[:report_sort] = arguments[:report_sort] options[:report_amount] = arguments[:report_amount] - options[:mailhost] = arguments[:mailhost] + options[:mailhost] = arguments[:mailhost] options[:mailsubject] = arguments[:mailsubject] - options[:silent] = arguments[:silent] - options[:parse_strategy] = arguments[:parse_strategy] - + options[:silent] = arguments[:silent] + options[:parse_strategy] = arguments[:parse_strategy] + # Apache format workaround if arguments[:rails_format] options[:format] = {:rails => arguments[:rails_format]} elsif arguments[:apache_format] options[:format] = {:apache => arguments[:apache_format]} end - + # Handle output format casing if options[:output].class == String options[:output] = 'FancyHTML' if options[:output] =~ /^fancy_?html$/i options[:output] = 'HTML' if options[:output] =~ /^html$/i options[:output] = 'FixedWidth' if options[:output] =~ /^fixed_?width$/i end - + # Register sources if arguments.parameters.length == 1 file = arguments.parameters[0] if file == '-' || file == 'STDIN' options.store(:source_files, $stdin) @@ -75,11 +75,11 @@ exit(0) end else options.store(:source_files, arguments.parameters) end - + # Guess file format if !options[:format] && options[:source_files] options[:format] = :rails # Default if options[:source_files] != $stdin @@ -89,11 +89,11 @@ elsif options[:source_files].class == Array && options[:source_files].first != $stdin options[:format] = RequestLogAnalyzer::FileFormat.autodetect(options[:source_files].first) end end end - + build(options) end # Build a new controller. # Returns a new RequestLogAnalyzer::Controller object. @@ -122,11 +122,11 @@ # # === Example # RequestLogAnalyzer::Controller.build( # :output => :HTML, # :mail => 'root@localhost', - # :after => Time.now - 24*60*60, + # :after => Time.now - 24*60*60, # :source_files => '/var/log/passenger.log' # ).run! # # === Todo # * Check if defaults work (Aggregator defaults seem wrong). @@ -139,31 +139,31 @@ options[:report_width] ||= 80 options[:report_amount] ||= 20 options[:report_sort] ||= 'sum,mean' options[:boring] ||= false options[:silent] ||= false - + options[:no_progress] = true if options[:silent] - + # Deprecation warnings if options[:dump] warn "[DEPRECATION] `:dump` is deprecated. Please use `:yaml` instead." options[:yaml] = options[:dump] end - + # Set the output class output_args = {} output_object = nil if options[:output].is_a?(Class) output_class = options[:output] else output_class = RequestLogAnalyzer::Output.const_get(options[:output]) end - + output_sort = options[:report_sort].split(',').map { |s| s.to_sym } output_amount = options[:report_amount] == 'all' ? :all : options[:report_amount].to_i - + if options[:file] output_object = %w[File StringIO].include?(options[:file].class.name) ? options[:file] : File.new(options[:file], "w+") output_args = {:width => 80, :color => false, :characters => :ascii, :sort => output_sort, :amount => output_amount } elsif options[:mail] output_object = RequestLogAnalyzer::Mailer.new(options[:mail], options[:mailhost], :subject => options[:mailsubject]) @@ -171,30 +171,30 @@ else output_object = STDOUT output_args = {:width => options[:report_width].to_i, :color => !options[:boring], :characters => (options[:boring] ? :ascii : :utf), :sort => output_sort, :amount => output_amount } end - + output_instance = output_class.new(output_object, output_args) - + # Create the controller with the correct file format if options[:format].kind_of?(Hash) file_format = RequestLogAnalyzer::FileFormat.load(options[:format].keys[0], options[:format].values[0]) else file_format = RequestLogAnalyzer::FileFormat.load(options[:format]) end - + # Kickstart the controller - controller = - Controller.new(RequestLogAnalyzer::Source::LogParser.new(file_format, - :source_files => options[:source_files], + controller = + Controller.new(RequestLogAnalyzer::Source::LogParser.new(file_format, + :source_files => options[:source_files], :parse_strategy => options[:parse_strategy]), { :output => output_instance, :database => options[:database], # FUGLY! - :yaml => options[:yaml], + :yaml => options[:yaml], :reset_database => options[:reset_database], - :no_progress => options[:no_progress], + :no_progress => options[:no_progress], :silent => options[:silent] }) # register filters if options[:after] || options[:before] @@ -228,11 +228,11 @@ controller.add_aggregator(:echo) if options[:debug] controller.add_aggregator(:database_inserter) if options[:database] && !options[:aggregator].include?('database') file_format.setup_environment(controller) return controller - end + end # Builds a new Controller for the given log file format. # <tt>format</tt> Logfile format. Defaults to :rails # Options are passd on to the LogParser. # * <tt>:database</tt> Database the controller should use. @@ -246,10 +246,10 @@ @options = options @aggregators = [] @filters = [] @output = options[:output] @interrupted = false - + # Register the request format for this session after checking its validity raise "Invalid file format!" unless @source.file_format.valid? # Install event handlers for wrnings, progress updates and source changes @source.warning = lambda { |type, message, lineno| @aggregators.each { |agg| agg.warning(type, message, lineno) } }