feat: update logging
This commit is contained in:
+19
-20
@@ -1,7 +1,7 @@
|
||||
/*
|
||||
* @Author: Uyanide pywang0608@foxmail.com
|
||||
* @Date: 2025-08-05 01:34:52
|
||||
* @LastEditTime: 2025-08-07 22:27:50
|
||||
* @LastEditTime: 2026-01-15 00:48:36
|
||||
* @Description: Configuration manager.
|
||||
*/
|
||||
#include "config.h"
|
||||
@@ -23,13 +23,13 @@ const QString Config::s_DefaultConfigFileName = "config.json";
|
||||
|
||||
Config::Config(const QString& configDir, const QStringList& searchDirs, QObject* parent)
|
||||
: 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);
|
||||
|
||||
info(QString("Additional search directories: %1").arg(searchDirs.join(", ")));
|
||||
debug(QString("Additional search directories: %1").arg(searchDirs.join(", ")));
|
||||
m_wallpaperConfig.dirs.append(searchDirs);
|
||||
|
||||
info("Loading wallpapers ...");
|
||||
debug("Loading wallpapers ...");
|
||||
_loadWallpapers();
|
||||
}
|
||||
|
||||
@@ -39,7 +39,7 @@ Config::~Config() {
|
||||
void Config::_loadConfig(const QString& configPath) {
|
||||
QFile configFile(configPath);
|
||||
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;
|
||||
}
|
||||
QByteArray configData = configFile.readAll();
|
||||
@@ -47,7 +47,7 @@ void Config::_loadConfig(const QString& configPath) {
|
||||
|
||||
QJsonDocument jsonDoc = QJsonDocument::fromJson(configData);
|
||||
if (jsonDoc.isNull() || !jsonDoc.isObject()) {
|
||||
error(QString("Invalid JSON format in config file"));
|
||||
critical(QString("Invalid JSON format in config file"));
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -83,43 +83,43 @@ void Config::_loadConfig(const QString& configPath) {
|
||||
{"action.confirm", "confirm", [this](const QJsonValue& val) {
|
||||
if (val.isString()) {
|
||||
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) {
|
||||
if (val.isDouble() && val.toDouble() > 0) {
|
||||
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) {
|
||||
if (val.isDouble() && val.toDouble() > 0) {
|
||||
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) {
|
||||
if (val.isDouble() && val.toDouble() > 0) {
|
||||
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) {
|
||||
if (val.isDouble() && val.toDouble() > 0) {
|
||||
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) {
|
||||
if (val.isDouble() && val.toDouble() > 0) {
|
||||
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) {
|
||||
if (val.isBool()) {
|
||||
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) {
|
||||
@@ -134,20 +134,19 @@ void Config::_loadConfig(const QString& configPath) {
|
||||
} else if (type == "size") {
|
||||
m_sortConfig.type = SortType::Size;
|
||||
} 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) {
|
||||
if (val.isBool()) {
|
||||
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) {
|
||||
([&mapping, &jsonObj]() {
|
||||
auto pathParts = mapping.path.split('.');
|
||||
@@ -180,12 +179,12 @@ void Config::_loadWallpapers() {
|
||||
|
||||
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) {
|
||||
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) {
|
||||
QDir dir(dirPath);
|
||||
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) {
|
||||
paths.remove(exclude);
|
||||
}
|
||||
|
||||
+3
-3
@@ -1,7 +1,7 @@
|
||||
/*
|
||||
* @Author: Uyanide pywang0608@foxmail.com
|
||||
* @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.
|
||||
*/
|
||||
#include "image_item.h"
|
||||
@@ -18,7 +18,7 @@ ImageData* ImageData::create(const QString& p, const int initWidth, const int in
|
||||
|
||||
QImageReader reader(p);
|
||||
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;
|
||||
return nullptr;
|
||||
}
|
||||
@@ -36,7 +36,7 @@ ImageData* ImageData::create(const QString& p, const int initWidth, const int in
|
||||
}
|
||||
|
||||
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;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
/*
|
||||
* @Author: Uyanide pywang0608@foxmail.com
|
||||
* @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.
|
||||
*/
|
||||
#include "images_carousel.h"
|
||||
@@ -343,7 +343,7 @@ void ImagesCarousel::focusCurrImage() {
|
||||
}
|
||||
auto item = getImageItemAt(m_currentIndex);
|
||||
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;
|
||||
}
|
||||
item->setFocus(true, m_animationEnabled);
|
||||
|
||||
+40
-24
@@ -1,7 +1,7 @@
|
||||
/*
|
||||
* @Author: Uyanide pywang0608@foxmail.com
|
||||
* @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.
|
||||
*/
|
||||
#include "logger.h"
|
||||
@@ -16,9 +16,8 @@
|
||||
|
||||
Q_LOGGING_CATEGORY(logMain, "wallpaper.carousel")
|
||||
|
||||
static FILE* g_logStream = stderr;
|
||||
static GeneralLogger::LogIndent g_maxIndent = GeneralLogger::DETAIL;
|
||||
static bool g_isColored = false;
|
||||
static FILE* g_logStream = stderr;
|
||||
static bool g_isColored = false;
|
||||
static QMutex g_logMutex;
|
||||
|
||||
static bool checkIsColored(FILE* stream) {
|
||||
@@ -33,7 +32,7 @@ static bool checkIsColored(FILE* stream) {
|
||||
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);
|
||||
|
||||
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) {
|
||||
g_logStream = stream;
|
||||
}
|
||||
g_maxIndent = maxIndent;
|
||||
g_isColored = checkIsColored(g_logStream);
|
||||
|
||||
qInstallMessageHandler(myMessageOutput);
|
||||
qInstallMessageHandler(messageOutput);
|
||||
}
|
||||
|
||||
void Logger::setMaxIndent(GeneralLogger::LogIndent indent) {
|
||||
g_maxIndent = indent;
|
||||
void Logger::setLogLevel(QtMsgType level) {
|
||||
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() {
|
||||
return g_maxIndent;
|
||||
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");
|
||||
}
|
||||
|
||||
void GeneralLogger::info(const QString& msg, const GeneralLogger::LogIndent indent) {
|
||||
if (indent > g_maxIndent) return;
|
||||
QString indentedMsg = QString(" ").repeated(indent) + msg;
|
||||
qCInfo(logMain).noquote() << indentedMsg;
|
||||
void GeneralLogger::debug(const QString& msg) {
|
||||
qCDebug(logMain).noquote() << msg;
|
||||
}
|
||||
|
||||
void GeneralLogger::warn(const QString& msg, const GeneralLogger::LogIndent indent) {
|
||||
if (indent > g_maxIndent) return;
|
||||
|
||||
QString indentedMsg = QString(" ").repeated(indent) + msg;
|
||||
qCWarning(logMain).noquote() << indentedMsg;
|
||||
void GeneralLogger::info(const QString& msg) {
|
||||
qCInfo(logMain).noquote() << msg;
|
||||
}
|
||||
|
||||
void GeneralLogger::error(const QString& msg, const GeneralLogger::LogIndent indent) {
|
||||
QString indentedMsg = QString(" ").repeated(indent) + msg;
|
||||
qCCritical(logMain).noquote() << indentedMsg;
|
||||
void GeneralLogger::warn(const QString& msg) {
|
||||
qCWarning(logMain).noquote() << msg;
|
||||
}
|
||||
|
||||
void GeneralLogger::critical(const QString& msg) {
|
||||
qCCritical(logMain).noquote() << msg;
|
||||
}
|
||||
|
||||
void GeneralLogger::fatal(const QString& msg) {
|
||||
qCFatal(logMain).noquote() << msg;
|
||||
}
|
||||
|
||||
+12
-16
@@ -1,7 +1,7 @@
|
||||
/*
|
||||
* @Author: Uyanide pywang0608@foxmail.com
|
||||
* @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.
|
||||
*/
|
||||
#ifndef GENERAL_LOGGER_H
|
||||
@@ -15,29 +15,25 @@ Q_DECLARE_LOGGING_CATEGORY(logMain)
|
||||
|
||||
namespace GeneralLogger {
|
||||
|
||||
enum LogIndent : qint32 {
|
||||
GENERAL = 0,
|
||||
STEP = 1,
|
||||
DETAIL = 2,
|
||||
};
|
||||
void debug(const QString& msg);
|
||||
|
||||
void info(const QString& msg,
|
||||
const LogIndent indent = GENERAL);
|
||||
void info(const QString& msg);
|
||||
|
||||
void warn(const QString& msg,
|
||||
const LogIndent indent = GENERAL);
|
||||
void warn(const QString& msg);
|
||||
|
||||
void critical(const QString& msg);
|
||||
|
||||
void fatal(const QString& msg);
|
||||
|
||||
void error(const QString& msg,
|
||||
const LogIndent indent = GENERAL);
|
||||
} // namespace GeneralLogger
|
||||
|
||||
class Logger {
|
||||
public:
|
||||
static void init(FILE* stream = stderr,
|
||||
GeneralLogger::LogIndent maxIndent = GeneralLogger::DETAIL);
|
||||
static void init(FILE* stream = stderr);
|
||||
|
||||
static void setMaxIndent(GeneralLogger::LogIndent indent);
|
||||
static GeneralLogger::LogIndent maxIndent();
|
||||
static void setLogLevel(QtMsgType level);
|
||||
|
||||
static void quiet();
|
||||
};
|
||||
|
||||
#endif // GENERAL_LOGGER_H
|
||||
|
||||
+31
-2
@@ -1,10 +1,11 @@
|
||||
/*
|
||||
* @Author: Uyanide pywang0608@foxmail.com
|
||||
* @Date: 2025-08-05 00:37:58
|
||||
* @LastEditTime: 2025-12-01 01:07:34
|
||||
* @LastEditTime: 2026-01-15 01:00:12
|
||||
* @Description: Entry point.
|
||||
*/
|
||||
#include <qapplication.h>
|
||||
#include <qlogging.h>
|
||||
|
||||
#include <QApplication>
|
||||
#include <QDir>
|
||||
@@ -24,10 +25,38 @@ static QString getConfigDir() {
|
||||
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[]) {
|
||||
QApplication a(argc, argv);
|
||||
|
||||
Logger::init(stderr, GeneralLogger::DETAIL);
|
||||
Logger::init(stderr);
|
||||
setLogLevel(argc, argv);
|
||||
|
||||
Config config(getConfigDir());
|
||||
|
||||
|
||||
+11
-8
@@ -1,7 +1,7 @@
|
||||
/*
|
||||
* @Author: Uyanide pywang0608@foxmail.com
|
||||
* @Date: 2025-08-05 00:37:58
|
||||
* @LastEditTime: 2025-12-01 00:44:06
|
||||
* @LastEditTime: 2026-01-15 00:47:54
|
||||
* @Description: MainWindow implementation.
|
||||
*/
|
||||
#include "main_window.h"
|
||||
@@ -139,7 +139,7 @@ void MainWindow::_stopLoadingAndQuit(const std::function<void()>& onStopped) {
|
||||
if (m_state != Loading) {
|
||||
return;
|
||||
}
|
||||
info("Stopping loading.");
|
||||
debug("Stopping loading...");
|
||||
connect(
|
||||
m_carousel,
|
||||
&ImagesCarousel::stopped,
|
||||
@@ -158,21 +158,24 @@ void MainWindow::_onCancelPressed() {
|
||||
case Loading:
|
||||
// case loading screen is disabled, quit the app
|
||||
if (m_config.getStyleConfig().noLoadingScreen) {
|
||||
debug("Stopping loading...");
|
||||
_stopLoadingAndQuit([this]() {
|
||||
info("Quitting app.");
|
||||
debug("Quitting app...");
|
||||
close();
|
||||
});
|
||||
}
|
||||
// otherwise, stop loading and display the loaded images
|
||||
else {
|
||||
debug("Stopping loading and displaying loaded images...");
|
||||
_stopLoadingAndQuit([this]() {
|
||||
debug("Loading stopped.");
|
||||
_onLoadingCompleted(m_carousel->getLoadedImagesCount());
|
||||
m_carousel->focusCurrImage();
|
||||
});
|
||||
}
|
||||
break;
|
||||
case Ready:
|
||||
info("Quitting app.");
|
||||
debug("Quitting app...");
|
||||
close();
|
||||
break;
|
||||
default:
|
||||
@@ -185,7 +188,7 @@ void MainWindow::_onConfirmPressed() {
|
||||
case Loading:
|
||||
// case loading screen is disabled, confirm the selection
|
||||
if (m_config.getStyleConfig().noLoadingScreen) {
|
||||
info("Stopping loading and confirming selection.");
|
||||
debug("Stopping loading and confirming selection...");
|
||||
connect(
|
||||
m_carousel,
|
||||
&ImagesCarousel::stopped,
|
||||
@@ -196,7 +199,7 @@ void MainWindow::_onConfirmPressed() {
|
||||
}
|
||||
break;
|
||||
case Ready:
|
||||
info("Confirming selection.");
|
||||
debug("Confirming selection...");
|
||||
onConfirm();
|
||||
break;
|
||||
default:
|
||||
@@ -222,7 +225,7 @@ void MainWindow::closeEvent(QCloseEvent* event) {
|
||||
if (m_state == Loading) {
|
||||
event->ignore();
|
||||
_stopLoadingAndQuit([this]() {
|
||||
info("Quitting app.");
|
||||
debug("Quitting app.");
|
||||
close();
|
||||
});
|
||||
} else {
|
||||
@@ -249,7 +252,7 @@ void MainWindow::onConfirm() {
|
||||
const auto arguments = QProcess::splitCommand(cmd);
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user