Package node_manager_fkie :: Module log_widget
[frames] | no frames]

Source Code for Module node_manager_fkie.log_widget

  1  # Software License Agreement (BSD License) 
  2  # 
  3  # Copyright (c) 2012, Fraunhofer FKIE/US, Alexander Tiderko 
  4  # All rights reserved. 
  5  # 
  6  # Redistribution and use in source and binary forms, with or without 
  7  # modification, are permitted provided that the following conditions 
  8  # are met: 
  9  # 
 10  #  * Redistributions of source code must retain the above copyright 
 11  #    notice, this list of conditions and the following disclaimer. 
 12  #  * Redistributions in binary form must reproduce the above 
 13  #    copyright notice, this list of conditions and the following 
 14  #    disclaimer in the documentation and/or other materials provided 
 15  #    with the distribution. 
 16  #  * Neither the name of Fraunhofer nor the names of its 
 17  #    contributors may be used to endorse or promote products derived 
 18  #    from this software without specific prior written permission. 
 19  # 
 20  # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
 21  # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
 22  # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
 23  # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
 24  # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
 25  # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
 26  # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
 27  # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
 28  # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
 29  # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
 30  # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 31  # POSSIBILITY OF SUCH DAMAGE. 
 32  import os 
 33  from datetime import datetime 
 34   
 35  from python_qt_binding import QtGui 
 36  from python_qt_binding import QtCore 
 37  from python_qt_binding import loadUi 
 38   
 39  from rosgraph_msgs.msg import Log 
 40   
 41  from .rosout_listener import RosoutListener 
 42   
43 -class LogWidget(QtGui.QDockWidget):
44 ''' 45 The collect the the warning log messages from rosout and print it in a text 46 browser. 47 ''' 48 49 added_signal = QtCore.Signal(int, int, int, int) 50 ''' 51 added_signal will be emitted on adding a new log entry. The parameter contains 52 the current count of messages (INFO, WARN, ERROR, FATAL) 53 ''' 54 55 cleared_signal = QtCore.Signal() 56
57 - def __init__(self, parent=None):
58 ''' 59 Creates the window, connects the signals and init the class. 60 ''' 61 QtGui.QDockWidget.__init__(self, parent) 62 self._log_info_count = 0 63 self._log_warn_count = 0 64 self._log_err_count = 0 65 self._log_fatal_count = 0 66 # load the UI file 67 log_dock_file = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'LogDockWidget.ui') 68 loadUi(log_dock_file, self) 69 self.hide() 70 # connect to the button signals 71 self.clearCloseButton.clicked.connect(self._on_log_clear_close_clicked) 72 self.closeButton.clicked.connect(self.hide) 73 # initialize the listener to the rosout topic 74 self._rosout_listener = RosoutListener() 75 self._rosout_listener.roswarn_signal.connect(self._on_roslog_warn) 76 self._rosout_listener.roserr_signal.connect(self._on_roslog_err) 77 self._rosout_listener.rosfatal_signal.connect(self._on_roslog_fatal) 78 self._rosout_listener.registerByROS()
79
80 - def count(self):
81 ''' 82 Returns the count all current viewed log messages. 83 ''' 84 return self._log_info_count + self._log_warn_count + self._log_err_count + self._log_fatal_count
85
86 - def clear(self):
87 ''' 88 Removes all log messages and emit the `cleared_signal`. 89 ''' 90 self._log_info_count = 0 91 self._log_warn_count = 0 92 self._log_err_count = 0 93 self._log_fatal_count = 0 94 self.textBrowser.clear() 95 self.infoLabel.setText('') 96 self.cleared_signal.emit()
97
98 - def stop(self):
99 ''' 100 Unregister the listener thread from the `/rosout` topic. This method must be 101 called at the exit! 102 ''' 103 self._rosout_listener.stop()
104
105 - def _on_roslog_info(self, msg):
106 self._log_info_count += 1 107 text = ('<pre style="padding:10px;"><dt><font color="#000000">' 108 '<b>[INFO]</b> %s (%s:%s:%s):' 109 '<br>%s</font></dt></pre>'%(self._formated_ts(msg.header.stamp), 110 msg.file, msg.function, msg.line, 111 msg.msg)) 112 self.textBrowser.append(text) 113 self._update_info_label()
114
115 - def _on_roslog_warn(self, msg):
116 self._log_warn_count += 1 117 text = ('<pre style="padding:10px;"><dt><font color="#FE9A2E">' 118 '<b>[WARN]</b> %s (%s:%s:%s):' 119 '<br>%s</font></dt></pre>'%(self._formated_ts(msg.header.stamp), 120 msg.file, msg.function, msg.line, 121 msg.msg)) 122 self.textBrowser.append(text) 123 self._update_info_label()
124
125 - def _on_roslog_err(self, msg):
126 self._log_err_count += 1 127 text = ('<pre style="padding:10px;"><dt><font color="#DF0101">' 128 '<b>[ERROR]</b> %s (%s:%s:%s):' 129 '<br>%s</font></dt></pre>'%(self._formated_ts(msg.header.stamp), 130 msg.file, msg.function, msg.line, 131 msg.msg)) 132 self.textBrowser.append(text) 133 self._update_info_label()
134
135 - def _on_roslog_fatal(self, msg):
136 self._log_fatal_count += 1 137 text = ('<pre style="padding:10px;"><dt><font color="#610B0B">' 138 '<b>[FATAL]</b> %s (%s:%s:%s):' 139 '<br>%s</font></dt></pre>'%(self._formated_ts(msg.header.stamp), 140 msg.file, msg.function, msg.line, 141 msg.msg)) 142 self.textBrowser.append(text) 143 self._update_info_label()
144
146 self.clear() 147 self.hide()
148
149 - def _update_info_label(self):
150 info_text = '' 151 if self._log_info_count > 0: 152 info_text = '%s INFO: %d '%(info_text, self._log_info_count) 153 if self._log_warn_count > 0: 154 info_text = '%s WARN: %d '%(info_text, self._log_warn_count) 155 if self._log_err_count > 0: 156 info_text = '%s ERROR: %d '%(info_text, self._log_err_count) 157 if self._log_fatal_count > 0: 158 info_text = '%s FATAL: %d'%(info_text, self._log_fatal_count) 159 self.infoLabel.setText(info_text) 160 self.added_signal.emit(self._log_info_count, self._log_warn_count, 161 self._log_err_count, self._log_fatal_count)
162
163 - def _formated_ts(self, stamp):
164 ts = stamp.secs + stamp.secs / 1000000000. 165 return datetime.fromtimestamp(ts).strftime("%d.%m.%Y %H:%M:%S.%f")
166