From 65060337dee5dff8e1a3f738f92efa3d8b10ec54 Mon Sep 17 00:00:00 2001 From: iaom Date: Fri, 14 Apr 2023 18:01:19 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=97=A5=E5=BF=97=E6=89=93?= =?UTF-8?q?=E5=8D=B0=E6=9C=BA=E5=88=B6=EF=BC=9B=E5=88=A0=E9=99=A4=E9=83=A8?= =?UTF-8?q?=E5=88=86=E6=97=A0=E7=94=A8=E4=BB=A3=E7=A0=81.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend/main.cpp | 82 ++---------- .../header-files/UkuiSearchPluginIface | 1 - libsearch/libsearch.pro | 6 +- libsearch/log-utils.cpp | 118 ++++++++++++++++++ libsearch/log-utils.h | 21 ++++ .../plugininterface/plugin-interface.pri | 1 - ukui-search-app-data-service/main.cpp | 52 +------- ukui-search-service-dir-manager/main.cpp | 53 +------- .../ukui-search-dir-manager-dbus.cpp | 2 +- .../ukui-search-service-dir-manager.pro | 6 +- ukui-search-service/main.cpp | 55 +------- ukui-search-service/ukui-search-service.pro | 3 +- 12 files changed, 174 insertions(+), 226 deletions(-) create mode 100644 libsearch/log-utils.cpp create mode 100644 libsearch/log-utils.h diff --git a/frontend/main.cpp b/frontend/main.cpp index 24cf0d7..ea41eb1 100644 --- a/frontend/main.cpp +++ b/frontend/main.cpp @@ -21,76 +21,16 @@ * */ - -#include -#include -#include #include -#if (QT_VERSION >= QT_VERSION_CHECK(5, 12, 0)) -#include -#endif -#include -#include -#include +#include #include "ukui-search-gui.h" +#include "log-utils.h" using namespace UkuiSearch; -void messageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg) -{ - QByteArray localMsg = msg.toLocal8Bit(); - QByteArray currentTime = QTime::currentTime().toString().toLocal8Bit(); - - bool showDebug = true; -// QString logFilePath = QStandardPaths::writableLocation(QStandardPaths::TempLocation) + "/ukui-search.log"; -// QString logFilePath = QStandardPaths::writableLocation(QStandardPaths::HomeLocation) + "/.config/org.ukui/ukui-search/ukui-search.log"; - QString logFilePath = QStandardPaths::writableLocation(QStandardPaths::HomeLocation) + "/.config/org.ukui/ukui-search.log"; - if (!QFile::exists(logFilePath)) { - showDebug = false; - } - FILE *log_file = nullptr; - - if (showDebug) { - log_file = fopen(logFilePath.toLocal8Bit().constData(), "a+"); - } - - const char *file = context.file ? context.file : ""; - const char *function = context.function ? context.function : ""; - switch (type) { - case QtDebugMsg: - if (!log_file) { - break; - } - fprintf(log_file, "Debug: %s: %s (%s:%u, %s)\n", currentTime.constData(), localMsg.constData(), file, context.line, function); - break; - case QtInfoMsg: - fprintf(log_file? log_file: stdout, "Info: %s: %s (%s:%u, %s)\n", currentTime.constData(), localMsg.constData(), file, context.line, function); - break; - case QtWarningMsg: - fprintf(log_file? log_file: stderr, "Warning: %s: %s (%s:%u, %s)\n", currentTime.constData(), localMsg.constData(), file, context.line, function); - break; - case QtCriticalMsg: - fprintf(log_file? log_file: stderr, "Critical: %s: %s (%s:%u, %s)\n", currentTime.constData(), localMsg.constData(), file, context.line, function); - break; - case QtFatalMsg: - fprintf(log_file? log_file: stderr, "Fatal: %s: %s (%s:%u, %s)\n", currentTime.constData(), localMsg.constData(), file, context.line, function); - break; - } - - if (log_file) - fclose(log_file); -} - int main(int argc, char *argv[]) { -//v101日志模块 -//#if (QT_VERSION >= QT_VERSION_CHECK(5, 12, 0)) -// //Init log module -// initUkuiLog4qt("ukui-search"); -//#endif - // Determine whether the home directory has been created, and if not, keep waiting. char *p_home = NULL; - unsigned int i = 0; while(p_home == NULL) { ::sleep(1); @@ -111,14 +51,8 @@ int main(int argc, char *argv[]) { } // Output log to file - qInstallMessageHandler(messageOutput); -//若使用v101日志模块,可以解放如下判断条件 -//#if (QT_VERSION < QT_VERSION_CHECK(5, 12, 0)) -// // Output log to file -// qInstallMessageHandler(messageOutput); -//#endif - - // Register meta type + LogUtils::initLogFile("ukui-search"); + qInstallMessageHandler(LogUtils::messageOutput); qDebug() << "ukui-search main start"; // If qt version bigger than 5.12, enable high dpi scaling and use high dpi pixmaps? #if (QT_VERSION >= QT_VERSION_CHECK(5, 12, 0)) @@ -128,7 +62,13 @@ int main(int argc, char *argv[]) { #if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)) QApplication::setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy::PassThrough); #endif - UkuiSearchGui app(argc, argv, QString("ukui-search-gui-%1").arg(QX11Info::appScreen())); + QString display; + if(KWindowSystem::isPlatformWayland()) { + display = getenv("WAYLAND_DISPLAY"); + } else if (KWindowSystem::isPlatformX11()) { + display = getenv("DISPLAY"); + } + UkuiSearchGui app(argc, argv, QString("ukui-search-gui-%1").arg(display)); if (app.isRunning()) return 0; diff --git a/libsearch/development-files/header-files/UkuiSearchPluginIface b/libsearch/development-files/header-files/UkuiSearchPluginIface index 2c75b88..63cd47e 100644 --- a/libsearch/development-files/header-files/UkuiSearchPluginIface +++ b/libsearch/development-files/header-files/UkuiSearchPluginIface @@ -1,6 +1,5 @@ #include "action-label.h" #include "action-transmiter.h" -#include "common-defines.h" #include "plugin-iface.h" #include "data-queue.h" #include "search-plugin-iface.h" diff --git a/libsearch/libsearch.pro b/libsearch/libsearch.pro index 179a0d5..51a3726 100644 --- a/libsearch/libsearch.pro +++ b/libsearch/libsearch.pro @@ -50,7 +50,8 @@ SOURCES += \ file-utils.cpp \ global-settings.cpp \ gobject-template.cpp \ - libsearch.cpp + libsearch.cpp \ + log-utils.cpp HEADERS += \ common.h \ @@ -58,7 +59,8 @@ HEADERS += \ global-settings.h \ gobject-template.h \ libsearch_global.h \ - libsearch.h + libsearch.h \ + log-utils.h RESOURCES += \ resource1.qrc \ diff --git a/libsearch/log-utils.cpp b/libsearch/log-utils.cpp new file mode 100644 index 0000000..c5854d1 --- /dev/null +++ b/libsearch/log-utils.cpp @@ -0,0 +1,118 @@ +#include "log-utils.h" +#include +#include +#include +#include +#include + +#define LOG_FILE_COUNT 2 +#define MAX_LOG_FILE_SIZE 4194304 +#define MAX_LOG_CHECK_INTERVAL 43200000 + +quint64 LogUtils::m_startUpTime = 0; +int LogUtils::m_logFileId = -1; +QString LogUtils::m_logFileName; +QString LogUtils::m_currentLogFile; +static QString logFilePath = QStandardPaths::writableLocation(QStandardPaths::HomeLocation) + "/.log/ukui-search/"; + +void LogUtils::messageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg) +{ + checkLogFile(); + + QByteArray localMsg = msg.toLocal8Bit(); + QByteArray currentTime = QTime::currentTime().toString().toLocal8Bit(); + const char *file = context.file ? context.file : ""; + const char *function = context.function ? context.function : ""; + + FILE *log_file = fopen(m_currentLogFile.toLocal8Bit().constData(), "a+"); + + switch (type) { + case QtDebugMsg: + if (!log_file) { + break; + } + fprintf(log_file, "Debug: %s: %s (%s:%u, %s)\n", currentTime.constData(), localMsg.constData(), file, context.line, function); + break; + case QtInfoMsg: + fprintf(log_file? log_file: stdout, "Info: %s: %s (%s:%u, %s)\n", currentTime.constData(), localMsg.constData(), file, context.line, function); + break; + case QtWarningMsg: + fprintf(log_file? log_file: stderr, "Warning: %s: %s (%s:%u, %s)\n", currentTime.constData(), localMsg.constData(), file, context.line, function); + break; + case QtCriticalMsg: + fprintf(log_file? log_file: stderr, "Critical: %s: %s (%s:%u, %s)\n", currentTime.constData(), localMsg.constData(), file, context.line, function); + break; + case QtFatalMsg: + fprintf(log_file? log_file: stderr, "Fatal: %s: %s (%s:%u, %s)\n", currentTime.constData(), localMsg.constData(), file, context.line, function); + break; + } + + if (log_file) { + fclose(log_file); + } +} + +void LogUtils::initLogFile(const QString &fileName) +{ + QDir dir; + if (!dir.exists(logFilePath)) { + if (!dir.mkpath(logFilePath)) { + qWarning() << "Unable to create" << logFilePath; + return; + } + } + m_logFileName = logFilePath + fileName + "-%1.log"; + + for (int i = 0; i < LOG_FILE_COUNT; ++i) { + m_currentLogFile = m_logFileName.arg(i); + if (QFile::exists(m_currentLogFile)) { + if (checkFileSize(m_currentLogFile)) { + m_logFileId = i; + break; + } + } else { + QFile file(m_currentLogFile); + file.open(QIODevice::WriteOnly); + file.close(); + } + } + + + if (m_logFileId < 0) { + m_logFileId = 0; + m_currentLogFile = m_logFileName.arg(m_logFileId); + clearFile(m_currentLogFile); + } + + qInfo() << "Current log file:" << m_currentLogFile; +} + +void LogUtils::checkLogFile() +{ + quint64 logTime = QDateTime::currentDateTime().toMSecsSinceEpoch(); + quint64 spacing = std::max(logTime, m_startUpTime) - std::min(logTime, m_startUpTime); + + if (spacing <= MAX_LOG_CHECK_INTERVAL || checkFileSize(m_currentLogFile)) { + return; + } + + m_logFileId = ((m_logFileId + 1) % LOG_FILE_COUNT); + m_currentLogFile = m_logFileName.arg(m_logFileId); + if (!checkFileSize(m_currentLogFile)) { + clearFile(m_currentLogFile); + } +} + +bool LogUtils::checkFileSize(const QString &fileName) +{ + return QFile(fileName).size() < MAX_LOG_FILE_SIZE; +} + +void LogUtils::clearFile(const QString &fileName) +{ + QFile file(fileName); + file.open(QIODevice::WriteOnly); + file.write(""); + file.flush(); + file.close(); +} diff --git a/libsearch/log-utils.h b/libsearch/log-utils.h new file mode 100644 index 0000000..a1c465f --- /dev/null +++ b/libsearch/log-utils.h @@ -0,0 +1,21 @@ +#ifndef LOGUTILS_H +#define LOGUTILS_H +#include + +class LogUtils +{ +public: + static void initLogFile(const QString &fileName); + static void messageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg); + +private: + static void checkLogFile(); + static bool checkFileSize(const QString &fileName); + static void clearFile(const QString &fileName); + static quint64 m_startUpTime; + static int m_logFileId; + static QString m_logFileName; + static QString m_currentLogFile; +}; + +#endif // LOGUTILS_H diff --git a/libsearch/plugininterface/plugin-interface.pri b/libsearch/plugininterface/plugin-interface.pri index 0c2a73b..67fc25b 100644 --- a/libsearch/plugininterface/plugin-interface.pri +++ b/libsearch/plugininterface/plugin-interface.pri @@ -3,7 +3,6 @@ INCLUDEPATH += $$PWD HEADERS += \ $$PWD/action-transmiter.h \ $$PWD/action-label.h \ - $$PWD/common-defines.h \ $$PWD/plugin-iface.h \ $$PWD/search-plugin-iface.h \ $$PWD/search-task-plugin-iface.h \ diff --git a/ukui-search-app-data-service/main.cpp b/ukui-search-app-data-service/main.cpp index ca5649c..acaef86 100644 --- a/ukui-search-app-data-service/main.cpp +++ b/ukui-search-app-data-service/main.cpp @@ -16,60 +16,16 @@ * along with this program. If not, see . * */ -#include -#include -#include -#include + #include "ukui-search-app-data-service.h" +#include "log-utils.h" using namespace UkuiSearch; -void messageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg) -{ - QByteArray localMsg = msg.toLocal8Bit(); - QByteArray currentTime = QTime::currentTime().toString().toLocal8Bit(); - - bool showDebug = true; - QString logFilePath = QStandardPaths::writableLocation(QStandardPaths::HomeLocation) + "/.config/org.ukui/ukui-search-app-data-service.log"; - if (!QFile::exists(logFilePath)) { - showDebug = false; - } - FILE *log_file = nullptr; - - if (showDebug) { - log_file = fopen(logFilePath.toLocal8Bit().constData(), "a+"); - } - - const char *file = context.file ? context.file : ""; - const char *function = context.function ? context.function : ""; - switch (type) { - case QtDebugMsg: - if (!log_file) { - break; - } - fprintf(log_file, "Debug: %s: %s (%s:%u, %s)\n", currentTime.constData(), localMsg.constData(), file, context.line, function); - break; - case QtInfoMsg: - fprintf(log_file? log_file: stdout, "Info: %s: %s (%s:%u, %s)\n", currentTime.constData(), localMsg.constData(), file, context.line, function); - break; - case QtWarningMsg: - fprintf(log_file? log_file: stderr, "Warning: %s: %s (%s:%u, %s)\n", currentTime.constData(), localMsg.constData(), file, context.line, function); - break; - case QtCriticalMsg: - fprintf(log_file? log_file: stderr, "Critical: %s: %s (%s:%u, %s)\n", currentTime.constData(), localMsg.constData(), file, context.line, function); - break; - case QtFatalMsg: - fprintf(log_file? log_file: stderr, "Fatal: %s: %s (%s:%u, %s)\n", currentTime.constData(), localMsg.constData(), file, context.line, function); - break; - } - - if (log_file) - fclose(log_file); -} - int main(int argc, char *argv[]) { // Output log to file - qInstallMessageHandler(messageOutput); + LogUtils::initLogFile("ukui-search-app-data-service"); + qInstallMessageHandler(LogUtils::messageOutput); #if (QT_VERSION >= QT_VERSION_CHECK(5, 12, 0)) QApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QApplication::setAttribute(Qt::AA_UseHighDpiPixmaps); diff --git a/ukui-search-service-dir-manager/main.cpp b/ukui-search-service-dir-manager/main.cpp index a51f2bc..1f8eed6 100644 --- a/ukui-search-service-dir-manager/main.cpp +++ b/ukui-search-service-dir-manager/main.cpp @@ -17,57 +17,13 @@ * * Authors: iaom */ -#include -#include -#include -#include -#include + #include #include +#include #include +#include "log-utils.h" #include "ukui-search-dir-manager-dbus.h" -void messageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg) -{ - QByteArray localMsg = msg.toLocal8Bit(); - QByteArray currentTime = QTime::currentTime().toString().toLocal8Bit(); - - bool showDebug = true; - QString logFilePath = QStandardPaths::writableLocation(QStandardPaths::HomeLocation) + "/.config/org.ukui/ukui-search-service-dir-manager.log"; - if (!QFile::exists(logFilePath)) { - showDebug = false; - } - FILE *log_file = nullptr; - - if (showDebug) { - log_file = fopen(logFilePath.toLocal8Bit().constData(), "a+"); - } - - const char *file = context.file ? context.file : ""; - const char *function = context.function ? context.function : ""; - switch (type) { - case QtDebugMsg: - if (!log_file) { - break; - } - fprintf(log_file, "Debug: %s: %s (%s:%u, %s)\n", currentTime.constData(), localMsg.constData(), file, context.line, function); - break; - case QtInfoMsg: - fprintf(log_file? log_file: stdout, "Info: %s: %s (%s:%u, %s)\n", currentTime.constData(), localMsg.constData(), file, context.line, function); - break; - case QtWarningMsg: - fprintf(log_file? log_file: stderr, "Warning: %s: %s (%s:%u, %s)\n", currentTime.constData(), localMsg.constData(), file, context.line, function); - break; - case QtCriticalMsg: - fprintf(log_file? log_file: stderr, "Critical: %s: %s (%s:%u, %s)\n", currentTime.constData(), localMsg.constData(), file, context.line, function); - break; - case QtFatalMsg: - fprintf(log_file? log_file: stderr, "Fatal: %s: %s (%s:%u, %s)\n", currentTime.constData(), localMsg.constData(), file, context.line, function); - break; - } - - if (log_file) - fclose(log_file); -} int main(int argc, char *argv[]) { @@ -92,7 +48,8 @@ int main(int argc, char *argv[]) } // Output log to file - qInstallMessageHandler(messageOutput); + LogUtils::initLogFile("ukui-search-service-dir-manager"); + qInstallMessageHandler(LogUtils::messageOutput); #if (QT_VERSION >= QT_VERSION_CHECK(5, 12, 0)) QApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QApplication::setAttribute(Qt::AA_UseHighDpiPixmaps); diff --git a/ukui-search-service-dir-manager/ukui-search-dir-manager-dbus.cpp b/ukui-search-service-dir-manager/ukui-search-dir-manager-dbus.cpp index 0aa38ba..45cc8aa 100644 --- a/ukui-search-service-dir-manager/ukui-search-dir-manager-dbus.cpp +++ b/ukui-search-service-dir-manager/ukui-search-dir-manager-dbus.cpp @@ -19,7 +19,7 @@ */ #include "ukui-search-dir-manager-dbus.h" #include "dir-watcher.h" - +#include #include #include #include diff --git a/ukui-search-service-dir-manager/ukui-search-service-dir-manager.pro b/ukui-search-service-dir-manager/ukui-search-service-dir-manager.pro index 7e1f308..506c278 100644 --- a/ukui-search-service-dir-manager/ukui-search-service-dir-manager.pro +++ b/ukui-search-service-dir-manager/ukui-search-service-dir-manager.pro @@ -15,12 +15,12 @@ PKGCONFIG += gio-2.0 gio-unix-2.0 # depend on your compiler). Please consult the documentation of the # deprecated API in order to know how to port your code away from it. DEFINES += QT_DEPRECATED_WARNINGS +QMAKE_CXXFLAGS += -Werror=return-type -Werror=return-local-addr -Werror=uninitialized # You can also make your code fail to compile if it uses deprecated APIs. # In order to do so, uncomment the following line. # You can also select to disable deprecated APIs only up to a certain version of Qt. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 - include(dirwatcher/dir-watcher-dbus.pri) include(../3rd-parties/qtsingleapplication/qtsingleapplication.pri) @@ -43,3 +43,7 @@ SOURCES += \ HEADERS += \ ukui-search-dir-manager-dbus.h + +LIBS += -L$$OUT_PWD/../libsearch -lukui-search +INCLUDEPATH += $$PWD/../libsearch +DEPENDPATH += $$PWD/../libsearch diff --git a/ukui-search-service/main.cpp b/ukui-search-service/main.cpp index bddb052..55ef2f9 100644 --- a/ukui-search-service/main.cpp +++ b/ukui-search-service/main.cpp @@ -17,60 +17,12 @@ * Authors: iaom * */ -#include -#include -#include -#include -#include + #include #include -#include #include "ukui-search-service.h" +#include "log-utils.h" using namespace UkuiSearch; -void messageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg) -{ - QByteArray localMsg = msg.toLocal8Bit(); - QByteArray currentTime = QTime::currentTime().toString().toLocal8Bit(); - - bool showDebug = true; -// QString logFilePath = QStandardPaths::writableLocation(QStandardPaths::TempLocation) + "/ukui-search.log"; -// QString logFilePath = QStandardPaths::writableLocation(QStandardPaths::HomeLocation) + "/.config/org.ukui/ukui-search/ukui-search.log"; - QString logFilePath = QStandardPaths::writableLocation(QStandardPaths::HomeLocation) + "/.config/org.ukui/ukui-search-service.log"; - if (!QFile::exists(logFilePath)) { - showDebug = false; - } - FILE *log_file = nullptr; - - if (showDebug) { - log_file = fopen(logFilePath.toLocal8Bit().constData(), "a+"); - } - - const char *file = context.file ? context.file : ""; - const char *function = context.function ? context.function : ""; - switch (type) { - case QtDebugMsg: - if (!log_file) { - break; - } - fprintf(log_file, "Debug: %s: %s (%s:%u, %s)\n", currentTime.constData(), localMsg.constData(), file, context.line, function); - break; - case QtInfoMsg: - fprintf(log_file? log_file: stdout, "Info: %s: %s (%s:%u, %s)\n", currentTime.constData(), localMsg.constData(), file, context.line, function); - break; - case QtWarningMsg: - fprintf(log_file? log_file: stderr, "Warning: %s: %s (%s:%u, %s)\n", currentTime.constData(), localMsg.constData(), file, context.line, function); - break; - case QtCriticalMsg: - fprintf(log_file? log_file: stderr, "Critical: %s: %s (%s:%u, %s)\n", currentTime.constData(), localMsg.constData(), file, context.line, function); - break; - case QtFatalMsg: - fprintf(log_file? log_file: stderr, "Fatal: %s: %s (%s:%u, %s)\n", currentTime.constData(), localMsg.constData(), file, context.line, function); - break; - } - - if (log_file) - fclose(log_file); -} int main(int argc, char *argv[]) { char *p_home = NULL; @@ -94,7 +46,8 @@ int main(int argc, char *argv[]) } // Output log to file - qInstallMessageHandler(messageOutput); + LogUtils::initLogFile("ukui-search-service"); + qInstallMessageHandler(LogUtils::messageOutput); #if (QT_VERSION >= QT_VERSION_CHECK(5, 12, 0)) QApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QApplication::setAttribute(Qt::AA_UseHighDpiPixmaps); diff --git a/ukui-search-service/ukui-search-service.pro b/ukui-search-service/ukui-search-service.pro index a6b327a..af4d729 100644 --- a/ukui-search-service/ukui-search-service.pro +++ b/ukui-search-service/ukui-search-service.pro @@ -24,8 +24,7 @@ include(../libsearch/libukui-search-headers.pri) include(../3rd-parties/qtsingleapplication/qtsingleapplication.pri) include(./qml/qml.pri) -LIBS += -L$$OUT_PWD/../libchinese-segmentation -lchinese-segmentation \ - -L$$OUT_PWD/../libsearch -lukui-search +LIBS += -L$$OUT_PWD/../libsearch -lukui-search SOURCES += \ main.cpp \