Skip to content

Commit

Permalink
Fix #88, fix crash on app exit
Browse files Browse the repository at this point in the history
  • Loading branch information
pierr3 committed Jun 4, 2024
1 parent 4969959 commit 0a7f213
Show file tree
Hide file tree
Showing 12 changed files with 37 additions and 70 deletions.
4 changes: 2 additions & 2 deletions backend/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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})
Expand Down
10 changes: 4 additions & 6 deletions backend/include/Helpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
#include "RadioSimulation.h"
#include "Shared.hpp"

#include "spdlog/spdlog.h"
#include <cmath>
#include <mutex>
#include <quill/Quill.h>
#include <sago/platform_folders.h>
#include <string>

Expand Down Expand Up @@ -62,12 +62,11 @@ class NapiHelpers {
return;
}

std::lock_guard<std::mutex> lock(_mutex);
std::lock_guard<std::mutex> 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) });
});
Expand All @@ -78,6 +77,5 @@ class NapiHelpers {
callElectron("error", message);
}

protected:
inline static std::mutex _mutex;
inline static std::mutex _callElectronMutex;
};
1 change: 0 additions & 1 deletion backend/include/InputHandler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#include <SFML/Window/Keyboard.hpp>
#include <atomic>
#include <mutex>
#include <quill/Quill.h>
#include <string>

#include <Shared.hpp>
Expand Down
2 changes: 0 additions & 2 deletions backend/include/RemoteData.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
#include <absl/strings/str_split.h>
#include <cstddef>
#include <mutex>
#include <quill/Quill.h>
#include <quill/detail/LogMacros.h>
#include <string>

#define WIN32_LEAN_AND_MEAN
Expand Down
16 changes: 6 additions & 10 deletions backend/include/Shared.hpp
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
#pragma once
#include "afv-native/atcClientWrapper.h"
#include "spdlog/spdlog.h"
#include <SimpleIni.h>
#include <filesystem>
#include <memory>
#include <napi.h>
#include <quill/Logger.h>
#include <quill/Quill.h>
#include <sago/platform_folders.h>
#include <semver.hpp>
#include <string>

#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"
Expand Down
1 change: 0 additions & 1 deletion backend/include/sdk.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
#include <absl/strings/str_join.h>
#include <mutex>
#include <nlohmann/json_fwd.hpp>
#include <quill/Quill.h>
#include <set>

using sdk::types::WebsocketMessage;
Expand Down
62 changes: 20 additions & 42 deletions backend/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
#include <absl/strings/ascii.h>
#include <absl/strings/match.h>
#include "afv-native/atcClientWrapper.h"
#include "afv-native/event.h"
#include "afv-native/hardwareType.h"
#include "spdlog/sinks/rotating_file_sink.h"
#include <absl/strings/ascii.h>
#include <absl/strings/match.h>
#include <atomic>
#include <chrono>
#include <cstddef>
#include <httplib.h>
#include <memory>
#include <mutex>
#include <napi.h>
#include <optional>
#include <quill/LogLevel.h>
#include <quill/Quill.h>
#include <quill/detail/LogMacros.h>
#include <sago/platform_folders.h>
#include <semver.hpp>
#include <string>
Expand Down Expand Up @@ -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<quill::Handler> 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<quill::Handler> 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);
});
}

Expand Down Expand Up @@ -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<std::mutex> HelperLock(NapiHelpers::_callElectronMutex);
TRACK_LOG_INFO("Exiting TrackAudio...")
if (mClient->IsVoiceConnected()) {
mClient->Disconnect();
}
Expand All @@ -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)
Expand Down
1 change: 0 additions & 1 deletion backend/src/sdk.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "sdk.hpp"
#include "Helpers.hpp"
#include "Shared.hpp"
#include <quill/Quill.h>

SDK::SDK() { this->buildServer(); }

Expand Down
2 changes: 1 addition & 1 deletion backend/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,5 +96,5 @@ declare namespace TrackAudioAfv {
needUpdate: boolean;
version: string;
};
export function Exit(): void;
export function Exit(): boolean;
}
2 changes: 1 addition & 1 deletion backend/vcpkg.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"openssl",
"restinio",
"nlohmann-json",
"quill",
"spdlog",
"platform-folders",
"sfml",
"simpleini",
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 0a7f213

Please sign in to comment.