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, index_path: nil) ⇒ Reader

Returns a new instance of Reader.



16
17
18
19
20
21
22
23
# File 'lib/roby/droby/logfile/reader.rb', line 16

def initialize(event_io, index_path: nil)
    @event_io = event_io
    @index_path = index_path ||
                  "#{event_io.path.gsub(/\.log$/, '')}.idx"
    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.



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

def event_io
  @event_io
end

#index_pathString (readonly)

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

Returns:

  • (String)


94
95
96
# File 'lib/roby/droby/logfile/reader.rb', line 94

def index_path
  @index_path
end

Class Method Details

.open(path, index_path: nil) ⇒ Reader .open(path, index_path: nil) {|reader| ... } ⇒ Object

Open a reader on the file indicated by path

Overloads:

  • .open(path, index_path: nil) ⇒ Reader

    Returns:

  • .open(path, index_path: nil) {|reader| ... } ⇒ Object

    Yield the reader, and automatically closes it on return of the block

    Yield Parameters:

    Returns:

    • the value returned by the block

Parameters:

  • path (String)

    the path to the file

  • index_path (String, nil) (defaults to: nil)

    the path to the file’s index. If nil, the index path will be the file’s path with the ‘.log’ extension replaced by ‘.idx’



148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/roby/droby/logfile/reader.rb', line 148

def self.open(path, index_path: nil)
    io = File.open(path)
    reader = new(File.open(path), index_path: index_path)
    return reader unless block_given?

    begin
        yield(reader)
    ensure
        reader.close unless reader.closed?
    end
rescue ::Exception => e
    io.close if io && !io.closed?
    raise
end

.process_options_hash(options_hash) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/roby/droby/logfile/reader.rb', line 74

def self.process_options_hash(options_hash)
    options_hash[:plugins]&.each do |plugin_name|
        begin
            Roby.app.using plugin_name
        rescue Roby::Application::PluginsDisabled => e
            Roby.warn "the log file mentions the #{plugin_name} plugin, "\
                      "but plugins are currently disabled. "\
                      "Some information might not be displayed"
        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

Instance Method Details

#closeObject



44
45
46
# File 'lib/roby/droby/logfile/reader.rb', line 44

def close
    event_io.close
end

#closed?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/roby/droby/logfile/reader.rb', line 52

def closed?
    event_io.closed?
end

#decode_one_chunk(chunk) ⇒ Object



64
65
66
# File 'lib/roby/droby/logfile/reader.rb', line 64

def decode_one_chunk(chunk)
    Logfile.decode_one_chunk(chunk)
end

#dupObject



36
37
38
# File 'lib/roby/droby/logfile/reader.rb', line 36

def dup
    Reader.new(event_io.dup)
end

#eof?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/roby/droby/logfile/reader.rb', line 48

def eof?
    event_io.eof?
end

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

Returns the index object for this event log



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/roby/droby/logfile/reader.rb', line 107

def index(path = index_path, rebuild: true)
    if @index
        return @index
    elsif !File.file?(path)
        unless 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

#load_one_cycleObject



68
69
70
71
72
# File 'lib/roby/droby/logfile/reader.rb', line 68

def load_one_cycle
    return unless (chunk = read_one_chunk)

    decode_one_chunk(chunk)
end

#read_headerObject



25
26
27
28
29
30
31
32
33
34
# File 'lib/roby/droby/logfile/reader.rb', line 25

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

#read_one_chunkObject



60
61
62
# File 'lib/roby/droby/logfile/reader.rb', line 60

def read_one_chunk
    Logfile.read_one_chunk(event_io)
end

#rebuild_index(path = index_path) ⇒ Object



96
97
98
99
100
101
102
103
104
# File 'lib/roby/droby/logfile/reader.rb', line 96

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

#seek(pos) ⇒ Object



56
57
58
# File 'lib/roby/droby/logfile/reader.rb', line 56

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

#tellObject



40
41
42
# File 'lib/roby/droby/logfile/reader.rb', line 40

def tell
    event_io.tell
end