From 0a7f21325d06a224cc91dcf702c6f1a3db881274 Mon Sep 17 00:00:00 2001 From: Pierre Ferran Date: Tue, 4 Jun 2024 17:30:05 -0400 Subject: [PATCH] Fix #88, fix crash on app exit --- backend/CMakeLists.txt | 4 +-- backend/include/Helpers.hpp | 10 +++--- backend/include/InputHandler.hpp | 1 - backend/include/RemoteData.hpp | 2 -- backend/include/Shared.hpp | 16 ++++----- backend/include/sdk.hpp | 1 - backend/src/main.cpp | 62 +++++++++++--------------------- backend/src/sdk.cpp | 1 - backend/types/index.d.ts | 2 +- backend/vcpkg.json | 2 +- package-lock.json | 2 +- src/main/index.ts | 4 +-- 12 files changed, 37 insertions(+), 70 deletions(-) diff --git a/backend/CMakeLists.txt b/backend/CMakeLists.txt index d898beb..f43e351 100644 --- a/backend/CMakeLists.txt +++ b/backend/CMakeLists.txt @@ -79,7 +79,7 @@ find_package(Poco COMPONENTS Foundation Util REQUIRED) find_package(semver CONFIG REQUIRED) find_package(restinio CONFIG REQUIRED) find_package(nlohmann_json CONFIG REQUIRED) -find_package(quill CONFIG REQUIRED) +find_package(spdlog CONFIG REQUIRED) find_package(platform_folders CONFIG REQUIRED) find_package(SFML COMPONENTS system window graphics CONFIG REQUIRED) find_path(SIMPLEINI_INCLUDE_DIRS "SimpleIni.h") @@ -104,7 +104,7 @@ target_link_libraries(trackaudio-afv PRIVATE semver::semver restinio::restinio nlohmann_json::nlohmann_json - quill::quill + spdlog::spdlog sago::platform_folders sfml-system sfml-graphics sfml-window ${CMAKE_JS_LIB}) diff --git a/backend/include/Helpers.hpp b/backend/include/Helpers.hpp index 620e66a..48834d1 100644 --- a/backend/include/Helpers.hpp +++ b/backend/include/Helpers.hpp @@ -2,9 +2,9 @@ #include "RadioSimulation.h" #include "Shared.hpp" +#include "spdlog/spdlog.h" #include #include -#include #include #include @@ -62,12 +62,11 @@ class NapiHelpers { return; } - std::lock_guard lock(_mutex); + std::lock_guard lock(_callElectronMutex); callbackRef->NonBlockingCall( [eventName, data, data2](Napi::Env env, Napi::Function jsCallback) { - LOG_TRACE_L1(quill::get_logger("trackaudio_logger"), - "Event name: {}, data: {}, data2: {}", eventName, data, data2); + SPDLOG_TRACE("Event name: {}, data: {}, data2: {}", eventName, data, data2); jsCallback.Call({ Napi::String::New(env, eventName), Napi::String::New(env, data), Napi::String::New(env, data2) }); }); @@ -78,6 +77,5 @@ class NapiHelpers { callElectron("error", message); } -protected: - inline static std::mutex _mutex; + inline static std::mutex _callElectronMutex; }; \ No newline at end of file diff --git a/backend/include/InputHandler.hpp b/backend/include/InputHandler.hpp index 64e55ef..4129a07 100644 --- a/backend/include/InputHandler.hpp +++ b/backend/include/InputHandler.hpp @@ -8,7 +8,6 @@ #include #include #include -#include #include #include diff --git a/backend/include/RemoteData.hpp b/backend/include/RemoteData.hpp index 7482dd8..d04f6d0 100644 --- a/backend/include/RemoteData.hpp +++ b/backend/include/RemoteData.hpp @@ -4,8 +4,6 @@ #include #include #include -#include -#include #include #define WIN32_LEAN_AND_MEAN diff --git a/backend/include/Shared.hpp b/backend/include/Shared.hpp index ab093c3..3e84b1b 100644 --- a/backend/include/Shared.hpp +++ b/backend/include/Shared.hpp @@ -1,22 +1,18 @@ #pragma once #include "afv-native/atcClientWrapper.h" +#include "spdlog/spdlog.h" #include +#include #include #include -#include -#include #include #include #include -#define TRACK_LOG_INFO(fmt, ...) \ - LOG_INFO(quill::get_logger("trackaudio_logger"), fmt, ##__VA_ARGS__) -#define TRACK_LOG_WARNING(fmt, ...) \ - LOG_WARNING(quill::get_logger("trackaudio_logger"), fmt, ##__VA_ARGS__) -#define TRACK_LOG_ERROR(fmt, ...) \ - LOG_ERROR(quill::get_logger("trackaudio_logger"), fmt, ##__VA_ARGS__) -#define TRACK_LOG_CRITICAL(fmt, ...) \ - LOG_CRITICAL(quill::get_logger("trackaudio_logger"), fmt, ##__VA_ARGS__) +#define TRACK_LOG_INFO(fmt, ...) spdlog::info(fmt, ##__VA_ARGS__); +#define TRACK_LOG_WARNING(fmt, ...) spdlog::warn(fmt, ##__VA_ARGS__); +#define TRACK_LOG_ERROR(fmt, ...) spdlog::error(fmt, ##__VA_ARGS__); +#define TRACK_LOG_CRITICAL(fmt, ...) spdlog::critical(fmt, ##__VA_ARGS__); #define TIMER_CALLBACK_INTERVAL_SEC 15 #define SLURPER_BASE_URL "https://slurper.vatsim.net" diff --git a/backend/include/sdk.hpp b/backend/include/sdk.hpp index 9bd318c..81909b5 100644 --- a/backend/include/sdk.hpp +++ b/backend/include/sdk.hpp @@ -18,7 +18,6 @@ #include #include #include -#include #include using sdk::types::WebsocketMessage; diff --git a/backend/src/main.cpp b/backend/src/main.cpp index f340813..4bee359 100644 --- a/backend/src/main.cpp +++ b/backend/src/main.cpp @@ -1,18 +1,17 @@ -#include -#include #include "afv-native/atcClientWrapper.h" #include "afv-native/event.h" #include "afv-native/hardwareType.h" +#include "spdlog/sinks/rotating_file_sink.h" +#include +#include #include #include #include #include #include +#include #include #include -#include -#include -#include #include #include #include @@ -638,47 +637,22 @@ void CreateLogFolders() void CreateLoggers() { - quill::configure([]() { - quill::Config cfg; - return cfg; - }()); - - // Starts the logging backend thread - quill::start(); - - std::shared_ptr trackaudio_logger - = quill::rotating_file_handler(FileSystem::GetStateFolderPath() / "trackaudio.log", []() { - quill::RotatingFileHandlerConfig cfg; - cfg.set_rotation_max_file_size(5e+6); // 5MB files - cfg.set_max_backup_files(2); - cfg.set_overwrite_rolled_files(true); - return cfg; - }()); + auto max_size = 1048576 * 5; + auto max_files = 3; + auto trackaudio_logger = spdlog::rotating_logger_mt("trackaudio_logger", + FileSystem::GetStateFolderPath() / "trackaudio.log", max_size, max_files); - auto* logger = quill::create_logger("trackaudio_logger", std::move(trackaudio_logger)); - - logger->set_log_level(quill::LogLevel::Info); - - std::shared_ptr afv_logger = quill::rotating_file_handler( - FileSystem::GetStateFolderPath() / "trackaudio-afv.log", []() { - quill::RotatingFileHandlerConfig cfg; - cfg.set_rotation_max_file_size(5e+6); // 5MB files - cfg.set_max_backup_files(2); - cfg.set_overwrite_rolled_files(true); - cfg.set_pattern("%(ascii_time) [%(thread)] %(message)"); - return cfg; - }()); - - // Create a file logger - auto* _afv_logger = quill::create_logger("afv_logger", std::move(afv_logger)); + spdlog::set_default_logger(trackaudio_logger); + auto afv_logger = spdlog::rotating_logger_mt( + "afv_logger", FileSystem::GetStateFolderPath() / "trackaudio.log", max_size, max_files); // NOLINTNEXTLINE this cannot be solved here but in afv afv_native::api::setLogger( // NOLINTNEXTLINE - [](std::string subsystem, std::string file, int line, std::string lineOut) { + [&](std::string subsystem, std::string file, int line, std::string lineOut) { auto strippedFiledName = file.substr(file.find_last_of('/') + 1); - LOG_INFO(quill::get_logger("afv_logger"), "[{}] [{}@{}] {}", subsystem, - strippedFiledName, line, lineOut); + spdlog::get("afv_logger") + ->info("{}:{}:{}: {}", subsystem, strippedFiledName, line, lineOut); }); } @@ -752,9 +726,11 @@ Napi::Object Bootstrap(const Napi::CallbackInfo& info) return outObject; } -void Exit(const Napi::CallbackInfo& /*info*/) +Napi::Boolean Exit(const Napi::CallbackInfo& info) { - TRACK_LOG_INFO("Exiting TrackAudio"); + TRACK_LOG_INFO("Awaiting to exit TrackAudio..."); + std::lock_guard HelperLock(NapiHelpers::_callElectronMutex); + TRACK_LOG_INFO("Exiting TrackAudio...") if (mClient->IsVoiceConnected()) { mClient->Disconnect(); } @@ -763,6 +739,8 @@ void Exit(const Napi::CallbackInfo& /*info*/) MainThreadShared::inputHandler.reset(); mClient.reset(); + + return Napi::Boolean::New(info.Env(), true); } Napi::Object Init(Napi::Env env, Napi::Object exports) diff --git a/backend/src/sdk.cpp b/backend/src/sdk.cpp index 4b6c9a4..a2ba530 100644 --- a/backend/src/sdk.cpp +++ b/backend/src/sdk.cpp @@ -1,7 +1,6 @@ #include "sdk.hpp" #include "Helpers.hpp" #include "Shared.hpp" -#include SDK::SDK() { this->buildServer(); } diff --git a/backend/types/index.d.ts b/backend/types/index.d.ts index 8a617ec..71a7535 100644 --- a/backend/types/index.d.ts +++ b/backend/types/index.d.ts @@ -96,5 +96,5 @@ declare namespace TrackAudioAfv { needUpdate: boolean; version: string; }; - export function Exit(): void; + export function Exit(): boolean; } diff --git a/backend/vcpkg.json b/backend/vcpkg.json index 556686c..6a42572 100644 --- a/backend/vcpkg.json +++ b/backend/vcpkg.json @@ -10,7 +10,7 @@ "openssl", "restinio", "nlohmann-json", - "quill", + "spdlog", "platform-folders", "sfml", "simpleini", diff --git a/package-lock.json b/package-lock.json index 5042ab5..0ecc337 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9644,7 +9644,7 @@ "node_modules/trackaudio-afv": { "version": "1.0.0", "resolved": "file:backend/trackaudio-afv-1.0.0.tgz", - "integrity": "sha512-T6UxW+8NqPjI9c6PK+YX0YmvQJEAWYfb+dANwAra0Yxeo2dkF98YMNJsULu+lKdgqyb/m828C30B965MPE/jeQ==", + "integrity": "sha512-0iMkLfzmcNYLH2FA8qUYbi40SbF7i+6IvzA5fTnYLIC0DgwgyCsoC1Y6RadE7o1X4+io6NhviuLsTMPoEBtx7A==", "license": "GPL-3.0-only", "dependencies": { "bindings": "^1.5.0", diff --git a/src/main/index.ts b/src/main/index.ts index cbcb2ec..e47aba9 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -272,8 +272,8 @@ app.on("window-all-closed", () => { app.quit(); }); -app.on("quit", () => { - TrackAudioAfv.Exit(); +app.on("quit", async () => { + await TrackAudioAfv.Exit(); }); // In this file you can include the rest of your app's specific main process