Skip to content

Commit

Permalink
fix: print error messages to the console in CLI (fix #3075)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bionus committed Dec 30, 2023
1 parent 9e4a0a0 commit 35fc1b2
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 20 deletions.
9 changes: 3 additions & 6 deletions src/cli/src/cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,9 @@ int parseAndRunCliArgs(QCoreApplication *app, Profile *profile, bool defaultToGu

// Log messages output and level
const bool verbose = parser.isSet(verboseOption);
#if !defined(QT_DEBUG)
Logger::setupMessageOutput(gui || verbose);
#endif
if (verbose) {
Logger::getInstance().setLogLevel(Logger::Debug);
}
Logger::setupMessageOutput(gui || verbose);
Logger::getInstance().setLogLevel(verbose ? Logger::Debug : Logger::Info);
Logger::getInstance().setConsoleOutputLevel(verbose ? Logger::Debug : Logger::Error);

// Stop here for GUI, but pass some information to the main window from the parser later
if (gui) {
Expand Down
56 changes: 43 additions & 13 deletions src/lib/src/logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@
#include <stdexcept>
#include "functions.h"

#ifdef QT_DEBUG
#include <QDebug>
#endif


void Logger::initialize()
{
Expand Down Expand Up @@ -56,17 +52,34 @@ void Logger::setLogFile(const QString &path)
QString Logger::logFile() const
{ return m_logFile.fileName(); }

/**
* Sets the minimum log level for which messages will not be ignored.
*/
void Logger::setLogLevel(LogLevel level)
{
m_level = level;
}

/**
* Sets the minimum log level for which messages will also be printed to the console.
*/
void Logger::setConsoleOutputLevel(LogLevel level)
{
m_consoleOutputLevel = level;
}

/**
* Sets whether an error should also cause the program to stop (all errors considered as fatal).
*/
void Logger::setExitOnError(bool val)
{
m_exitOnError = val;
}


/**
* Qt message handler that formats and redirects the message to Grabber's logger.
*/
void Logger::messageOutput(QtMsgType type, const QMessageLogContext &context, const QString &message)
{
static const QMap<QtMsgType, LogLevel> messageTypes
Expand Down Expand Up @@ -98,24 +111,34 @@ void Logger::messageOutput(QtMsgType type, const QMessageLogContext &context, co
Logger::getInstance().log(QStringLiteral("%1 %2").arg(label, message), level);
}

/**
* Qt message handler that ignores the message.
*/
void Logger::noMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &message)
{
Q_UNUSED(type);
Q_UNUSED(context);
Q_UNUSED(message);
}

/**
* Installs a message handler for Qt internal logs (using qDebug, qWarning, etc.).
* @param log If true, will send all Qt internal logs to the internal log function. If false, drop them.
*/
void Logger::setupMessageOutput(bool log)
{
qInstallMessageHandler(log ? Logger::messageOutput : Logger::noMessageOutput);
}

/**
* Append text in the log in a new line.
* @param l The message to append.
* @param message The message to append.
*/
void Logger::log(const QString &l, LogLevel level)
void Logger::log(const QString &message, LogLevel level)
{
static QTextStream qStdOut(stdout);
static QTextStream qStdErr(stderr);

if (level < m_level) {
return;
}
Expand All @@ -132,22 +155,29 @@ void Logger::log(const QString &l, LogLevel level)
<< QStringLiteral("Warning")
<< QStringLiteral("Error");
const QString &levelStr = levels[level];
QDateTime time = QDateTime::currentDateTime();
const QDateTime time = QDateTime::currentDateTime();
const QString timeStr = time.toString(timeFormat);

// Write ASCII log to file
m_logFile.write(QString("[" + time.toString(timeFormat) + "][" + levelStr + "] " + stripTags(l) + "\n").toUtf8());
const QString strippedMsg("[" + timeStr + "][" + levelStr + "] " + stripTags(message));
m_logFile.write((strippedMsg + "\n").toUtf8());
m_logFile.flush();

// Emit colored HTML log
const QString msg = "[" + time.toString(timeFormat) + "][" + levelStr + "] " + l;
const QString msg("[" + timeStr + "][" + levelStr + "] " + message);
emit newLog(msg);

#ifdef QT_DEBUG
qDebug() << time.toString(timeFormat) << levelStr << l;
#endif
// Print the message to the console
if (level >= m_consoleOutputLevel) {
if (level == LogLevel::Debug || level == LogLevel::Info) {
qStdOut << strippedMsg << Qt::endl;
} else {
qStdErr << strippedMsg << Qt::endl;
}
}

if (m_exitOnError && level == Logger::LogLevel::Error) {
throw std::runtime_error(l.toStdString());
throw std::runtime_error(message.toStdString());
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/lib/src/logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ class Logger : public QObject
void setExitOnError(bool val);
void setLogFile(const QString &path);
void setLogLevel(LogLevel level);
void log(const QString &, LogLevel level = Info);
void setConsoleOutputLevel(LogLevel level);
void log(const QString &message, LogLevel level = Info);
void logCommand(const QString &);
void logCommandSql(const QString &);
void logUpdate(const QString &);
Expand All @@ -58,6 +59,7 @@ class Logger : public QObject
Logger() = default;
QFile m_logFile, m_fCommandsLog, m_fCommandsSqlLog;
LogLevel m_level = LogLevel::Info;
LogLevel m_consoleOutputLevel = LogLevel::Error;
bool m_exitOnError = false;
};

Expand Down

0 comments on commit 35fc1b2

Please sign in to comment.