@@ -230,24 +230,33 @@ class ASCWriter(Listener):
230230 """Logs CAN data to an ASCII log file (.asc)"""
231231
232232 LOG_STRING = "{time: 9.4f} {channel} {id:<15} Rx d {dlc} {data}\n "
233+ EVENT_STRING = "{time: 9.4f} {message}\n "
233234
234- def __init__ (self , filename ):
235+ def __init__ (self , filename , channel = 1 ):
235236 now = datetime .now ().strftime ("%a %b %m %I:%M:%S %p %Y" )
237+ self .channel = channel
236238 self .started = time .time ()
237239 self .log_file = open (filename , "w" )
238240 self .log_file .write ("date %s\n " % now )
239241 self .log_file .write ("base hex timestamps absolute\n " )
240242 self .log_file .write ("internal events logged\n " )
241243 self .log_file .write ("Begin Triggerblock %s\n " % now )
242- self .log_file . write ( " 0.0000 Start of measurement\n " )
244+ self .log_event ( " Start of measurement" )
243245
244246 def stop (self ):
245247 """Stops logging and closes the file."""
246- if self .log_file :
248+ if self .log_file is not None :
247249 self .log_file .write ("End TriggerBlock\n " )
248250 self .log_file .close ()
249251 self .log_file = None
250252
253+ def log_event (self , message ):
254+ """Add an arbitrary message to the log file."""
255+ timestamp = time .time () - self .started
256+ line = self .EVENT_STRING .format (time = timestamp , message = message )
257+ if self .log_file is not None :
258+ self .log_file .write (line )
259+
251260 def on_message_received (self , msg ):
252261 data = ["{:02X}" .format (byte ) for byte in msg .data ]
253262 arb_id = "{:X}" .format (msg .arbitration_id )
@@ -258,9 +267,9 @@ def on_message_received(self, msg):
258267 timestamp -= self .started
259268
260269 line = self .LOG_STRING .format (time = timestamp ,
261- channel = 1 ,
270+ channel = self . channel ,
262271 id = arb_id ,
263272 dlc = msg .dlc ,
264273 data = " " .join (data ))
265- if self .log_file :
274+ if self .log_file is not None :
266275 self .log_file .write (line )
0 commit comments