Share file line logic between logger and message box (#7441)

* Share file line logic between logger and message box

* Update ChangeLog
This commit is contained in:
Nick Bolton 2024-08-13 22:39:09 +01:00 committed by GitHub
parent 7bb1bb2611
commit 28cefff13b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 18 additions and 21 deletions

View file

@ -82,6 +82,7 @@ Enhancements:
- #7437 Show message box explaining why settings are read-only - #7437 Show message box explaining why settings are read-only
- #7438 Compare result of `clickedButton` instead of `exec` - #7438 Compare result of `clickedButton` instead of `exec`
- #7440 Fixed light logo filename on about screen - #7440 Fixed light logo filename on about screen
- #7441 Share file line logic between logger and message box
# 1.14.6 # 1.14.6

View file

@ -40,13 +40,6 @@ const auto kForceDebugMessages = QStringList{
Logger Logger::s_instance; Logger Logger::s_instance;
QString fileLine(const QMessageLogContext &context) {
if (!context.file) {
return "";
}
return QString("%1:%2").arg(context.file).arg(context.line);
}
QString printLine( QString printLine(
FILE *out, const QString &type, const QString &message, FILE *out, const QString &type, const QString &message,
const QString &fileLine = "") { const QString &fileLine = "") {
@ -97,8 +90,7 @@ void Logger::logVerbose(const QString &message) const {
} }
void Logger::handleMessage( void Logger::handleMessage(
const QtMsgType type, const QMessageLogContext &context, const QtMsgType type, const QString &fileLine, const QString &message) {
const QString &message) {
auto mutatedType = type; auto mutatedType = type;
if (kForceDebugMessages.contains(message)) { if (kForceDebugMessages.contains(message)) {
@ -131,7 +123,7 @@ void Logger::handleMessage(
break; break;
} }
const auto logLine = printLine(out, typeString, message, fileLine(context)); const auto logLine = printLine(out, typeString, message, fileLine);
emit newLine(logLine); emit newLine(logLine);
} }

View file

@ -35,8 +35,7 @@ public:
void loadEnvVars(); void loadEnvVars();
void handleMessage( void handleMessage(
const QtMsgType type, const QMessageLogContext &context, const QtMsgType type, const QString &fileLine, const QString &message);
const QString &message);
void logVerbose(const QString &message) const; void logVerbose(const QString &message) const;
signals: signals:

View file

@ -50,9 +50,7 @@ void raiseCriticalDialog() {
} }
void showErrorDialog( void showErrorDialog(
const QString &message, const QMessageLogContext &context, QtMsgType type) { const QString &message, const QString &fileLine, QtMsgType type) {
auto filename = QFileInfo(context.file).fileName();
auto contextString = QString("%1:%2").arg(filename).arg(context.line);
auto title = type == QtFatalMsg ? "Fatal error" : "Critical error"; auto title = type == QtFatalMsg ? "Fatal error" : "Critical error";
QString text; QString text;
@ -75,8 +73,7 @@ void showErrorDialog(
} }
const QString version = QString::fromStdString(synergy::version()); const QString version = QString::fromStdString(synergy::version());
text += text += QString("<pre>v%1\n%2\n%3</pre>").arg(version, message, fileLine);
QString("<pre>v%1\n%2\n%3</pre>").arg(version, message, contextString);
if (type == QtFatalMsg) { if (type == QtFatalMsg) {
// create a blocking message box for fatal errors, as we want to wait // create a blocking message box for fatal errors, as we want to wait
@ -109,13 +106,21 @@ void showErrorDialog(
} }
} }
QString fileLine(const QMessageLogContext &context) {
if (!context.file) {
return "";
}
return QString("%1:%2").arg(context.file).arg(context.line);
}
void messageHandler( void messageHandler(
QtMsgType type, const QMessageLogContext &context, const QString &message) { QtMsgType type, const QMessageLogContext &context, const QString &message) {
Logger::instance().handleMessage(type, context, message); const auto fileLine = messages::fileLine(context);
Logger::instance().handleMessage(type, fileLine, message);
if (type == QtFatalMsg || type == QtCriticalMsg) { if (type == QtFatalMsg || type == QtCriticalMsg) {
showErrorDialog(message, context, type); showErrorDialog(message, fileLine, type);
} }
if (type == QtFatalMsg) { if (type == QtFatalMsg) {

View file

@ -34,7 +34,7 @@ TEST(LoggerTests, handleMessage_withDebugEnvVarOn_emitsNewLine) {
qputenv("SYNERGY_GUI_DEBUG", "true"); qputenv("SYNERGY_GUI_DEBUG", "true");
logger.loadEnvVars(); logger.loadEnvVars();
logger.handleMessage(QtDebugMsg, QMessageLogContext(), "test"); logger.handleMessage(QtDebugMsg, "stub", "test");
EXPECT_THAT(newLineEmitted, HasSubstr("test")); EXPECT_THAT(newLineEmitted, HasSubstr("test"));
@ -50,7 +50,7 @@ TEST(LoggerTests, handleMessage_withDebugEnvVarOff_doesNotEmitNewLine) {
qputenv("SYNERGY_GUI_DEBUG", "false"); qputenv("SYNERGY_GUI_DEBUG", "false");
logger.loadEnvVars(); logger.loadEnvVars();
logger.handleMessage(QtDebugMsg, QMessageLogContext(), "test"); logger.handleMessage(QtDebugMsg, "stub", "test");
EXPECT_FALSE(newLineEmitted); EXPECT_FALSE(newLineEmitted);