Files
EJM_Display/main.cpp
2025-09-30 15:36:46 +08:00

152 lines
4.9 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "LaunchPage.h"
#include <QObject>
#include <QApplication>
#include <QLoggingCategory>
#include <QLocale>
#include <QTranslator>
#include <QFile>
#include <QTextStream>
#include <QDateTime>
#include <QMutex>
#include <QDir>
#include <QDebug>
#include <QScreen>
#include <GlobalDefinitions/Configure.h>
#include <GlobalDefinitions/Variable.h>
QtMessageHandler gDefaultHandler = NULL; // 程序集变量,用于保存默认的消息处理函数
#if CONFIG_EN_LOG
/**
* @brief myMessageOutput 日志文件输出
* @param type
* @param context
* @param msg
*/
void myMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
QString strMsg("");
switch(type)
{
case QtDebugMsg:
strMsg = QString("调试");
break;
case QtWarningMsg:
strMsg = QString("警告");
break;
case QtCriticalMsg:
strMsg = QString("错误");
break;
case QtFatalMsg:
strMsg = QString("严重");
break;
case QtInfoMsg:
strMsg = QString("信息");
break;
default:
strMsg = QString("未知");
break;
}
QDateTime time = QDateTime::currentDateTime();
QString strTime = time.toString("yyyy-MM-dd hh:mm:ss");
QString fileName = context.file ? context.file : "未知";
int line = context.line ? context.line : -1;
QString function = context.function ? context.function : "未知";
QString strMessage = QString("%1 文件:(%2)行:[%3] 函数名:{%4} %5>> %6")
.arg(strTime).arg(fileName).arg(line).arg(function).arg(strMsg).arg(msg);
// 加锁
static QMutex mutex;
mutex.lock();
//用系统原来的函数完成原来的功能. 比如输出到调试窗
if(gDefaultHandler)
{
switch(type)
{
case QtDebugMsg:
gDefaultHandler(type,context,"\033[36m"+strMessage);
break;
case QtWarningMsg:
gDefaultHandler(type,context,"\033[33m"+strMessage);
break;
case QtCriticalMsg:
gDefaultHandler(type,context,"\033[35m"+strMessage);
break;
case QtFatalMsg:
gDefaultHandler(type,context,"\033[31m"+strMessage);
break;
case QtInfoMsg:
gDefaultHandler(type,context,"\033[32m"+strMessage);
break;
default:
gDefaultHandler(type,context,"\033[27m"+strMessage);
break;
}
}
// 输出信息至文件中(读写、追加形式)
QString current_time = QDateTime::currentDateTime().toString("yyyy_MM_dd");
// 获取当前目录,如果目录不存在则创建
QString fullPath = qApp->applicationDirPath()+ "/Log/";
QDir dir(fullPath);
if(!dir.exists()) dir.mkdir(fullPath);
// 输出信息至文件中(读写、追加形式)
QFile file(fullPath+current_time+CONFIG_LOG_SUFFIX);
file.open(QIODevice::ReadWrite | QIODevice::Append);
QTextStream stream(&file);
stream << strMessage << "\r\n";
file.flush();
file.close();
// 解锁
mutex.unlock();
}
#endif
int main(int argc, char *argv[])
{
#if CONFIG_EN_DPI //开启高DPI
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endif
QApplication a(argc, argv); // 创建一个QApplication对象它是所有GUI程序的起点
// 打印 Qt 自带调试信息
//QLoggingCategory::setFilterRules("*.debug=true");
//qputenv("QT_RESOURCE_DEBUG", "1"); // 开启资源加载调试
#if CONFIG_EN_LOG
gDefaultHandler = qInstallMessageHandler(myMessageOutput);//注册日志函数,在其它地方调用qInstallMessageHandler设置新的输出函数但保存原来的函数
#endif
#if defined(Q_OS_LINUX) // 判断操作系统是否为Linux
qDebug()<<"操作系统为:Linux";
#elif defined(Q_OS_WIN) // 判断操作系统是否为Windows
qDebug()<<"操作系统为:Windows";
#else // 如果不是Linux也不是Windows
qDebug()<<"操作系统为:其他操作系统";
#endif
#if CONFIG_EN_DISPLAY_INFO // 获取默认屏幕
DisplayInfo = QGuiApplication::primaryScreen()->geometry();
#endif
QTranslator translator;
const QStringList uiLanguages = QLocale::system().uiLanguages();
for (const QString &locale : uiLanguages) {
const QString baseName = "EJM_JueMao_" + QLocale(locale).name();
if (translator.load(":/i18n/" + baseName)) {
a.installTranslator(&translator);
break;
}
}
// 屏蔽QOpenGLContext::swapBuffers()的警告
QLoggingCategory::setFilterRules(
"qt.opengl.context.swapBuffers=false\n"
"Qt3DRender::QOpenGLContext.swapBuffers=false"
);
LaunchPage w;
w.show();
return a.exec();
}