Class: Roby::DRoby::EventLogger

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

Overview

Object that acts as an observer for ExecutablePlan, handling the droby marshalling/demarshalling. Dumping to IO is delegated to #logfile, a separate object that must provide a #dump method the way Logfile::Writer does

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logfile, queue_size: 50) ⇒ EventLogger

Returns a new instance of EventLogger.

Parameters:

  • marshal (#dump)

    the object that transforms the arguments into droby-compatible objects

  • queue_size (Integer) (defaults to: 50)

    if non-zero, the access to I/O will be done in a separate thread, and this parameter is the maximum amount of cycles that can be queued in a backlog until the main thread waits on the logger



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

def initialize(logfile, queue_size: 50)
    @stats_mode = false
    @logfile = logfile
    @object_manager = ObjectManager.new(nil)
    @marshal = Marshal.new(object_manager, nil)
    @current_cycle = Array.new
    @sync = true
    @dump_time = 0
    @mutex = Mutex.new
    if queue_size > 0
        @dump_queue  = SizedQueue.new(queue_size)
        @dump_thread = Thread.new(&method(:dump_loop))
    end
end

Instance Attribute Details

#current_cycleObject (readonly)

The set of events for the current cycle. This is dumped only when the cycle_end event is received



15
16
17
# File 'lib/roby/droby/event_logger.rb', line 15

def current_cycle
  @current_cycle
end

#dump_timeObject (readonly)

The time spent logging so far



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

def dump_time
  @dump_time
end

#logfile#dump (readonly)

The object that will be given the cycles to be written

Returns:



11
12
13
# File 'lib/roby/droby/event_logger.rb', line 11

def logfile
  @logfile
end

#marshalDRoby::Marshal (readonly)

The marshalling object

Returns:



25
26
27
# File 'lib/roby/droby/event_logger.rb', line 25

def marshal
  @marshal
end

#object_managerDRoby::ObjectManager (readonly)

The object manager



20
21
22
# File 'lib/roby/droby/event_logger.rb', line 20

def object_manager
  @object_manager
end

Instance Method Details

#append_message(m, time, args) ⇒ Object



106
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
132
133
# File 'lib/roby/droby/event_logger.rb', line 106

def append_message(m, time, args)
    if m == :merged_plan
        plan_id, merged_plan = *args

        merged_plan.tasks.each do |t|
            object_manager.register_object(t)
        end
        merged_plan.free_events.each do |e|
            object_manager.register_object(e)
        end
        merged_plan.task_events.each do |e|
            object_manager.register_object(e)
        end
        args = [plan_id, merged_plan.droby_dump(marshal)]
    elsif m == :finalized_task
        task = args[1]
        args = marshal.dump(args)
        object_manager.deregister_object(task)
    elsif m == :finalized_event
        event = args[1]
        args = marshal.dump(args)
        object_manager.deregister_object(event)
    else
        args = marshal.dump(args)
    end

    @current_cycle << m << time.tv_sec << time.tv_usec << args
end

#closeObject

Close this logger, flushing the remaining data to I/O



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

def close
    dump(:cycle_end, Time.now, [Hash.new])
    if threaded?
        @dump_queue.push nil
        @dump_thread.join
    end

ensure
    logfile.close
end

#dump(m, time, args) ⇒ Object

Dump one log message



144
145
146
147
148
149
150
151
152
# File 'lib/roby/droby/event_logger.rb', line 144

def dump(m, time, args)
    return if stats_mode?

    start = Time.now
    synchronize do
        append_message(m, time, args)
    end
ensure @dump_time += (Time.now - start)
end

#dump_loopObject

Main dump loop if the logger is threaded



178
179
180
181
182
183
184
185
# File 'lib/roby/droby/event_logger.rb', line 178

def dump_loop
    while cycle = @dump_queue.pop
        logfile.dump(cycle)
        if sync?
            logfile.flush
        end
    end
end

#dump_timepoint(event, time, args) ⇒ Object



135
136
137
138
139
140
141
# File 'lib/roby/droby/event_logger.rb', line 135

def dump_timepoint(event, time, args)
    return if stats_mode?

    synchronize do
        @current_cycle << event << time.tv_sec << time.tv_usec << args
    end
end

#flushObject



83
84
85
86
87
88
89
90
91
92
# File 'lib/roby/droby/event_logger.rb', line 83

def flush
    if threaded?
        @dump_queue.push nil
        @dump_thread.join
        logfile.flush
        @dump_thread = Thread.new(&method(:dump_loop))
    else
        logfile.flush
    end
end

#flush_cycle(*last_message) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/roby/droby/event_logger.rb', line 154

def flush_cycle(*last_message)
    start = Time.now
    if threaded?
        if !@dump_thread.alive?
            @dump_thread.value
        end

        synchronize do
            append_message(*last_message)
            @dump_queue << @current_cycle
            @current_cycle = Array.new
        end
    else
        append_message(*last_message)
        logfile.dump(@current_cycle)
        if sync?
            logfile.flush
        end
        @current_cycle.clear
    end
ensure @dump_time += (Time.now - start)
end

#log_queue_sizeObject



73
74
75
76
77
# File 'lib/roby/droby/event_logger.rb', line 73

def log_queue_size
    if threaded? then @dump_queue.size
    else 0
    end
end

#stats_mode=(flag) ⇒ Object

Controls whether the logger should only dump statistics, or the full set of plan events



35
# File 'lib/roby/droby/event_logger.rb', line 35

attr_predicate :stats_mode, true

#stats_mode?Object

Controls whether the logger should only dump statistics, or the full set of plan events



35
# File 'lib/roby/droby/event_logger.rb', line 35

attr_predicate :stats_mode, true

#sync=(flag) ⇒ Object

Controls whether log data should be flushed on disk after each cycle. It is set by default. Disable for improved performance if the data will not be displayed live

Application disables it by default if the log server is disabled



46
# File 'lib/roby/droby/event_logger.rb', line 46

attr_predicate :sync?, true

#sync?Object

Controls whether log data should be flushed on disk after each cycle. It is set by default. Disable for improved performance if the data will not be displayed live

Application disables it by default if the log server is disabled



46
# File 'lib/roby/droby/event_logger.rb', line 46

attr_predicate :sync?, true

#synchronize(&block) ⇒ Object



69
70
71
# File 'lib/roby/droby/event_logger.rb', line 69

def synchronize(&block)
    @mutex.synchronize(&block)
end

#threaded?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/roby/droby/event_logger.rb', line 79

def threaded?
    !!@dump_queue
end