class Roby::DRoby::EventLogger
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
Attributes
current_cycle[R]
The set of events for the current cycle. This is dumped only when the cycle_end
event is received
dump_time[R]
The time spent logging so far
logfile[R]
The object that will be given the cycles to be written
@return [#dump]
marshal[R]
The marshalling object
@return [DRoby::Marshal]
object_manager[R]
The object manager
@return [DRoby::ObjectManager]
Public Class Methods
new(logfile, queue_size: 50)
click to toggle source
@param [#dump] marshal the object that transforms the arguments
into droby-compatible objects
@param [Integer] queue_size 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
# 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
Public Instance Methods
append_message(m, time, args)
click to toggle source
# 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
close()
click to toggle source
Close this logger, flushing the remaining data to I/O
# 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)
click to toggle source
Dump one log message
# 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_loop()
click to toggle source
Main dump loop if the logger is threaded
# 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)
click to toggle source
# 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
flush()
click to toggle source
# 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)
click to toggle source
# 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_size()
click to toggle source
# File lib/roby/droby/event_logger.rb, line 73 def log_queue_size if threaded? then @dump_queue.size else 0 end end
synchronize(&block)
click to toggle source
# File lib/roby/droby/event_logger.rb, line 69 def synchronize(&block) @mutex.synchronize(&block) end
threaded?()
click to toggle source
# File lib/roby/droby/event_logger.rb, line 79 def threaded? !!@dump_queue end