Skip to content

Commit

Permalink
Reduce logging of session updates, column limit in clang format
Browse files Browse the repository at this point in the history
  • Loading branch information
pierr3 committed May 2, 2024
1 parent d27bed7 commit d988257
Show file tree
Hide file tree
Showing 8 changed files with 217 additions and 259 deletions.
2 changes: 1 addition & 1 deletion backend/.clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ BreakBeforeTernaryOperators: true
BreakConstructorInitializers: BeforeComma
BreakInheritanceList: BeforeColon
BreakStringLiterals: true
ColumnLimit: 0
ColumnLimit: 100
CommentPragmas: '^ IWYU pragma:'
CompactNamespaces: false
ConstructorInitializerIndentWidth: 4
Expand Down
32 changes: 25 additions & 7 deletions backend/src/Helpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@

class Helpers {
public:
/**
* Cleans up the given frequency value.
*
* This function takes an integer frequency value as input and rounds it up to a valid 8.33 kHz channel.
* The cleaned up frequency value is then returned.
*
* @param frequency The frequency value to be cleaned up.
* @return The cleaned up frequency value.
*/
static int CleanUpFrequency(int frequency)
{
// We don't clean up an unset frequency
Expand All @@ -18,22 +27,31 @@ class Helpers {
return RadioSimulation::round8_33kHzChannel(frequency);
}

/**
* Calls the NAPI frontend callback function with an error message.
*
* @param message The error message to pass to the callback.
*/
static void CallbackWithError(const std::string& message)
{
std::lock_guard<std::mutex> lock(errorCallbackMutex);
if (!callbackAvailable) {
return;
}

callbackRef.NonBlockingCall(
[message](Napi::Env env, Napi::Function jsCallback) {
jsCallback.Call({ Napi::String::New(env, "error"),
Napi::String::New(env, message),
Napi::String::New(env, "") });
});
callbackRef.NonBlockingCall([message](Napi::Env env, Napi::Function jsCallback) {
jsCallback.Call({ Napi::String::New(env, "error"), Napi::String::New(env, message),
Napi::String::New(env, "") });
});
}

static std::string ConvertHzToHumanString(int frequencyHz)
/**
* Converts a frequency in Hertz to a human-readable string representation.
*
* @param frequencyHz The frequency in Hertz to convert.
* @return A string representation of the frequency in a human-readable format (e.g. "122.800" for 122800000 Hz)
*/
static std::string ConvertHzToHumanString(unsigned int frequencyHz)
{
std::string temp = std::to_string(frequencyHz / 1000);
return temp.substr(0, 3) + "." + temp.substr(3, 7);
Expand Down
68 changes: 38 additions & 30 deletions backend/src/RemoteData.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,29 +138,35 @@ class RemoteData {
int u334 = std::atoi(res3.c_str()) * 1000;
int k422 = std::stoi(res2, nullptr, 16) == 10 && pYx ? 1 : 0;

k422 = (u334 != OBS_FREQUENCY || absl::StrContains("_M_", callsign)) && k422 == 1
? 1
: k422 == 1 && absl::EndsWith(callsign, "_SUP") ? 1
: 0;
k422 = (u334 != OBS_FREQUENCY || absl::StrContains("_M_", callsign)) && k422 == 1 ? 1
: k422 == 1 && absl::EndsWith(callsign, "_SUP") ? 1
: 0;

auto cleanedFrequency = Helpers::CleanUpFrequency(u334);
if (UserSession::callsign == callsign && UserSession::frequency == cleanedFrequency
&& UserSession::isATC == (k422 == 1) && UserSession::lat == std::stod(lat)
&& UserSession::lon == std::stod(lon)) {
return true; // No changes
}

// Update the session info
UserSession::callsign = callsign;
UserSession::frequency = Helpers::CleanUpFrequency(u334);
UserSession::frequency = cleanedFrequency;
UserSession::isATC = k422 == 1;
UserSession::lat = std::stod(lat);
UserSession::lon = std::stod(lon);
TRACK_LOG_INFO(
"Updating session data - Callsign: {}, Frequency: {}, ATC: {}",
callsign, UserSession::frequency, k422);
TRACK_LOG_INFO("Updating session data - Callsign: {}, Frequency: {}, ATC: {}, Latitude: "
"{}, Longitude: {}",
callsign, UserSession::frequency, k422, UserSession::lat, UserSession::lon);
return true;
}

static void updateSessionStatus(std::string previousCallsign, bool isConnected)
{
if (UserSession::isConnectedToTheNetwork && UserSession::callsign != previousCallsign && !previousCallsign.empty() && isConnected && mClient->IsVoiceConnected()) {
TRACK_LOG_INFO(
"Callsign changed during an active session, "
"disconnecting ({} -> {})",
if (UserSession::isConnectedToTheNetwork && UserSession::callsign != previousCallsign
&& !previousCallsign.empty() && isConnected && mClient->IsVoiceConnected()) {
TRACK_LOG_INFO("Callsign changed during an active session, "
"disconnecting ({} -> {})",
previousCallsign, UserSession::callsign);
mClient->Disconnect();
Helpers::CallbackWithError("Callsign changed during an active session, "
Expand Down Expand Up @@ -199,12 +205,10 @@ class RemoteData {
UserSession::callsign = "";

if (callbackAvailable) {
callbackRef.NonBlockingCall(
[&](Napi::Env env, Napi::Function jsCallback) {
jsCallback.Call({ Napi::String::New(env, "network-disconnected"),
Napi::String::New(env, ""),
Napi::String::New(env, "") });
});
callbackRef.NonBlockingCall([&](Napi::Env env, Napi::Function jsCallback) {
jsCallback.Call({ Napi::String::New(env, "network-disconnected"),
Napi::String::New(env, ""), Napi::String::New(env, "") });
});
}
}
}
Expand All @@ -231,12 +235,12 @@ class RemoteData {
return;
}

callbackRef.NonBlockingCall(
[&](Napi::Env env, Napi::Function jsCallback) {
jsCallback.Call({ Napi::String::New(env, "error"),
Napi::String::New(env, "Slurper is back online. You can now connect to the network."),
Napi::String::New(env, "") });
});
callbackRef.NonBlockingCall([&](Napi::Env env, Napi::Function jsCallback) {
jsCallback.Call({ Napi::String::New(env, "error"),
Napi::String::New(
env, "Slurper is back online. You can now connect to the network."),
Napi::String::New(env, "") });
});
}

void notifyUserOfSlurperUnavalability()
Expand All @@ -253,11 +257,15 @@ class RemoteData {
userHasBeenNotifiedOfSlurperUnavailability = true;
}

callbackRef.NonBlockingCall(
[&](Napi::Env env, Napi::Function jsCallback) {
jsCallback.Call({ Napi::String::New(env, "error"),
Napi::String::New(env, "Error while parsing slurper data, check the log file. This means your internet may be down or the VATSIM servers may experience an outage. You will not be able to connect until this is resolved. TrackAudio will keep retrying in the background."),
Napi::String::New(env, "") });
});
callbackRef.NonBlockingCall([&](Napi::Env env, Napi::Function jsCallback) {
jsCallback.Call({ Napi::String::New(env, "error"),
Napi::String::New(env,
"Error while parsing slurper data, check the log file. This means your "
"internet may be down or the VATSIM servers may "
"experience an outage. You will not be able to connect until this is resolved. "
"TrackAudio will keep retrying in the "
"background."),
Napi::String::New(env, "") });
});
};
};
17 changes: 10 additions & 7 deletions backend/src/Shared.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@
#include <string>
#include <vector>

#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, ...) \
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__)

constexpr semver::version VERSION = semver::version { 1, 0, 2, semver::prerelease::beta, 2 };

Expand All @@ -34,9 +38,8 @@ static std::unique_ptr<afv_native::api::atcClient> mClient = nullptr;

#define API_SERVER_PORT 49080

const std::vector<std::string> allowedYx = { "_CTR", "_APP", "_TWR", "_GND",
"_DEP", "_DEL", "_FSS", "_SUP",
"_RDO", "_RMP", "_TMU", "_FMP" };
const std::vector<std::string> allowedYx = { "_CTR", "_APP", "_TWR", "_GND", "_DEP", "_DEL", "_FSS",
"_SUP", "_RDO", "_RMP", "_TMU", "_FMP" };

namespace UserSession {
static std::string cid;
Expand Down
Loading

0 comments on commit d988257

Please sign in to comment.