LogTableModel.h
Go to the documentation of this file.
1/*
2* This file is part of ArmarX.
3*
4* ArmarX is free software; you can redistribute it and/or modify
5* it under the terms of the GNU General Public License version 2 as
6* published by the Free Software Foundation.
7*
8* ArmarX is distributed in the hope that it will be useful, but
9* WITHOUT ANY WARRANTY; without even the implied warranty of
10* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11* GNU General Public License for more details.
12*
13* You should have received a copy of the GNU General Public License
14* along with this program. If not, see <http://www.gnu.org/licenses/>.
15*
16* @package ArmarX::
17* @author Mirko Waechter ( mirko.waechter at kit dot edu)
18* @date 2012
19* @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20* GNU General Public License
21*/
22
23#pragma once
24
25#include <QAbstractTableModel>
26#include <QColor>
27
28// ArmarX
31#include <ArmarXCore/interface/core/Log.h>
32
33#include "LogMarkerRegistry.h"
34
35// C++
36#include <mutex>
37#include <vector>
38
39namespace armarx
40{
41 // Identifies a filterable log column. The integer values match the model's column
42 // indices (see data() / headerData()).
43 enum class LogColumn : int
44 {
45 Time = 0,
47 Tag = 2,
50 File = 5,
52 Group = 7,
54 };
55
56 // How a filter value is compared against a column: as a substring (for manual,
57 // user-typed filters) or as a whole-value match (for auto-generated per-component /
58 // per-group filters, which must isolate exactly one component/group).
59 enum class MatchMode
60 {
63 };
64
65 // A single active filter criterion: which column, the value to match against, and how.
72
73 class LogTableModel : public QAbstractTableModel
74 {
75 Q_OBJECT
76 public:
77 enum class UserRoles : int
78 {
79 FullMsgRole = Qt::UserRole + 1
80 };
81
82 explicit LogTableModel(QObject* parent = 0);
83 int rowCount(const QModelIndex& parent = QModelIndex()) const override;
84 int columnCount(const QModelIndex& parent = QModelIndex()) const override;
85 QVariant data(const QModelIndex& index, int role) const override;
86 QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
87 bool setData(const QModelIndex& index, const QVariant& value, int role) override;
88 Qt::ItemFlags flags(const QModelIndex& index) const override;
89 void updateView();
90 bool insertRows(int row, int count, const QModelIndex& parent) override;
91
92 void addFilter(const std::string& columnName,
93 const std::string& filter,
95 void reapplyAllFilters();
96 void resetFilters();
97 bool rowContainsString(int row, const QString& searchStr) const;
98 bool msgContainsString(const LogMessage& logMsg, QString searchStr) const;
99
100 void search(const QString& searchStr);
101
102 QString
104 {
105 return activeSearchStr;
106 }
107
108 MessageType
110 {
111 return maxNewLogLevelType;
112 }
113
114 int getColumn(const std::string& columnName) const;
115 int clearData();
116
117 //! Upper bound on retained log entries. 0 disables the bound. When the buffer
118 //! grows past maxEntries by a small headroom factor, the oldest entries are
119 //! trimmed back down to maxEntries.
120 void setMaxEntries(std::size_t maxEntries);
121
122 const std::vector<LogFilter>&
124 {
125 return activeFilters;
126 }
127
128 // thread-unsafe access!
129 const LogMessage& getLogEntry(size_t row) const;
130
131 //! Number of display lines of the message column for the given row (precomputed).
132 int lineCountAt(int row) const;
133
134 //! Attach the shared pin registry. The model then shows registry markers that are
135 //! not already present as native rows (injected, in time position), colors pinned
136 //! rows, and refreshes whenever the registry changes.
137 void setMarkerRegistry(LogMarkerRegistry* registry);
138
139 //! Display row of the (first) line identical to \p message, or -1 if not shown.
140 int rowForMessage(const LogMessage& message) const;
141
142 //! Whether this model holds \p message as a *native* (non-injected) row. Used by the
143 //! viewer to expire pins once their source line has aged out of every buffer.
144 bool containsNativeLine(const LogMessage& message) const;
145
146 //! Whether the given display row is currently pinned (has a label color).
147 bool isRowMarked(int row) const;
148
149
150 signals:
151
152 protected slots:
153
154 public slots:
155 bool addEntry(const LogMessage& entry, int* entriesAdded = NULL);
156 int addEntries(const std::vector<LogMessage>& entryList, const QString& filterStr);
157 //! Rebuild the set of injected pin rows and re-color native rows (full reset).
158 void refreshMarkers();
159
160 protected:
161 bool applyFilter(const LogFilter& filter, const LogMessage& logMsg);
162 bool applyFilters(const LogMessage& logMsg);
163 //! Trim the oldest entries once the buffer exceeds its bound (see setMaxEntries).
164 void enforceMaxEntries();
165
166 private:
167 //! A stored log message plus display data derived once at insert time, so the
168 //! hot data() / rendering paths do not recompute it on every repaint.
169 struct LogEntry
170 {
171 LogMessage message;
172 QString timeString; //!< formatted time column (0)
173 QString displayMessage; //!< truncated message column (4)
174 int lineCount = 1; //!< number of display lines in displayMessage
175 bool matchesSearch = false; //!< whether the entry matches the active search
176 bool injected = false; //!< true if this is an injected pin (not native here)
177 QColor markerColor; //!< valid iff pinned; the pin's row background color
178 };
179
180 //! Build a LogEntry with all derived display fields (time/message/search/marker).
181 LogEntry buildLogEntry(const LogMessage& message) const;
182
183 QString activeSearchStr;
184 int newEntryCount;
185 MessageType maxNewLogLevelType;
186 std::vector<LogFilter> activeFilters;
187
188 //! Retained-entry bound; 0 disables it. Default matches the previous (never
189 //! wired up) MAX_BUFFER_SIZE.
190 std::size_t maxEntries = 10000;
191
192 std::vector<LogEntry> logEntries;
193 mutable std::mutex logEntriesMutex;
194
195 //! Shared pin registry (non-owning); nullptr until setMarkerRegistry().
196 LogMarkerRegistry* markerRegistry = nullptr;
197
199 };
200} // namespace armarx
uint8_t index
Baseclass for all ArmarX ManagedIceObjects requiring properties.
Definition Component.h:94
Shared, session-only store of pinned ("labeled") log lines.
MessageType getMaxNewLogLevelType()
QVariant headerData(int section, Qt::Orientation orientation, int role) const override
void setMarkerRegistry(LogMarkerRegistry *registry)
Attach the shared pin registry.
void addFilter(const std::string &columnName, const std::string &filter, MatchMode mode=MatchMode::Contains)
bool applyFilters(const LogMessage &logMsg)
int rowCount(const QModelIndex &parent=QModelIndex()) const override
Qt::ItemFlags flags(const QModelIndex &index) const override
LogTableModel(QObject *parent=0)
bool isRowMarked(int row) const
Whether the given display row is currently pinned (has a label color).
bool setData(const QModelIndex &index, const QVariant &value, int role) override
int addEntries(const std::vector< LogMessage > &entryList, const QString &filterStr)
bool applyFilter(const LogFilter &filter, const LogMessage &logMsg)
bool insertRows(int row, int count, const QModelIndex &parent) override
int lineCountAt(int row) const
Number of display lines of the message column for the given row (precomputed).
int rowForMessage(const LogMessage &message) const
Display row of the (first) line identical to message, or -1 if not shown.
void setMaxEntries(std::size_t maxEntries)
Upper bound on retained log entries.
const LogMessage & getLogEntry(size_t row) const
int columnCount(const QModelIndex &parent=QModelIndex()) const override
void enforceMaxEntries()
Trim the oldest entries once the buffer exceeds its bound (see setMaxEntries).
int getColumn(const std::string &columnName) const
bool addEntry(const LogMessage &entry, int *entriesAdded=NULL)
bool msgContainsString(const LogMessage &logMsg, QString searchStr) const
const std::vector< LogFilter > & getFilters() const
bool rowContainsString(int row, const QString &searchStr) const
void refreshMarkers()
Rebuild the set of injected pin rows and re-color native rows (full reset).
void search(const QString &searchStr)
bool containsNativeLine(const LogMessage &message) const
Whether this model holds message as a native (non-injected) row.
IceUtil::Handle< RunningTask< T > > pointer_type
Shared pointer type for convenience.
This file offers overloads of toIce() and fromIce() functions for STL container types.
bool Contains(const ContainerType &container, const ElementType &searchElement)
Definition algorithm.h:330
std::string value