logs_table_model.cpp
Go to the documentation of this file.
1 #include "logs_table_model.hpp"
2 #include <QDateTime>
3 #include <QBrush>
4 #include <QColor>
5 #include <QDebug>
6 
8  : QAbstractTableModel(parent),
9  _logs(MAX_CAPACITY) // initial capacity
10 {
11  _count = 0;
12 }
13 
14 QVariant LogsTableModel::headerData(int section, Qt::Orientation orientation, int role) const
15 {
16 
17  if (role != Qt::DisplayRole)
18  return QVariant();
19 
20  if (orientation == Qt::Horizontal){
21  switch(section)
22  {
23  case 0: return "#"; break;
24  case 1: return "Time"; break;
25  case 2: return "Severity";break;
26  case 3: return "Node";break;
27  case 4: return "Message";break;
28  case 5: return "Source";break;
29  }
30  }
31  else{
32  return QString("%1").arg(section);
33  }
34 
35  return QVariant();
36 }
37 
38 int LogsTableModel::rowCount(const QModelIndex &parent) const
39 {
40  if (parent.isValid())
41  return 0;
42 
43  return _logs.size();
44 }
45 
46 int LogsTableModel::columnCount(const QModelIndex &parent) const
47 {
48  if (parent.isValid())
49  return 0;
50 
51  return 5;
52 }
53 
54 QVariant LogsTableModel::data(const QModelIndex &index, int role) const
55 {
56  if (!index.isValid())
57  return QVariant();
58 
59  if (index.row() >= _logs.size())
60  return QVariant();
61 
62  const LogItem& log = _logs[ index.row() ];
63 
64  if (role == Qt::DisplayRole)
65  {
66  switch( index.column() )
67  {
68  case 0: return (int)log.count;
69  case 1: return log.time_text;
70  case 2: {
71  switch( log.level_raw )
72  {
73  case DEBUG: return "DEBUG";
74  case INFO: return "INFO";
75  case WARNINGS: return "WARNINGS";
76  case ERROR: return "ERROR";
77  }
78  } break;
79  case 3: return ( *log.node );
80  case 4: return log.message;
81  case 5: return ( *log.source );
82  }
83  }
84  else if( role== Qt::ForegroundRole){
85  switch( log.level_raw )
86  {
87  case DEBUG: return QBrush( QColor::fromRgb(50, 50 , 50)) ; // black
88  case INFO: return QBrush( QColor::fromRgb(0, 0 , 255)); // blue
89  case WARNINGS: return QBrush( QColor::fromRgb(240, 120, 0)); // orange
90  case ERROR: return QBrush( QColor::fromRgb(255, 0 , 0)); // red
91  }
92  }
93  else if( role == Qt::UserRole){
94  switch( index.column() )
95  {
96  case 0: return (int)log.count;
97  case 1: {
98  auto usec = (long long)log.time_usec_since_epoch;
99  return QVariant(usec);
100  }
101  case 2: return log.level_raw;
102  case 3: return *log.node;
103  case 4: return log.message;
104  case 5: return *log.source;
105  }
106  }
107  else{
108  return QVariant();
109  }
110  return QVariant();
111 }
112 
114 {
115  _count++;
116 
117  LogItem item;
118  switch( log.level ){
119  case rosgraph_msgs::Log::DEBUG : item.level_raw = DEBUG; break;
120  case rosgraph_msgs::Log::INFO : item.level_raw = INFO;break;
121  case rosgraph_msgs::Log::WARN : item.level_raw = WARNINGS;break;
122  case rosgraph_msgs::Log::ERROR : item.level_raw = ERROR;break;
123  }
124 
125  item.count = _count;
126 
127  QString node_name = QString::fromStdString(log.name);
128  auto node_it =_node_list.find(node_name);
129 
130  if( node_it == _node_list.end()){
131  auto inserted_ret = _node_list.insert(node_name);
132  node_it = inserted_ret.first;
133  }
134  item.node = &(*node_it);
135 
136  QString source_name(log.file.c_str());
137  source_name += (" ");
138  source_name += QString::fromStdString(log.function);
139  source_name += (":");
140  source_name += QString::number(log.line);
141 
142 
143  auto source_it =_source_list.find(source_name);
144 
145  if( source_it == _source_list.end()){
146  auto inserted_ret = _source_list.insert(source_name);
147  source_it = inserted_ret.first;
148  }
149  item.source = &(*source_it);
150 
151  item.message = log.msg.c_str();
152 
153  item.time_usec_since_epoch = log.header.stamp.toNSec() / 1000;
154  item.time_text = QDateTime::fromMSecsSinceEpoch( item.time_usec_since_epoch / 1000 ).toString("d/M/yy HH:mm::ss.zzz");
155  return item;
156 }
157 
158 void LogsTableModel::push_back(const rosgraph_msgs::Log::ConstPtr& pushed_log)
159 {
160  bool to_shift = (_logs.size() == MAX_CAPACITY);
161 
162  _logs.push_back( convertRosout( *pushed_log) );
163 
164  if(to_shift)
165  {
166  this->beginRemoveRows( QModelIndex(), 0 , 0);
167  this->endRemoveRows();
168  emit dataChanged( index(0,0), index( rowCount()-1, columnCount() -1));
169  }
170 
171  this->beginInsertRows( QModelIndex(), _logs.size() - 1 , _logs.size() - 1);
172  this->endInsertRows();
173 }
174 
175 void LogsTableModel::push_back(const std::vector<rosgraph_msgs::Log::ConstPtr>& pushed_logs)
176 {
177  size_t old_size = _logs.size();
178  size_t new_size = old_size + pushed_logs.size();
179 
180  int to_add = pushed_logs.size();
181  int to_shift = 0;
182 
183  if( new_size > MAX_CAPACITY)
184  {
185  to_add = (MAX_CAPACITY - old_size);
186  to_shift = new_size - MAX_CAPACITY;
187  new_size = MAX_CAPACITY;
188  }
189 
190  const size_t last_row = new_size - 1;
191  const size_t first_row = new_size - pushed_logs.size() ;
192 
193  for (int i=0; i < pushed_logs.size(); i++){
194  _logs.push_back( convertRosout( *pushed_logs[i]) );
195  }
196 
197  std::sort(_logs.begin(), _logs.end(),
198  [](const LogItem& a, const LogItem& b) { return a.time_usec_since_epoch < b.time_usec_since_epoch; } );
199 
200  if(to_shift > 0)
201  {
202  this->beginRemoveRows( QModelIndex(), 0 , 0);
203  this->endRemoveRows();
204 
205  emit dataChanged( index(0,0), index( rowCount()-1, columnCount() -1));
206  }
207 
208  this->beginInsertRows( QModelIndex(), first_row , last_row);
209  this->endInsertRows();
210 }
211 
212 
213 const QString &LogsTableModel::message(int index) const
214 {
215  return _logs[ index ].message;
216 }
217 
218 const QString& LogsTableModel::nodeName(int index) const
219 {
220  return *( _logs[ index ].node );
221 }
222 
224 {
225  return _logs[ index ].level_raw;
226 }
227 
229 {
230  std::chrono::microseconds since_epoch( _logs[ index ].time_usec_since_epoch );
231  return TimePoint() + since_epoch;
232 }
233 
235  this->beginRemoveRows( QModelIndex(), 0 , _logs.size() -1);
236  this->endRemoveRows();
237  _logs.clear();
238  _count = 0;
239 }
240 
241 
int columnCount(const QModelIndex &parent=QModelIndex()) const override
LogItem convertRosout(const rosgraph_msgs::Log &log)
std::chrono::high_resolution_clock::time_point TimePoint
TimePoint timestamp(int index) const
LogsTableModel(QObject *parent=0)
std::set< QString > _node_list
const QString & message(int index) const
boost::circular_buffer< LogItem > _logs
std::set< QString > _source_list
Severity severity(int index) const
int rowCount(const QModelIndex &parent=QModelIndex()) const override
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
bool log
const QString & nodeName(int index) const
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const override
void push_back(const rosgraph_msgs::Log::ConstPtr &pushed_log)
uint64_t b
int i
int a


plotjuggler
Author(s): Davide Faconti
autogenerated on Sat Jul 6 2019 03:44:17