forked from quickshell/quickshell
core/command: add --tail to log subcommand
This commit is contained in:
parent
f810c63ffc
commit
c78381f6d0
3 changed files with 30 additions and 6 deletions
|
@ -28,6 +28,7 @@
|
||||||
#include "logging_p.hpp"
|
#include "logging_p.hpp"
|
||||||
#include "logging_qtprivate.cpp" // NOLINT
|
#include "logging_qtprivate.cpp" // NOLINT
|
||||||
#include "paths.hpp"
|
#include "paths.hpp"
|
||||||
|
#include "ringbuf.hpp"
|
||||||
|
|
||||||
Q_LOGGING_CATEGORY(logBare, "quickshell.bare");
|
Q_LOGGING_CATEGORY(logBare, "quickshell.bare");
|
||||||
|
|
||||||
|
@ -65,6 +66,7 @@ void LogMessage::formatMessage(
|
||||||
}
|
}
|
||||||
|
|
||||||
if (msg.category == "quickshell.bare") {
|
if (msg.category == "quickshell.bare") {
|
||||||
|
if (!prefix.isEmpty()) stream << ' ';
|
||||||
stream << msg.body;
|
stream << msg.body;
|
||||||
} else {
|
} else {
|
||||||
if (color) {
|
if (color) {
|
||||||
|
@ -243,9 +245,9 @@ void LogManager::init(
|
||||||
thread->start();
|
thread->start();
|
||||||
|
|
||||||
QMetaObject::invokeMethod(
|
QMetaObject::invokeMethod(
|
||||||
&instance->threadProxy,
|
&instance->threadProxy,
|
||||||
&LoggingThreadProxy::initInThread,
|
&LoggingThreadProxy::initInThread,
|
||||||
Qt::BlockingQueuedConnection
|
Qt::BlockingQueuedConnection
|
||||||
);
|
);
|
||||||
|
|
||||||
qCDebug(logLogging) << "Logger initialized.";
|
qCDebug(logLogging) << "Logger initialized.";
|
||||||
|
@ -735,7 +737,7 @@ bool EncodedLogReader::registerCategory() {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool readEncodedLogs(QIODevice* device, bool timestamps, const QString& rulespec) {
|
bool readEncodedLogs(QIODevice* device, bool timestamps, int tail, const QString& rulespec) {
|
||||||
QList<QLoggingRule> rules;
|
QList<QLoggingRule> rules;
|
||||||
|
|
||||||
{
|
{
|
||||||
|
@ -767,6 +769,8 @@ bool readEncodedLogs(QIODevice* device, bool timestamps, const QString& rulespec
|
||||||
|
|
||||||
auto filters = QHash<quint16, CategoryFilter>();
|
auto filters = QHash<quint16, CategoryFilter>();
|
||||||
|
|
||||||
|
auto tailRing = RingBuffer<LogMessage>(tail);
|
||||||
|
|
||||||
LogMessage message;
|
LogMessage message;
|
||||||
auto stream = QTextStream(stdout);
|
auto stream = QTextStream(stdout);
|
||||||
while (reader.read(&message)) {
|
while (reader.read(&message)) {
|
||||||
|
@ -782,6 +786,18 @@ bool readEncodedLogs(QIODevice* device, bool timestamps, const QString& rulespec
|
||||||
}
|
}
|
||||||
|
|
||||||
if (filter.shouldDisplay(message.type)) {
|
if (filter.shouldDisplay(message.type)) {
|
||||||
|
if (tail == 0) {
|
||||||
|
LogMessage::formatMessage(stream, message, color, timestamps);
|
||||||
|
stream << '\n';
|
||||||
|
} else {
|
||||||
|
tailRing.emplace(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (tail != 0) {
|
||||||
|
for (auto i = tailRing.size() - 1; i != -1; i--) {
|
||||||
|
auto& message = tailRing.at(i);
|
||||||
LogMessage::formatMessage(stream, message, color, timestamps);
|
LogMessage::formatMessage(stream, message, color, timestamps);
|
||||||
stream << '\n';
|
stream << '\n';
|
||||||
}
|
}
|
||||||
|
|
|
@ -130,7 +130,7 @@ private:
|
||||||
LoggingThreadProxy threadProxy;
|
LoggingThreadProxy threadProxy;
|
||||||
};
|
};
|
||||||
|
|
||||||
bool readEncodedLogs(QIODevice* device, bool timestamps, const QString& rulespec);
|
bool readEncodedLogs(QIODevice* device, bool timestamps, int tail, const QString& rulespec);
|
||||||
|
|
||||||
} // namespace qs::log
|
} // namespace qs::log
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
|
#include <limits>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#include <CLI/App.hpp>
|
#include <CLI/App.hpp>
|
||||||
|
@ -94,6 +95,7 @@ struct CommandState {
|
||||||
bool noColor = !qEnvironmentVariableIsEmpty("NO_COLOR");
|
bool noColor = !qEnvironmentVariableIsEmpty("NO_COLOR");
|
||||||
bool sparse = false;
|
bool sparse = false;
|
||||||
size_t verbosity = 0;
|
size_t verbosity = 0;
|
||||||
|
int tail = 0;
|
||||||
QStringOption rules;
|
QStringOption rules;
|
||||||
QStringOption readoutRules;
|
QStringOption readoutRules;
|
||||||
QStringOption file;
|
QStringOption file;
|
||||||
|
@ -249,6 +251,10 @@ int runCommand(int argc, char** argv, QCoreApplication* coreApplication) {
|
||||||
|
|
||||||
auto* file = sub->add_option("--file", state.log.file, "Log file to read.");
|
auto* file = sub->add_option("--file", state.log.file, "Log file to read.");
|
||||||
|
|
||||||
|
sub->add_option("-t,--tail", state.log.tail)
|
||||||
|
->description("Maximum number of lines to print, starting from the bottom.")
|
||||||
|
->check(CLI::Range(1, std::numeric_limits<int>::max(), "INT > 0"));
|
||||||
|
|
||||||
sub->add_option("-r,--rules", state.log.readoutRules, "Log file to read.")
|
sub->add_option("-r,--rules", state.log.readoutRules, "Log file to read.")
|
||||||
->description("Rules to apply to the log being read, in the format of QT_LOGGING_RULES.");
|
->description("Rules to apply to the log being read, in the format of QT_LOGGING_RULES.");
|
||||||
|
|
||||||
|
@ -466,7 +472,9 @@ int readLogFile(CommandState& cmd) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
return qs::log::readEncodedLogs(&file, cmd.log.timestamp, *cmd.log.readoutRules) ? 0 : -1;
|
return qs::log::readEncodedLogs(&file, cmd.log.timestamp, cmd.log.tail, *cmd.log.readoutRules)
|
||||||
|
? 0
|
||||||
|
: -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int listInstances(CommandState& cmd) {
|
int listInstances(CommandState& cmd) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue