refactor: QTextStream instead of stdio

This commit is contained in:
2026-01-15 02:11:35 +01:00
parent ed47e2eaa6
commit 92ba8738fe
2 changed files with 19 additions and 16 deletions
+15 -12
View File
@@ -1,7 +1,7 @@
/* /*
* @Author: Uyanide pywang0608@foxmail.com * @Author: Uyanide pywang0608@foxmail.com
* @Date: 2025-08-07 01:12:37 * @Date: 2025-08-07 01:12:37
* @LastEditTime: 2026-01-15 00:58:04 * @LastEditTime: 2026-01-15 02:10:15
* @Description: Implementation of logger. * @Description: Implementation of logger.
*/ */
#include "logger.h" #include "logger.h"
@@ -12,13 +12,15 @@
#include <QDateTime> #include <QDateTime>
#include <QMutex> #include <QMutex>
#include <QProcessEnvironment> #include <QProcessEnvironment>
#include <QTextStream>
#include <cstdio> #include <cstdio>
Q_LOGGING_CATEGORY(logMain, "wallpaper.carousel") Q_LOGGING_CATEGORY(logMain, "wallpaper.carousel")
static FILE* g_logStream = stderr; static QTextStream* g_logStream = nullptr;
static bool g_isColored = false; static bool g_isColored = false;
static QMutex g_logMutex; static QMutex g_logMutex;
static const QString g_appName = "wallpaper.carousel"; // same as above
static bool checkIsColored(FILE* stream) { static bool checkIsColored(FILE* stream) {
if (!stream || !isatty(fileno(stream))) { if (!stream || !isatty(fileno(stream))) {
@@ -69,16 +71,17 @@ static void messageOutput(QtMsgType type, const QMessageLogContext& context, con
} }
if (g_logStream) { if (g_logStream) {
fprintf(g_logStream, "%s%s %s%s%s\n", qPrintable(colorTag), qPrintable(levelTag), qPrintable(colorText), qPrintable(msg), qPrintable(resetColor)); (*g_logStream) << colorTag << levelTag << " " << colorText << msg << resetColor << Qt::endl;
fflush(g_logStream); g_logStream->flush();
} }
} }
void Logger::init(FILE* stream) { void Logger::init(FILE* stream) {
if (stream) { if (stream) {
g_logStream = stream; delete g_logStream;
g_logStream = new QTextStream(stream);
} }
g_isColored = checkIsColored(g_logStream); g_isColored = checkIsColored(stream);
qInstallMessageHandler(messageOutput); qInstallMessageHandler(messageOutput);
} }
@@ -86,25 +89,25 @@ void Logger::init(FILE* stream) {
void Logger::setLogLevel(QtMsgType level) { void Logger::setLogLevel(QtMsgType level) {
switch (level) { switch (level) {
case QtDebugMsg: case QtDebugMsg:
QLoggingCategory::setFilterRules("wallpaper.carousel.debug=true"); QLoggingCategory::setFilterRules(QString("%1.debug=true").arg(g_appName));
break; break;
case QtInfoMsg: case QtInfoMsg:
QLoggingCategory::setFilterRules("wallpaper.carousel.debug=false\nwallpaper.carousel.info=true"); QLoggingCategory::setFilterRules(QString("%1.debug=false\n%1.info=true").arg(g_appName));
break; break;
case QtWarningMsg: case QtWarningMsg:
QLoggingCategory::setFilterRules("wallpaper.carousel.debug=false\nwallpaper.carousel.info=false\nwallpaper.carousel.warning=true"); QLoggingCategory::setFilterRules(QString("%1.debug=false\n%1.info=false\n%1.warning=true").arg(g_appName));
break; break;
case QtCriticalMsg: case QtCriticalMsg:
QLoggingCategory::setFilterRules("wallpaper.carousel.debug=false\nwallpaper.carousel.info=false\nwallpaper.carousel.warning=false\nwallpaper.carousel.critical=true"); QLoggingCategory::setFilterRules(QString("%1.debug=false\n%1.info=false\n%1.warning=false\n%1.critical=true").arg(g_appName));
break; break;
case QtFatalMsg: case QtFatalMsg:
QLoggingCategory::setFilterRules("wallpaper.carousel.debug=false\nwallpaper.carousel.info=false\nwallpaper.carousel.warning=false\nwallpaper.carousel.critical=false"); QLoggingCategory::setFilterRules(QString("%1.debug=false\n%1.info=false\n%1.warning=false\n%1.critical=false").arg(g_appName));
break; break;
} }
} }
void Logger::quiet() { void Logger::quiet() {
QLoggingCategory::setFilterRules("wallpaper.carousel.debug=false\nwallpaper.carousel.info=false\nwallpaper.carousel.warning=false\nwallpaper.carousel.critical=false\nwallpaper.carousel.fatal=false"); QLoggingCategory::setFilterRules(QString("%1.debug=false\n%1.info=false\n%1.warning=false\n%1.critical=false\n%1.fatal=false").arg(g_appName));
} }
void GeneralLogger::debug(const QString& msg) { void GeneralLogger::debug(const QString& msg) {
+3 -3
View File
@@ -1,7 +1,7 @@
/* /*
* @Author: Uyanide pywang0608@foxmail.com * @Author: Uyanide pywang0608@foxmail.com
* @Date: 2025-08-05 00:37:58 * @Date: 2025-08-05 00:37:58
* @LastEditTime: 2026-01-15 01:40:30 * @LastEditTime: 2026-01-15 02:01:19
* @Description: MainWindow implementation. * @Description: MainWindow implementation.
*/ */
#include "main_window.h" #include "main_window.h"
@@ -10,7 +10,6 @@
#include <QKeyEvent> #include <QKeyEvent>
#include <QProcess> #include <QProcess>
#include <QPushButton> #include <QPushButton>
#include <cstdio>
#include <functional> #include <functional>
#include "./ui_main_window.h" #include "./ui_main_window.h"
@@ -242,7 +241,8 @@ void MainWindow::onConfirm() {
return; return;
} }
info(QString("Selected image: %1").arg(path)); info(QString("Selected image: %1").arg(path));
puts(path.toStdString().c_str()); QTextStream out(stdout);
out << path << Qt::endl;
const auto cmdOrig = m_config.getActionConfig().confirm; const auto cmdOrig = m_config.getActionConfig().confirm;
if (cmdOrig.isEmpty()) { if (cmdOrig.isEmpty()) {
warn("No action defined for confirmation"); warn("No action defined for confirmation");