feat(server): 增加日志处理
This commit is contained in:
parent
f1d339642d
commit
4782eae340
|
@ -16,7 +16,8 @@ set(notificationServer_SRCS
|
|||
${3rdParties_DIR}qtlocalpeer.cpp
|
||||
notification-server-application.cpp
|
||||
notification-server-application.h
|
||||
)
|
||||
log-utils.h
|
||||
log-utils.cpp)
|
||||
if(COMMAND qt_add_dbus_adaptor)
|
||||
qt_add_dbus_adaptor(notificationServer_SRCS ../dbus/org.freedesktop.Notifications.xml server-private.h NotificationServer::ServerPrivate)
|
||||
qt_add_dbus_adaptor(notificationServer_SRCS ../dbus/org.ukui.NotificationServer.xml server-private.h NotificationServer::ServerPrivate)
|
||||
|
|
|
@ -0,0 +1,136 @@
|
|||
/*
|
||||
* Copyright (C) 2023, KylinSoft Co., Ltd.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
* Authors: iaom <zhangpengfei@kylinos.cn>
|
||||
*/
|
||||
#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-notification/";
|
||||
|
||||
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();
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
/*
|
||||
* Copyright (C) 2023, KylinSoft Co., Ltd.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
* Authors: iaom <zhangpengfei@kylinos.cn>
|
||||
*/
|
||||
#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
|
|
@ -17,10 +17,10 @@
|
|||
* Authors: iaom <zhangpengfei@kylinos.cn>
|
||||
*/
|
||||
|
||||
#include <QCoreApplication>
|
||||
#include <QLocale>
|
||||
#include <QTranslator>
|
||||
#include "notification-server-application.h"
|
||||
#include "log-utils.h"
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
|
@ -34,6 +34,8 @@ int main(int argc, char *argv[])
|
|||
// break;
|
||||
// }
|
||||
// }
|
||||
LogUtils::initLogFile("ukui-notification-server");
|
||||
qInstallMessageHandler(LogUtils::messageOutput);
|
||||
NotificationServer::NotificationServerApplication app(argc, argv);
|
||||
if(app.isRunning()) {
|
||||
return 0;
|
||||
|
|
Loading…
Reference in New Issue