Class: Roby::DRoby::Logfile::Writer

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

Overview

A class that marshals DRoby cycle events into a log file using Ruby’s Marshal facility

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, **options) ⇒ Writer

Returns a new instance of Writer.



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

def initialize(event_io, **options)
    @event_io = event_io
    @buffer_io = StringIO.new("".dup, "w")

    Logfile.write_header(event_io, **options)
end

Instance Attribute Details

#buffer_ioObject (readonly)

Returns the value of attribute buffer_io.



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

def buffer_io
  @buffer_io
end

#event_ioObject (readonly)

Returns the value of attribute event_io.



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

def event_io
  @event_io
end

Class Method Details

.find_invalid_marshalling_object(obj, stack = Set.new) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/roby/droby/logfile/writer.rb', line 82

def self.find_invalid_marshalling_object(obj, stack = Set.new)
    return if stack.include?(obj)

    stack << obj

    case obj
    when Enumerable
        obj.each do |value|
            invalid, exception =
                find_invalid_marshalling_object(value, stack)
            return "#{invalid}, []", exception if invalid
        end
    end

    # Generic check for instance variables
    obj.instance_variables.each do |iv|
        value = obj.instance_variable_get(iv)
        invalid, exception =
            find_invalid_marshalling_object(value, stack)
        return "#{invalid}, #{iv}", exception if invalid
    end

    begin
        ::Marshal.dump(obj)
        nil
    rescue StandardError => e
        begin
            ["#{obj} (#{obj.class})", e]
        rescue StandardError
            ["-- cannot display object, #to_s raised -- "\
                   "(#{obj.class})", e]
        end
    end
end

.find_invalid_marshalling_object_in_cycle(cycle) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/roby/droby/logfile/writer.rb', line 55

def self.find_invalid_marshalling_object_in_cycle(cycle)
    cycle.each_slice(4) do |m, _, _, args|
        begin
            ::Marshal.dump(args)
        rescue StandardError => e
            Roby::DRoby::Logfile.fatal "failed to dump message #{m}: #{e}"
            args.each do |obj|
                begin
                    ::Marshal.dump(obj)
                rescue StandardError => e
                    Roby::DRoby::Logfile.fatal "cannot dump #{obj}"
                    Roby::DRoby::Logfile.fatal e.to_s
                    obj, exception = find_invalid_marshalling_object(obj)
                    if obj
                        Roby::DRoby::Logfile.fatal(
                            "  it seems that #{obj} can't be marshalled"
                        )
                        Roby::DRoby::Logfile.fatal(
                            "    #{exception.class}: #{exception.message}"
                        )
                    end
                end
            end
        end
    end
end

.open(path, **options) ⇒ Object



23
24
25
26
# File 'lib/roby/droby/logfile/writer.rb', line 23

def self.open(path, **options)
    event_io = File.open(path, "w")
    new(event_io, **options)
end

Instance Method Details

#closeObject



32
33
34
# File 'lib/roby/droby/logfile/writer.rb', line 32

def close
    event_io.close
end

#dump(cycle) ⇒ Object



48
49
50
51
52
53
# File 'lib/roby/droby/logfile/writer.rb', line 48

def dump(cycle)
    dump_object(cycle, event_io)
rescue StandardError
    self.class.find_invalid_marshalling_object_in_cycle(cycle)
    raise
end

#dump_object(object, io) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/roby/droby/logfile/writer.rb', line 36

def dump_object(object, io)
    buffer_io.truncate(0)
    buffer_io.seek(0)
    ::Marshal.dump(object, buffer_io)
    io.write([buffer_io.size].pack("L<"))
    io.write(buffer_io.string)
end

#flushObject



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

def flush
    event_io.flush
end

#tellObject



28
29
30
# File 'lib/roby/droby/logfile/writer.rb', line 28

def tell
    @event_io.tell
end