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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
|
// Copyright (C) 2021 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#include "loggingviewer.h"
#include "coreicons.h"
#include "coreplugintr.h"
#include "icore.h"
#include <utils/async.h>
#include <utils/basetreeview.h>
#include <utils/fancylineedit.h>
#include <utils/fileutils.h>
#include <utils/layoutbuilder.h>
#include <utils/listmodel.h>
#include <utils/qtcassert.h>
#include <utils/stringutils.h>
#include <utils/theme/theme.h>
#include <utils/utilsicons.h>
#include <QAction>
#include <QColorDialog>
#include <QDialog>
#include <QHBoxLayout>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QLoggingCategory>
#include <QMenu>
#include <QMessageBox>
#include <QPushButton>
#include <QSortFilterProxyModel>
#include <QSplitter>
#include <QToolButton>
#include <QVBoxLayout>
using namespace Utils;
namespace Core::Internal {
static QColor colorForCategory(const QString &category);
void setCategoryColor(const QString &category, const QColor &color);
QHash<QString, QColor> s_categoryColor;
static inline QString messageTypeToString(QtMsgType type)
{
switch (type) {
case QtDebugMsg:
return {"Debug"};
case QtInfoMsg:
return {"Info"};
case QtCriticalMsg:
return {"Critical"};
case QtWarningMsg:
return {"Warning"};
case QtFatalMsg:
return {"Fatal"};
default:
return {"Unknown"};
}
}
class LogCategoryRegistry : public QObject
{
Q_OBJECT
public:
static LogCategoryRegistry &instance()
{
static LogCategoryRegistry s_instance;
return s_instance;
}
static void filter(QLoggingCategory *category)
{
if (s_oldFilter)
s_oldFilter(category);
LogCategoryRegistry::instance().onFilter(category);
}
void start()
{
if (m_started)
return;
m_started = true;
s_oldFilter = QLoggingCategory::installFilter(&LogCategoryRegistry::filter);
}
QList<QLoggingCategory *> categories() { return m_categories; }
signals:
void newLogCategory(QLoggingCategory *category);
private:
LogCategoryRegistry() = default;
~LogCategoryRegistry() { QLoggingCategory::installFilter(s_oldFilter); }
void onFilter(QLoggingCategory *category)
{
if (QThread::currentThread() != thread()) {
QMetaObject::invokeMethod(
this, [category, this] { onFilter(category); }, Qt::QueuedConnection);
return;
}
if (!m_categories.contains(category)) {
m_categories.append(category);
emit newLogCategory(category);
}
}
private:
static QLoggingCategory::CategoryFilter s_oldFilter;
QList<QLoggingCategory *> m_categories;
bool m_started{false};
};
QLoggingCategory::CategoryFilter LogCategoryRegistry::s_oldFilter = nullptr;
struct SavedEntry
{
QColor color;
QString name;
QtMsgType level{QtFatalMsg};
std::optional<std::array<bool, 5>> levels;
static Result<SavedEntry> fromJson(const QJsonObject &obj)
{
if (!obj.contains("name"))
return ResultError(Tr::tr("Entry is missing a logging category name."));
SavedEntry result;
result.name = obj.value("name").toString();
if (!obj.contains("entry"))
return ResultError(Tr::tr("Entry is missing data."));
auto entry = obj.value("entry").toObject();
if (entry.contains("color"))
result.color = QColor(entry.value("color").toString());
if (entry.contains("level")) {
int lvl = entry.value("level").toInt(0);
if (lvl < QtDebugMsg || lvl > QtInfoMsg)
return ResultError(Tr::tr("Invalid level: %1").arg(lvl));
result.level = static_cast<QtMsgType>(lvl);
}
if (entry.contains("levels")) {
QVariantMap map = entry.value("levels").toVariant().toMap();
std::array<bool, 5> levels{
map.contains("Debug") && map["Debug"].toBool(),
map.contains("Warning") && map["Warning"].toBool(),
map.contains("Critical") && map["Critical"].toBool(),
true,
map.contains("Info") && map["Info"].toBool(),
};
result.levels = levels;
}
return result;
}
};
class LoggingCategoryEntry
{
public:
LoggingCategoryEntry(const QString &name)
: m_name(name)
{}
LoggingCategoryEntry(QLoggingCategory *category)
: m_name(QString::fromUtf8(category->categoryName()))
{
setLogCategory(category);
}
LoggingCategoryEntry(const SavedEntry &savedEntry)
: m_name(savedEntry.name)
{
m_saved = savedEntry.levels;
m_color = savedEntry.color;
if (!m_saved) {
m_saved = std::array<bool, 5>();
for (int i = QtDebugMsg; i <= QtInfoMsg; ++i) {
(*m_saved)[i] = savedEntry.level <= i;
}
}
}
QString name() const { return m_name; }
QColor color() const { return m_color; }
void setColor(const QColor &c) { m_color = c; }
void setUseOriginal(bool useOriginal)
{
if (!m_useOriginal && m_category && m_originalSettings) {
m_saved = std::array<bool, 5>{};
for (int i = QtDebugMsg; i < QtInfoMsg; i++) {
(*m_saved)[i] = m_category->isEnabled(static_cast<QtMsgType>(i));
m_category->setEnabled(static_cast<QtMsgType>(i), (*m_originalSettings)[i]);
}
} else if (!useOriginal && m_useOriginal && m_saved && m_category) {
for (int i = QtDebugMsg; i < QtInfoMsg; i++)
m_category->setEnabled(static_cast<QtMsgType>(i), (*m_saved)[i]);
}
m_useOriginal = useOriginal;
}
bool isEnabled(QtMsgType msgType) const
{
if (m_category)
return m_category->isEnabled(msgType);
if (m_saved)
return (*m_saved)[msgType];
return false;
}
bool isEnabledOriginally(QtMsgType msgType) const
{
if (m_originalSettings)
return (*m_originalSettings)[msgType];
return isEnabled(msgType);
}
void setEnabled(QtMsgType msgType, bool isEnabled)
{
QTC_ASSERT(!m_useOriginal, return);
if (m_category)
m_category->setEnabled(msgType, isEnabled);
if (m_saved)
(*m_saved)[msgType] = isEnabled;
}
void setSaved(const SavedEntry &entry)
{
QTC_ASSERT(entry.name == name(), return);
m_saved = entry.levels;
m_color = entry.color;
if (!m_saved) {
m_saved = std::array<bool, 5>();
for (int i = QtDebugMsg; i <= QtInfoMsg; ++i) {
(*m_saved)[i] = entry.level <= i;
}
}
if (m_category)
setLogCategory(m_category);
}
void setLogCategory(QLoggingCategory *category)
{
QTC_ASSERT(QString::fromUtf8(category->categoryName()) == m_name, return);
m_category = category;
if (!m_originalSettings) {
m_originalSettings = {
category->isDebugEnabled(),
category->isWarningEnabled(),
category->isCriticalEnabled(),
true, // always enable fatal
category->isInfoEnabled(),
};
}
if (m_saved && !m_useOriginal) {
m_category->setEnabled(QtDebugMsg, m_saved->at(0));
m_category->setEnabled(QtWarningMsg, m_saved->at(1));
m_category->setEnabled(QtCriticalMsg, m_saved->at(2));
m_category->setEnabled(QtInfoMsg, m_saved->at(4));
}
}
bool isDebugEnabled() const { return isEnabled(QtDebugMsg); }
bool isWarningEnabled() const { return isEnabled(QtWarningMsg); }
bool isCriticalEnabled() const { return isEnabled(QtCriticalMsg); }
bool isInfoEnabled() const { return isEnabled(QtInfoMsg); }
private:
QString m_name;
QLoggingCategory *m_category{nullptr};
std::optional<std::array<bool, 5>> m_originalSettings;
std::optional<std::array<bool, 5>> m_saved;
QColor m_color;
bool m_useOriginal{false};
};
class LoggingCategoryModel : public QAbstractListModel
{
Q_OBJECT
public:
LoggingCategoryModel(QObject *parent)
: QAbstractListModel(parent)
{
auto newCategory = [this](QLoggingCategory *category) {
QString name = QString::fromUtf8(category->categoryName());
auto itExists = std::find_if(m_categories.begin(),
m_categories.end(),
[name](const auto &cat) { return name == cat.name(); });
if (itExists != m_categories.end()) {
itExists->setLogCategory(category);
} else {
LoggingCategoryEntry entry(category);
append(entry);
}
};
for (QLoggingCategory *cat : LogCategoryRegistry::instance().categories())
newCategory(cat);
connect(&LogCategoryRegistry::instance(),
&LogCategoryRegistry::newLogCategory,
this,
newCategory);
LogCategoryRegistry::instance().start();
};
~LoggingCategoryModel() override;
enum Column { Color, Name, Debug, Warning, Critical, Fatal, Info };
enum Role { OriginalStateRole = Qt::UserRole + 1 };
void append(const LoggingCategoryEntry &entry);
int columnCount(const QModelIndex &) const final { return 7; }
int rowCount(const QModelIndex & = QModelIndex()) const final { return m_categories.size(); }
QVariant data(const QModelIndex &index, int role) const final;
bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) final;
Qt::ItemFlags flags(const QModelIndex &index) const final;
QVariant headerData(int section,
Qt::Orientation orientation,
int role = Qt::DisplayRole) const final;
void saveEnabledCategoryPreset() const;
void loadAndUpdateFromPreset();
void setUseOriginal(bool useOriginal)
{
if (useOriginal != m_useOriginal) {
beginResetModel();
for (auto &entry : m_categories)
entry.setUseOriginal(useOriginal);
m_useOriginal = useOriginal;
endResetModel();
}
}
const QList<LoggingCategoryEntry> &categories() const { return m_categories; }
private:
QList<LoggingCategoryEntry> m_categories;
bool m_useOriginal{false};
};
LoggingCategoryModel::~LoggingCategoryModel() {}
void LoggingCategoryModel::append(const LoggingCategoryEntry &entry)
{
beginInsertRows(QModelIndex(), m_categories.size(), m_categories.size() + 1);
m_categories.push_back(entry);
endInsertRows();
}
QVariant LoggingCategoryModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return {};
if (index.column() == Column::Name && role == Qt::DisplayRole) {
return m_categories.at(index.row()).name();
} else if (role == Qt::DecorationRole && index.column() == Column::Color) {
const QColor color = m_categories.at(index.row()).color();
if (color.isValid())
return color;
static const QColor defaultColor = Utils::creatorTheme()->palette().text().color();
return defaultColor;
} else if (index.column() >= Column::Debug && index.column() <= Column::Info) {
if (role == Qt::CheckStateRole) {
const LoggingCategoryEntry &entry = m_categories.at(index.row());
const bool isEnabled = entry.isEnabled(
static_cast<QtMsgType>(index.column() - Column::Debug));
return isEnabled ? Qt::Checked : Qt::Unchecked;
} else if (role == OriginalStateRole) {
const LoggingCategoryEntry &entry = m_categories.at(index.row());
return entry.isEnabledOriginally(static_cast<QtMsgType>(index.column() - Column::Debug))
? Qt::Checked
: Qt::Unchecked;
}
}
return {};
}
bool LoggingCategoryModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (!index.isValid())
return false;
if (role == Qt::CheckStateRole && index.column() >= Column::Debug
&& index.column() <= Column::Info) {
QtMsgType msgType = static_cast<QtMsgType>(index.column() - Column::Debug);
auto &entry = m_categories[index.row()];
bool isEnabled = entry.isEnabled(msgType);
const Qt::CheckState current = isEnabled ? Qt::Checked : Qt::Unchecked;
if (current != value.toInt()) {
entry.setEnabled(msgType, value.toInt() == Qt::Checked);
return true;
}
} else if (role == Qt::DecorationRole && index.column() == Column::Color) {
auto &category = m_categories[index.row()];
QColor currentColor = category.color();
QColor color = value.value<QColor>();
if (color.isValid() && color != currentColor) {
category.setColor(color);
setCategoryColor(category.name(), color);
emit dataChanged(index, index, {Qt::DisplayRole});
return true;
}
}
return false;
}
Qt::ItemFlags LoggingCategoryModel::flags(const QModelIndex &index) const
{
if (!index.isValid())
return Qt::NoItemFlags;
if (index.column() == LoggingCategoryModel::Column::Fatal)
return Qt::NoItemFlags;
if (index.column() == Column::Name || index.column() == Column::Color)
return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
if (m_useOriginal)
return Qt::ItemIsSelectable | Qt::ItemIsUserCheckable;
return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable;
}
QVariant LoggingCategoryModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (role == Qt::DisplayRole && orientation == Qt::Horizontal && section >= 0 && section < 8) {
switch (section) {
case Column::Name:
return Tr::tr("Category");
case Column::Color:
return Tr::tr("Color");
case Column::Debug:
return Tr::tr("Debug");
case Column::Warning:
return Tr::tr("Warning");
case Column::Critical:
return Tr::tr("Critical");
case Column::Fatal:
return Tr::tr("Fatal");
case Column::Info:
return Tr::tr("Info");
}
}
return {};
}
class LogEntry
{
public:
QString timestamp;
QString type;
QString category;
QString message;
QString outputLine(bool printTimestamp, bool printType) const
{
QString line;
if (printTimestamp)
line.append(timestamp + ' ');
line.append(category);
if (printType)
line.append('.' + type.toLower());
line.append(": ");
line.append(message);
line.append('\n');
return line;
}
};
class LoggingEntryModel : public Utils::ListModel<LogEntry>
{
public:
~LoggingEntryModel() { qInstallMessageHandler(m_originalMessageHandler); }
static void logMessageHandler(QtMsgType type,
const QMessageLogContext &context,
const QString &mssg)
{
instance().msgHandler(type, context, mssg);
}
static QVariant logEntryDataAccessor(const LogEntry &entry, int column, int role)
{
if (column >= 0 && column <= 3 && (role == Qt::DisplayRole || role == Qt::ToolTipRole)) {
switch (column) {
case 0:
return entry.timestamp;
case 1:
return entry.category;
case 2:
return entry.type;
case 3: {
if (role == Qt::ToolTipRole)
return entry.message;
return entry.message.left(1000);
}
}
}
if (role == Qt::TextAlignmentRole)
return Qt::AlignTop;
if (column == 1 && role == Qt::ForegroundRole)
return colorForCategory(entry.category);
return {};
}
static LoggingEntryModel &instance()
{
static LoggingEntryModel model;
return model;
}
void setEnabled(bool enabled) { m_enabled = enabled; }
private:
LoggingEntryModel()
{
setHeader({Tr::tr("Timestamp"), Tr::tr("Category"), Tr::tr("Type"), Tr::tr("Message")});
setDataAccessor(&logEntryDataAccessor);
m_originalMessageHandler = qInstallMessageHandler(logMessageHandler);
}
void msgHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
if (!m_enabled) {
m_originalMessageHandler(type, context, msg);
return;
}
if (!context.category) {
m_originalMessageHandler(type, context, msg);
return;
}
const QString category = QString::fromLocal8Bit(context.category);
const QString timestamp = QDateTime::currentDateTime().toString("HH:mm:ss.zzz");
auto append = [this, timestamp, type, category, msg] {
if (rowCount() >= 1000000) // limit log to 1000000 items
destroyItem(itemForIndex(index(0, 0)));
appendItem(LogEntry{timestamp, messageTypeToString(type), category, msg});
};
if (QThread::currentThread() != thread())
QMetaObject::invokeMethod(this, append, Qt::QueuedConnection);
else
append();
}
private:
QtMessageHandler m_originalMessageHandler{nullptr};
bool m_enabled{true};
};
class LoggingViewManagerWidget : public QDialog
{
public:
~LoggingViewManagerWidget() { LoggingEntryModel::instance().setEnabled(false); }
static LoggingViewManagerWidget *instance()
{
static QPointer<LoggingViewManagerWidget> instance = new LoggingViewManagerWidget(
Core::ICore::dialogParent());
return instance;
}
protected:
void showEvent(QShowEvent *) override
{
if (!m_stopLog->isChecked())
m_categoryModel->setUseOriginal(false);
LoggingEntryModel::instance().setEnabled(!m_stopLog->isChecked());
}
void hideEvent(QHideEvent *) override
{
m_categoryModel->setUseOriginal(true);
LoggingEntryModel::instance().setEnabled(false);
}
private:
explicit LoggingViewManagerWidget(QWidget *parent);
private:
void showLogViewContextMenu(const QPoint &pos) const;
void showLogCategoryContextMenu(const QPoint &pos) const;
void saveLoggingsToFile() const;
QSortFilterProxyModel *m_sortFilterModel = nullptr;
LoggingCategoryModel *m_categoryModel = nullptr;
Utils::BaseTreeView *m_logView = nullptr;
Utils::BaseTreeView *m_categoryView = nullptr;
QToolButton *m_timestamps = nullptr;
QToolButton *m_messageTypes = nullptr;
QToolButton *m_stopLog = nullptr;
};
LoggingViewManagerWidget::LoggingViewManagerWidget(QWidget *parent)
: QDialog(parent)
{
setWindowTitle(Tr::tr("Logging Category Viewer"));
auto save = new QToolButton;
save->setIcon(Utils::Icons::SAVEFILE.icon());
save->setToolTip(Tr::tr("Save Log"));
auto clean = new QToolButton;
clean->setIcon(Utils::Icons::CLEAN.icon());
clean->setToolTip(Tr::tr("Clear"));
m_stopLog = new QToolButton;
m_stopLog->setIcon(Utils::Icons::STOP_SMALL.icon());
m_stopLog->setToolTip(Tr::tr("Stop Logging"));
m_stopLog->setCheckable(true);
auto qtInternal = new QToolButton;
qtInternal->setIcon(Core::Icons::QTLOGO.icon());
qtInternal->setToolTip(Tr::tr("Filter Qt Internal Log Categories"));
qtInternal->setCheckable(false);
auto autoScroll = new QToolButton;
autoScroll->setIcon(Utils::Icons::ARROW_DOWN.icon());
autoScroll->setToolTip(Tr::tr("Auto Scroll"));
autoScroll->setCheckable(true);
autoScroll->setChecked(true);
m_timestamps = new QToolButton;
auto icon = Utils::Icon({{":/utils/images/stopwatch.png", Utils::Theme::PanelTextColorMid}},
Utils::Icon::Tint);
m_timestamps->setIcon(icon.icon());
m_timestamps->setToolTip(Tr::tr("Timestamps"));
m_timestamps->setCheckable(true);
m_timestamps->setChecked(true);
m_messageTypes = new QToolButton;
icon = Utils::Icon({{":/utils/images/message.png", Utils::Theme::PanelTextColorMid}},
Utils::Icon::Tint);
m_messageTypes->setIcon(icon.icon());
m_messageTypes->setToolTip(Tr::tr("Message Types"));
m_messageTypes->setCheckable(true);
m_messageTypes->setChecked(false);
m_logView = new Utils::BaseTreeView;
m_logView->setModel(&LoggingEntryModel::instance());
m_logView->setUniformRowHeights(true);
m_logView->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
m_logView->setFrameStyle(QFrame::Box);
m_logView->setAttribute(Qt::WA_MacShowFocusRect, false);
m_logView->setSelectionMode(QAbstractItemView::ExtendedSelection);
m_logView->setColumnHidden(2, true);
m_logView->setContextMenuPolicy(Qt::CustomContextMenu);
m_categoryModel = new LoggingCategoryModel(this);
m_sortFilterModel = new QSortFilterProxyModel(m_categoryModel);
m_sortFilterModel->setSourceModel(m_categoryModel);
m_sortFilterModel->sort(LoggingCategoryModel::Column::Name);
m_sortFilterModel->setSortRole(Qt::DisplayRole);
m_sortFilterModel->setFilterKeyColumn(LoggingCategoryModel::Column::Name);
m_categoryView = new Utils::BaseTreeView;
m_categoryView->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
m_categoryView->setFrameStyle(QFrame::Box);
m_categoryView->setAttribute(Qt::WA_MacShowFocusRect, false);
m_categoryView->setSelectionMode(QAbstractItemView::SingleSelection);
m_categoryView->setContextMenuPolicy(Qt::CustomContextMenu);
m_categoryView->setModel(m_sortFilterModel);
for (int i = LoggingCategoryModel::Column::Color; i < LoggingCategoryModel::Column::Info; i++)
m_categoryView->resizeColumnToContents(i);
auto filterEdit = new Utils::FancyLineEdit;
filterEdit->setHistoryCompleter("LogFilterCompletionHistory");
filterEdit->setFiltering(true);
filterEdit->setPlaceholderText(Tr::tr("Filter categories by regular expression"));
filterEdit->setText("^(?!qt\\.).+");
filterEdit->setValidationFunction(
[](const QString &input) {
return Utils::asyncRun([input]() -> Utils::Result<QString> {
QRegularExpression re(input);
if (re.isValid())
return input;
return ResultError(
Tr::tr("Invalid regular expression: %1").arg(re.errorString()));
});
});
QSplitter *splitter{nullptr};
using namespace Layouting;
// clang-format off
Column {
Splitter {
bindTo(&splitter),
Column {
noMargin,
Row {
spacing(0),
save,
clean,
m_stopLog,
autoScroll,
m_timestamps,
m_messageTypes,
st,
},
m_logView
},
Column {
noMargin,
Row {
qtInternal,
filterEdit,
},
m_categoryView,
}
}
}.attachTo(this);
// clang-format on
splitter->setOrientation(Qt::Horizontal);
resize(800, 300);
connect(
&LoggingEntryModel::instance(),
&LoggingEntryModel::rowsInserted,
this,
[this, autoScroll] {
if (autoScroll->isChecked())
m_logView->scrollToBottom();
},
Qt::QueuedConnection);
connect(m_categoryView,
&QAbstractItemView::activated,
m_sortFilterModel,
[this](const QModelIndex &index) {
const QVariant value = m_sortFilterModel->data(index, Qt::DecorationRole);
if (!value.isValid())
return;
const QColor original = value.value<QColor>();
if (!original.isValid())
return;
QColor changed = QColorDialog::getColor(original, this);
if (!changed.isValid())
return;
if (original != changed)
m_sortFilterModel->setData(index, changed, Qt::DecorationRole);
});
connect(save, &QToolButton::clicked, this, &LoggingViewManagerWidget::saveLoggingsToFile);
connect(m_logView,
&QAbstractItemView::customContextMenuRequested,
this,
&LoggingViewManagerWidget::showLogViewContextMenu);
connect(m_categoryView,
&QAbstractItemView::customContextMenuRequested,
this,
&LoggingViewManagerWidget::showLogCategoryContextMenu);
connect(clean,
&QToolButton::clicked,
&LoggingEntryModel::instance(),
&Utils::ListModel<LogEntry>::clear);
connect(m_stopLog, &QToolButton::toggled, this, [this](bool checked) {
LoggingEntryModel::instance().setEnabled(!checked);
if (checked) {
m_stopLog->setIcon(Utils::Icons::RUN_SMALL.icon());
m_stopLog->setToolTip(Tr::tr("Start Logging"));
m_categoryModel->setUseOriginal(true);
} else {
m_stopLog->setIcon(Utils::Icons::STOP_SMALL.icon());
m_stopLog->setToolTip(Tr::tr("Stop Logging"));
m_categoryModel->setUseOriginal(false);
}
});
m_sortFilterModel->setFilterRegularExpression("^(?!qt\\.).+");
connect(qtInternal, &QToolButton::clicked, filterEdit, [filterEdit] {
filterEdit->setText("^(?!qt\\.).+");
});
connect(filterEdit,
&Utils::FancyLineEdit::textChanged,
m_sortFilterModel,
[this](const QString &f) {
QRegularExpression re(f);
if (re.isValid())
m_sortFilterModel->setFilterRegularExpression(f);
});
connect(m_timestamps, &QToolButton::toggled, this, [this](bool checked) {
m_logView->setColumnHidden(0, !checked);
});
connect(m_messageTypes, &QToolButton::toggled, this, [this](bool checked) {
m_logView->setColumnHidden(2, !checked);
});
ICore::registerWindow(this, Context("Qtc.LogViewer"));
}
void LoggingViewManagerWidget::showLogViewContextMenu(const QPoint &pos) const
{
QMenu m;
auto copy = new QAction(Tr::tr("Copy Selected Logs"), &m);
m.addAction(copy);
auto copyAll = new QAction(Tr::tr("Copy All"), &m);
m.addAction(copyAll);
connect(copy, &QAction::triggered, &m, [this] {
auto selectionModel = m_logView->selectionModel();
QString copied;
const bool useTS = m_timestamps->isChecked();
const bool useLL = m_messageTypes->isChecked();
for (int row = 0, end = LoggingEntryModel::instance().rowCount(); row < end; ++row) {
if (selectionModel->isRowSelected(row, QModelIndex()))
copied.append(LoggingEntryModel::instance().dataAt(row).outputLine(useTS, useLL));
}
Utils::setClipboardAndSelection(copied);
});
connect(copyAll, &QAction::triggered, &m, [this] {
QString copied;
const bool useTS = m_timestamps->isChecked();
const bool useLL = m_messageTypes->isChecked();
for (int row = 0, end = LoggingEntryModel::instance().rowCount(); row < end; ++row)
copied.append(LoggingEntryModel::instance().dataAt(row).outputLine(useTS, useLL));
Utils::setClipboardAndSelection(copied);
});
m.exec(m_logView->mapToGlobal(pos));
}
void LoggingViewManagerWidget::showLogCategoryContextMenu(const QPoint &pos) const
{
QModelIndex idx = m_categoryView->indexAt(pos);
QMenu m;
auto uncheckAll = new QAction(Tr::tr("Uncheck All"), &m);
auto resetAll = new QAction(Tr::tr("Reset All"), &m);
auto isTypeColumn = [](int column) {
return column >= LoggingCategoryModel::Column::Debug
&& column <= LoggingCategoryModel::Column::Info;
};
auto setChecked = [this](std::initializer_list<LoggingCategoryModel::Column> columns,
Qt::CheckState checked) {
for (int row = 0, count = m_sortFilterModel->rowCount(); row < count; ++row) {
for (int column : columns) {
m_sortFilterModel->setData(m_sortFilterModel->index(row, column),
checked,
Qt::CheckStateRole);
}
}
};
auto resetToOriginal = [this](std::initializer_list<LoggingCategoryModel::Column> columns) {
for (int row = 0, count = m_sortFilterModel->rowCount(); row < count; ++row) {
for (int column : columns) {
const QModelIndex id = m_sortFilterModel->index(row, column);
m_sortFilterModel->setData(id,
id.data(LoggingCategoryModel::OriginalStateRole),
Qt::CheckStateRole);
}
}
};
if (idx.isValid() && isTypeColumn(idx.column())) {
const LoggingCategoryModel::Column column = static_cast<LoggingCategoryModel::Column>(
idx.column());
bool isChecked = idx.data(Qt::CheckStateRole).toInt() == Qt::Checked;
const QString uncheckText = isChecked ? Tr::tr("Uncheck All %1") : Tr::tr("Check All %1");
uncheckAll->setText(uncheckText.arg(messageTypeToString(
static_cast<QtMsgType>(column - LoggingCategoryModel::Column::Debug))));
resetAll->setText(Tr::tr("Reset All %1")
.arg(messageTypeToString(static_cast<QtMsgType>(
column - LoggingCategoryModel::Column::Debug))));
Qt::CheckState newState = isChecked ? Qt::Unchecked : Qt::Checked;
connect(uncheckAll,
&QAction::triggered,
m_sortFilterModel,
[setChecked, column, newState]() { setChecked({column}, newState); });
connect(resetAll, &QAction::triggered, m_sortFilterModel, [resetToOriginal, column]() {
resetToOriginal({column});
});
} else {
// No need to add Fatal here, as it is read-only
static auto allColumns = {LoggingCategoryModel::Column::Debug,
LoggingCategoryModel::Column::Warning,
LoggingCategoryModel::Column::Critical,
LoggingCategoryModel::Column::Info};
connect(uncheckAll, &QAction::triggered, m_sortFilterModel, [setChecked]() {
setChecked(allColumns, Qt::Unchecked);
});
connect(resetAll, &QAction::triggered, m_sortFilterModel, [resetToOriginal]() {
resetToOriginal(allColumns);
});
}
// minimal load/save - plugins could later provide presets on their own?
auto savePreset = new QAction(Tr::tr("Save Enabled as Preset..."), &m);
m.addAction(savePreset);
auto loadPreset = new QAction(Tr::tr("Update from Preset..."), &m);
m.addAction(loadPreset);
m.addAction(uncheckAll);
m.addAction(resetAll);
connect(savePreset,
&QAction::triggered,
m_categoryModel,
&LoggingCategoryModel::saveEnabledCategoryPreset);
connect(loadPreset,
&QAction::triggered,
m_categoryModel,
&LoggingCategoryModel::loadAndUpdateFromPreset);
m.exec(m_categoryView->mapToGlobal(pos));
}
void LoggingViewManagerWidget::saveLoggingsToFile() const
{
const FilePath fp = FileUtils::getSaveFilePath(Tr::tr("Save Logs As"), {}, "*.log");
if (fp.isEmpty())
return;
const bool useTS = m_timestamps->isChecked();
const bool useLL = m_messageTypes->isChecked();
QFile file(fp.path());
if (file.open(QIODevice::WriteOnly)) {
for (int row = 0, end = LoggingEntryModel::instance().rowCount(); row < end; ++row) {
qint64 res = file.write(
LoggingEntryModel::instance().dataAt(row).outputLine(useTS, useLL).toUtf8());
if (res == -1) {
QMessageBox::critical(ICore::dialogParent(),
Tr::tr("Error"),
Tr::tr("Failed to write logs to \"%1\".")
.arg(fp.toUserOutput()));
break;
}
}
file.close();
} else {
QMessageBox::critical(ICore::dialogParent(),
Tr::tr("Error"),
Tr::tr("Failed to open file \"%1\" for writing logs.")
.arg(fp.toUserOutput()));
}
}
void LoggingCategoryModel::saveEnabledCategoryPreset() const
{
FilePath fp = FileUtils::getSaveFilePath(Tr::tr("Save Enabled Categories As..."), {}, "*.json");
if (fp.isEmpty())
return;
auto minLevel = [](const LoggingCategoryEntry &logCategory) {
for (int i = QtDebugMsg; i <= QtInfoMsg; i++) {
if (logCategory.isEnabled(static_cast<QtMsgType>(i)))
return i;
}
return QtInfoMsg + 1;
};
QJsonArray array;
for (const auto &item : m_categories) {
QJsonObject itemObj;
itemObj.insert("name", item.name());
QJsonObject entryObj;
entryObj.insert("level", minLevel(item));
if (item.color().isValid())
entryObj.insert("color", item.color().name(QColor::HexArgb));
QVariantMap levels = {{"Debug", item.isDebugEnabled()},
{"Warning", item.isWarningEnabled()},
{"Critical", item.isCriticalEnabled()},
{"Info", item.isInfoEnabled()}};
entryObj.insert("levels", QJsonValue::fromVariant(levels));
itemObj.insert("entry", entryObj);
array.append(itemObj);
}
QJsonDocument doc(array);
if (!fp.writeFileContents(doc.toJson(QJsonDocument::Compact)))
QMessageBox::critical(ICore::dialogParent(),
Tr::tr("Error"),
Tr::tr("Failed to write preset file \"%1\".").arg(fp.toUserOutput()));
}
void LoggingCategoryModel::loadAndUpdateFromPreset()
{
FilePath fp = FileUtils::getOpenFilePath(Tr::tr("Load Enabled Categories From"));
if (fp.isEmpty())
return;
// read file, update categories
const Utils::Result<QByteArray> contents = fp.fileContents();
if (!contents) {
QMessageBox::critical(ICore::dialogParent(),
Tr::tr("Error"),
Tr::tr("Failed to open preset file \"%1\" for reading.")
.arg(fp.toUserOutput()));
return;
}
QJsonParseError error;
QJsonDocument doc = QJsonDocument::fromJson(*contents, &error);
if (error.error != QJsonParseError::NoError) {
QMessageBox::critical(ICore::dialogParent(),
Tr::tr("Error"),
Tr::tr("Failed to read preset file \"%1\": %2")
.arg(fp.toUserOutput())
.arg(error.errorString()));
return;
}
bool formatError = false;
QList<SavedEntry> presetItems;
if (doc.isArray()) {
const QJsonArray array = doc.array();
for (const QJsonValue &value : array) {
if (!value.isObject()) {
formatError = true;
break;
}
const QJsonObject itemObj = value.toObject();
Utils::Result<SavedEntry> item = SavedEntry::fromJson(itemObj);
if (!item) {
formatError = true;
break;
}
presetItems.append(*item);
}
} else {
formatError = true;
}
if (formatError) {
QMessageBox::critical(ICore::dialogParent(),
Tr::tr("Error"),
Tr::tr("Unexpected preset file format."));
}
int idx = 0;
for (auto it = presetItems.begin(); it != presetItems.end(); ++it, ++idx) {
QList<LoggingCategoryEntry>::iterator itExisting
= std::find_if(m_categories.begin(),
m_categories.end(),
[e = *it](const LoggingCategoryEntry &cat) {
return cat.name() == e.name;
});
if (it->color.isValid())
setCategoryColor(it->name, it->color);
if (itExisting != m_categories.end()) {
itExisting->setSaved(*it);
emit dataChanged(createIndex(idx, Column::Color), createIndex(idx, Column::Info));
} else {
LoggingCategoryEntry newEntry(*it);
append(newEntry);
}
}
}
QColor colorForCategory(const QString &category)
{
auto entry = s_categoryColor.find(category);
if (entry == s_categoryColor.end())
return Utils::creatorTheme()->palette().text().color();
return entry.value();
}
void setCategoryColor(const QString &category, const QColor &color)
{
const QColor baseColor = Utils::creatorTheme()->palette().text().color();
if (color != baseColor)
s_categoryColor.insert(category, color);
else
s_categoryColor.remove(category);
}
static bool wasLogViewerShown = false;
void LoggingViewer::showLoggingView()
{
LoggingViewManagerWidget *staticLogWidget = LoggingViewManagerWidget::instance();
QTC_ASSERT(staticLogWidget, return);
staticLogWidget->show();
staticLogWidget->raise();
staticLogWidget->activateWindow();
wasLogViewerShown = true;
}
void LoggingViewer::hideLoggingView()
{
if (!wasLogViewerShown)
return;
LoggingViewManagerWidget *staticLogWidget = LoggingViewManagerWidget::instance();
QTC_ASSERT(staticLogWidget, return);
staticLogWidget->close();
delete staticLogWidget;
}
} // namespace Core::Internal
#include "loggingviewer.moc"
|