diff --git a/lib/hydra.rb b/lib/hydra.rb index fc81862..bcd4f1f 100644 --- a/lib/hydra.rb +++ b/lib/hydra.rb @@ -1,3 +1,4 @@ +require 'hydra/trace' require 'hydra/pipe' require 'hydra/ssh' require 'hydra/stdio' diff --git a/lib/hydra/runner.rb b/lib/hydra/runner.rb index 5670eb9..62caf9b 100644 --- a/lib/hydra/runner.rb +++ b/lib/hydra/runner.rb @@ -10,6 +10,7 @@ module Hydra #:nodoc: # of a machine. class Runner include Hydra::Messages::Runner + traceable('RUNNER') # Boot up a runner. It takes an IO object (generally a pipe from its # parent) to send it messages on which files to execute. def initialize(opts = {}) @@ -18,20 +19,20 @@ module Hydra #:nodoc: Test::Unit.run = true $stdout.sync = true - $stdout.write "RUNNER| Booted. Sending Request for file\n" if @verbose + trace 'Booted. Sending Request for file' @io.write RequestFile.new begin process_messages rescue => ex - $stdout.write "#{ex.to_s}\n" if @verbose + trace ex.to_s raise ex end end # Run a test file and report the results def run_file(file) - $stdout.write "RUNNER| Running file: #{file}\n" if @verbose + trace "Running file: #{file}" require file output = [] @result = Test::Unit::TestResult.new @@ -60,20 +61,20 @@ module Hydra #:nodoc: # The runner will continually read messages and handle them. def process_messages - $stdout.write "RUNNER| Processing Messages\n" if @verbose + trace "Processing Messages" @running = true while @running begin message = @io.gets if message - $stdout.write "RUNNER| Received message from worker\n" if @verbose - $stdout.write " | #{message.inspect}\n" if @verbose + trace "Received message from worker" + trace "\t#{message.inspect}" message.handle(self) else @io.write Ping.new end rescue IOError => ex - $stderr.write "Runner lost Worker\n" if @verbose + $stderr.write "Runner lost Worker" @running = false end end diff --git a/lib/hydra/trace.rb b/lib/hydra/trace.rb new file mode 100644 index 0000000..3c41567 --- /dev/null +++ b/lib/hydra/trace.rb @@ -0,0 +1,23 @@ +module Hydra #:nodoc: + # Trace output when in verbose mode. + module Trace + module ClassMethods + # Make a class traceable. Takes one parameter, + # which is the prefix for the trace to identify this class + def traceable(prefix = self.class.to_s) + include Hydra::Trace::InstanceMethods + class << self; attr_accessor :_traceable_prefix; end + self._traceable_prefix = prefix + end + end + + module InstanceMethods + # Trace some output with the class's prefix and a newline. + # Checks to ensure we're running verbosely. + def trace(str) + $stdout.write "#{self.class._traceable_prefix}| #{str}\n" if @verbose + end + end + end +end +Object.extend(Hydra::Trace::ClassMethods)