Skip to content

Commit c78381f

Browse files
committed
core/command: add --tail to log subcommand
1 parent f810c63 commit c78381f

File tree

3 files changed

+30
-6
lines changed

3 files changed

+30
-6
lines changed

src/core/logging.cpp

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "logging_p.hpp"
2929
#include "logging_qtprivate.cpp" // NOLINT
3030
#include "paths.hpp"
31+
#include "ringbuf.hpp"
3132

3233
Q_LOGGING_CATEGORY(logBare, "quickshell.bare");
3334

@@ -65,6 +66,7 @@ void LogMessage::formatMessage(
6566
}
6667

6768
if (msg.category == "quickshell.bare") {
69+
if (!prefix.isEmpty()) stream << ' ';
6870
stream << msg.body;
6971
} else {
7072
if (color) {
@@ -243,9 +245,9 @@ void LogManager::init(
243245
thread->start();
244246

245247
QMetaObject::invokeMethod(
246-
&instance->threadProxy,
247-
&LoggingThreadProxy::initInThread,
248-
Qt::BlockingQueuedConnection
248+
&instance->threadProxy,
249+
&LoggingThreadProxy::initInThread,
250+
Qt::BlockingQueuedConnection
249251
);
250252

251253
qCDebug(logLogging) << "Logger initialized.";
@@ -735,7 +737,7 @@ bool EncodedLogReader::registerCategory() {
735737
return true;
736738
}
737739

738-
bool readEncodedLogs(QIODevice* device, bool timestamps, const QString& rulespec) {
740+
bool readEncodedLogs(QIODevice* device, bool timestamps, int tail, const QString& rulespec) {
739741
QList<QLoggingRule> rules;
740742

741743
{
@@ -767,6 +769,8 @@ bool readEncodedLogs(QIODevice* device, bool timestamps, const QString& rulespec
767769

768770
auto filters = QHash<quint16, CategoryFilter>();
769771

772+
auto tailRing = RingBuffer<LogMessage>(tail);
773+
770774
LogMessage message;
771775
auto stream = QTextStream(stdout);
772776
while (reader.read(&message)) {
@@ -782,6 +786,18 @@ bool readEncodedLogs(QIODevice* device, bool timestamps, const QString& rulespec
782786
}
783787

784788
if (filter.shouldDisplay(message.type)) {
789+
if (tail == 0) {
790+
LogMessage::formatMessage(stream, message, color, timestamps);
791+
stream << '\n';
792+
} else {
793+
tailRing.emplace(message);
794+
}
795+
}
796+
}
797+
798+
if (tail != 0) {
799+
for (auto i = tailRing.size() - 1; i != -1; i--) {
800+
auto& message = tailRing.at(i);
785801
LogMessage::formatMessage(stream, message, color, timestamps);
786802
stream << '\n';
787803
}

src/core/logging.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ class LogManager: public QObject {
130130
LoggingThreadProxy threadProxy;
131131
};
132132

133-
bool readEncodedLogs(QIODevice* device, bool timestamps, const QString& rulespec);
133+
bool readEncodedLogs(QIODevice* device, bool timestamps, int tail, const QString& rulespec);
134134

135135
} // namespace qs::log
136136

src/core/main.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <algorithm>
33
#include <cstdio>
44
#include <cstdlib>
5+
#include <limits>
56
#include <string>
67

78
#include <CLI/App.hpp>
@@ -94,6 +95,7 @@ struct CommandState {
9495
bool noColor = !qEnvironmentVariableIsEmpty("NO_COLOR");
9596
bool sparse = false;
9697
size_t verbosity = 0;
98+
int tail = 0;
9799
QStringOption rules;
98100
QStringOption readoutRules;
99101
QStringOption file;
@@ -249,6 +251,10 @@ int runCommand(int argc, char** argv, QCoreApplication* coreApplication) {
249251

250252
auto* file = sub->add_option("--file", state.log.file, "Log file to read.");
251253

254+
sub->add_option("-t,--tail", state.log.tail)
255+
->description("Maximum number of lines to print, starting from the bottom.")
256+
->check(CLI::Range(1, std::numeric_limits<int>::max(), "INT > 0"));
257+
252258
sub->add_option("-r,--rules", state.log.readoutRules, "Log file to read.")
253259
->description("Rules to apply to the log being read, in the format of QT_LOGGING_RULES.");
254260

@@ -466,7 +472,9 @@ int readLogFile(CommandState& cmd) {
466472
return -1;
467473
}
468474

469-
return qs::log::readEncodedLogs(&file, cmd.log.timestamp, *cmd.log.readoutRules) ? 0 : -1;
475+
return qs::log::readEncodedLogs(&file, cmd.log.timestamp, cmd.log.tail, *cmd.log.readoutRules)
476+
? 0
477+
: -1;
470478
}
471479

472480
int listInstances(CommandState& cmd) {

0 commit comments

Comments
 (0)