def _append_message(self, msg, latched, current_time=None, store=True): ''' Adds a label to the dialog's layout and shows the given text. @param msg: the text to add to the dialog @type msg: message object ''' if current_time is None: current_time = time.time() self._latched = latched if store: self._msgs.append((msg, current_time)) if len(self._msgs) > 25: self._msgs.pop() msg_len = -1 if (self.SHOW_BYTES or self.show_only_rate): buff = None try: from cStringIO import StringIO # Python 2.x buff = StringIO() import os msg.serialize(buff) buff.seek(0, os.SEEK_END) msg_len = buff.tell() except ImportError: from io import BytesIO # Python 3.x buff = BytesIO() msg.serialize(buff) msg_len = buff.getbuffer().nbytes self._count_messages(current_time, msg_len) # skip messages, if they are received often then MESSAGE_HZ_LIMIT if self._last_received_ts != 0 and self.receiving_hz != 0: if current_time - self._last_received_ts < 1.0 / self.receiving_hz: if (not latched or (latched and current_time - self._start_time > 3.0)): self._scrapped_msgs += 1 self._scrapped_msgs_sl += 1 return self._last_received_ts = current_time if not self.show_only_rate: # convert message to string and reduce line width to current limit msg = self.strify_message(msg, field_filter=self.field_filter_fn) if isinstance(msg, tuple): msg = msg[0] msg = self._trim_width(msg) msg = msg.replace('<', '<').replace('>', '>') msg_cated = False if self.chars_limit != 0 and len(msg) > self.chars_limit: msg = msg[0:self.chars_limit] msg_cated = True # create a notification about scrapped messages if self._scrapped_msgs_sl > 0: txt = '<pre style="color:red; font-family:Fixedsys,Courier,monospace; padding:10px;">scrapped %s message because of Hz-settings</pre>' % self._scrapped_msgs_sl self.display.append(txt) self._scrapped_msgs_sl = 0 txt = '<pre style="background-color:#FFFCCC; color:#000000;font-family:Fixedsys,Courier; padding:10px;">---------- %s --------------------\n%s</pre>' % (datetime.now().strftime("%d.%m.%Y %H:%M:%S.%f"), msg) # set the count of the displayed messages on receiving the first message self._update_max_msg_count(txt) self.display.append(txt) if msg_cated: txt = '<pre style="color:red; font-family:Fixedsys,Courier,monospace; padding:10px;">message has been cut off</pre>' self.display.append(txt) if store: self._print_status()
def _append_message(self, msg, latched, current_time=None, store=True): ''' Adds a label to the dialog's layout and shows the given text. @param msg: the text to add to the dialog @type msg: message object ''' if current_time is None: current_time = time.time() self._latched = latched if store: with self.lock: self._msgs.append((msg, current_time)) if len(self._msgs) > 25: self._msgs.pop() msg_len = -1 if (self.SHOW_BYTES or self.show_only_rate): buff = None try: from cStringIO import StringIO # Python 2.x buff = StringIO() import os msg.serialize(buff) buff.seek(0, os.SEEK_END) msg_len = buff.tell() except ImportError: from io import BytesIO # Python 3.x buff = BytesIO() msg.serialize(buff) msg_len = buff.getbuffer().nbytes self._count_messages(current_time, msg_len) # skip messages, if they are received often then MESSAGE_HZ_LIMIT if self._last_received_ts != 0 and self.receiving_hz != 0: if current_time - self._last_received_ts < 1.0 / self.receiving_hz: if (not latched or (latched and current_time - self._start_time > 3.0)): self._scrapped_msgs += 1 self._scrapped_msgs_sl += 1 return self._last_received_ts = current_time if not self.show_only_rate: # convert message to string and reduce line width to current limit msg = self.strify_message(msg, field_filter=self.field_filter_fn) if isinstance(msg, tuple): msg = msg[0] msg = self._trim_width(msg) msg = msg.replace('<', '<').replace('>', '>') msg_cated = False if self.chars_limit != 0 and len(msg) > self.chars_limit: msg = msg[0:self.chars_limit] msg_cated = True # create a notification about scrapped messages if self._scrapped_msgs_sl > 0: txt = '<pre style="color:red; font-family:Fixedsys,Courier,monospace; padding:10px;">scrapped %s message because of Hz-settings</pre>' % self._scrapped_msgs_sl self.display.append(txt) self._scrapped_msgs_sl = 0 txt = '<pre style="background-color:#FFFCCC; color:#000000;font-family:Fixedsys,Courier; padding:10px;">---------- %s --------------------\n%s</pre>' % (datetime.now().strftime("%d.%m.%Y %H:%M:%S.%f"), msg) # set the count of the displayed messages on receiving the first message self._update_max_msg_count(txt) self.display.append(txt) if msg_cated: txt = '<pre style="color:red; font-family:Fixedsys,Courier,monospace; padding:10px;">message has been cut off</pre>' self.display.append(txt) if store: self._print_status()