forked from quickshell/quickshell
		
	core/log: encode category log levels
This commit is contained in:
		
							parent
							
								
									a82fbf40c2
								
							
						
					
					
						commit
						01deefe241
					
				
					 3 changed files with 39 additions and 8 deletions
				
			
		| 
						 | 
					@ -17,6 +17,7 @@
 | 
				
			||||||
#include <qnamespace.h>
 | 
					#include <qnamespace.h>
 | 
				
			||||||
#include <qobject.h>
 | 
					#include <qobject.h>
 | 
				
			||||||
#include <qobjectdefs.h>
 | 
					#include <qobjectdefs.h>
 | 
				
			||||||
 | 
					#include <qpair.h>
 | 
				
			||||||
#include <qstring.h>
 | 
					#include <qstring.h>
 | 
				
			||||||
#include <qstringview.h>
 | 
					#include <qstringview.h>
 | 
				
			||||||
#include <qsysinfo.h>
 | 
					#include <qsysinfo.h>
 | 
				
			||||||
| 
						 | 
					@ -200,16 +201,15 @@ void LogManager::filterCategory(QLoggingCategory* category) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if (isQs && !instance->sparse) {
 | 
						if (isQs && !instance->sparse) {
 | 
				
			||||||
		// We assume the category name pointer will always be the same and be comparable in the message handler.
 | 
							// We assume the category name pointer will always be the same and be comparable in the message handler.
 | 
				
			||||||
		LogManager::instance()->sparseFilters.insert(
 | 
							instance->sparseFilters.insert(static_cast<const void*>(category->categoryName()), filter);
 | 
				
			||||||
		    static_cast<const void*>(category->categoryName()),
 | 
					 | 
				
			||||||
		    filter
 | 
					 | 
				
			||||||
		);
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
		// all enabled by default
 | 
							// all enabled by default
 | 
				
			||||||
		CategoryFilter().apply(category);
 | 
							CategoryFilter().apply(category);
 | 
				
			||||||
	} else {
 | 
						} else {
 | 
				
			||||||
		filter.apply(category);
 | 
							filter.apply(category);
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						instance->allFilters.insert(categoryName, filter);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
LogManager* LogManager::instance() {
 | 
					LogManager* LogManager::instance() {
 | 
				
			||||||
| 
						 | 
					@ -269,6 +269,10 @@ QString LogManager::rulesString() const { return this->mRulesString; }
 | 
				
			||||||
QtMsgType LogManager::defaultLevel() const { return this->mDefaultLevel; }
 | 
					QtMsgType LogManager::defaultLevel() const { return this->mDefaultLevel; }
 | 
				
			||||||
bool LogManager::isSparse() const { return this->sparse; }
 | 
					bool LogManager::isSparse() const { return this->sparse; }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					CategoryFilter LogManager::getFilter(QLatin1StringView category) {
 | 
				
			||||||
 | 
						return this->allFilters.value(category);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void LoggingThreadProxy::initInThread() {
 | 
					void LoggingThreadProxy::initInThread() {
 | 
				
			||||||
	this->logging = new ThreadLogging(this);
 | 
						this->logging = new ThreadLogging(this);
 | 
				
			||||||
	this->logging->init();
 | 
						this->logging->init();
 | 
				
			||||||
| 
						 | 
					@ -527,7 +531,7 @@ bool DeviceReader::readU64(quint64* data) {
 | 
				
			||||||
void EncodedLogWriter::setDevice(QIODevice* target) { this->buffer.setDevice(target); }
 | 
					void EncodedLogWriter::setDevice(QIODevice* target) { this->buffer.setDevice(target); }
 | 
				
			||||||
void EncodedLogReader::setDevice(QIODevice* source) { this->reader.setDevice(source); }
 | 
					void EncodedLogReader::setDevice(QIODevice* source) { this->reader.setDevice(source); }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
constexpr quint8 LOG_VERSION = 1;
 | 
					constexpr quint8 LOG_VERSION = 2;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
bool EncodedLogWriter::writeHeader() {
 | 
					bool EncodedLogWriter::writeHeader() {
 | 
				
			||||||
	this->buffer.writeU8(LOG_VERSION);
 | 
						this->buffer.writeU8(LOG_VERSION);
 | 
				
			||||||
| 
						 | 
					@ -673,7 +677,7 @@ start:
 | 
				
			||||||
		QByteArray body;
 | 
							QByteArray body;
 | 
				
			||||||
		if (!this->readString(&body)) return false;
 | 
							if (!this->readString(&body)) return false;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		*slot = LogMessage(msgType, QLatin1StringView(category), body, this->lastMessageTime);
 | 
							*slot = LogMessage(msgType, QLatin1StringView(category.first), body, this->lastMessageTime);
 | 
				
			||||||
		slot->readCategoryId = categoryId;
 | 
							slot->readCategoryId = categoryId;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -681,6 +685,10 @@ start:
 | 
				
			||||||
	return true;
 | 
						return true;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					CategoryFilter EncodedLogReader::categoryFilterById(quint16 id) {
 | 
				
			||||||
 | 
						return this->categories.value(id).second;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void EncodedLogWriter::writeOp(EncodedLogOpcode opcode) { this->buffer.writeU8(opcode); }
 | 
					void EncodedLogWriter::writeOp(EncodedLogOpcode opcode) { this->buffer.writeU8(opcode); }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void EncodedLogWriter::writeVarInt(quint32 n) {
 | 
					void EncodedLogWriter::writeVarInt(quint32 n) {
 | 
				
			||||||
| 
						 | 
					@ -742,14 +750,31 @@ quint16 EncodedLogWriter::getOrCreateCategory(QLatin1StringView category) {
 | 
				
			||||||
		auto id = this->nextCategory++;
 | 
							auto id = this->nextCategory++;
 | 
				
			||||||
		this->categories.insert(category, id);
 | 
							this->categories.insert(category, id);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							auto filter = LogManager::instance()->getFilter(category);
 | 
				
			||||||
 | 
							quint8 flags = 0;
 | 
				
			||||||
 | 
							flags |= filter.debug << 0;
 | 
				
			||||||
 | 
							flags |= filter.info << 1;
 | 
				
			||||||
 | 
							flags |= filter.warn << 2;
 | 
				
			||||||
 | 
							flags |= filter.critical << 3;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							this->buffer.writeU8(flags);
 | 
				
			||||||
		return id;
 | 
							return id;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
bool EncodedLogReader::registerCategory() {
 | 
					bool EncodedLogReader::registerCategory() {
 | 
				
			||||||
	QByteArray name;
 | 
						QByteArray name;
 | 
				
			||||||
 | 
						quint8 flags = 0;
 | 
				
			||||||
	if (!this->readString(&name)) return false;
 | 
						if (!this->readString(&name)) return false;
 | 
				
			||||||
	this->categories.append(name);
 | 
						if (!this->reader.readU8(&flags)) return false;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						CategoryFilter filter;
 | 
				
			||||||
 | 
						filter.debug = (flags >> 0) & 1;
 | 
				
			||||||
 | 
						filter.info = (flags >> 1) & 1;
 | 
				
			||||||
 | 
						filter.warn = (flags >> 2) & 1;
 | 
				
			||||||
 | 
						filter.critical = (flags >> 3) & 1;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						this->categories.append(qMakePair(name, filter));
 | 
				
			||||||
	return true;
 | 
						return true;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -789,6 +814,8 @@ bool LogReader::continueReading() {
 | 
				
			||||||
		if (this->filters.contains(message.readCategoryId)) {
 | 
							if (this->filters.contains(message.readCategoryId)) {
 | 
				
			||||||
			filter = this->filters.value(message.readCategoryId);
 | 
								filter = this->filters.value(message.readCategoryId);
 | 
				
			||||||
		} else {
 | 
							} else {
 | 
				
			||||||
 | 
								filter = this->reader.categoryFilterById(message.readCategoryId);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			for (const auto& rule: this->rules) {
 | 
								for (const auto& rule: this->rules) {
 | 
				
			||||||
				filter.applyRule(message.category, rule);
 | 
									filter.applyRule(message.category, rule);
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -110,6 +110,8 @@ public:
 | 
				
			||||||
	[[nodiscard]] QtMsgType defaultLevel() const;
 | 
						[[nodiscard]] QtMsgType defaultLevel() const;
 | 
				
			||||||
	[[nodiscard]] bool isSparse() const;
 | 
						[[nodiscard]] bool isSparse() const;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						[[nodiscard]] CategoryFilter getFilter(QLatin1StringView category);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
signals:
 | 
					signals:
 | 
				
			||||||
	void logMessage(LogMessage msg, bool showInSparse);
 | 
						void logMessage(LogMessage msg, bool showInSparse);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -126,6 +128,7 @@ private:
 | 
				
			||||||
	QList<qt_logging_registry::QLoggingRule>* rules = nullptr;
 | 
						QList<qt_logging_registry::QLoggingRule>* rules = nullptr;
 | 
				
			||||||
	QtMsgType mDefaultLevel = QtWarningMsg;
 | 
						QtMsgType mDefaultLevel = QtWarningMsg;
 | 
				
			||||||
	QHash<const void*, CategoryFilter> sparseFilters;
 | 
						QHash<const void*, CategoryFilter> sparseFilters;
 | 
				
			||||||
 | 
						QHash<QLatin1StringView, CategoryFilter> allFilters;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	QTextStream stdoutStream;
 | 
						QTextStream stdoutStream;
 | 
				
			||||||
	LoggingThreadProxy threadProxy;
 | 
						LoggingThreadProxy threadProxy;
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -94,6 +94,7 @@ public:
 | 
				
			||||||
	[[nodiscard]] bool readHeader(bool* success, quint8* logVersion, quint8* readerVersion);
 | 
						[[nodiscard]] bool readHeader(bool* success, quint8* logVersion, quint8* readerVersion);
 | 
				
			||||||
	// WARNING: log messages written to the given slot are invalidated when the log reader is destroyed.
 | 
						// WARNING: log messages written to the given slot are invalidated when the log reader is destroyed.
 | 
				
			||||||
	[[nodiscard]] bool read(LogMessage* slot);
 | 
						[[nodiscard]] bool read(LogMessage* slot);
 | 
				
			||||||
 | 
						[[nodiscard]] CategoryFilter categoryFilterById(quint16 id);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
private:
 | 
					private:
 | 
				
			||||||
	[[nodiscard]] bool readVarInt(quint32* slot);
 | 
						[[nodiscard]] bool readVarInt(quint32* slot);
 | 
				
			||||||
| 
						 | 
					@ -101,7 +102,7 @@ private:
 | 
				
			||||||
	[[nodiscard]] bool registerCategory();
 | 
						[[nodiscard]] bool registerCategory();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	DeviceReader reader;
 | 
						DeviceReader reader;
 | 
				
			||||||
	QVector<QByteArray> categories;
 | 
						QVector<QPair<QByteArray, CategoryFilter>> categories;
 | 
				
			||||||
	QDateTime lastMessageTime = QDateTime::fromSecsSinceEpoch(0);
 | 
						QDateTime lastMessageTime = QDateTime::fromSecsSinceEpoch(0);
 | 
				
			||||||
	RingBuffer<LogMessage> recentMessages {256};
 | 
						RingBuffer<LogMessage> recentMessages {256};
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue