YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
logmodel.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-License-Identifier: LGPL-2.1-or-later
4 */
5
6#include "logmodel.h"
7
8#include <QBrush>
9#include <QFontDatabase>
10
11const QString TRACE_STRING = "TRACE";
12const QString DEBUG_STRING = "DEBUG";
13const QString INFO_STRING = "INFO";
14const QString WARNING_STRING = "WARNING";
15const QString ERROR_STRING = "ERROR";
16const QString FATAL_STRING = "FATAL";
17
18
19LogModel::LogModel(QObject *parent) :
20 QAbstractTableModel(parent),
21 m_font(QFontDatabase::systemFont(QFontDatabase::FixedFont)),
22 m_names(QAbstractTableModel::roleNames())
23{
24 m_names[YarprunTimestampRole] = "yarprun_timestamp";
25 m_names[LocalTimestampRole] = "local_timestamp";
26 m_names[SystemTimeRole] = "systemtime";
27 m_names[SystemTimeStringRole] = "systemtime_string";
28 m_names[NetworkTimeRole] = "networktime";
29 m_names[NetworkTimeStringRole] = "networktime_string";
30 m_names[ExternalTimeRole] = "externaltime";
31 m_names[ExternalTimeStringRole] = "externaltime_string";
32 m_names[LogLevelRole] = "level";
33 m_names[LogLevelStringRole] = "level_string";
34 m_names[FilenameRole] = "filename";
35 m_names[LineRole] = "line";
36 m_names[LineStringRole] = "line_string";
37 m_names[FunctionRole] = "function";
38 m_names[HostnameRole] = "hostname";
39 m_names[CmdRole] = "cmd";
40 m_names[ArgsRole] = "args";
41 m_names[PidRole] = "pid";
42 m_names[ThreadIdRole] = "thread_id";
43 m_names[ThreadIdStringRole] = "thread_id_string";
44 m_names[ComponentRole] = "component";
45 m_names[IdRole] = "id";
46 m_names[TextRole] = "text";
47 m_names[BacktraceRole] = "backtrace";
48}
49
50LogModel::~LogModel() = default;
51
52int LogModel::rowCount(const QModelIndex &parent) const
53{
54 // If the index is the root item, then return the row count,
55 // otherwise return 0
56 if (parent == QModelIndex()) {
57 return m_messages.size();
58 }
59 return 0;
60}
61
62int LogModel::columnCount(const QModelIndex &parent) const
63{
64 // If the index is the root item, then return the column count,
65 // otherwise return 0
66 if (parent == QModelIndex()) {
67 return COLUMN_COUNT;
68 }
69 return 0;
70}
71
72QVariant LogModel::headerData(int section, Qt::Orientation orientation, int role) const
73{
74 if (orientation == Qt::Vertical) {
75 return QVariant();
76 }
77
78 if (role != Qt::DisplayRole) {
79 return QVariant();
80 }
81
82 switch (section) {
84 return tr("yarprun timestamp");
86 return tr("local timestamp");
88 return tr("system time");
90 return tr("network time");
92 return tr("external time");
93 case LOGLEVEL_COLUMN:
94 return tr("level");
95 case FILENAME_COLUMN:
96 return tr("filename");
97 case LINE_COLUMN:
98 return tr("line");
99 case FUNCTION_COLUMN:
100 return tr("function");
101 case HOSTNAME_COLUMN:
102 return tr("hostname");
103 case CMD_COLUMN:
104 return tr("cmd");
105 case ARGS_COLUMN:
106 return tr("args");
107 case PID_COLUMN:
108 return tr("pid");
109 case THREADID_COLUMN:
110 return tr("thread id");
111 case COMPONENT_COLUMN:
112 return tr("component");
113 case ID_COLUMN:
114 return tr("id");
115 case TEXT_COLUMN:
116 return tr("message");
117 default:
118 return QVariant();
119 }
120}
121
122QVariant LogModel::data(const QModelIndex &index, int role) const
123{
124 if (!index.isValid()) {
125 return QVariant();
126 }
127
128 if (role == Qt::DisplayRole) {
129 switch (index.column()) {
131 return data(index, YarprunTimestampRole);
133 return data(index, LocalTimestampRole);
135 return data(index, SystemTimeStringRole);
137 return data(index, NetworkTimeStringRole);
139 return data(index, ExternalTimeStringRole);
140 case LOGLEVEL_COLUMN:
141 return data(index, LogLevelStringRole);
142 case FILENAME_COLUMN:
143 return data(index, FilenameRole);
144 case LINE_COLUMN:
145 return data(index, LineStringRole);
146 case FUNCTION_COLUMN:
147 return data(index, FunctionRole);
148 case CMD_COLUMN:
149 return data(index, CmdRole);
150 case HOSTNAME_COLUMN:
151 return data(index, HostnameRole);
152 case ARGS_COLUMN:
153 return data(index, ArgsRole);
154 case PID_COLUMN:
155 return data(index, PidStringRole);
156 case THREADID_COLUMN:
157 return data(index, ThreadIdStringRole);
158 case COMPONENT_COLUMN:
159 return data(index, ComponentRole);
160 case ID_COLUMN:
161 return data(index, IdRole);
162 case TEXT_COLUMN:
163 return data(index, TextRole);
164 default:
165 return QString();
166 }
167 }
168
169 if (role == Qt::BackgroundRole) {
170 if(!m_color) {
171 return QBrush();
172 }
173 switch (static_cast<yarp::yarpLogger::LogLevelEnum>(m_messages.at(index.row()).level)) {
175 return QBrush(Qt::white);
177 return QBrush(QColor("#E9E9E9"));
179 return QBrush(QColor("#78E678"));
181 return QBrush(QColor("#8DCFE8"));
183 return QBrush(QColor("#FFFF70"));
185 return QBrush(QColor("#DE4E4E"));
187 return QBrush(Qt::black);
188 default:
189 return QBrush(Qt::white);
190 }
191 }
192
193 if (role == Qt::ForegroundRole) {
194 if(!m_color) {
195 return QBrush();
196 }
197 if (m_messages.at(index.row()).level == yarp::yarpLogger::LOGLEVEL_FATAL) {
198 return QBrush(Qt::white);
199 }
200 return QBrush(Qt::black);
201 }
202
203 if (role == Qt::FontRole) {
204 return m_font;
205 }
206
207 if (role == Qt::TextAlignmentRole) {
208 switch (index.column()) {
213 case LINE_COLUMN:
214 case PID_COLUMN:
215 case THREADID_COLUMN:
216 return QVariant(Qt::AlignRight | (m_wordwrap ? Qt::AlignTop : Qt::AlignVCenter));
218 case LOGLEVEL_COLUMN:
219 case FILENAME_COLUMN:
220 case FUNCTION_COLUMN:
221 case HOSTNAME_COLUMN:
222 case CMD_COLUMN:
223 case ARGS_COLUMN:
224 case COMPONENT_COLUMN:
225 case ID_COLUMN:
226 case TEXT_COLUMN:
227 default:
228 return QVariant(Qt::AlignLeft | (m_wordwrap ? Qt::AlignTop : Qt::AlignVCenter));
229 }
230 }
231
232 if (role == Qt::ToolTipRole) {
233 return data(index, BacktraceRole);
234 }
235
236 if (role == YarprunTimestampRole) {
237 return QString(m_messages.at(index.row()).yarprun_timestamp.c_str());
238 }
239
240 if (role == LocalTimestampRole) {
241 return QString(m_messages.at(index.row()).local_timestamp.c_str());
242 }
243
244 if (role == SystemTimeRole) {
245 return m_messages.at(index.row()).systemtime;
246 }
247
248 if (role == SystemTimeStringRole) {
249 auto systemtime = m_messages.at(index.row()).systemtime;
250 if (systemtime != 0.0) {
251 return QString::number(systemtime, 'f'); // FIXME Return properly formatted date/time
252 }
253 return QVariant();
254 }
255
256 if (role == NetworkTimeRole) {
257 return m_messages.at(index.row()).networktime;
258 }
259
260 if (role == NetworkTimeStringRole) {
261 auto networktime = m_messages.at(index.row()).networktime;
262 if (networktime != 0.0) {
263 return QString::number(networktime, 'f'); // FIXME Return properly formatted date/time
264 }
265 return QVariant();
266 }
267
268 if (role == ExternalTimeRole) {
269 return m_messages.at(index.row()).externaltime;
270 }
271
272 if (role == ExternalTimeStringRole) {
273 auto externaltime = m_messages.at(index.row()).externaltime;
274 if (externaltime != 0.0) {
275 return QString::number(externaltime, 'f'); // FIXME Return properly formatted date/time
276 }
277 return QVariant();
278 }
279
280 if (role == LogLevelRole) {
281 return static_cast<yarp::yarpLogger::LogLevelEnum>(m_messages.at(index.row()).level);
282 }
283
284 if (role == LogLevelStringRole) {
285 return logLevelToString(m_messages.at(index.row()).level);
286 }
287
288 if (role == FilenameRole) {
289 return QString(m_messages.at(index.row()).filename.c_str());
290 }
291
292 if (role == LineRole) {
293 return m_messages.at(index.row()).line;
294 }
295
296 if (role == LineStringRole) {
297 auto line = m_messages.at(index.row()).line;
298 if (line != 0) {
299 return QString::number(line);
300 }
301 }
302
303 if (role == FunctionRole) {
304 return QString(m_messages.at(index.row()).function.c_str());
305 }
306
307 if (role == HostnameRole) {
308 return QString(m_messages.at(index.row()).hostname.c_str());
309 }
310
311 if (role == CmdRole) {
312 return QString(m_messages.at(index.row()).cmd.c_str());
313 }
314
315 if (role == ArgsRole) {
316 return QString(m_messages.at(index.row()).args.c_str());
317 }
318
319 if (role == PidRole) {
320 return m_messages.at(index.row()).pid;
321 }
322
323 if (role == PidStringRole) {
324 auto pid = m_messages.at(index.row()).pid;
325 if (pid != 0) {
326 return QString::number(pid);
327 }
328 return QVariant();
329 }
330
331 if (role == ThreadIdRole) {
332 return static_cast<qlonglong>(m_messages.at(index.row()).thread_id);
333 }
334
335 if (role == ThreadIdStringRole) {
336 auto thread_id = m_messages.at(index.row()).thread_id;
337 if (thread_id != 0) {
338 return QString("0x") + QString::number(thread_id, 16).rightJustified(8, '0');
339 }
340 return QVariant();
341 }
342
343 if (role == ComponentRole) {
344 return QString(m_messages.at(index.row()).component.c_str());
345 }
346
347 if (role == IdRole) {
348 return QString(m_messages.at(index.row()).id.c_str());
349 }
350
351 if (role == TextRole) {
352 return QString(m_messages.at(index.row()).text.c_str());
353 }
354
355 if (role == BacktraceRole) {
356 return QString(m_messages.at(index.row()).backtrace.c_str());
357 }
358
359 return QVariant();
360}
361
362QHash<int, QByteArray> LogModel::roleNames() const
363{
364 return m_names;
365}
366
367void LogModel::addMessages(const std::list<yarp::yarpLogger::MessageEntry> &m_messages)
368{
369 if (m_messages.size()==0) return;
370 beginInsertRows(QModelIndex(),
371 rowCount(),
372 rowCount() + m_messages.size() - 1);
373#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
374 this->m_messages.append(QList<yarp::yarpLogger::MessageEntry>(m_messages.begin(), m_messages.end()));
375#else
376 this->m_messages.append(QList<yarp::yarpLogger::MessageEntry>::fromStdList(m_messages));
377#endif
378 endInsertRows();
379}
380
381void LogModel::setColor(bool enabled)
382{
383 if (m_color != enabled) {
384 m_color = enabled;
385 Q_EMIT dataChanged(index(0, 0),
386 index(rowCount()-1,columnCount()-1),
387 QVector<int>() << Qt::BackgroundRole
388 << Qt::ForegroundRole);
389 }
390}
391
392void LogModel::setWordWrap(bool wordwrap)
393{
394 if (m_wordwrap != wordwrap) {
395 m_wordwrap = wordwrap;
396 Q_EMIT dataChanged(index(0, 0),
397 index(rowCount()-1,columnCount()-1),
398 QVector<int>() << Qt::TextAlignmentRole);
399 }
400}
401
403{
404 beginResetModel();
405 this->m_messages.clear();
406 endResetModel();
407}
408
static constexpr int YARPRUNTIMESTAMP_COLUMN
Definition logmodel.h:49
static constexpr int LINE_COLUMN
Definition logmodel.h:56
QHash< int, QByteArray > roleNames() const override
Definition logmodel.cpp:362
void setWordWrap(bool wordwrap)
Definition logmodel.cpp:392
static constexpr int SYSTEMTIME_COLUMN
Definition logmodel.h:51
static constexpr int FUNCTION_COLUMN
Definition logmodel.h:57
@ LogLevelStringRole
Definition logmodel.h:31
@ ExternalTimeRole
Definition logmodel.h:28
@ PidRole
Definition logmodel.h:37
@ SystemTimeRole
Definition logmodel.h:24
@ LocalTimestampRole
Definition logmodel.h:23
@ ComponentRole
Definition logmodel.h:43
@ PidStringRole
Definition logmodel.h:40
@ ArgsRole
Definition logmodel.h:39
@ LineStringRole
Definition logmodel.h:34
@ LogLevelRole
Definition logmodel.h:30
@ NetworkTimeRole
Definition logmodel.h:26
@ ThreadIdStringRole
Definition logmodel.h:42
@ NetworkTimeStringRole
Definition logmodel.h:27
@ ExternalTimeStringRole
Definition logmodel.h:29
@ HostnameRole
Definition logmodel.h:36
@ YarprunTimestampRole
Definition logmodel.h:22
@ SystemTimeStringRole
Definition logmodel.h:25
@ BacktraceRole
Definition logmodel.h:46
@ FunctionRole
Definition logmodel.h:35
@ LineRole
Definition logmodel.h:33
@ TextRole
Definition logmodel.h:45
@ FilenameRole
Definition logmodel.h:32
@ ThreadIdRole
Definition logmodel.h:41
@ CmdRole
Definition logmodel.h:38
void addMessages(const std::list< yarp::yarpLogger::MessageEntry > &messages)
Definition logmodel.cpp:367
static constexpr int TEXT_COLUMN
Definition logmodel.h:65
static constexpr int CMD_COLUMN
Definition logmodel.h:60
static constexpr int COMPONENT_COLUMN
Definition logmodel.h:63
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const override
Definition logmodel.cpp:72
~LogModel() override
static constexpr int LOCALTIMESTAMP_COLUMN
Definition logmodel.h:50
static constexpr int PID_COLUMN
Definition logmodel.h:59
static constexpr int LOGLEVEL_COLUMN
Definition logmodel.h:54
static constexpr int FILENAME_COLUMN
Definition logmodel.h:55
int columnCount(const QModelIndex &parent=QModelIndex()) const override
Definition logmodel.cpp:62
static constexpr int ARGS_COLUMN
Definition logmodel.h:61
void clear()
Definition logmodel.cpp:402
void setColor(bool enabled)
Definition logmodel.cpp:381
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
Definition logmodel.cpp:122
LogModel(QObject *parent=nullptr)
Definition logmodel.cpp:19
static QString logLevelToString(yarp::yarpLogger::LogLevel l)
Definition logmodel.cpp:409
static constexpr int THREADID_COLUMN
Definition logmodel.h:62
static constexpr int HOSTNAME_COLUMN
Definition logmodel.h:58
static constexpr int NETWORKTIME_COLUMN
Definition logmodel.h:52
static constexpr int ID_COLUMN
Definition logmodel.h:64
static constexpr int EXTERNALTIME_COLUMN
Definition logmodel.h:53
int rowCount(const QModelIndex &parent=QModelIndex()) const override
Definition logmodel.cpp:52
static constexpr int COLUMN_COUNT
Definition logmodel.h:66
const QString ERROR_STRING
Definition logmodel.cpp:15
const QString FATAL_STRING
Definition logmodel.cpp:16
const QString WARNING_STRING
Definition logmodel.cpp:14
const QString TRACE_STRING
Definition logmodel.cpp:11
const QString DEBUG_STRING
Definition logmodel.cpp:12
const QString INFO_STRING
Definition logmodel.cpp:13