forked from openkylin/ukui-panel
增加日志处理功能
This commit is contained in:
parent
cffb841993
commit
4237e16d5e
|
@ -24,6 +24,7 @@ set(PROJECT_SOURCES
|
||||||
src/main.cpp
|
src/main.cpp
|
||||||
src/shell.cpp
|
src/shell.cpp
|
||||||
src/view/panel.cpp
|
src/view/panel.cpp
|
||||||
|
src/log-utils.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
|
if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
|
||||||
|
|
|
@ -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-panel/";
|
||||||
|
|
||||||
|
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,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
|
|
@ -6,6 +6,7 @@
|
||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
|
|
||||||
#include "shell.h"
|
#include "shell.h"
|
||||||
|
#include "log-utils.h"
|
||||||
|
|
||||||
void loadTranslation() {
|
void loadTranslation() {
|
||||||
const QString dir = ":/i18n/";
|
const QString dir = ":/i18n/";
|
||||||
|
@ -26,6 +27,8 @@ void loadTranslation() {
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
|
LogUtils::initLogFile("ukui-panel");
|
||||||
|
qInstallMessageHandler(LogUtils::messageOutput);
|
||||||
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
|
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
|
||||||
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
|
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in New Issue