From 8a8d967a5874304ea7985773ec7fd29154951f47 Mon Sep 17 00:00:00 2001 From: Uyanide Date: Thu, 7 Aug 2025 21:37:58 +0200 Subject: [PATCH] fix: maybe to much time spent on this logger :/ --- CMakeLists.txt | 6 ++++++ config.example.json | 5 +++-- src/config.cpp | 20 ++++++++++---------- src/loading_indicator.cpp | 4 ++-- src/loading_indicator.h | 4 ++-- src/logger.cpp | 35 ++++++++++++++++++++--------------- src/logger.h | 13 ++++++++++--- src/main.cpp | 2 +- 8 files changed, 54 insertions(+), 35 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 1ca1207..01d4d51 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -53,6 +53,12 @@ target_link_libraries(wallpaper_chooser PRIVATE Qt${QT_VERSION_MAJOR}::Widgets) target_include_directories(wallpaper_chooser PRIVATE src) +if(NOT ${CMAKE_BUILD_TYPE} STREQUAL "Debug") + target_compile_definitions(wallpaper_chooser PRIVATE + GENERAL_LOGGER_DISABLED + ) +endif() + # Qt for iOS sets MACOSX_BUNDLE_GUI_IDENTIFIER automatically since Qt 6.1. # If you are developing for iOS or macOS you should consider setting an # explicit, fixed bundle identifier manually though. diff --git a/config.example.json b/config.example.json index 4ee436e..90f6913 100644 --- a/config.example.json +++ b/config.example.json @@ -4,7 +4,8 @@ "~/Pictures/116327446_p0.jpg" ], "dirs": [ - "~/.config/backgrounds" + "~/.config/backgrounds", + "/run/media/kolkas/移硬1T/逆流茶会/" ], "excludes": [ "~/.config/backgrounds/nao-stars-crop-adjust-flop.jpg", @@ -26,4 +27,4 @@ "type": "name", "reverse": false } -} +} \ No newline at end of file diff --git a/src/config.cpp b/src/config.cpp index 8e08286..58d5e5e 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -1,7 +1,7 @@ /* * @Author: Uyanide pywang0608@foxmail.com * @Date: 2025-08-05 01:34:52 - * @LastEditTime: 2025-08-07 00:09:51 + * @LastEditTime: 2025-08-07 21:27:26 * @Description: Configuration manager. */ #include "config.h" @@ -66,37 +66,37 @@ void Config::_loadConfig(const QString &configPath) { {"actions.confirm", "confirm", [this](const QJsonValue &val) { if (val.isString()) { m_configItems.actionsConfirm = ::expandPath(val.toString()); - info(QString("Action confirm: %1").arg(m_configItems.actionsConfirm)); + info(QString("Action confirm: %1").arg(m_configItems.actionsConfirm), GeneralLogger::STEP); } }}, {"style.aspect_ratio", "aspect_ratio", [this](const QJsonValue &val) { if (val.isDouble() && val.toDouble() > 0) { m_configItems.styleAspectRatio = val.toDouble(); - info(QString("Aspect ratio: %1").arg(m_configItems.styleAspectRatio)); + info(QString("Aspect ratio: %1").arg(m_configItems.styleAspectRatio), GeneralLogger::STEP); } }}, {"style.image_width", "image_width", [this](const QJsonValue &val) { if (val.isDouble() && val.toDouble() > 0) { m_configItems.styleImageWidth = val.toInt(); - info(QString("Image width: %1").arg(m_configItems.styleImageWidth)); + info(QString("Image width: %1").arg(m_configItems.styleImageWidth), GeneralLogger::STEP); } }}, {"style.image_focus_width", "image_focus_width", [this](const QJsonValue &val) { if (val.isDouble() && val.toDouble() > 0) { m_configItems.styleImageFocusWidth = val.toInt(); - info(QString("Image focus width: %1").arg(m_configItems.styleImageFocusWidth)); + info(QString("Image focus width: %1").arg(m_configItems.styleImageFocusWidth), GeneralLogger::STEP); } }}, {"style.window_width", "window_width", [this](const QJsonValue &val) { if (val.isDouble() && val.toDouble() > 0) { m_configItems.styleWindowWidth = val.toInt(); - info(QString("Window width: %1").arg(m_configItems.styleWindowWidth)); + info(QString("Window width: %1").arg(m_configItems.styleWindowWidth), GeneralLogger::STEP); } }}, {"style.window_height", "window_height", [this](const QJsonValue &val) { if (val.isDouble() && val.toDouble() > 0) { m_configItems.styleWindowHeight = val.toInt(); - info(QString("Window height: %1").arg(m_configItems.styleWindowHeight)); + info(QString("Window height: %1").arg(m_configItems.styleWindowHeight), GeneralLogger::STEP); } }}, {"sort.type", "type", [this](const QJsonValue &val) { @@ -111,15 +111,15 @@ void Config::_loadConfig(const QString &configPath) { } else if (type == "size") { m_configItems.sortType = SortType::Size; } else { - warn(QString("Unknown sort type: %1").arg(type)); + warn(QString("Unknown sort type: %1").arg(type), GeneralLogger::STEP); } } - info(QString("Sort type: %1").arg(static_cast(m_configItems.sortType))); + info(QString("Sort type: %1").arg(static_cast(m_configItems.sortType)), GeneralLogger::STEP); }}, {"sort.reverse", "reverse", [this](const QJsonValue &val) { if (val.isBool()) { m_configItems.sortReverse = val.toBool(); - info(QString("Sort reverse: %1").arg(m_configItems.sortReverse)); + info(QString("Sort reverse: %1").arg(m_configItems.sortReverse), GeneralLogger::STEP); } }}, }; diff --git a/src/loading_indicator.cpp b/src/loading_indicator.cpp index 053ef9e..556964f 100644 --- a/src/loading_indicator.cpp +++ b/src/loading_indicator.cpp @@ -1,8 +1,8 @@ /* * @Author: Uyanide pywang0608@foxmail.com * @Date: 2025-08-07 00:32:25 - * @LastEditTime: 2025-08-07 01:09:49 - * @Description: A loading indicator widget with a progress bar. + * @LastEditTime: 2025-08-07 21:14:09 + * @Description: LoadingIndicator implementation. */ #include "loading_indicator.h" diff --git a/src/loading_indicator.h b/src/loading_indicator.h index 3f09a7c..0177f22 100644 --- a/src/loading_indicator.h +++ b/src/loading_indicator.h @@ -1,8 +1,8 @@ /* * @Author: Uyanide pywang0608@foxmail.com * @Date: 2025-08-07 00:32:25 - * @LastEditTime: 2025-08-07 01:00:29 - * @Description: LoadingIndicator implementation. + * @LastEditTime: 2025-08-07 21:13:59 + * @Description: A loading indicator widget with a progress bar. */ #ifndef LOADING_INDICATOR_H #define LOADING_INDICATOR_H diff --git a/src/logger.cpp b/src/logger.cpp index 044d076..ba14d80 100644 --- a/src/logger.cpp +++ b/src/logger.cpp @@ -1,11 +1,13 @@ /* * @Author: Uyanide pywang0608@foxmail.com * @Date: 2025-08-07 01:12:37 - * @LastEditTime: 2025-08-07 21:11:22 + * @LastEditTime: 2025-08-07 21:21:45 * @Description: Implementation of logger. */ #include "logger.h" +#ifndef GENERAL_LOGGER_DISABLED + #include #include @@ -15,27 +17,25 @@ #include #include -#ifdef GENERAL_LOGGER_DISABLED -#define ENSURE_ENABLED return; -#else -#define ENSURE_ENABLED -#endif - -Logger* Logger::instance(FILE* stream) { +Logger* Logger::instance(FILE* stream, + GeneralLogger::LogIndent indent, + QObject* parent) { static Logger* logger{}; if (!logger) { if (!stream) { stream = stderr; // Default to stderr if no stream provided } - logger = new Logger(stream); + logger = new Logger(stream, indent, parent); // Ensure logger runs in the main thread logger->moveToThread(QCoreApplication::instance()->thread()); } return logger; } -Logger::Logger(FILE* stream, QObject* parent) - : QObject(parent), m_stream(stream), m_logStream(stream) { +Logger::Logger(FILE* stream, + GeneralLogger::LogIndent indent, + QObject* parent) + : QObject(parent), m_stream(stream), m_logStream(stream), m_indent(indent) { connect(this, &Logger::logSig, this, @@ -49,8 +49,8 @@ void Logger::_log( const QString& levelColorString, const QString& textColorString, const GeneralLogger::LogIndent indent) { - ENSURE_ENABLED + if (indent > m_indent) return; m_logStream << levelColorString << levelString << ' '; for (qint32 i = 0; i < indent; i++) m_logStream << " "; m_logStream << textColorString << msg << (textColorString.isEmpty() ? "\n" : "\033[0m\n"); @@ -70,10 +70,12 @@ bool Logger::isColored() { return true; } +#endif // GENERAL_LOGGER_DISABLED + void GeneralLogger::info( const QString& msg, const GeneralLogger::LogIndent indent) { - ENSURE_ENABLED +#ifndef GENERAL_LOGGER_DISABLED constexpr const char* colorInfoMsg[]{"\033[32m", "\033[0m", "\033[0m"}; const auto color = Logger::isColored(); emit Logger::instance() -> logSig(msg, @@ -81,28 +83,31 @@ void GeneralLogger::info( color ? "\033[92m" : "", color ? colorInfoMsg[indent] : "", indent); +#endif // GENERAL_LOGGER_DISABLED } void GeneralLogger::warn( const QString& msg, const GeneralLogger::LogIndent indent) { - ENSURE_ENABLED +#ifndef GENERAL_LOGGER_DISABLED const auto color = Logger::isColored(); emit Logger::instance() -> logSig(msg, "[WARN]", color ? "\033[93m" : "", color ? "\033[33m" : "", indent); +#endif // GENERAL_LOGGER_DISABLED } void GeneralLogger::error( const QString& msg, const GeneralLogger::LogIndent indent) { - ENSURE_ENABLED +#ifndef GENERAL_LOGGER_DISABLED const auto color = Logger::isColored(); emit Logger::instance() -> logSig(msg, "[ERROR]", color ? "\033[91m" : "", color ? "\033[31m" : "", indent); +#endif // GENERAL_LOGGER_DISABLED } \ No newline at end of file diff --git a/src/logger.h b/src/logger.h index fdd39d8..9f26fb9 100644 --- a/src/logger.h +++ b/src/logger.h @@ -1,7 +1,7 @@ /* * @Author: Uyanide pywang0608@foxmail.com * @Date: 2025-08-05 10:43:31 - * @LastEditTime: 2025-08-07 01:55:42 + * @LastEditTime: 2025-08-07 21:26:09 * @Description: A simple thread-safe logger for general use. */ #ifndef GENERAL_LOGGER_H @@ -29,16 +29,21 @@ void error(const QString& msg, const LogIndent indent = GENERAL); } // namespace GeneralLogger +#ifndef GENERAL_LOGGER_DISABLED class Logger : public QObject { Q_OBJECT public: - static Logger* instance(FILE* stream = nullptr); + static Logger* instance(FILE* stream = nullptr, + GeneralLogger::LogIndent indent = GeneralLogger::DETAIL, + QObject* parent = nullptr); static bool isColored(); private: - explicit Logger(FILE* stream, QObject* parent = nullptr); + explicit Logger(FILE* stream, + GeneralLogger::LogIndent indent, + QObject* parent); private slots: void _log(const QString& msg, @@ -50,6 +55,7 @@ class Logger : public QObject { private: FILE* m_stream; QTextStream m_logStream; + GeneralLogger::LogIndent m_indent; signals: void logSig(const QString& msg, @@ -59,4 +65,5 @@ class Logger : public QObject { const GeneralLogger::LogIndent indent); }; +#endif // GENERAL_LOGGER_DISABLED #endif // GENERAL_LOGGER_H diff --git a/src/main.cpp b/src/main.cpp index 1366e16..d16895c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,7 +1,7 @@ /* * @Author: Uyanide pywang0608@foxmail.com * @Date: 2025-08-05 00:37:58 - * @LastEditTime: 2025-08-07 01:49:07 + * @LastEditTime: 2025-08-07 21:25:43 * @Description: Entry point. */ #include