main.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #include <QApplication>
  2. #include <QCoreApplication>
  3. #include <QCommandLineParser>
  4. #include <QDebug>
  5. #include <QDir>
  6. #include <QLocale>
  7. #include <QProcess>
  8. #include <QSettings>
  9. #include <QTranslator>
  10. #include "hostthread.h"
  11. #include "messagequeue.h"
  12. #include "processthread.h"
  13. #include "qdatetime.h"
  14. #include "qobject.h"
  15. #include "qtsingleapplication.h"
  16. #include "updaterthread.h"
  17. void addToStartup()
  18. {
  19. QSettings settings("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Run",
  20. QSettings::NativeFormat);
  21. QString appPath = QCoreApplication::applicationFilePath(); // 获取应用程序路径
  22. settings.setValue("BaseMainApp", appPath); // 将应用程序路径添加到注册表
  23. }
  24. void redirectOutputToLogFile()
  25. {
  26. // 重定向 qDebug、qWarning 和 qCritical 的输出
  27. qInstallMessageHandler([](QtMsgType type,
  28. const QMessageLogContext &context,
  29. const QString &msg) { // 获取当前日期
  30. Q_UNUSED(context)
  31. QString dateString = QDate::currentDate().toString("yyyy-MM-dd");
  32. // 获取应用程序的日志文件路径
  33. QString logFileName = "./log/" + dateString + "_base.txt";
  34. // 创建 QFile 对象
  35. QFile logFile(logFileName);
  36. // 打开文件进行写入,如果文件不存在则创建
  37. if (!logFile.open(QIODevice::Append | QIODevice::Text)) {
  38. qWarning() << "Unable to open log file for writing:" << logFile.errorString();
  39. return;
  40. }
  41. QTextStream out(&logFile);
  42. QString timeString = QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss");
  43. switch (type) {
  44. case QtDebugMsg:
  45. out << timeString << " [DEBUG] " << msg << "\n";
  46. break;
  47. case QtWarningMsg:
  48. out << timeString << " [WARNING] " << msg << "\n";
  49. break;
  50. case QtCriticalMsg:
  51. out << timeString << " [CRITICAL] " << msg << "\n";
  52. break;
  53. case QtFatalMsg:
  54. out << timeString << " [FATAL] " << msg << "\n";
  55. // abort(); // 终止程序
  56. case QtInfoMsg:
  57. break;
  58. }
  59. logFile.close();
  60. });
  61. }
  62. int main(int argc, char *argv[])
  63. {
  64. // QApplication a(argc, argv);
  65. SharedTools::QtSingleApplication a("BaseMainApp", argc, argv);
  66. QDir dir;
  67. dir.mkdir("log"); // 创建日志目录
  68. addToStartup(); // 开机启动
  69. // redirectOutputToLogFile(); // 日志
  70. a.setApplicationVersion("1.1.2");
  71. a.setQuitOnLastWindowClosed(false);
  72. QCommandLineParser parser;
  73. parser.addHelpOption();
  74. parser.addVersionOption();
  75. QCommandLineOption debugOption("debug", "Enable debug mode");
  76. parser.addOption(debugOption);
  77. parser.process(a);
  78. if (a.isRunning()) {
  79. a.sendMessage("", 1000);
  80. return 0;
  81. }
  82. QTranslator translator;
  83. const QStringList uiLanguages = QLocale::system().uiLanguages();
  84. for (const QString &locale : uiLanguages) {
  85. const QString baseName = "basemain_" + QLocale(locale).name();
  86. if (translator.load(":/i18n/" + baseName)) {
  87. a.installTranslator(&translator);
  88. break;
  89. }
  90. }
  91. HostThread host;
  92. UpdaterThread thread;
  93. ProcessThread Process;
  94. MessageQueue messageQueue;
  95. QObject::connect(&Process, &ProcessThread::messageBox, &messageQueue, &MessageQueue::addMessage);
  96. QObject::connect(&Process, &ProcessThread::finished, &Process, &QObject::deleteLater);
  97. thread.start();
  98. Process.start();
  99. host.start();
  100. return a.exec();
  101. }