3232Q_LOGGING_CATEGORY (logBare, " quickshell.bare" );
3333
3434namespace qs ::log {
35+ using namespace qt_logging_registry ;
3536
3637Q_LOGGING_CATEGORY (logLogging, " quickshell.logging" , QtWarningMsg);
3738
@@ -48,8 +49,16 @@ void LogMessage::formatMessage(
4849 QTextStream& stream,
4950 const LogMessage& msg,
5051 bool color,
51- bool timestamp
52+ bool timestamp,
53+ const QString& prefix
5254) {
55+ if (!prefix.isEmpty ()) {
56+ if (color) stream << " \033 [90m" ;
57+ stream << ' [' << prefix << ' ]' ;
58+ if (timestamp) stream << ' ' ;
59+ if (color) stream << " \033 [0m" ;
60+ }
61+
5362 if (timestamp) {
5463 if (color) stream << " \033 [90m" ;
5564 stream << msg.time .toString (" yyyy-MM-dd hh:mm:ss.zzz" );
@@ -102,6 +111,30 @@ bool CategoryFilter::shouldDisplay(QtMsgType type) const {
102111 }
103112}
104113
114+ void CategoryFilter::apply (QLoggingCategory* category) const {
115+ category->setEnabled (QtDebugMsg, this ->debug );
116+ category->setEnabled (QtInfoMsg, this ->info );
117+ category->setEnabled (QtWarningMsg, this ->warn );
118+ category->setEnabled (QtCriticalMsg, this ->critical );
119+ }
120+
121+ void CategoryFilter::applyRule (
122+ QLatin1StringView category,
123+ const qt_logging_registry::QLoggingRule& rule
124+ ) {
125+ auto filterpass = rule.pass (category, QtDebugMsg);
126+ if (filterpass != 0 ) this ->debug = filterpass > 0 ;
127+
128+ filterpass = rule.pass (category, QtInfoMsg);
129+ if (filterpass != 0 ) this ->info = filterpass > 0 ;
130+
131+ filterpass = rule.pass (category, QtWarningMsg);
132+ if (filterpass != 0 ) this ->warn = filterpass > 0 ;
133+
134+ filterpass = rule.pass (category, QtCriticalMsg);
135+ if (filterpass != 0 ) this ->critical = filterpass > 0 ;
136+ }
137+
105138LogManager::LogManager (): stdoutStream(stdout) {}
106139
107140void LogManager::messageHandler (
@@ -122,7 +155,14 @@ void LogManager::messageHandler(
122155 }
123156
124157 if (display) {
125- LogMessage::formatMessage (self->stdoutStream , message, self->colorLogs , false );
158+ LogMessage::formatMessage (
159+ self->stdoutStream ,
160+ message,
161+ self->colorLogs ,
162+ self->timestampLogs ,
163+ self->prefix
164+ );
165+
126166 self->stdoutStream << Qt::endl;
127167 }
128168
@@ -132,21 +172,37 @@ void LogManager::messageHandler(
132172void LogManager::filterCategory (QLoggingCategory* category) {
133173 auto * instance = LogManager::instance ();
134174
175+ auto categoryName = QLatin1StringView (category->categoryName ());
176+ auto isQs = categoryName.startsWith (QLatin1StringView (" quickshell." ));
177+
135178 if (instance->lastCategoryFilter ) {
136179 instance->lastCategoryFilter (category);
137180 }
138181
139- if (QLatin1StringView (category->categoryName ()).startsWith (QLatin1StringView (" quickshell" ))) {
182+ auto filter = CategoryFilter (category);
183+
184+ if (isQs) {
185+ filter.debug = filter.debug || instance->mDefaultLevel == QtDebugMsg;
186+ filter.info = filter.debug || instance->mDefaultLevel == QtInfoMsg;
187+ filter.warn = filter.info || instance->mDefaultLevel == QtWarningMsg;
188+ filter.critical = filter.warn || instance->mDefaultLevel == QtCriticalMsg;
189+ }
190+
191+ for (const auto & rule: *instance->rules ) {
192+ filter.applyRule (categoryName, rule);
193+ }
194+
195+ if (isQs && instance->sparse ) {
140196 // We assume the category name pointer will always be the same and be comparable in the message handler.
141197 LogManager::instance ()->sparseFilters .insert (
142198 static_cast <const void *>(category->categoryName ()),
143- CategoryFilter (category)
199+ filter
144200 );
145201
146- category-> setEnabled (QtDebugMsg, true );
147- category-> setEnabled (QtInfoMsg, true );
148- category-> setEnabled (QtWarningMsg, true );
149- category-> setEnabled (QtCriticalMsg, true );
202+ // all enabled by default
203+ CategoryFilter (). apply (category );
204+ } else {
205+ filter. apply (category );
150206 }
151207}
152208
@@ -155,15 +211,31 @@ LogManager* LogManager::instance() {
155211 return instance;
156212}
157213
158- void LogManager::init (bool color, bool sparseOnly) {
214+ void LogManager::init (
215+ bool color,
216+ bool timestamp,
217+ bool sparseOnly,
218+ QtMsgType defaultLevel,
219+ const QString& rules,
220+ const QString& prefix
221+ ) {
159222 auto * instance = LogManager::instance ();
160223 instance->colorLogs = color;
224+ instance->timestampLogs = timestamp;
225+ instance->sparse = sparseOnly;
226+ instance->prefix = prefix;
227+ instance->mDefaultLevel = defaultLevel;
228+ instance->mRulesString = rules;
229+
230+ {
231+ QLoggingSettingsParser parser;
232+ parser.setContent (rules);
233+ instance->rules = new QList (parser.rules ());
234+ }
161235
162236 qInstallMessageHandler (&LogManager::messageHandler);
163237
164- if (!sparseOnly) {
165- instance->lastCategoryFilter = QLoggingCategory::installFilter (&LogManager::filterCategory);
166- }
238+ instance->lastCategoryFilter = QLoggingCategory::installFilter (&LogManager::filterCategory);
167239
168240 qCDebug (logLogging) << " Creating offthread logger..." ;
169241 auto * thread = new QThread ();
@@ -181,6 +253,10 @@ void LogManager::initFs() {
181253 );
182254}
183255
256+ QString LogManager::rulesString () const { return this ->mRulesString ; }
257+ QtMsgType LogManager::defaultLevel () const { return this ->mDefaultLevel ; }
258+ bool LogManager::isSparse () const { return this ->sparse ; }
259+
184260void LoggingThreadProxy::initInThread () {
185261 this ->logging = new ThreadLogging (this );
186262 this ->logging ->init ();
@@ -654,8 +730,6 @@ bool EncodedLogReader::registerCategory() {
654730}
655731
656732bool readEncodedLogs (QIODevice* device, bool timestamps, const QString& rulespec) {
657- using namespace qt_logging_registry ;
658-
659733 QList<QLoggingRule> rules;
660734
661735 {
@@ -695,17 +769,7 @@ bool readEncodedLogs(QIODevice* device, bool timestamps, const QString& rulespec
695769 filter = filters.value (message.readCategoryId );
696770 } else {
697771 for (const auto & rule: rules) {
698- auto filterpass = rule.pass (message.category , QtDebugMsg);
699- if (filterpass != 0 ) filter.debug = filterpass > 0 ;
700-
701- filterpass = rule.pass (message.category , QtInfoMsg);
702- if (filterpass != 0 ) filter.info = filterpass > 0 ;
703-
704- filterpass = rule.pass (message.category , QtWarningMsg);
705- if (filterpass != 0 ) filter.warn = filterpass > 0 ;
706-
707- filterpass = rule.pass (message.category , QtCriticalMsg);
708- if (filterpass != 0 ) filter.critical = filterpass > 0 ;
772+ filter.applyRule (message.category , rule);
709773 }
710774
711775 filters.insert (message.readCategoryId , filter);
0 commit comments