Class: Roby::DRoby::Logfile::Reader

Inherits:
Object
  • Object
show all
Defined in:
lib/roby/droby/logfile/reader.rb

Overview

A class that reads log files generated by Writer

Constant Summary collapse

FORMAT_VERSION =

The current log format version

5

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(event_io) ⇒ Reader



14
15
16
17
18
19
# File 'lib/roby/droby/logfile/reader.rb', line 14

def initialize(event_io)
    @event_io = event_io
    event_io.rewind
    options_hash = read_header
    self.class.process_options_hash(options_hash)
end

Instance Attribute Details

#event_ioObject (readonly)

Returns the value of attribute event_io.



12
13
14
# File 'lib/roby/droby/logfile/reader.rb', line 12

def event_io
  @event_io
end

Class Method Details

.open(path) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/roby/droby/logfile/reader.rb', line 123

def self.open(path)
    io = new(File.open(path))
    if block_given?
        begin
            yield(io)
        ensure
            io.close unless io.closed?
        end
    else
        io
    end
end

.process_options_hash(options_hash) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/roby/droby/logfile/reader.rb', line 68

def self.process_options_hash(options_hash)
    if options_hash[:plugins]
        options_hash[:plugins].each do |plugin_name|
            begin
                Roby.app.using plugin_name
            rescue ArgumentError => e
                Roby.warn "the log file mentions the #{plugin_name} plugin, but it is not available on this system. Some information might not be displayed"
            end
        end
    end
end

Instance Method Details

#closeObject



38
39
40
# File 'lib/roby/droby/logfile/reader.rb', line 38

def close
    event_io.close
end

#closed?Boolean



46
47
48
# File 'lib/roby/droby/logfile/reader.rb', line 46

def closed?
    event_io.closed?
end

#dupObject



30
31
32
# File 'lib/roby/droby/logfile/reader.rb', line 30

def dup
    Reader.new(event_io.dup)
end

#eof?Boolean



42
43
44
# File 'lib/roby/droby/logfile/reader.rb', line 42

def eof?
    event_io.eof?
end

#index(path = index_path, rebuild: true) ⇒ Object

Returns the index object for this event log



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/roby/droby/logfile/reader.rb', line 98

def index(path = index_path, rebuild: true)
    if @index
        return @index
    elsif !File.file?(path)
        if !rebuild
            raise IndexMissing, "there's no file #{path}"
        end
        rebuild_index(path)
    end

    index =
        begin Index.read(path)
        rescue Exception => e
            raise e, "while reading index file #{path}: #{e.message}", e.backtrace
        end
    if index.valid_for?(event_io.path)
        @index = index
    elsif !rebuild
        raise IndexInvalid, "#{path} is not a valid index for #{self}"
    else
        rebuild_index(path)
        @index = Index.read(path)
    end
end

#index_pathString

The standard Roby index path, inferred from the log file’s own path



84
85
86
# File 'lib/roby/droby/logfile/reader.rb', line 84

def index_path
    event_io.path.gsub(/\.log$/, '') + ".idx"
end

#load_one_cycleObject



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/roby/droby/logfile/reader.rb', line 54

def load_one_cycle
    if chunk = Logfile.read_one_chunk(event_io)
        begin ::Marshal.load_with_missing_constants(chunk)
        rescue ArgumentError => e
            if e.message == "marshal data too short"
                raise TruncatedFileError, "marshal data invalid"
            else raise
            end
        end
    end
rescue Exception => e
    raise e, "#{e.message}, running roby-log repair might repair the file", e.backtrace
end

#read_headerObject



21
22
23
24
25
26
27
28
# File 'lib/roby/droby/logfile/reader.rb', line 21

def read_header
    Logfile.read_prologue(event_io)
    if chunk = Logfile.read_one_chunk(event_io)
        ::Marshal.load(chunk)
    else
        raise InvalidFileError, "expected the prologue to be followed by one chunk, but got nothing"
    end
end

#rebuild_index(path = index_path) ⇒ Object



88
89
90
91
92
93
94
95
# File 'lib/roby/droby/logfile/reader.rb', line 88

def rebuild_index(path = index_path)
    Logfile.warn "rebuilding index file for #{event_io.path}"
    File.open(path, 'w') do |index_io|
        event_io = self.event_io.dup
        Index.rebuild(File.open(event_io.path), index_io)
    end
    @index = nil
end

#seek(pos) ⇒ Object



50
51
52
# File 'lib/roby/droby/logfile/reader.rb', line 50

def seek(pos)
    event_io.seek(pos)
end

#tellObject



34
35
36
# File 'lib/roby/droby/logfile/reader.rb', line 34

def tell
    event_io.tell
end