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, log_timepoints: false) ⇒ EventLogger
constructor
A new instance of EventLogger.
- #log_queue_size ⇒ Object
-
#log_timepoints=(flag) ⇒ Object
Controls whether the logger should save generated timepoints or ignore them.
-
#log_timepoints? ⇒ Object
Controls whether the logger should save generated timepoints or ignore them.
-
#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, log_timepoints: false) ⇒ EventLogger
Returns a new instance of EventLogger.
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_cycle ⇒ Object (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_time ⇒ Object (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
13 14 15 |
# File 'lib/roby/droby/event_logger.rb', line 13 def logfile @logfile end |
#marshal ⇒ DRoby::Marshal (readonly)
The marshalling object
27 28 29 |
# File 'lib/roby/droby/event_logger.rb', line 27 def marshal @marshal end |
#object_manager ⇒ DRoby::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 (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 |
#close ⇒ Object
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 (m, time, args) end ensure @dump_time += (Time.now - start) end |
#dump_loop ⇒ Object
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 |
#flush ⇒ Object
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(*) start = Time.now if threaded? @dump_thread.value unless @dump_thread.alive? synchronize do (*) @dump_queue << @current_cycle @current_cycle = [] end else (*) logfile.dump(@current_cycle) logfile.flush if sync? @current_cycle.clear end ensure @dump_time += (Time.now - start) end |
#log_queue_size ⇒ Object
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
91 92 93 |
# File 'lib/roby/droby/event_logger.rb', line 91 def threaded? @dump_queue end |