feat: update logging

This commit is contained in:
2026-01-15 01:02:45 +01:00
parent e98c0f42cc
commit 1cadf6a873
7 changed files with 118 additions and 75 deletions
+19 -20
View File
@@ -1,7 +1,7 @@
/* /*
* @Author: Uyanide pywang0608@foxmail.com * @Author: Uyanide pywang0608@foxmail.com
* @Date: 2025-08-05 01:34:52 * @Date: 2025-08-05 01:34:52
* @LastEditTime: 2025-08-07 22:27:50 * @LastEditTime: 2026-01-15 00:48:36
* @Description: Configuration manager. * @Description: Configuration manager.
*/ */
#include "config.h" #include "config.h"
@@ -23,13 +23,13 @@ const QString Config::s_DefaultConfigFileName = "config.json";
Config::Config(const QString& configDir, const QStringList& searchDirs, QObject* parent) Config::Config(const QString& configDir, const QStringList& searchDirs, QObject* parent)
: QObject(parent), m_configDir(configDir) { : QObject(parent), m_configDir(configDir) {
info(QString("Loading configuration from: %1").arg(configDir)); debug(QString("Loading configuration from: %1 ...").arg(configDir));
_loadConfig(configDir + QDir::separator() + s_DefaultConfigFileName); _loadConfig(configDir + QDir::separator() + s_DefaultConfigFileName);
info(QString("Additional search directories: %1").arg(searchDirs.join(", "))); debug(QString("Additional search directories: %1").arg(searchDirs.join(", ")));
m_wallpaperConfig.dirs.append(searchDirs); m_wallpaperConfig.dirs.append(searchDirs);
info("Loading wallpapers ..."); debug("Loading wallpapers ...");
_loadWallpapers(); _loadWallpapers();
} }
@@ -39,7 +39,7 @@ Config::~Config() {
void Config::_loadConfig(const QString& configPath) { void Config::_loadConfig(const QString& configPath) {
QFile configFile(configPath); QFile configFile(configPath);
if (!configFile.open(QIODevice::ReadOnly)) { if (!configFile.open(QIODevice::ReadOnly)) {
error(QString("Failed to open config file: %1").arg(configPath)); critical(QString("Failed to open config file: %1").arg(configPath));
return; return;
} }
QByteArray configData = configFile.readAll(); QByteArray configData = configFile.readAll();
@@ -47,7 +47,7 @@ void Config::_loadConfig(const QString& configPath) {
QJsonDocument jsonDoc = QJsonDocument::fromJson(configData); QJsonDocument jsonDoc = QJsonDocument::fromJson(configData);
if (jsonDoc.isNull() || !jsonDoc.isObject()) { if (jsonDoc.isNull() || !jsonDoc.isObject()) {
error(QString("Invalid JSON format in config file")); critical(QString("Invalid JSON format in config file"));
return; return;
} }
@@ -83,43 +83,43 @@ void Config::_loadConfig(const QString& configPath) {
{"action.confirm", "confirm", [this](const QJsonValue& val) { {"action.confirm", "confirm", [this](const QJsonValue& val) {
if (val.isString()) { if (val.isString()) {
m_actionConfig.confirm = ::expandPath(val.toString()); m_actionConfig.confirm = ::expandPath(val.toString());
info(QString("Action confirm: %1").arg(m_actionConfig.confirm), GeneralLogger::STEP); debug(QString("Action confirm: %1").arg(m_actionConfig.confirm));
} }
}}, }},
{"style.aspect_ratio", "aspect_ratio", [this](const QJsonValue& val) { {"style.aspect_ratio", "aspect_ratio", [this](const QJsonValue& val) {
if (val.isDouble() && val.toDouble() > 0) { if (val.isDouble() && val.toDouble() > 0) {
m_styleConfig.aspectRatio = val.toDouble(); m_styleConfig.aspectRatio = val.toDouble();
info(QString("Aspect ratio: %1").arg(m_styleConfig.aspectRatio), GeneralLogger::STEP); debug(QString("Aspect ratio: %1").arg(m_styleConfig.aspectRatio));
} }
}}, }},
{"style.image_width", "image_width", [this](const QJsonValue& val) { {"style.image_width", "image_width", [this](const QJsonValue& val) {
if (val.isDouble() && val.toDouble() > 0) { if (val.isDouble() && val.toDouble() > 0) {
m_styleConfig.imageWidth = val.toInt(); m_styleConfig.imageWidth = val.toInt();
info(QString("Image width: %1").arg(m_styleConfig.imageWidth), GeneralLogger::STEP); debug(QString("Image width: %1").arg(m_styleConfig.imageWidth));
} }
}}, }},
{"style.image_focus_width", "image_focus_width", [this](const QJsonValue& val) { {"style.image_focus_width", "image_focus_width", [this](const QJsonValue& val) {
if (val.isDouble() && val.toDouble() > 0) { if (val.isDouble() && val.toDouble() > 0) {
m_styleConfig.imageFocusWidth = val.toInt(); m_styleConfig.imageFocusWidth = val.toInt();
info(QString("Image focus width: %1").arg(m_styleConfig.imageFocusWidth), GeneralLogger::STEP); debug(QString("Image focus width: %1").arg(m_styleConfig.imageFocusWidth));
} }
}}, }},
{"style.window_width", "window_width", [this](const QJsonValue& val) { {"style.window_width", "window_width", [this](const QJsonValue& val) {
if (val.isDouble() && val.toDouble() > 0) { if (val.isDouble() && val.toDouble() > 0) {
m_styleConfig.windowWidth = val.toInt(); m_styleConfig.windowWidth = val.toInt();
info(QString("Window width: %1").arg(m_styleConfig.windowWidth), GeneralLogger::STEP); debug(QString("Window width: %1").arg(m_styleConfig.windowWidth));
} }
}}, }},
{"style.window_height", "window_height", [this](const QJsonValue& val) { {"style.window_height", "window_height", [this](const QJsonValue& val) {
if (val.isDouble() && val.toDouble() > 0) { if (val.isDouble() && val.toDouble() > 0) {
m_styleConfig.windowHeight = val.toInt(); m_styleConfig.windowHeight = val.toInt();
info(QString("Window height: %1").arg(m_styleConfig.windowHeight), GeneralLogger::STEP); debug(QString("Window height: %1").arg(m_styleConfig.windowHeight));
} }
}}, }},
{"style.no_loading_screen", "no_loading_screen", [this](const QJsonValue& val) { {"style.no_loading_screen", "no_loading_screen", [this](const QJsonValue& val) {
if (val.isBool()) { if (val.isBool()) {
m_styleConfig.noLoadingScreen = val.toBool(); m_styleConfig.noLoadingScreen = val.toBool();
info(QString("No loading screen: %1").arg(m_styleConfig.noLoadingScreen), GeneralLogger::STEP); debug(QString("No loading screen: %1").arg(m_styleConfig.noLoadingScreen));
} }
}}, }},
{"sort.type", "type", [this](const QJsonValue& val) { {"sort.type", "type", [this](const QJsonValue& val) {
@@ -134,20 +134,19 @@ void Config::_loadConfig(const QString& configPath) {
} else if (type == "size") { } else if (type == "size") {
m_sortConfig.type = SortType::Size; m_sortConfig.type = SortType::Size;
} else { } else {
warn(QString("Unknown sort type: %1").arg(type), GeneralLogger::STEP); warn(QString("Unknown sort type: %1").arg(type));
} }
} }
info(QString("Sort type: %1").arg(static_cast<int>(m_sortConfig.type)), GeneralLogger::STEP); debug(QString("Sort type: %1").arg(static_cast<int>(m_sortConfig.type)));
}}, }},
{"sort.reverse", "reverse", [this](const QJsonValue& val) { {"sort.reverse", "reverse", [this](const QJsonValue& val) {
if (val.isBool()) { if (val.isBool()) {
m_sortConfig.reverse = val.toBool(); m_sortConfig.reverse = val.toBool();
info(QString("Sort reverse: %1").arg(m_sortConfig.reverse), GeneralLogger::STEP); debug(QString("Sort reverse: %1").arg(m_sortConfig.reverse));
} }
}}, }},
}; };
// 统一解析
for (const auto& mapping : mappings) { for (const auto& mapping : mappings) {
([&mapping, &jsonObj]() { ([&mapping, &jsonObj]() {
auto pathParts = mapping.path.split('.'); auto pathParts = mapping.path.split('.');
@@ -180,12 +179,12 @@ void Config::_loadWallpapers() {
QSet<QString> paths; QSet<QString> paths;
info(QString("Loading wallpapers from %1 specified paths").arg(m_wallpaperConfig.paths.size()), LogIndent::STEP); debug(QString("Loading wallpapers from %1 specified paths...").arg(m_wallpaperConfig.paths.size()));
for (const QString& path : m_wallpaperConfig.paths) { for (const QString& path : m_wallpaperConfig.paths) {
paths.insert(path); paths.insert(path);
} }
info(QString("Loading wallpapers from %1 specified directories").arg(m_wallpaperConfig.dirs.size()), LogIndent::STEP); debug(QString("Loading wallpapers from %1 specified directories...").arg(m_wallpaperConfig.dirs.size()));
for (const QString& dirPath : m_wallpaperConfig.dirs) { for (const QString& dirPath : m_wallpaperConfig.dirs) {
QDir dir(dirPath); QDir dir(dirPath);
if (dir.exists()) { if (dir.exists()) {
@@ -199,7 +198,7 @@ void Config::_loadWallpapers() {
} }
} }
info(QString("Excluding %1 specified paths").arg(m_wallpaperConfig.excludes.size()), LogIndent::STEP); debug(QString("Excluding %1 specified paths...").arg(m_wallpaperConfig.excludes.size()));
for (const QString& exclude : m_wallpaperConfig.excludes) { for (const QString& exclude : m_wallpaperConfig.excludes) {
paths.remove(exclude); paths.remove(exclude);
} }
+3 -3
View File
@@ -1,7 +1,7 @@
/* /*
* @Author: Uyanide pywang0608@foxmail.com * @Author: Uyanide pywang0608@foxmail.com
* @Date: 2025-11-30 20:32:27 * @Date: 2025-11-30 20:32:27
* @LastEditTime: 2026-01-14 23:31:41 * @LastEditTime: 2026-01-15 00:48:24
* @Description: Image item widget for displaying an image. * @Description: Image item widget for displaying an image.
*/ */
#include "image_item.h" #include "image_item.h"
@@ -18,7 +18,7 @@ ImageData* ImageData::create(const QString& p, const int initWidth, const int in
QImageReader reader(p); QImageReader reader(p);
if (!reader.canRead()) { if (!reader.canRead()) {
error(QString("Failed to load image from path: %1").arg(p)); warn(QString("Failed to load image from path: %1").arg(p));
delete data; delete data;
return nullptr; return nullptr;
} }
@@ -36,7 +36,7 @@ ImageData* ImageData::create(const QString& p, const int initWidth, const int in
} }
if (!reader.read(data->image)) { if (!reader.read(data->image)) {
error(QString("Failed to load image from path: %1").arg(p)); warn(QString("Failed to load image from path: %1").arg(p));
delete data; delete data;
return nullptr; return nullptr;
} }
+2 -2
View File
@@ -1,7 +1,7 @@
/* /*
* @Author: Uyanide pywang0608@foxmail.com * @Author: Uyanide pywang0608@foxmail.com
* @Date: 2025-08-05 01:22:53 * @Date: 2025-08-05 01:22:53
* @LastEditTime: 2026-01-14 23:58:52 * @LastEditTime: 2026-01-15 00:48:17
* @Description: Animated carousel widget for displaying and selecting images. * @Description: Animated carousel widget for displaying and selecting images.
*/ */
#include "images_carousel.h" #include "images_carousel.h"
@@ -343,7 +343,7 @@ void ImagesCarousel::focusCurrImage() {
} }
auto item = getImageItemAt(m_currentIndex); auto item = getImageItemAt(m_currentIndex);
if (!item) { if (!item) {
error(QString("Failed to get item at index: %1").arg(m_currentIndex)); warn(QString("Failed to get item at index: %1").arg(m_currentIndex));
return; return;
} }
item->setFocus(true, m_animationEnabled); item->setFocus(true, m_animationEnabled);
+38 -22
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: 2025-12-01 01:12:49 * @LastEditTime: 2026-01-15 00:58:04
* @Description: Implementation of logger. * @Description: Implementation of logger.
*/ */
#include "logger.h" #include "logger.h"
@@ -17,7 +17,6 @@
Q_LOGGING_CATEGORY(logMain, "wallpaper.carousel") Q_LOGGING_CATEGORY(logMain, "wallpaper.carousel")
static FILE* g_logStream = stderr; static FILE* g_logStream = stderr;
static GeneralLogger::LogIndent g_maxIndent = GeneralLogger::DETAIL;
static bool g_isColored = false; static bool g_isColored = false;
static QMutex g_logMutex; static QMutex g_logMutex;
@@ -33,7 +32,7 @@ static bool checkIsColored(FILE* stream) {
return true; return true;
} }
void myMessageOutput(QtMsgType type, const QMessageLogContext& context, const QString& msg) { static void messageOutput(QtMsgType type, const QMessageLogContext& context, const QString& msg) {
Q_UNUSED(context); Q_UNUSED(context);
QMutexLocker locker(&g_logMutex); QMutexLocker locker(&g_logMutex);
@@ -75,38 +74,55 @@ void myMessageOutput(QtMsgType type, const QMessageLogContext& context, const QS
} }
} }
void Logger::init(FILE* stream, GeneralLogger::LogIndent maxIndent) { void Logger::init(FILE* stream) {
if (stream) { if (stream) {
g_logStream = stream; g_logStream = stream;
} }
g_maxIndent = maxIndent;
g_isColored = checkIsColored(g_logStream); g_isColored = checkIsColored(g_logStream);
qInstallMessageHandler(myMessageOutput); qInstallMessageHandler(messageOutput);
} }
void Logger::setMaxIndent(GeneralLogger::LogIndent indent) { void Logger::setLogLevel(QtMsgType level) {
g_maxIndent = indent; switch (level) {
case QtDebugMsg:
QLoggingCategory::setFilterRules("wallpaper.carousel.debug=true");
break;
case QtInfoMsg:
QLoggingCategory::setFilterRules("wallpaper.carousel.debug=false\nwallpaper.carousel.info=true");
break;
case QtWarningMsg:
QLoggingCategory::setFilterRules("wallpaper.carousel.debug=false\nwallpaper.carousel.info=false\nwallpaper.carousel.warning=true");
break;
case QtCriticalMsg:
QLoggingCategory::setFilterRules("wallpaper.carousel.debug=false\nwallpaper.carousel.info=false\nwallpaper.carousel.warning=false\nwallpaper.carousel.critical=true");
break;
case QtFatalMsg:
QLoggingCategory::setFilterRules("wallpaper.carousel.debug=false\nwallpaper.carousel.info=false\nwallpaper.carousel.warning=false\nwallpaper.carousel.critical=false");
break;
}
} }
GeneralLogger::LogIndent Logger::maxIndent() { void Logger::quiet() {
return g_maxIndent; QLoggingCategory::setFilterRules("wallpaper.carousel.debug=false\nwallpaper.carousel.info=false\nwallpaper.carousel.warning=false\nwallpaper.carousel.critical=false\nwallpaper.carousel.fatal=false");
} }
void GeneralLogger::info(const QString& msg, const GeneralLogger::LogIndent indent) { void GeneralLogger::debug(const QString& msg) {
if (indent > g_maxIndent) return; qCDebug(logMain).noquote() << msg;
QString indentedMsg = QString(" ").repeated(indent) + msg;
qCInfo(logMain).noquote() << indentedMsg;
} }
void GeneralLogger::warn(const QString& msg, const GeneralLogger::LogIndent indent) { void GeneralLogger::info(const QString& msg) {
if (indent > g_maxIndent) return; qCInfo(logMain).noquote() << msg;
QString indentedMsg = QString(" ").repeated(indent) + msg;
qCWarning(logMain).noquote() << indentedMsg;
} }
void GeneralLogger::error(const QString& msg, const GeneralLogger::LogIndent indent) { void GeneralLogger::warn(const QString& msg) {
QString indentedMsg = QString(" ").repeated(indent) + msg; qCWarning(logMain).noquote() << msg;
qCCritical(logMain).noquote() << indentedMsg; }
void GeneralLogger::critical(const QString& msg) {
qCCritical(logMain).noquote() << msg;
}
void GeneralLogger::fatal(const QString& msg) {
qCFatal(logMain).noquote() << msg;
} }
+12 -16
View File
@@ -1,7 +1,7 @@
/* /*
* @Author: Uyanide pywang0608@foxmail.com * @Author: Uyanide pywang0608@foxmail.com
* @Date: 2025-08-05 10:43:31 * @Date: 2025-08-05 10:43:31
* @LastEditTime: 2025-12-01 01:12:53 * @LastEditTime: 2026-01-15 00:57:58
* @Description: A simple thread-safe logger. * @Description: A simple thread-safe logger.
*/ */
#ifndef GENERAL_LOGGER_H #ifndef GENERAL_LOGGER_H
@@ -15,29 +15,25 @@ Q_DECLARE_LOGGING_CATEGORY(logMain)
namespace GeneralLogger { namespace GeneralLogger {
enum LogIndent : qint32 { void debug(const QString& msg);
GENERAL = 0,
STEP = 1,
DETAIL = 2,
};
void info(const QString& msg, void info(const QString& msg);
const LogIndent indent = GENERAL);
void warn(const QString& msg, void warn(const QString& msg);
const LogIndent indent = GENERAL);
void critical(const QString& msg);
void fatal(const QString& msg);
void error(const QString& msg,
const LogIndent indent = GENERAL);
} // namespace GeneralLogger } // namespace GeneralLogger
class Logger { class Logger {
public: public:
static void init(FILE* stream = stderr, static void init(FILE* stream = stderr);
GeneralLogger::LogIndent maxIndent = GeneralLogger::DETAIL);
static void setMaxIndent(GeneralLogger::LogIndent indent); static void setLogLevel(QtMsgType level);
static GeneralLogger::LogIndent maxIndent();
static void quiet();
}; };
#endif // GENERAL_LOGGER_H #endif // GENERAL_LOGGER_H
+31 -2
View File
@@ -1,10 +1,11 @@
/* /*
* @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: 2025-12-01 01:07:34 * @LastEditTime: 2026-01-15 01:00:12
* @Description: Entry point. * @Description: Entry point.
*/ */
#include <qapplication.h> #include <qapplication.h>
#include <qlogging.h>
#include <QApplication> #include <QApplication>
#include <QDir> #include <QDir>
@@ -24,10 +25,38 @@ static QString getConfigDir() {
return configDir; return configDir;
} }
static void setLogLevel(int argc, char* argv[]) {
for (int i = 1; i < argc; ++i) {
QString arg(argv[i]);
if (arg == "--debug" || arg == "-vvv" || arg == "--verbose") {
Logger::setLogLevel(QtDebugMsg);
break;
} else if (arg == "--info" || arg == "-vv") {
Logger::setLogLevel(QtInfoMsg);
break;
} else if (arg == "--warn" || arg == "-v") {
Logger::setLogLevel(QtWarningMsg);
break;
} else if (arg == "--critical") {
Logger::setLogLevel(QtCriticalMsg);
break;
} else if (arg == "--fatal") {
Logger::setLogLevel(QtFatalMsg);
break;
} else if (arg == "--quiet") {
Logger::quiet();
break;
} else {
Logger::setLogLevel(QtInfoMsg);
}
}
}
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
QApplication a(argc, argv); QApplication a(argc, argv);
Logger::init(stderr, GeneralLogger::DETAIL); Logger::init(stderr);
setLogLevel(argc, argv);
Config config(getConfigDir()); Config config(getConfigDir());
+11 -8
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: 2025-12-01 00:44:06 * @LastEditTime: 2026-01-15 00:47:54
* @Description: MainWindow implementation. * @Description: MainWindow implementation.
*/ */
#include "main_window.h" #include "main_window.h"
@@ -139,7 +139,7 @@ void MainWindow::_stopLoadingAndQuit(const std::function<void()>& onStopped) {
if (m_state != Loading) { if (m_state != Loading) {
return; return;
} }
info("Stopping loading."); debug("Stopping loading...");
connect( connect(
m_carousel, m_carousel,
&ImagesCarousel::stopped, &ImagesCarousel::stopped,
@@ -158,21 +158,24 @@ void MainWindow::_onCancelPressed() {
case Loading: case Loading:
// case loading screen is disabled, quit the app // case loading screen is disabled, quit the app
if (m_config.getStyleConfig().noLoadingScreen) { if (m_config.getStyleConfig().noLoadingScreen) {
debug("Stopping loading...");
_stopLoadingAndQuit([this]() { _stopLoadingAndQuit([this]() {
info("Quitting app."); debug("Quitting app...");
close(); close();
}); });
} }
// otherwise, stop loading and display the loaded images // otherwise, stop loading and display the loaded images
else { else {
debug("Stopping loading and displaying loaded images...");
_stopLoadingAndQuit([this]() { _stopLoadingAndQuit([this]() {
debug("Loading stopped.");
_onLoadingCompleted(m_carousel->getLoadedImagesCount()); _onLoadingCompleted(m_carousel->getLoadedImagesCount());
m_carousel->focusCurrImage(); m_carousel->focusCurrImage();
}); });
} }
break; break;
case Ready: case Ready:
info("Quitting app."); debug("Quitting app...");
close(); close();
break; break;
default: default:
@@ -185,7 +188,7 @@ void MainWindow::_onConfirmPressed() {
case Loading: case Loading:
// case loading screen is disabled, confirm the selection // case loading screen is disabled, confirm the selection
if (m_config.getStyleConfig().noLoadingScreen) { if (m_config.getStyleConfig().noLoadingScreen) {
info("Stopping loading and confirming selection."); debug("Stopping loading and confirming selection...");
connect( connect(
m_carousel, m_carousel,
&ImagesCarousel::stopped, &ImagesCarousel::stopped,
@@ -196,7 +199,7 @@ void MainWindow::_onConfirmPressed() {
} }
break; break;
case Ready: case Ready:
info("Confirming selection."); debug("Confirming selection...");
onConfirm(); onConfirm();
break; break;
default: default:
@@ -222,7 +225,7 @@ void MainWindow::closeEvent(QCloseEvent* event) {
if (m_state == Loading) { if (m_state == Loading) {
event->ignore(); event->ignore();
_stopLoadingAndQuit([this]() { _stopLoadingAndQuit([this]() {
info("Quitting app."); debug("Quitting app.");
close(); close();
}); });
} else { } else {
@@ -249,7 +252,7 @@ void MainWindow::onConfirm() {
const auto arguments = QProcess::splitCommand(cmd); const auto arguments = QProcess::splitCommand(cmd);
if (QProcess::execute(arguments.first(), arguments.mid(1))) { if (QProcess::execute(arguments.first(), arguments.mid(1))) {
error(QString("Failed to execute command: %1").arg(cmd)); critical(QString("Failed to execute command: %1").arg(cmd));
return; return;
} }
} }