Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 2462

Show
Ignore:
Timestamp:
10/05/05 00:54:26 (3 years ago)
Author:
minam
Message:

gauge: make the log reader really work

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • tools/gauge/lib/gauge/log_reader.rb

    r2461 r2462  
    55    LogRecord = Struct.new(:time, :pid, :name, :data) 
    66 
     7    attr_reader :wait_duration 
     8 
     9    def initialize(application, directory_or_io, options={}) 
     10      @wait_duration = options[:wait_duration] 
     11      super 
     12    end 
     13 
    714    def next_record 
     15      loop do 
     16        record = read_record 
     17        return record if record 
     18 
     19        # if number is nil, then we're not reading a log sequence. Just return 
     20        # nil to indicate the end of the file. 
     21        return nil if number.nil? 
     22 
     23        # okay, we're at the very end of the sequence. let's check to see if 
     24        # there is another log file in the sequence... 
     25        if File.exist?(name_for(number + 1)) 
     26          # if so, close the current file, open the next file, and grab the 
     27          # first record from it. 
     28          self.next! 
     29          return next_record 
     30        else 
     31          # if not, just return nil to indicate the end of the sequence 
     32          return nil 
     33        end 
     34      end 
     35    end 
     36 
     37    def read_record 
    838      return nil unless @io 
    9       @partial_read ||= "" 
    1039 
    11       size = @io.read(4 - @partial_read.length) 
    12       if size.nil? || size.length < 4 - @partial_read.length 
    13         @io.pos = @io.pos # reset eof 
    14         @partial_read << size if size 
     40      size = @io.read(4) 
     41      if size.nil? 
     42        @io.pos = @io.pos # reset eof flag 
    1543        return nil 
     44      elsif size.length < 4 
     45        size << safe_read(4 - size.length) 
    1646      end 
    1747 
    18       size = (@partial_read << size).unpack("N").first 
    19       @partial_read =  nil 
     48      buffer = safe_read(size.unpack("N").first) 
     49      record = LogRecord.new(*buffer.unpack("GN")) 
    2050 
    21       buffer = "" 
    22       loop do 
    23         unless v = @io.read(size - buffer.length) 
    24           sleep 0.1 
    25           @io.pos = @io.pos # reset eof 
    26           next 
    27         end 
    28  
    29         buffer << v 
    30         break if buffer.length == size 
    31       end 
    32  
    33       record = LogRecord.new(*buffer.unpack("GN")) 
    3451      name = buffer[13,buffer[12]] 
    3552      record.name = name.to_sym 
     
    3956 
    4057    private 
     58 
     59      def safe_read(length) 
     60        buffer = "" 
     61        loop do 
     62          unless v = @io.read(length - buffer.length) 
     63            sleep 0.1 
     64            @io.pos = @io.pos # reset eof 
     65            next 
     66          end 
     67 
     68          buffer << v 
     69          return buffer if buffer.length == length 
     70        end 
     71      end 
    4172 
    4273      def open_log 
  • tools/gauge/lib/gauge/sequential_log.rb

    r2461 r2462  
    1313        @io = directory_or_io 
    1414      else 
     15        @directory = directory_or_io 
    1516        setup_log(directory_or_io) 
    1617      end 
     
    3132    private 
    3233 
    33       def setup_log_sequence_in(directory) 
    34         @directory = directory 
    35         existing_logs = Dir["#{directory}/#{@application}-*.log"].sort 
    36         @number = existing_logs.empty? ? 0 : 
    37           existing_logs.last.match(/#{@application}-(\d+)\.log/)[1].to_i 
    38         @io = open_log 
     34      def name_for(number) 
     35        "#{directory}/#{@application}-%04d.log" % number 
    3936      end 
    4037 
    4138      def open_log 
    42         @name = "#{directory}/#{@application}-%04d.log" % number 
     39        @name = name_for(number) 
    4340        log = File.open(name, log_access_mode) 
    4441        log.sync = true 
  • tools/gauge/test/log_reader_test.rb

    r2461 r2462  
    2929  end 
    3030 
    31   def test_partial_read 
    32     data = record 
    33     io = StringIO.new(data[0,3]) 
    34     reader = Gauge::LogReader.new(:test, io) 
    35     record = reader.next_record 
    36     assert_nil record 
    37     io.string, io.pos = data, 3 # needed because of bug in StringIO 
    38     record = reader.next_record 
    39     assert_not_nil record 
    40     assert_equal $$, record.pid 
    41     assert_equal :test, record.name 
    42     assert_equal "demo", record.data 
    43     assert_nil reader.next_record 
    44   end 
    45  
    4631  private 
    4732