Skip to content

Commit 01deefe

Browse files
committed
core/log: encode category log levels
1 parent a82fbf4 commit 01deefe

File tree

3 files changed

+39
-8
lines changed

3 files changed

+39
-8
lines changed

src/core/logging.cpp

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <qnamespace.h>
1818
#include <qobject.h>
1919
#include <qobjectdefs.h>
20+
#include <qpair.h>
2021
#include <qstring.h>
2122
#include <qstringview.h>
2223
#include <qsysinfo.h>
@@ -200,16 +201,15 @@ void LogManager::filterCategory(QLoggingCategory* category) {
200201

201202
if (isQs && !instance->sparse) {
202203
// We assume the category name pointer will always be the same and be comparable in the message handler.
203-
LogManager::instance()->sparseFilters.insert(
204-
static_cast<const void*>(category->categoryName()),
205-
filter
206-
);
204+
instance->sparseFilters.insert(static_cast<const void*>(category->categoryName()), filter);
207205

208206
// all enabled by default
209207
CategoryFilter().apply(category);
210208
} else {
211209
filter.apply(category);
212210
}
211+
212+
instance->allFilters.insert(categoryName, filter);
213213
}
214214

215215
LogManager* LogManager::instance() {
@@ -269,6 +269,10 @@ QString LogManager::rulesString() const { return this->mRulesString; }
269269
QtMsgType LogManager::defaultLevel() const { return this->mDefaultLevel; }
270270
bool LogManager::isSparse() const { return this->sparse; }
271271

272+
CategoryFilter LogManager::getFilter(QLatin1StringView category) {
273+
return this->allFilters.value(category);
274+
}
275+
272276
void LoggingThreadProxy::initInThread() {
273277
this->logging = new ThreadLogging(this);
274278
this->logging->init();
@@ -527,7 +531,7 @@ bool DeviceReader::readU64(quint64* data) {
527531
void EncodedLogWriter::setDevice(QIODevice* target) { this->buffer.setDevice(target); }
528532
void EncodedLogReader::setDevice(QIODevice* source) { this->reader.setDevice(source); }
529533

530-
constexpr quint8 LOG_VERSION = 1;
534+
constexpr quint8 LOG_VERSION = 2;
531535

532536
bool EncodedLogWriter::writeHeader() {
533537
this->buffer.writeU8(LOG_VERSION);
@@ -673,14 +677,18 @@ bool EncodedLogReader::read(LogMessage* slot) {
673677
QByteArray body;
674678
if (!this->readString(&body)) return false;
675679

676-
*slot = LogMessage(msgType, QLatin1StringView(category), body, this->lastMessageTime);
680+
*slot = LogMessage(msgType, QLatin1StringView(category.first), body, this->lastMessageTime);
677681
slot->readCategoryId = categoryId;
678682
}
679683

680684
this->recentMessages.emplace(*slot);
681685
return true;
682686
}
683687

688+
CategoryFilter EncodedLogReader::categoryFilterById(quint16 id) {
689+
return this->categories.value(id).second;
690+
}
691+
684692
void EncodedLogWriter::writeOp(EncodedLogOpcode opcode) { this->buffer.writeU8(opcode); }
685693

686694
void EncodedLogWriter::writeVarInt(quint32 n) {
@@ -742,14 +750,31 @@ quint16 EncodedLogWriter::getOrCreateCategory(QLatin1StringView category) {
742750
auto id = this->nextCategory++;
743751
this->categories.insert(category, id);
744752

753+
auto filter = LogManager::instance()->getFilter(category);
754+
quint8 flags = 0;
755+
flags |= filter.debug << 0;
756+
flags |= filter.info << 1;
757+
flags |= filter.warn << 2;
758+
flags |= filter.critical << 3;
759+
760+
this->buffer.writeU8(flags);
745761
return id;
746762
}
747763
}
748764

749765
bool EncodedLogReader::registerCategory() {
750766
QByteArray name;
767+
quint8 flags = 0;
751768
if (!this->readString(&name)) return false;
752-
this->categories.append(name);
769+
if (!this->reader.readU8(&flags)) return false;
770+
771+
CategoryFilter filter;
772+
filter.debug = (flags >> 0) & 1;
773+
filter.info = (flags >> 1) & 1;
774+
filter.warn = (flags >> 2) & 1;
775+
filter.critical = (flags >> 3) & 1;
776+
777+
this->categories.append(qMakePair(name, filter));
753778
return true;
754779
}
755780

@@ -789,6 +814,8 @@ bool LogReader::continueReading() {
789814
if (this->filters.contains(message.readCategoryId)) {
790815
filter = this->filters.value(message.readCategoryId);
791816
} else {
817+
filter = this->reader.categoryFilterById(message.readCategoryId);
818+
792819
for (const auto& rule: this->rules) {
793820
filter.applyRule(message.category, rule);
794821
}

src/core/logging.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ class LogManager: public QObject {
110110
[[nodiscard]] QtMsgType defaultLevel() const;
111111
[[nodiscard]] bool isSparse() const;
112112

113+
[[nodiscard]] CategoryFilter getFilter(QLatin1StringView category);
114+
113115
signals:
114116
void logMessage(LogMessage msg, bool showInSparse);
115117

@@ -126,6 +128,7 @@ class LogManager: public QObject {
126128
QList<qt_logging_registry::QLoggingRule>* rules = nullptr;
127129
QtMsgType mDefaultLevel = QtWarningMsg;
128130
QHash<const void*, CategoryFilter> sparseFilters;
131+
QHash<QLatin1StringView, CategoryFilter> allFilters;
129132

130133
QTextStream stdoutStream;
131134
LoggingThreadProxy threadProxy;

src/core/logging_p.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,15 @@ class EncodedLogReader {
9494
[[nodiscard]] bool readHeader(bool* success, quint8* logVersion, quint8* readerVersion);
9595
// WARNING: log messages written to the given slot are invalidated when the log reader is destroyed.
9696
[[nodiscard]] bool read(LogMessage* slot);
97+
[[nodiscard]] CategoryFilter categoryFilterById(quint16 id);
9798

9899
private:
99100
[[nodiscard]] bool readVarInt(quint32* slot);
100101
[[nodiscard]] bool readString(QByteArray* slot);
101102
[[nodiscard]] bool registerCategory();
102103

103104
DeviceReader reader;
104-
QVector<QByteArray> categories;
105+
QVector<QPair<QByteArray, CategoryFilter>> categories;
105106
QDateTime lastMessageTime = QDateTime::fromSecsSinceEpoch(0);
106107
RingBuffer<LogMessage> recentMessages {256};
107108
};

0 commit comments

Comments
 (0)