Skip to content

Commit 94e881e

Browse files
committed
core!: refactor launch sequence
Also includes slight changes to the command syntax. See --help for details.
1 parent da043e0 commit 94e881e

File tree

12 files changed

+682
-604
lines changed

12 files changed

+682
-604
lines changed

src/core/instanceinfo.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,16 @@ QDataStream& operator>>(QDataStream& stream, InstanceInfo& info) {
1313
}
1414

1515
QDataStream& operator<<(QDataStream& stream, const RelaunchInfo& info) {
16-
stream << info.instance << info.noColor << info.sparseLogsOnly;
16+
stream << info.instance << info.noColor << info.timestamp << info.sparseLogsOnly
17+
<< info.defaultLogLevel << info.logRules;
18+
1719
return stream;
1820
}
1921

2022
QDataStream& operator>>(QDataStream& stream, RelaunchInfo& info) {
21-
stream >> info.instance >> info.noColor >> info.sparseLogsOnly;
23+
stream >> info.instance >> info.noColor >> info.timestamp >> info.sparseLogsOnly
24+
>> info.defaultLogLevel >> info.logRules;
25+
2226
return stream;
2327
}
2428

src/core/instanceinfo.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
#pragma once
22

33
#include <qdatetime.h>
4+
#include <qlogging.h>
45
#include <qstring.h>
56

67
struct InstanceInfo {
78
QString instanceId;
89
QString configPath;
910
QString shellId;
10-
QString initialWorkdir;
1111
QDateTime launchTime;
1212

1313
static InstanceInfo CURRENT; // NOLINT
@@ -16,7 +16,10 @@ struct InstanceInfo {
1616
struct RelaunchInfo {
1717
InstanceInfo instance;
1818
bool noColor = false;
19+
bool timestamp = false;
1920
bool sparseLogsOnly = false;
21+
QtMsgType defaultLogLevel = QtWarningMsg;
22+
QString logRules;
2023
};
2124

2225
QDataStream& operator<<(QDataStream& stream, const InstanceInfo& info);

src/core/ipc.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,16 +104,16 @@ void IpcClient::onError(QLocalSocket::LocalSocketError error) {
104104
qCCritical(logIpc) << "Socket Error" << error;
105105
}
106106

107-
bool IpcClient::connect(const QString& id, const std::function<void(IpcClient& client)>& callback) {
107+
int IpcClient::connect(const QString& id, const std::function<void(IpcClient& client)>& callback) {
108108
auto path = QsPaths::ipcPath(id);
109109
auto client = IpcClient(path);
110110
qCDebug(logIpc) << "Connecting to instance" << id << "at" << path;
111111

112112
client.waitForConnected();
113-
if (!client.isConnected()) return false;
113+
if (!client.isConnected()) return -1;
114114
qCDebug(logIpc) << "Connected.";
115115

116116
callback(client);
117-
return true;
117+
return 0;
118118
}
119119
} // namespace qs::ipc

src/core/ipc.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class IpcClient: public QObject {
5656

5757
void kill();
5858

59-
[[nodiscard]] static bool
59+
[[nodiscard]] static int
6060
connect(const QString& id, const std::function<void(IpcClient& client)>& callback);
6161

6262
signals:

src/core/logging.cpp

Lines changed: 89 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
Q_LOGGING_CATEGORY(logBare, "quickshell.bare");
3333

3434
namespace qs::log {
35+
using namespace qt_logging_registry;
3536

3637
Q_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+
105138
LogManager::LogManager(): stdoutStream(stdout) {}
106139

107140
void 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(
132172
void 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+
184260
void LoggingThreadProxy::initInThread() {
185261
this->logging = new ThreadLogging(this);
186262
this->logging->init();
@@ -654,8 +730,6 @@ bool EncodedLogReader::registerCategory() {
654730
}
655731

656732
bool 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);

src/core/logging.hpp

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,13 @@ struct LogMessage {
3737
QByteArray body;
3838
quint16 readCategoryId = 0;
3939

40-
static void formatMessage(QTextStream& stream, const LogMessage& msg, bool color, bool timestamp);
40+
static void formatMessage(
41+
QTextStream& stream,
42+
const LogMessage& msg,
43+
bool color,
44+
bool timestamp,
45+
const QString& prefix = ""
46+
);
4147
};
4248

4349
size_t qHash(const LogMessage& message);
@@ -58,6 +64,10 @@ public slots:
5864
ThreadLogging* logging = nullptr;
5965
};
6066

67+
namespace qt_logging_registry {
68+
class QLoggingRule;
69+
}
70+
6171
struct CategoryFilter {
6272
explicit CategoryFilter() = default;
6373
explicit CategoryFilter(QLoggingCategory* category)
@@ -67,6 +77,8 @@ struct CategoryFilter {
6777
, critical(category->isCriticalEnabled()) {}
6878

6979
[[nodiscard]] bool shouldDisplay(QtMsgType type) const;
80+
void apply(QLoggingCategory* category) const;
81+
void applyRule(QLatin1StringView category, const qt_logging_registry::QLoggingRule& rule);
7082

7183
bool debug = true;
7284
bool info = true;
@@ -78,11 +90,24 @@ class LogManager: public QObject {
7890
Q_OBJECT;
7991

8092
public:
81-
static void init(bool color, bool sparseOnly);
93+
static void init(
94+
bool color,
95+
bool timestamp,
96+
bool sparseOnly,
97+
QtMsgType defaultLevel,
98+
const QString& rules,
99+
const QString& prefix = ""
100+
);
101+
82102
static void initFs();
83103
static LogManager* instance();
84104

85105
bool colorLogs = true;
106+
bool timestampLogs = false;
107+
108+
[[nodiscard]] QString rulesString() const;
109+
[[nodiscard]] QtMsgType defaultLevel() const;
110+
[[nodiscard]] bool isSparse() const;
86111

87112
signals:
88113
void logMessage(LogMessage msg, bool showInSparse);
@@ -94,6 +119,11 @@ class LogManager: public QObject {
94119
static void filterCategory(QLoggingCategory* category);
95120

96121
QLoggingCategory::CategoryFilter lastCategoryFilter = nullptr;
122+
bool sparse = false;
123+
QString prefix;
124+
QString mRulesString;
125+
QList<qt_logging_registry::QLoggingRule>* rules = nullptr;
126+
QtMsgType mDefaultLevel = QtWarningMsg;
97127
QHash<const void*, CategoryFilter> sparseFilters;
98128

99129
QTextStream stdoutStream;

0 commit comments

Comments
 (0)