diff options
Diffstat (limited to 'src/init.cpp')
-rw-r--r-- | src/init.cpp | 143 |
1 files changed, 81 insertions, 62 deletions
diff --git a/src/init.cpp b/src/init.cpp index ddab4add..f9a1dabd 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -1,5 +1,5 @@ /** - * Copyright (C) 2021 Saturneric + * Copyright (C) 2021 Saturneric <[email protected]> * * This file is part of GpgFrontend. * @@ -20,23 +20,26 @@ * the gpg4usb project, which is under GPL-3.0-or-later. * * All the source code of GpgFrontend was modified and released by - * Saturneric<[email protected]> starting on May 12, 2021. + * Saturneric <[email protected]> starting on May 12, 2021. * * SPDX-License-Identifier: GPL-3.0-or-later * */ -#include <spdlog/async.h> -#include <spdlog/common.h> -#include <spdlog/sinks/rotating_file_sink.h> -#include <spdlog/sinks/stdout_color_sinks.h> +#include "init.h" -#include <filesystem> -#include <string> - -#include "GpgFrontend.h" -#include "GpgFrontendBuildInfo.h" +#include "core/GpgCoreInit.h" #include "core/function/GlobalSettingStation.h" +#include "core/thread/TaskRunnerGetter.h" +#include "core/utils/LogUtils.h" +#include "module/GpgFrontendModuleInit.h" +#include "ui/GpgFrontendUIInit.h" + +// main +#include "GpgFrontendContext.h" +#include "main.h" + +namespace GpgFrontend { #ifdef WINDOWS int setenv(const char *name, const char *value, int overwrite) { @@ -50,66 +53,82 @@ int setenv(const char *name, const char *value, int overwrite) { } #endif -void init_logging_system() { - using namespace boost::posix_time; - using namespace boost::gregorian; +void InitLoggingSystem(const GFCxtSPtr &ctx) { + RegisterSyncLogger("core", ctx->log_level); + RegisterSyncLogger("main", ctx->log_level); + RegisterSyncLogger("module", ctx->log_level); + RegisterSyncLogger("ui", ctx->log_level); + RegisterSyncLogger("test", ctx->log_level); +} - // sinks - std::vector<spdlog::sink_ptr> sinks; - sinks.push_back(std::make_shared<spdlog::sinks::stderr_color_sink_mt>()); +void InitGlobalPathEnv() { + // read settings + bool use_custom_gnupg_install_path = + GlobalSettingStation::GetInstance() + .GetSettings() + .value("basic/use_custom_gnupg_install_path", false) + .toBool(); - // thread pool - spdlog::init_thread_pool(1024, 2); + QString custom_gnupg_install_path = + GlobalSettingStation::GetInstance() + .GetSettings() + .value("basic/custom_gnupg_install_path") + .toString(); - // logger - auto main_logger = std::make_shared<spdlog::async_logger>( - "main", begin(sinks), end(sinks), spdlog::thread_pool()); - main_logger->set_pattern( - "[%H:%M:%S.%e] [T:%t] [%=4n] %^[%=8l]%$ [%s:%#] [%!] -> %v (+%ius)"); + // add custom gnupg install path into env $PATH + if (use_custom_gnupg_install_path && !custom_gnupg_install_path.isEmpty()) { + QString path_value = getenv("PATH"); + GF_MAIN_LOG_DEBUG("Current System PATH: {}", path_value); + setenv("PATH", + (QDir(custom_gnupg_install_path).absolutePath() + ":" + path_value) + .toUtf8(), + 1); + QString modified_path_value = getenv("PATH"); + GF_MAIN_LOG_DEBUG("Modified System PATH: {}", modified_path_value); + } +} -#ifdef DEBUG - main_logger->set_level(spdlog::level::trace); -#else - main_logger->set_level(spdlog::level::info); -#endif +void InitGlobalBasicalEnv(const GFCxtWPtr &p_ctx, bool gui_mode) { + GFCxtSPtr ctx = p_ctx.lock(); + if (ctx == nullptr) { + return; + } - // flush policy - main_logger->flush_on(spdlog::level::err); - spdlog::flush_every(std::chrono::seconds(5)); + // initialize logging system + SetDefaultLogLevel(ctx->log_level); + InitLoggingSystem(ctx); - // register it as default logger - spdlog::set_default_logger(main_logger); -} + // change path to search for related + InitGlobalPathEnv(); -void shutdown_logging_system() { -#ifdef WINDOWS - // Under VisualStudio, this must be called before main finishes to workaround - // a known VS issue - spdlog::drop_all(); - spdlog::shutdown(); -#endif -} + // init application + ctx->InitApplication(); -void init_global_path_env() { - // read settings - bool use_custom_gnupg_install_path = - GpgFrontend::GlobalSettingStation::GetInstance().LookupSettings( - "general.use_custom_gnupg_install_path", false); + // should load module system first + Module::ModuleInitArgs module_init_args; + module_init_args.log_level = ctx->log_level; + Module::LoadGpgFrontendModules(module_init_args); - std::string custom_gnupg_install_path = - GpgFrontend::GlobalSettingStation::GetInstance().LookupSettings( - "general.custom_gnupg_install_path", std::string{}); + // then preload ui + UI::PreInitGpgFrontendUI(); - // add custom gnupg install path into env $PATH - if (use_custom_gnupg_install_path && !custom_gnupg_install_path.empty()) { - std::string path_value = getenv("PATH"); - SPDLOG_DEBUG("Current System PATH: {}", path_value); - setenv("PATH", - ((std::filesystem::path{custom_gnupg_install_path}).u8string() + - ":" + path_value) - .c_str(), - 1); - std::string modified_path_value = getenv("PATH"); - SPDLOG_DEBUG("Modified System PATH: {}", modified_path_value); + CoreInitArgs core_init_args; + core_init_args.gather_external_gnupg_info = ctx->gather_external_gnupg_info; + core_init_args.load_default_gpg_context = ctx->load_default_gpg_context; + + // then load core + InitGpgFrontendCore(core_init_args); +} + +void ShutdownGlobalBasicalEnv(const GFCxtWPtr &p_ctx) { + GFCxtSPtr ctx = p_ctx.lock(); + if (ctx == nullptr) { + return; } + + Thread::TaskRunnerGetter::GetInstance().StopAllTeakRunner(); + + DestroyGpgFrontendCore(); } + +} // namespace GpgFrontend |