aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSaturneric <[email protected]>2022-05-19 18:41:21 +0000
committerSaturneric <[email protected]>2022-05-19 18:41:21 +0000
commit6c884e583326b08aa7e354b47c18cdedaf9308f6 (patch)
tree4d3a198d9c13aca7693fe12571d961f1ba25b753
parentfix: solve user manual navbar.md link to downloads (diff)
downloadGpgFrontend-6c884e583326b08aa7e354b47c18cdedaf9308f6.tar.gz
GpgFrontend-6c884e583326b08aa7e354b47c18cdedaf9308f6.zip
perf: improve initialized and recover process
1. init logging system 2. init ui 3. init main
-rw-r--r--src/core/GpgCoreInit.cpp25
-rw-r--r--src/core/GpgCoreInit.h2
-rw-r--r--src/init.cpp65
-rw-r--r--src/main.cpp93
-rw-r--r--src/ui/GpgFrontendUIInit.cpp71
-rw-r--r--src/ui/GpgFrontendUIInit.h4
6 files changed, 165 insertions, 95 deletions
diff --git a/src/core/GpgCoreInit.cpp b/src/core/GpgCoreInit.cpp
index 41cf99cb..f1664b2a 100644
--- a/src/core/GpgCoreInit.cpp
+++ b/src/core/GpgCoreInit.cpp
@@ -28,8 +28,6 @@
#include "GpgCoreInit.h"
-#include <memory>
-
#include "GpgFunctionObject.h"
#include "core/GpgContext.h"
#include "core/function/GlobalSettingStation.h"
@@ -43,28 +41,37 @@ namespace GpgFrontend {
* @brief setup logging system and do proper initialization
*
*/
-void init_logging() {
+void InitLoggingSystem() {
using namespace boost::posix_time;
using namespace boost::gregorian;
- ptime now = second_clock::local_time();
-
el::Loggers::addFlag(el::LoggingFlag::AutoSpacing);
+ el::Loggers::addFlag(el::LoggingFlag::ColoredTerminalOutput);
+ el::Loggers::addFlag(el::LoggingFlag::StrictLogFileSizeCheck);
el::Configurations defaultConf;
defaultConf.setToDefault();
- el::Loggers::reconfigureLogger("default", defaultConf);
// apply settings
defaultConf.setGlobally(el::ConfigurationType::Format,
- "%datetime %level %func %msg");
+ "%datetime %level [core] {%func} -> %msg");
+
+ // apply settings no written to file
+ defaultConf.setGlobally(el::ConfigurationType::ToFile, "false");
+
+ // set the logger
+ el::Loggers::reconfigureLogger("default", defaultConf);
// get the log directory
- auto logfile_path =
- (GlobalSettingStation::GetInstance().GetLogDir() / to_iso_string(now));
+ auto logfile_path = (GlobalSettingStation::GetInstance().GetLogDir() /
+ to_iso_string(second_clock::local_time()));
logfile_path.replace_extension(".log");
defaultConf.setGlobally(el::ConfigurationType::Filename,
logfile_path.u8string());
+ // apply settings written to file
+ defaultConf.setGlobally(el::ConfigurationType::ToFile, "true");
+
+ // set the logger
el::Loggers::reconfigureLogger("default", defaultConf);
LOG(INFO) << _("log file path") << logfile_path;
diff --git a/src/core/GpgCoreInit.h b/src/core/GpgCoreInit.h
index 752fe4c3..150e85e9 100644
--- a/src/core/GpgCoreInit.h
+++ b/src/core/GpgCoreInit.h
@@ -37,7 +37,7 @@ namespace GpgFrontend {
* @brief
*
*/
-void init_logging();
+void GPGFRONTEND_CORE_EXPORT InitLoggingSystem();
/**
* @brief
diff --git a/src/init.cpp b/src/init.cpp
index 2f4955f5..775ccbd5 100644
--- a/src/init.cpp
+++ b/src/init.cpp
@@ -26,25 +26,50 @@
*
*/
+#include "GpgFrontend.h"
+#include "GpgFrontendBuildInfo.h"
#include "core/function/GlobalSettingStation.h"
-/**
- * @brief Get the files of a given directory
- *
- * @param _path target directory
- * @return std::vector<std::filesystem::path>
- */
-std::vector<std::filesystem::path> get_files_of_directory(
- const std::filesystem::path& _path) {
- namespace fs = std::filesystem;
- std::vector<fs::path> path_list;
- if (!_path.empty()) {
- fs::recursive_directory_iterator end;
-
- for (fs::recursive_directory_iterator i(_path); i != end; ++i) {
- const fs::path cp = (*i);
- path_list.push_back(cp);
- }
- }
- return path_list;
-} \ No newline at end of file
+QApplication* init_qapplication(int argc, char* argv[]) {
+ auto* app = new QApplication(argc, argv);
+#ifndef MACOS
+ app->setWindowIcon(QIcon(":gpgfrontend.png"));
+#endif
+
+#ifdef MACOS
+ // support retina screen
+ app->setAttribute(Qt::AA_UseHighDpiPixmaps);
+#endif
+
+ // set the extra information of the build
+ app->setApplicationVersion(BUILD_VERSION);
+ app->setApplicationName(PROJECT_NAME);
+ app->setQuitOnLastWindowClosed(true);
+
+ // don't show icons in menus
+ app->setAttribute(Qt::AA_DontShowIconsInMenus);
+
+ // unicode in source
+ QTextCodec::setCodecForLocale(QTextCodec::codecForName("utf-8"));
+ return app;
+}
+
+void destory_qapplication(QApplication* app) {
+ app->quit();
+ delete app;
+}
+
+void init_logging_system() {
+ el::Loggers::addFlag(el::LoggingFlag::AutoSpacing);
+ el::Configurations defaultConf;
+ defaultConf.setToDefault();
+
+ // apply settings
+ defaultConf.setGlobally(el::ConfigurationType::Format,
+ "%datetime %level [main] %func %msg");
+ // apply settings no written to file
+ defaultConf.setGlobally(el::ConfigurationType::ToFile, "false");
+
+ // set the logger
+ el::Loggers::reconfigureLogger("default", defaultConf);
+}
diff --git a/src/main.cpp b/src/main.cpp
index fd20a664..6ef5a7bc 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -32,16 +32,11 @@
#include <csetjmp>
#include <csignal>
-#include <cstdlib>
+#include <cstddef>
-#include "GpgFrontendBuildInfo.h"
-#include "core/GpgFunctionObject.h"
-#include "ui/GpgFrontendUIInit.h"
-#include "ui/main_window/MainWindow.h"
-
-#if !defined(RELEASE) && defined(WINDOWS)
+#include "core/GpgCoreInit.h"
#include "core/function/GlobalSettingStation.h"
-#endif
+#include "ui/GpgFrontendUIInit.h"
/**
* \brief initialize the easylogging++ library.
@@ -54,19 +49,40 @@ INITIALIZE_EASYLOGGINGPP
jmp_buf recover_env;
/**
- * @brief
+ * @brief handle the signal SIGSEGV
*
* @param sig
*/
extern void handle_signal(int sig);
/**
- * @brief
+ * @brief processes before exit the program.
*
*/
extern void before_exit();
/**
+ * @brief init a new instance of QApplication.
+ *
+ * @param argc
+ * @param argv
+ */
+extern QApplication* init_qapplication(int argc, char* argv[]);
+
+/**
+ * @brief destroy the instance of QApplication.
+ *
+ * @param app
+ */
+extern void destory_qapplication(QApplication* app);
+
+/**
+ * @brief initialize the logging system.
+ *
+ */
+extern void init_logging_system();
+
+/**
*
* @param argc
* @param argv
@@ -81,42 +97,17 @@ int main(int argc, char* argv[]) {
// clean something before exit
atexit(before_exit);
+ // init the logging system
+ init_logging_system();
+
+ // init the logging system for core
+ GpgFrontend::InitLoggingSystem();
+
// initialize qt resources
Q_INIT_RESOURCE(gpgfrontend);
// create qt application
- QApplication app(argc, argv);
-
-#ifndef MACOS
- QApplication::setWindowIcon(QIcon(":gpgfrontend.png"));
-#endif
-
-#ifdef MACOS
- // support retina screen
- QApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
-#endif
-
- // set the extra information of the build
- QApplication::setApplicationVersion(BUILD_VERSION);
- QApplication::setApplicationName(PROJECT_NAME);
-
- // don't show icons in menus
- QApplication::setAttribute(Qt::AA_DontShowIconsInMenus);
-
- // unicode in source
- QTextCodec::setCodecForLocale(QTextCodec::codecForName("utf-8"));
-
-#if !defined(RELEASE) && defined(WINDOWS)
- // css
- std::filesystem::path css_path =
- GpgFrontend::GlobalSettingStation::GetInstance().GetResourceDir() /
- "css" / "default.qss";
- QFile file(css_path.u8string().c_str());
- file.open(QFile::ReadOnly);
- QString styleSheet = QLatin1String(file.readAll());
- qApp->setStyleSheet(styleSheet);
- file.close();
-#endif
+ auto* app = init_qapplication(argc, argv);
/**
* internationalisation. loop to restart main window
@@ -134,13 +125,14 @@ int main(int argc, char* argv[]) {
#ifdef RELEASE
try {
#endif
- QApplication::setQuitOnLastWindowClosed(true);
+ // renew application
+ if (app == nullptr) app = init_qapplication(argc, argv);
// init ui library
- GpgFrontend::UI::InitGpgFrontendUI();
+ GpgFrontend::UI::InitGpgFrontendUI(app);
// create main window
- return_from_event_loop_code = GpgFrontend::UI::RunGpgFrontendUI();
+ return_from_event_loop_code = GpgFrontend::UI::RunGpgFrontendUI(app);
#ifdef RELEASE
} catch (...) {
// catch all unhandled exceptions and notify the user
@@ -152,10 +144,8 @@ int main(int argc, char* argv[]) {
"serious problem, it may be the negligence of the programmer, "
"please report this problem if you can."));
return_from_event_loop_code = RESTART_CODE;
- continue;
}
#endif
-
} else {
// when signal is caught, restart the main window
QMessageBox::information(
@@ -163,11 +153,16 @@ int main(int argc, char* argv[]) {
_("Oh no! GpgFrontend caught a serious error in the software, so it "
"needs to be restarted. If the problem recurs, please manually "
"terminate the program and report the problem to the developer."));
- QCoreApplication::quit();
return_from_event_loop_code = RESTART_CODE;
LOG(INFO) << "return_from_event_loop_code" << return_from_event_loop_code;
- continue;
}
+
+ // destory the application
+ if (app) {
+ destory_qapplication(app);
+ app = nullptr;
+ }
+
LOG(INFO) << "loop refresh";
} while (return_from_event_loop_code == RESTART_CODE);
diff --git a/src/ui/GpgFrontendUIInit.cpp b/src/ui/GpgFrontendUIInit.cpp
index e7751ee6..8fa102af 100644
--- a/src/ui/GpgFrontendUIInit.cpp
+++ b/src/ui/GpgFrontendUIInit.cpp
@@ -28,6 +28,7 @@
#include "GpgFrontendUIInit.h"
+#include "GpgFrontendBuildInfo.h"
#include "core/function/GlobalSettingStation.h"
#include "core/thread/CtxCheckTask.h"
#include "core/thread/TaskRunnerGetter.h"
@@ -35,6 +36,10 @@
#include "ui/UserInterfaceUtils.h"
#include "ui/main_window/MainWindow.h"
+#if !defined(RELEASE) && defined(WINDOWS)
+#include "core/function/GlobalSettingStation.h"
+#endif
+
// init easyloggingpp library
INITIALIZE_EASYLOGGINGPP
@@ -43,10 +48,50 @@ namespace GpgFrontend::UI {
extern void init_logging();
extern void init_locale();
-void InitGpgFrontendUI() {
+void InitGpgFrontendUI(QApplication* app) {
+ app->setQuitOnLastWindowClosed(true);
+
+#ifndef MACOS
+ app->setWindowIcon(QIcon(":gpgfrontend.png"));
+#endif
+
+#ifdef MACOS
+ // support retina screen
+ app->setAttribute(Qt::AA_UseHighDpiPixmaps);
+#endif
+
+ // set the extra information of the build
+ app->setApplicationVersion(BUILD_VERSION);
+ app->setApplicationName(PROJECT_NAME);
+
+ // don't show icons in menus
+ app->setAttribute(Qt::AA_DontShowIconsInMenus);
+
+ // unicode in source
+ QTextCodec::setCodecForLocale(QTextCodec::codecForName("utf-8"));
+
+#if !defined(RELEASE) && defined(WINDOWS)
+ // css
+ std::filesystem::path css_path =
+ GpgFrontend::GlobalSettingStation::GetInstance().GetResourceDir() /
+ "css" / "default.qss";
+ QFile file(css_path.u8string().c_str());
+ file.open(QFile::ReadOnly);
+ QString styleSheet = QLatin1String(file.readAll());
+ qApp->setStyleSheet(styleSheet);
+ file.close();
+#endif
+
+ // init locale
init_locale();
+
+ // init logging system
init_logging();
+
+ // init signal station
SignalStation::GetInstance();
+
+ // init common utils
CommonUtils::GetInstance();
// create the thread to load the gpg context
@@ -65,15 +110,14 @@ void InitGpgFrontendUI() {
waiting_dialog_label->setWordWrap(true);
waiting_dialog->setLabel(waiting_dialog_label);
waiting_dialog->resize(420, 120);
- QApplication::connect(init_ctx_task,
- &Thread::CtxCheckTask::SignalTaskFinished,
- waiting_dialog, [=]() {
- LOG(INFO) << "Gpg context loaded";
- waiting_dialog->finished(0);
- waiting_dialog->deleteLater();
- });
-
- QApplication::connect(waiting_dialog, &QProgressDialog::canceled, [=]() {
+ app->connect(init_ctx_task, &Thread::CtxCheckTask::SignalTaskFinished,
+ waiting_dialog, [=]() {
+ LOG(INFO) << "Gpg context loaded";
+ waiting_dialog->finished(0);
+ waiting_dialog->deleteLater();
+ });
+
+ app->connect(waiting_dialog, &QProgressDialog::canceled, [=]() {
LOG(INFO) << "cancel clicked";
QCoreApplication::quit();
exit(0);
@@ -86,9 +130,8 @@ void InitGpgFrontendUI() {
// new local event looper
QEventLoop looper;
- QApplication::connect(init_ctx_task,
- &Thread::CtxCheckTask::SignalTaskFinished, &looper,
- &QEventLoop::quit);
+ app->connect(init_ctx_task, &Thread::CtxCheckTask::SignalTaskFinished,
+ &looper, &QEventLoop::quit);
// start the thread to load the gpg context
Thread::TaskRunnerGetter::GetInstance().GetTaskRunner()->PostTask(
@@ -98,7 +141,7 @@ void InitGpgFrontendUI() {
looper.exec();
}
-int RunGpgFrontendUI() {
+int RunGpgFrontendUI(QApplication* app) {
// create main window and show it
auto main_window = std::make_unique<GpgFrontend::UI::MainWindow>();
main_window->Init();
diff --git a/src/ui/GpgFrontendUIInit.h b/src/ui/GpgFrontendUIInit.h
index d7b6390d..9b490c0f 100644
--- a/src/ui/GpgFrontendUIInit.h
+++ b/src/ui/GpgFrontendUIInit.h
@@ -37,12 +37,12 @@ namespace GpgFrontend::UI {
* @brief init the UI library
*
*/
-void GPGFRONTEND_UI_EXPORT InitGpgFrontendUI();
+void GPGFRONTEND_UI_EXPORT InitGpgFrontendUI(QApplication *);
/**
* @brief run main window
*/
-int GPGFRONTEND_UI_EXPORT RunGpgFrontendUI();
+int GPGFRONTEND_UI_EXPORT RunGpgFrontendUI(QApplication *);
}; // namespace GpgFrontend::UI