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, log_timepoints: false) ⇒ 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



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/roby/droby/event_logger.rb', line 64

def initialize(logfile, queue_size: 50, log_timepoints: false)
    @stats_mode = false
    @logfile = logfile
    @object_manager = ObjectManager.new(nil)
    @marshal = Marshal.new(object_manager, nil)
    @current_cycle = []
    @sync = true
    @dump_time = 0
    @mutex = Mutex.new
    @log_timepoints = log_timepoints
    return unless queue_size > 0

    @dump_queue  = SizedQueue.new(queue_size)
    @dump_thread = Thread.new(&method(:dump_loop))
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



17
18
19
# File 'lib/roby/droby/event_logger.rb', line 17

def current_cycle
  @current_cycle
end

#dump_timeObject (readonly)

The time spent logging so far



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

def dump_time
  @dump_time
end

#logfile#dump (readonly)

The object that will be given the cycles to be written

Returns:



13
14
15
# File 'lib/roby/droby/event_logger.rb', line 13

def logfile
  @logfile
end

#marshalDRoby::Marshal (readonly)

The marshalling object

Returns:



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

def marshal
  @marshal
end

#object_managerDRoby::ObjectManager (readonly)

The object manager



22
23
24
# File 'lib/roby/droby/event_logger.rb', line 22

def object_manager
  @object_manager
end

Instance Method Details

#append_message(m, time, args) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/roby/droby/event_logger.rb', line 116

def append_message(m, time, args)
    case m
    when :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)]
    when :finalized_task
        task = args[1]
        args = marshal.dump(args)
        object_manager.deregister_object(task)
    when :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



107
108
109
110
111
112
113
114
# File 'lib/roby/droby/event_logger.rb', line 107

def close
    if threaded?
        @dump_queue.push nil
        @dump_thread.join
    end
ensure
    logfile.close
end

#dump(m, time, args) ⇒ Object

Dump one log message



155
156
157
158
159
160
161
162
163
# File 'lib/roby/droby/event_logger.rb', line 155

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



185
186
187
188
189
190
# File 'lib/roby/droby/event_logger.rb', line 185

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

#dump_timepoint(event, time, args) ⇒ Object



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

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

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

#flushObject



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

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



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/roby/droby/event_logger.rb', line 165

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

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

#log_queue_sizeObject



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

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

#log_timepoints=(flag) ⇒ Object

Controls whether the logger should save generated timepoints or ignore them. This makes the logs bigger by an order of magnitude (at least)



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

attr_predicate :log_timepoints, true

#log_timepoints?Object

Controls whether the logger should save generated timepoints or ignore them. This makes the logs bigger by an order of magnitude (at least)



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

attr_predicate :log_timepoints, true

#stats_mode=(flag) ⇒ Object

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



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

attr_predicate :stats_mode, true

#stats_mode?Object

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



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

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



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

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



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

attr_predicate :sync?, true

#synchronize(&block) ⇒ Object



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

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

#threaded?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/roby/droby/event_logger.rb', line 91

def threaded?
    @dump_queue
end