logs_table_model.cpp
Go to the documentation of this file.
00001 #include "logs_table_model.hpp"
00002 #include <QDateTime>
00003 #include <QBrush>
00004 #include <QColor>
00005 #include <QDebug>
00006 
00007 LogsTableModel::LogsTableModel(QObject *parent)
00008     : QAbstractTableModel(parent),
00009       _logs(MAX_CAPACITY) // initial capacity
00010 {
00011     _count = 0;
00012 }
00013 
00014 QVariant LogsTableModel::headerData(int section, Qt::Orientation orientation, int role) const
00015 {
00016 
00017     if (role != Qt::DisplayRole)
00018         return QVariant();
00019 
00020     if (orientation == Qt::Horizontal){
00021         switch(section)
00022         {
00023         case 0: return "#"; break;
00024         case 1: return "Time"; break;
00025         case 2: return "Severity";break;
00026         case 3: return "Node";break;
00027         case 4: return "Message";break;
00028         case 5: return "Source";break;
00029         }
00030     }
00031     else{
00032         return QString("%1").arg(section);
00033     }
00034 
00035     return QVariant();
00036 }
00037 
00038 int LogsTableModel::rowCount(const QModelIndex &parent) const
00039 {
00040     if (parent.isValid())
00041         return 0;
00042 
00043     return _logs.size();
00044 }
00045 
00046 int LogsTableModel::columnCount(const QModelIndex &parent) const
00047 {
00048     if (parent.isValid())
00049         return 0;
00050 
00051     return 5;
00052 }
00053 
00054 QVariant LogsTableModel::data(const QModelIndex &index, int role) const
00055 {
00056     if (!index.isValid())
00057         return QVariant();
00058 
00059     if (index.row() >= _logs.size())
00060         return QVariant();
00061 
00062     const LogItem& log = _logs[ index.row() ];
00063 
00064     if (role == Qt::DisplayRole)
00065     {
00066         switch( index.column() )
00067         {
00068         case 0: return (int)log.count;
00069         case 1: return log.time_text;
00070         case 2: {
00071             switch( log.level_raw )
00072             {
00073             case DEBUG:      return "DEBUG";
00074             case INFO:       return "INFO";
00075             case WARNINGS:   return "WARNINGS";
00076             case ERROR:      return "ERROR";
00077             }
00078         } break;
00079         case 3: return ( *log.node );
00080         case 4: return log.message;
00081         case 5: return ( *log.source );
00082         }
00083     }
00084     else if( role== Qt::ForegroundRole){
00085         switch( log.level_raw )
00086         {
00087         case DEBUG:    return QBrush( QColor::fromRgb(50,  50 , 50)) ;  // black
00088         case INFO:     return QBrush( QColor::fromRgb(0,   0  , 255));  // blue
00089         case WARNINGS: return QBrush( QColor::fromRgb(240, 120, 0));    // orange
00090         case ERROR:    return QBrush( QColor::fromRgb(255, 0  , 0));    // red
00091         }
00092     }
00093     else if( role == Qt::UserRole){
00094         switch( index.column() )
00095         {
00096         case 0: return (int)log.count;
00097         case 1: {
00098             auto usec = (long long)log.time_usec_since_epoch;
00099             return QVariant(usec);
00100         }
00101         case 2: return log.level_raw;
00102         case 3: return *log.node;
00103         case 4: return log.message;
00104         case 5: return *log.source;
00105         }
00106     }
00107     else{
00108         return QVariant();
00109     }
00110     return QVariant();
00111 }
00112 
00113 LogsTableModel::LogItem LogsTableModel::convertRosout( const rosgraph_msgs::Log &log)
00114 {
00115     _count++;
00116 
00117     LogItem item;
00118     switch( log.level ){
00119     case rosgraph_msgs::Log::DEBUG : item.level_raw = DEBUG; break;
00120     case rosgraph_msgs::Log::INFO  : item.level_raw = INFO;break;
00121     case rosgraph_msgs::Log::WARN  : item.level_raw = WARNINGS;break;
00122     case rosgraph_msgs::Log::ERROR : item.level_raw = ERROR;break;
00123     }
00124 
00125     item.count   = _count;
00126 
00127     QString node_name = QString::fromStdString(log.name);
00128     auto node_it =_node_list.find(node_name);
00129 
00130     if( node_it == _node_list.end()){
00131         auto inserted_ret = _node_list.insert(node_name);
00132         node_it = inserted_ret.first;
00133     }
00134     item.node    = &(*node_it);
00135 
00136     QString source_name(log.file.c_str());
00137     source_name  += (" ");
00138     source_name  += QString::fromStdString(log.function);
00139     source_name  += (":");
00140     source_name  += QString::number(log.line);
00141 
00142 
00143     auto source_it =_source_list.find(source_name);
00144 
00145     if( source_it == _source_list.end()){
00146         auto inserted_ret = _source_list.insert(source_name);
00147         source_it = inserted_ret.first;
00148     }
00149     item.source = &(*source_it);
00150 
00151     item.message = log.msg.c_str();
00152 
00153     item.time_usec_since_epoch  = log.header.stamp.toNSec() / 1000;
00154     item.time_text = QDateTime::fromMSecsSinceEpoch( item.time_usec_since_epoch / 1000 ).toString("d/M/yy HH:mm::ss.zzz");
00155     return item;
00156 }
00157 
00158 void LogsTableModel::push_back(const rosgraph_msgs::Log::ConstPtr& pushed_log)
00159 {
00160     bool to_shift = (_logs.size() == MAX_CAPACITY);
00161 
00162     _logs.push_back( convertRosout( *pushed_log) );
00163 
00164     if(to_shift)
00165     {
00166         this->beginRemoveRows( QModelIndex(), 0 , 0);
00167         this->endRemoveRows();
00168         emit dataChanged( index(0,0), index( rowCount()-1, columnCount() -1));
00169     }
00170 
00171     this->beginInsertRows( QModelIndex(), _logs.size() - 1 , _logs.size() - 1);
00172     this->endInsertRows();
00173 }
00174 
00175 void LogsTableModel::push_back(const std::vector<rosgraph_msgs::Log::ConstPtr>& pushed_logs)
00176 {
00177     size_t old_size = _logs.size();
00178     size_t new_size = old_size + pushed_logs.size();
00179 
00180     int to_add   = pushed_logs.size();
00181     int to_shift = 0;
00182 
00183     if( new_size > MAX_CAPACITY)
00184     {
00185         to_add = (MAX_CAPACITY - old_size);
00186         to_shift = new_size - MAX_CAPACITY;
00187         new_size = MAX_CAPACITY;
00188     }
00189 
00190     const size_t last_row  = new_size - 1;
00191     const size_t first_row = new_size - pushed_logs.size() ;
00192 
00193     for (int i=0; i < pushed_logs.size(); i++){
00194         _logs.push_back( convertRosout( *pushed_logs[i]) );
00195     }
00196 
00197      std::sort(_logs.begin(), _logs.end(),
00198               [](const LogItem& a, const LogItem& b) {   return a.time_usec_since_epoch < b.time_usec_since_epoch; }   );
00199 
00200     if(to_shift > 0)
00201     {
00202         this->beginRemoveRows( QModelIndex(), 0 , 0);
00203         this->endRemoveRows();
00204 
00205         emit dataChanged( index(0,0), index( rowCount()-1, columnCount() -1));
00206     }
00207 
00208     this->beginInsertRows( QModelIndex(), first_row , last_row);
00209     this->endInsertRows();
00210 }
00211 
00212 
00213 const QString &LogsTableModel::message(int index) const
00214 {
00215     return _logs[ index ].message;
00216 }
00217 
00218 const QString& LogsTableModel::nodeName(int index) const
00219 {
00220     return *( _logs[ index ].node );
00221 }
00222 
00223 LogsTableModel::Severity LogsTableModel::severity(int index) const
00224 {
00225     return _logs[ index ].level_raw;
00226 }
00227 
00228 TimePoint LogsTableModel::timestamp(int index) const
00229 {
00230     std::chrono::microseconds since_epoch( _logs[ index ].time_usec_since_epoch );
00231     return TimePoint() + since_epoch;
00232 }
00233 
00234 void LogsTableModel::clear() {
00235     this->beginRemoveRows( QModelIndex(), 0 , _logs.size() -1);
00236     this->endRemoveRows();
00237     _logs.clear();
00238     _count = 0;
00239 }
00240 
00241 


plotjuggler
Author(s): Davide Faconti
autogenerated on Wed Jul 3 2019 19:28:04