优化日志打印机制;删除部分无用代码.

This commit is contained in:
iaom 2023-04-14 18:01:19 +08:00
parent d7c962177e
commit 92f22930eb
12 changed files with 174 additions and 226 deletions

View File

@ -21,76 +21,16 @@
*
*/
#include <QDesktopWidget>
#include <QFile>
#include <QDir>
#include <syslog.h>
#if (QT_VERSION >= QT_VERSION_CHECK(5, 12, 0))
#include <ukui-log4qt.h>
#endif
#include <QObject>
#include <QApplication>
#include <QX11Info>
#include <KWindowSystem>
#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;

View File

@ -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"

View File

@ -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 \

118
libsearch/log-utils.cpp Normal file
View File

@ -0,0 +1,118 @@
#include "log-utils.h"
#include <QFile>
#include <QDebug>
#include <QDir>
#include <QDateTime>
#include <QStandardPaths>
#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();
}

21
libsearch/log-utils.h Normal file
View File

@ -0,0 +1,21 @@
#ifndef LOGUTILS_H
#define LOGUTILS_H
#include <QtMessageHandler>
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

View File

@ -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 \

View File

@ -16,60 +16,16 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
#include <QCoreApplication>
#include <QTime>
#include <QStandardPaths>
#include <QFile>
#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);

View File

@ -17,57 +17,13 @@
*
* Authors: iaom <zhangpengfei@kylinos.cn>
*/
#include <QCoreApplication>
#include <QTime>
#include <QFile>
#include <QStandardPaths>
#include <QDir>
#include <syslog.h>
#include <unistd.h>
#include <QDir>
#include <QDebug>
#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);

View File

@ -19,7 +19,7 @@
*/
#include "ukui-search-dir-manager-dbus.h"
#include "dir-watcher.h"
#include <QCommandLineParser>
#include <QDBusConnection>
#include <QDBusError>
#include <QDebug>

View File

@ -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

View File

@ -17,60 +17,12 @@
* Authors: iaom <zhangpengfei@kylinos.cn>
*
*/
#include <QApplication>
#include <QFile>
#include <QTime>
#include <QStandardPaths>
#include <QDir>
#include <syslog.h>
#include <unistd.h>
#include <QDebug>
#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);

View File

@ -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 \