forked from Vector35/binaryninja-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogview.h
350 lines (282 loc) · 9.31 KB
/
logview.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#pragma once
#include <QtWidgets/QListView>
#include <QtCore/QItemSelection>
#include <QtCore/QPointer>
#include <QtCore/QTimer>
#include <QtWidgets/QStyledItemDelegate>
#include <QtWidgets/QLabel>
#include <QtWidgets/QToolButton>
#include <QtWidgets/QComboBox>
#include <QtWidgets/QLineEdit>
#include <QtCore/QSortFilterProxyModel>
#include <QtWidgets/QStackedWidget>
#include <mutex>
#include <string>
#include <utility>
#include <vector>
#include "binaryninjaapi.h"
#include "action.h"
#include "globalarea.h"
#include "render.h"
#include "filter.h"
#define LOG_UPDATE_INTERVAL 100
class LogStatus;
class View;
class ViewFrame;
/*!
\defgroup logview LogView
\ingroup uiapi
*/
/*!
\ingroup logview
*/
struct BINARYNINJAUIAPI LogTokenList
{
std::vector<std::pair<int, int>> tokens;
};
struct BINARYNINJAUIAPI LogListItem
{
size_t sessionId;
BNLogLevel level;
QString text;
LogTokenList tokens;
bool selected;
QString logger;
size_t threadId{0};
LogListItem(size_t sessionId, BNLogLevel level, const QString& text, const QString& logger_name = QString(), size_t tid = 0);
};
/*!
\ingroup logview
*/
enum LoggingScope
{
CurrentTabOnly,
CurrentTabAndGlobal,
GlobalOnly,
AllTabs
};
/*!
\ingroup logview
*/
class BINARYNINJAUIAPI LogListFilterProxyModel : public QSortFilterProxyModel
{
Q_OBJECT
QString m_loggerName;
size_t m_sessionId {0};
LoggingScope m_scope;
public:
LogListFilterProxyModel(QObject* parent);
virtual bool filterAcceptsRow(int source_row, const QModelIndex& source_parent) const override;
virtual QVariant data(const QModelIndex& idx, int role) const override;
void setScope(LoggingScope scope);
LoggingScope getScope() const { return m_scope; }
public Q_SLOTS:
void updateSession(size_t sessionId);
void updateLogger(QString loggerName);
void updateFilter();
};
/*!
\ingroup logview
*/
class BINARYNINJAUIAPI LogListModel : public QAbstractItemModel, BinaryNinja::LogListener
{
Q_OBJECT
QWidget* m_owner;
std::deque<LogListItem> m_items;
std::vector<LogListItem> m_pendingItems;
mutable std::mutex m_mutex;
std::mutex m_pendingMutex;
std::string m_logger;
size_t m_sessionId {0};
bool m_showSessionId {false};
bool m_showThreadId {false};
bool m_showLoggerName {false};
bool m_showLogLevel {false};
public:
static constexpr int Level = Qt::UserRole + 1;
static constexpr int Logger = Qt::UserRole + 2;
static constexpr int ThreadId = Qt::UserRole + 3;
static constexpr int Message = Qt::UserRole + 4;
static constexpr int Session = Qt::UserRole + 5;
static constexpr int FormattedMessage = Qt::UserRole + 6;
static constexpr int Tokens = Qt::UserRole + 7;
LogListModel(QWidget* parent);
~LogListModel();
void addPendingItems();
void clear();
virtual void LogMessage(size_t sessionId, BNLogLevel level, const std::string& msg, const std::string& loggerName = "", size_t tid = 0) override;
virtual BNLogLevel GetLogLevel() override;
QString getFormattedMessage(const LogListItem& item) const;
void updateTokens();
virtual QModelIndex index(int row, int col, const QModelIndex& parent) const override;
virtual QModelIndex parent(const QModelIndex& i) const override;
virtual bool hasChildren(const QModelIndex& parent) const override;
virtual int rowCount(const QModelIndex& parent) const override;
virtual int columnCount(const QModelIndex& parent) const override;
virtual QVariant data(const QModelIndex& i, int role) const override;
void setDisplaySessionId(bool value);
void setDisplayThreadId(bool value);
void setDisplayLoggerName(bool value);
void setDisplayLogLevel(bool value);
void setMinLogLevel(BNLogLevel level);
void setMaxLogLength(size_t length);
size_t getSessionId() const { return m_sessionId; }
bool getDisplaySessionId() const { return m_showSessionId; }
bool getDisplayThreadId() const { return m_showThreadId; }
bool getDisplayLoggerName() const { return m_showLoggerName; }
bool getDisplayLogLevel() const { return m_showLogLevel; }
Q_SIGNALS:
void settingsUpdated();
public Q_SLOTS:
void notifySessionChanged(size_t sessionId);
};
/*!
\ingroup logview
*/
class BINARYNINJAUIAPI LogItemDelegate : public QStyledItemDelegate, public BinaryNinja::BinaryDataNotification
{
Q_OBJECT
QWidget* m_owner;
BinaryViewRef m_data;
std::vector<BNAddressRange> m_mappedRanges;
QFont m_font;
int m_height;
bool isNavigable(const QString& str, const std::pair<int, int>& offsetLen, uint64_t& value, bool highlight) const;
void cacheValidRanges();
bool isAddressValid(uint64_t addr) const;
public:
LogItemDelegate(QWidget* parent);
void updateFonts();
virtual QSize sizeHint(const QStyleOptionViewItem& option, const QModelIndex& idx) const override;
virtual void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& idx) const override;
virtual void OnSegmentAdded(BinaryNinja::BinaryView*, BinaryNinja::Segment*) override { cacheValidRanges(); }
virtual void OnSegmentRemoved(BinaryNinja::BinaryView*, BinaryNinja::Segment*) override { cacheValidRanges(); }
virtual void OnSegmentUpdated(BinaryNinja::BinaryView*, BinaryNinja::Segment*) override { cacheValidRanges(); }
protected:
bool editorEvent(QEvent* event, QAbstractItemModel* model, const QStyleOptionViewItem& option,
const QModelIndex& index) override;
Q_SIGNALS:
void notifySessionChanged(size_t sessionId);
public Q_SLOTS:
void viewChanged(QWidget* frame);
};
/*!
\ingroup logview
*/
class BINARYNINJAUIAPI LogViewComboBox : public QComboBox
{
Q_OBJECT
public:
LogViewComboBox(QWidget* parent);
void updateLoggers();
void showPopup();
public Q_SLOTS:
void signalItemSelected(size_t);
Q_SIGNALS:
void itemSelected(QString text);
};
/*!
\ingroup logview
*/
class BINARYNINJAUIAPI LogView : public SidebarWidget, public FilterTarget
{
Q_OBJECT
QPointer<LogStatus> m_logStatus;
std::vector<std::pair<QAction*, bool>> m_actionEnableList;
QListView* m_list;
LogListModel* m_listModel;
LogListFilterProxyModel* m_model;
LogItemDelegate* m_itemDelegate;
QTimer* m_updateTimer;
LogViewComboBox* m_comboBox;
FilteredView* m_filteredView;
QWidget* m_filterWidget;
RenderContext m_render;
bool m_doClear;
bool m_scrolledToEnd;
bool m_hasSelection = false;
bool m_isRestored = false;
public:
LogView(LogStatus* logStatus);
virtual void copy();
virtual bool canCopy();
static void setLogLevel(BNLogLevel level);
static void setLogSize(size_t maxSize);
static void setWordWrap(bool wrap);
static bool IsHexString(const QString& str, std::pair<int, int> offsetLen);
static bool StartsWith0x(const QString& str, std::pair<int, int> offsetLen);
void notifyWordWrapChanged();
void notifyFontChanged() override;
void notifyThemeChanged() override;
void notifyViewChanged(ViewFrame* frame) override;
void focus() override;
LogListModel* model() { return m_listModel; }
void setFilter(const std::string& filter) override;
LoggingScope getScope() const { return m_model->getScope(); }
void setScope(LoggingScope scope) { m_model->setScope(scope); }
QWidget* headerWidget() override { return m_filterWidget; }
void scrollToFirstItem() override;
void scrollToCurrentItem() override;
void selectFirstItem() override;
void activateFirstItem() override {}
void closeFilter() override;
// std::pair<size_t, size_t> GetSelectionIndexAndOffsetFromPosition(const QPoint& position) const;
// virtual void mousePressEvent(QMouseEvent* event) override;
// virtual void mouseReleaseEvent(QMouseEvent* event) override;
// virtual void mouseMoveEvent(QMouseEvent* event) override;
// bool IsInSubSelectionMode() const { return m_subSelectionMode; }
// std::pair<size_t, size_t> GetSelectionIndicies() const { return {m_baseSelectionOffset, m_currentSelectionOffset}; }
protected:
void contextMenuEvent(QContextMenuEvent* event) override;
Q_SIGNALS:
void notifyUiStatus();
void viewChanged(QWidget* frame);
public Q_SLOTS:
void clear();
private Q_SLOTS:
void scrollRangeChanged(int minimum, int maximum);
void scrollValueChanged(int value);
void updateSelection(const QItemSelection& selected, const QItemSelection& deselected);
void updateTimerEvent();
void updateUiStatus();
void showContextMenu();
};
/*!
\ingroup logview
*/
class BINARYNINJAUIAPI LogStatus : public QWidget
{
Q_OBJECT
QToolButton* m_errorIndicator;
QToolButton* m_warnIndicator;
ContextMenuManager* m_contextMenuManager;
QMenu* m_menu;
std::mutex m_countMutex;
std::map<uint64_t, int> m_sessionErrorCount;
std::map<uint64_t, int> m_sessionWarnCount;
size_t m_sessionId {0};
LogView* m_logView;
public:
LogStatus(QWidget* parent);
void setLogView(LogView* view) { m_logView = view; }
void incrementErrorCount(uint64_t session, int count);
void incrementWarningCount(uint64_t session, int count);
void checkForErrors();
void focusTab(UIContext* context, QWidget* tab, size_t m_sessionId);
void clearIndicators();
void updateTheme();
public Q_SLOTS:
void notifySessionChanged(size_t sessionId);
public Q_SLOTS:
void clicked(bool error);
};
/*!
\ingroup logview
*/
class BINARYNINJAUIAPI LogViewSidebarWidgetType : public SidebarWidgetType
{
public:
LogViewSidebarWidgetType();
SidebarWidgetLocation defaultLocation() const override { return SidebarWidgetLocation::RightBottom; }
SidebarContextSensitivity contextSensitivity() const override { return GlobalSidebarContext; }
};