Class: Roby::DRoby::EventLogger
- 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
-
#current_cycle ⇒ Object
readonly
The set of events for the current cycle.
-
#dump_time ⇒ Object
readonly
The time spent logging so far.
-
#logfile ⇒ #dump
readonly
The object that will be given the cycles to be written.
-
#marshal ⇒ DRoby::Marshal
readonly
The marshalling object.
-
#object_manager ⇒ DRoby::ObjectManager
readonly
The object manager.
Instance Method Summary collapse
- #append_message(m, time, args) ⇒ Object
-
#close ⇒ Object
Close this logger, flushing the remaining data to I/O.
-
#dump(m, time, args) ⇒ Object
Dump one log message.
-
#dump_loop ⇒ Object
Main dump loop if the logger is threaded.
- #dump_timepoint(event, time, args) ⇒ Object
- #flush ⇒ Object
- #flush_cycle(*last_message) ⇒ Object
-
#initialize(logfile, queue_size: 50) ⇒ EventLogger
constructor
A new instance of EventLogger.
- #log_queue_size ⇒ Object
-
#stats_mode=(flag) ⇒ Object
Controls whether the logger should only dump statistics, or the full set of plan events.
-
#stats_mode? ⇒ Object
Controls whether the logger should only dump statistics, or the full set of plan events.
-
#sync=(flag) ⇒ Object
Controls whether log data should be flushed on disk after each cycle.
-
#sync? ⇒ Object
Controls whether log data should be flushed on disk after each cycle.
- #synchronize(&block) ⇒ Object
- #threaded? ⇒ Boolean
Constructor Details
#initialize(logfile, queue_size: 50) ⇒ EventLogger
Returns a new instance of EventLogger.
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_cycle ⇒ Object (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_time ⇒ Object (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
11 12 13 |
# File 'lib/roby/droby/event_logger.rb', line 11 def logfile @logfile end |
#marshal ⇒ DRoby::Marshal (readonly)
The marshalling object
25 26 27 |
# File 'lib/roby/droby/event_logger.rb', line 25 def marshal @marshal end |
#object_manager ⇒ DRoby::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 (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 ⇒ Object
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 (m, time, args) end ensure @dump_time += (Time.now - start) end |
#dump_loop ⇒ Object
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 |
#flush ⇒ Object
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(*) start = Time.now if threaded? if !@dump_thread.alive? @dump_thread.value end synchronize do (*) @dump_queue << @current_cycle @current_cycle = Array.new end else (*) logfile.dump(@current_cycle) if sync? logfile.flush end @current_cycle.clear end ensure @dump_time += (Time.now - start) end |
#log_queue_size ⇒ Object
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
79 80 81 |
# File 'lib/roby/droby/event_logger.rb', line 79 def threaded? !!@dump_queue end |