From a2c1f8362980c43afb2b28aae33b69663309ab29 Mon Sep 17 00:00:00 2001 From: Jason Watkins Date: Mon, 11 May 2015 14:51:08 -0700 Subject: [PATCH] Refactored logging. - Logging levels are now named preprocessor defines - Logging level is now a parameter for WriteLine/FormatLine --- xpcPlugin/DataManager.cpp | 274 ++++++++++---------------- xpcPlugin/Log.cpp | 27 +-- xpcPlugin/Log.h | 46 ++--- xpcPlugin/Message.cpp | 52 +++-- xpcPlugin/MessageHandlers.cpp | 157 +++++---------- xpcPlugin/UDPSocket.cpp | 40 ++-- xpcPlugin/XPCPlugin.cpp | 20 +- xpcPlugin/xpcPlugin/xpcPlugin.vcxproj | 2 +- 8 files changed, 239 insertions(+), 379 deletions(-) diff --git a/xpcPlugin/DataManager.cpp b/xpcPlugin/DataManager.cpp index 435096f..980d1da 100644 --- a/xpcPlugin/DataManager.cpp +++ b/xpcPlugin/DataManager.cpp @@ -29,8 +29,9 @@ namespace XPC { using namespace std; + const size_t PLANE_COUNT = 20; static map drefs; - static map mdrefs[20]; + static map mdrefs[PLANE_COUNT]; static map sdrefs; DREF XPData[134][8] = { DREF_None }; @@ -143,7 +144,7 @@ namespace XPC drefs.insert(make_pair(DREF_MP7Alt, XPLMFindDataRef("sim/multiplayer/position/plane7_el"))); char multi[256]; - for (int i = 1; i < 20; i++) + for (int i = 1; i < PLANE_COUNT; i++) { sprintf(multi, "sim/multiplayer/position/plane%i_x", i); mdrefs[i][DREF_LocalX] = XPLMFindDataRef(multi); @@ -302,6 +303,7 @@ namespace XPC int DataManager::Get(const string& dref, float values[], int size) { + Log::WriteLine(LOG_TRACE, "DMAN", "Entered Get(string, float*, int)"); XPLMDataRef& xdref = sdrefs[dref]; if (xdref == NULL) { @@ -309,24 +311,18 @@ namespace XPC } if (!xdref) // DREF does not exist { -#if LOG_VERBOSITY > 0 - Log::FormatLine("[DMAN] ERROR: invalid DREF %s", dref.c_str()); -#endif + Log::FormatLine(LOG_ERROR, "DMAN", "ERROR: invalid DREF %s", dref); return 0; } XPLMDataTypeID dataType = XPLMGetDataRefTypes(xdref); -#if LOG_VERBOSITY > 4 - Log::FormatLine("[DMAN] Get DREF %s (x:%X) Type: %i", dref.c_str(), xdref, dataType); -#endif + Log::FormatLine(LOG_INFO, "DMAN", "Get DREF %s (x:%X) Type: %i", dref, xdref, dataType); // XPLMDataTypeID is a bit flag, so it may contain more than one of the // following types. We prefer types as close to float as possible. if ((dataType & 2) == 2) // Float { values[0] = XPLMGetDataf(xdref); -#if LOG_VERBOSITY > 4 - Log::FormatLine("[DMAN] -- value was %f", values[0]); -#endif + Log::FormatLine(LOG_INFO, "DMAN", " -- value was %f", values[0]); return 1; } if ((dataType & 8) == 8) // Float array @@ -334,99 +330,80 @@ namespace XPC int drefSize = XPLMGetDatavf(xdref, NULL, 0, 0); if (drefSize > size) { -#if LOG_VERBOSITY > 1 - Log::WriteLine("[DMAN] WARN: dref size is larger than available space"); - Log::FormatLine(" Actual dref size: %i, Available size: %i", drefSize, size); -#endif + Log::WriteLine(LOG_WARN, "DMAN", "Warning: dref size is larger than available space"); + Log::FormatLine(LOG_DEBUG, "DMAN", "Actual dref size : %i, Available size : %i", drefSize, size); drefSize = size; } XPLMGetDatavf(xdref, values, 0, drefSize); -#if LOG_VERBOSITY > 4 - Log::FormatLine("[DMAN] -- value count was %i", drefSize); -#endif + Log::FormatLine(LOG_INFO, "DMAN", " -- value count was %i", drefSize); return drefSize; } if ((dataType & 4) == 4) // Double { values[0] = (float)XPLMGetDatad(xdref); -#if LOG_VERBOSITY > 4 - Log::FormatLine("[DMAN] -- value was %f", values[0]); -#endif + Log::FormatLine(LOG_INFO, "DMAN", " -- value was %f", values[0]); return 1; } if ((dataType & 1) == 1) // Integer { - values[0] = (float)XPLMGetDatai(xdref); -#if LOG_VERBOSITY > 4 - Log::FormatLine("[DMAN] -- value was %i", (int)values[0]); -#endif + int iValue = XPLMGetDatai(xdref); + values[0] = (float)iValue; + Log::FormatLine(LOG_INFO, "DMAN", " -- Real value was %i, cast to %f", iValue, values[0]); return 1; } if ((dataType & 16) == 16) // Integer array { - int iValues[200]; + const std::size_t TMP_SIZE = 200; + int iValues[TMP_SIZE]; int drefSize = XPLMGetDatavi(xdref, NULL, 0, 0); if (drefSize > size) { -#if LOG_VERBOSITY > 1 - Log::WriteLine("[DMAN] WARN: dref size is larger than available space"); - Log::FormatLine(" Actual dref size: %i, Available size: %i", drefSize, size); -#endif + Log::WriteLine(LOG_WARN, "DMAN", "Warning: dref size is larger than available space"); + Log::FormatLine(LOG_DEBUG, "DMAN", "Actual dref size : %i, Available size : %i", drefSize, size); drefSize = size; } - if (drefSize > 200) + if (drefSize > TMP_SIZE) { -#if LOG_VERBOSITY > 1 - Log::WriteLine("[DMAN] WARN: dref size is larger than temp buffer"); - Log::FormatLine(" Actual dref size: %i, Temp buffer size: 200", drefSize); -#endif - drefSize = 200; + Log::WriteLine(LOG_WARN, "DMAN", "Warning: dref size is larger than temp buffer"); + Log::FormatLine(LOG_DEBUG, "DMAN", "Actual dref size : %i, Temp buffer size: %u", drefSize, TMP_SIZE); + drefSize = TMP_SIZE; } XPLMGetDatavi(xdref, iValues, 0, drefSize); for (int i = 0; i < drefSize; ++i) { values[i] = (float)iValues[i]; } -#if LOG_VERBOSITY > 4 - Log::FormatLine("[DMAN] -- value count was %i", drefSize); -#endif + Log::FormatLine(LOG_INFO, "DMAN", " -- value count was %i", drefSize); return drefSize; } if ((dataType & 32) == 32) // Byte array { - char bValues[1024]; + const std::size_t TMP_SIZE = 1024; + char bValues[TMP_SIZE]; int drefSize = XPLMGetDatab(xdref, NULL, 0, 0); if (drefSize > size) { -#if LOG_VERBOSITY > 1 - Log::WriteLine("[DMAN] WARN: dref size is larger than available space"); - Log::FormatLine(" Actual dref size: %i, Available size: %i", drefSize, size); -#endif + Log::WriteLine(LOG_WARN, "DMAN", "Warning: dref size is larger than available space"); + Log::FormatLine(LOG_DEBUG, "DMAN", "Actual dref size : %i, Available size : %i", drefSize, size); drefSize = size; } - if (drefSize > 1024) + if (drefSize > TMP_SIZE) { -#if LOG_VERBOSITY > 1 - Log::WriteLine("[DMAN] WARN: dref size is larger than temp buffer"); - Log::FormatLine(" Actual dref size: %i, Temp buffer size: 1024", drefSize); -#endif - drefSize = 1024; + Log::WriteLine(LOG_WARN, "DMAN", "Warning: dref size is larger than temp buffer"); + Log::FormatLine(LOG_DEBUG, "DMAN", "Actual dref size : %i, Temp buffer size: %u", drefSize, TMP_SIZE); + drefSize = TMP_SIZE; } XPLMGetDatab(xdref, bValues, 0, drefSize); for (int i = 0; i < drefSize; ++i) { values[i] = (float)bValues[i]; } -#if LOG_VERBOSITY > 4 - Log::FormatLine("[DMAN] -- value count was %i", drefSize); -#endif + Log::FormatLine(LOG_INFO, "DMAN", " -- value count was %i", drefSize); return drefSize; } // No match -#if LOG_VERBOSITY > 0 - Log::WriteLine("[DMAN] ERROR: Unrecognized data type."); -#endif + Log::WriteLine(LOG_ERROR, "DMAN", "ERROR: Unrecognized data type."); return 0; } @@ -434,9 +411,8 @@ namespace XPC { const XPLMDataRef& xdref = aircraft == 0 ? drefs[dref] : mdrefs[aircraft][dref]; double value = XPLMGetDatad(xdref); -#if LOG_VERBOSITY > 4 - Log::FormatLine("[DMAN] Get DREF %i (x:%X) result %f for a/c %i", dref, xdref, value, aircraft); -#endif + Log::FormatLine(LOG_INFO, "DMAN", "Get DREF %i (x:%X) result %f for a/c %i", + dref, xdref, value, aircraft); return value; } @@ -444,9 +420,8 @@ namespace XPC { const XPLMDataRef& xdref = aircraft == 0 ? drefs[dref] : mdrefs[aircraft][dref]; float value = XPLMGetDataf(xdref); -#if LOG_VERBOSITY > 4 - Log::FormatLine("[DMAN] Get DREF %i (x:%X) result %f for a/c %i", dref, xdref, value, aircraft); -#endif + Log::FormatLine(LOG_INFO, "DMAN", "Get DREF %i (x:%X) result %f for a/c %i", + dref, xdref, value, aircraft); return value; } @@ -454,9 +429,8 @@ namespace XPC { const XPLMDataRef& xdref = aircraft == 0 ? drefs[dref] : mdrefs[aircraft][dref]; int value = XPLMGetDatai(xdref); -#if LOG_VERBOSITY > 4 - Log::FormatLine("[DMAN] Get DREF %i (x:%X) result %i for a/c %i", dref, xdref, value, aircraft); -#endif + Log::FormatLine(LOG_INFO, "DMAN", "Get DREF %i (x:%X) result %i for a/c %i", + dref, xdref, value, aircraft); return value; } @@ -464,9 +438,8 @@ namespace XPC { const XPLMDataRef& xdref = aircraft == 0 ? drefs[dref] : mdrefs[aircraft][dref]; int resultSize = XPLMGetDatavf(xdref, values, 0, size); -#if LOG_VERBOSITY > 4 - Log::FormatLine("[DMAN] Get DREF %i (x:%X) result size %i for a/c %i", dref, xdref, resultSize, aircraft); -#endif + Log::FormatLine(LOG_INFO, "DMAN", "Get DREF %i (x:%X) result size %i for a/c %i", + dref, xdref, resultSize, aircraft); return resultSize; } @@ -474,46 +447,46 @@ namespace XPC { const XPLMDataRef& xdref = aircraft == 0 ? drefs[dref] : mdrefs[aircraft][dref]; int resultSize = XPLMGetDatavi(xdref, values, 0, size); -#if LOG_VERBOSITY > 4 - Log::FormatLine("[DMAN] Get DREF %i (x:%X) result size %i for a/c %i", dref, xdref, resultSize, aircraft); -#endif + Log::FormatLine(LOG_INFO, "DMAN", "Get DREF %i (x:%X) result size %i for a/c %i", + dref, xdref, resultSize, aircraft); return resultSize; } void DataManager::Set(DREF dref, double value, char aircraft) { const XPLMDataRef& xdref = aircraft == 0 ? drefs[dref] : mdrefs[aircraft][dref]; -#if LOG_VERBOSITY > 4 - Log::FormatLine("[DMAN] Setting DREF %i (x:%X) to %f for a/c %i", dref, xdref, value, aircraft); -#endif + Log::FormatLine(LOG_INFO, "DMAN", "Setting DREF %i (x:%X) to %f for a/c %i", + dref, xdref, value, aircraft); XPLMSetDatad(xdref, value); } void DataManager::Set(DREF dref, float value, char aircraft) { const XPLMDataRef& xdref = aircraft == 0 ? drefs[dref] : mdrefs[aircraft][dref]; -#if LOG_VERBOSITY > 4 - Log::FormatLine("[DMAN] Set DREF %i (x:%X) to %f for a/c %i", dref, xdref, value, aircraft); -#endif + Log::FormatLine(LOG_INFO, "DMAN", "Setting DREF %i (x:%X) to %f for a/c %i", + dref, xdref, value, aircraft); XPLMSetDataf(xdref, value); } void DataManager::Set(DREF dref, int value, char aircraft) { const XPLMDataRef& xdref = aircraft == 0 ? drefs[dref] : mdrefs[aircraft][dref]; -#if LOG_VERBOSITY > 4 - Log::FormatLine("[DMAN] Set DREF %i (x:%X) to %i for a/c %i", dref, xdref, value, aircraft); -#endif + Log::FormatLine(LOG_INFO, "DMAN", "Setting DREF %i (x:%X) to %i for a/c %i", + dref, xdref, value, aircraft); XPLMSetDatai(xdref, value); } void DataManager::Set(DREF dref, float values[], int size, char aircraft) { const XPLMDataRef& xdref = aircraft == 0 ? drefs[dref] : mdrefs[aircraft][dref]; -#if LOG_VERBOSITY > 4 - Log::FormatLine("[DMAN] Set DREF %i (x:%X) (%i values) for a/c %i", dref, xdref, size, aircraft); -#endif + Log::FormatLine(LOG_INFO, "DMAN", "Setting DREF %i (x:%X) (%i values) for a/c %i", + dref, xdref, size, aircraft); int drefSize = XPLMGetDatavf(xdref, NULL, 0, 0); + if (drefSize < size) + { + Log::FormatLine(LOG_WARN, "DMAN", "Warning: Too many values when setting DREF %i. Expected %i, got %i", + dref, drefSize, size); + } drefSize = min(drefSize, size); XPLMSetDatavf(xdref, values, 0, drefSize); } @@ -521,10 +494,14 @@ namespace XPC void DataManager::Set(DREF dref, int values[], int size, char aircraft) { const XPLMDataRef& xdref = aircraft == 0 ? drefs[dref] : mdrefs[aircraft][dref]; -#if LOG_VERBOSITY > 4 - Log::FormatLine("[DMAN] Setting DREF %i (x:%X) (%i values) for a/c %i", dref, xdref, size, aircraft); -#endif + Log::FormatLine(LOG_INFO, "DMAN", "Setting DREF %i (x:%X) (%i values) for a/c %i", + dref, xdref, size, aircraft); int drefSize = XPLMGetDatavi(xdref, NULL, 0, 0); + if (drefSize < size) + { + Log::FormatLine(LOG_WARN, "DMAN", "Warning: Too many values when setting DREF %i. Expected %i, got %i", + dref, drefSize, size); + } drefSize = min(drefSize, size); XPLMSetDatavi(xdref, values, 0, drefSize); } @@ -539,139 +516,113 @@ namespace XPC if (!xdref) { // DREF does not exist -#if LOG_VERBOSITY > 0 - Log::FormatLine("[DMAN] ERROR: invalid DREF %s", dref.c_str()); -#endif + Log::FormatLine(LOG_ERROR, "DMAN", "ERROR: invalid DREF %s", dref); return; } if (isnan(values[0])) { -#if LOG_VERBOSITY > 0 - Log::WriteLine("[DMAN] ERROR: Value must be a number (NaN received)"); -#endif + Log::WriteLine(LOG_ERROR, "DMAN", "ERROR: Value must be a number (NaN received)"); return; } XPLMDataTypeID dataType = XPLMGetDataRefTypes(xdref); -#if LOG_VERBOSITY > 4 - Log::FormatLine("[DMAN] Setting DREF %s (x:%X) Type: %i", dref.c_str(), xdref, dataType); -#endif + Log::FormatLine(LOG_INFO, "DMAN", "Setting DREF %s (x:%X) Type: %i", xdref, dataType); if ((dataType & 2) == 2) // Float { XPLMSetDataf(xdref, values[0]); -#if LOG_VERBOSITY > 4 - Log::FormatLine("[DMAN] -- value was %f", values[0]); -#endif + Log::FormatLine(LOG_INFO, "DMAN", " -- value was %f", values[0]); } else if ((dataType & 8) == 8) // Float Array { int drefSize = XPLMGetDatavf(xdref, NULL, 0, 0); -#if LOG_VERBOSITY > 1 if (size > drefSize) { - Log::WriteLine("[DMAN] WARN: Provided size is larger than actual dref size"); - Log::FormatLine(" Actual dref size: %i, Provided buffer size: %i", drefSize, size); + Log::WriteLine(LOG_WARN, "DMAN", "Warning: dref size is larger than actual dref size"); + Log::FormatLine(LOG_DEBUG, "DMAN", "Actual dref size : %i, Provided buffer size : %i", + drefSize, size); } -#endif drefSize = min(drefSize, size); XPLMSetDatavf(xdref, values, 0, drefSize); -#if LOG_VERBOSITY > 4 - Log::FormatLine("[DMAN] -- value count was %i", drefSize); -#endif + Log::FormatLine(LOG_INFO, "DMAN", " -- value count was %i", drefSize); } else if ((dataType & 4) == 4) // Double { XPLMSetDatad(xdref, values[0]); -#if LOG_VERBOSITY > 4 - Log::FormatLine("[DMAN] -- value was %f", values[0]); -#endif + Log::FormatLine(LOG_INFO, "DMAN", " -- value was %f", values[0]); } else if ((dataType & 1) == 1) // Integer { XPLMSetDatai(xdref, (int)values[0]); -#if LOG_VERBOSITY > 4 - Log::FormatLine("[DMAN] -- value was %i", (int)values[0]); -#endif + Log::FormatLine(LOG_INFO, "DMAN", " -- value was %i", (int)values[0]); } else if ((dataType & 16) == 16) // Integer Array { - int iValues[200]; + const std::size_t TMP_SIZE = 200; + int iValues[TMP_SIZE]; int drefSize = XPLMGetDatavi(xdref, NULL, 0, 0); -#if LOG_VERBOSITY > 1 if (size > drefSize) { - Log::WriteLine("[DMAN] WARN: Provided size is larger than actual dref size"); - Log::FormatLine(" Actual dref size: %i, Provided buffer size: %i", drefSize, size); + Log::WriteLine(LOG_WARN, "DMAN", "Warning: dref size is larger than actual dref size"); + Log::FormatLine(LOG_DEBUG, "DMAN", "Actual dref size : %i, Provided buffer size : %i", + drefSize, size); } -#endif drefSize = min(drefSize, size); - if (drefSize > 200) + if (drefSize > TMP_SIZE) { -#if LOG_VERBOSITY > 1 - Log::WriteLine("[DMAN] WARN: drefSize larger than temp buffer size."); - Log::FormatLine(" Actual dref size: %i, Temp buffer size: 200", drefSize); -#endif - drefSize = 200; + Log::WriteLine(LOG_WARN, "DMAN", "Warning: dref size is larger temp buffer size"); + Log::FormatLine(LOG_DEBUG, "DMAN", "Actual dref size : %i, Temp buffer size: %i", + drefSize, TMP_SIZE); + drefSize = TMP_SIZE; } for (int i = 0; i < drefSize; ++i) { iValues[i] = (int)values[i]; } XPLMSetDatavi(xdref, iValues, 0, drefSize); -#if LOG_VERBOSITY > 4 - Log::FormatLine("[DMAN] -- value count was %i", drefSize); -#endif + Log::FormatLine(LOG_INFO, "DMAN", " -- value count was %i", drefSize); } else if ((dataType & 32) == 32) // Byte Array { - char bValues[1024]; + const std::size_t TMP_SIZE = 1024; + char bValues[TMP_SIZE]; int drefSize = XPLMGetDatab(xdref, NULL, 0, 0); -#if LOG_VERBOSITY > 1 if (size > drefSize) { - Log::WriteLine("[DMAN] WARN: Provided size is larger than actual dref size"); - Log::FormatLine(" Actual dref size: %i, Provided buffer size: %i", drefSize, size); + Log::WriteLine(LOG_WARN, "DMAN", "Warning: dref size is larger than actual dref size"); + Log::FormatLine(LOG_DEBUG, "DMAN", "Actual dref size : %i, Provided buffer size : %i", + drefSize, size); } -#endif drefSize = min(drefSize, size); - if (drefSize > 1024) + if (drefSize > TMP_SIZE) { -#if LOG_VERBOSITY > 1 - Log::WriteLine("[DMAN] WARN: drefSize larger than temp buffer size."); - Log::FormatLine(" Actual dref size: %i, Temp buffer size: 1024", drefSize); -#endif - drefSize = 1024; + Log::WriteLine(LOG_WARN, "DMAN", "Warning: dref size is larger temp buffer size"); + Log::FormatLine(LOG_DEBUG, "DMAN", "Actual dref size : %i, Temp buffer size: %i", + drefSize, TMP_SIZE); + drefSize = TMP_SIZE; } for (int i = 0; i < drefSize; ++i) { bValues[i] = (char)values[i]; } XPLMSetDatab(xdref, bValues, 0, drefSize); -#if LOG_VERBOSITY > 4 - Log::FormatLine("[DMAN] -- value count was %i", drefSize); -#endif + Log::FormatLine(LOG_INFO, "DMAN", " -- value count was %i", drefSize); } else { -#if LOG_VERBOSITY > 0 - Log::WriteLine("[DMAN] ERROR: Unknown type."); -#endif + Log::WriteLine(LOG_ERROR, "DMAN", "ERROR: Unknown type."); } -#if LOG_VERBOSITY > 1 if (!XPLMCanWriteDataRef(xdref)) { - Log::WriteLine("[DMAN] WARN: dref is not writable. The write operation probably failed."); + Log::WriteLine(LOG_WARN, "DMAN", "WARN: dref is not writable. The write operation probably failed."); } -#endif } void DataManager::SetGear(float gear, bool immediate, char aircraft) { -#if LOG_VERBOSITY > 3 - Log::FormatLine("[DMAN] Setting gear (value:%f, immediate:%i) for aircraft %i", gear, immediate, aircraft); -#endif + Log::FormatLine(LOG_INFO, "DMAN", "Setting gear (value:%f, immediate:%i) for aircraft %i", + gear, immediate, aircraft); if ((gear < -8.5 && gear > -9.5) || (gear < -997.9 && gear > -999.1)) { @@ -679,9 +630,7 @@ namespace XPC } if (isnan(gear) || gear < 0 || gear > 1) { -#if LOG_VERBOSITY > 1 - Log::WriteLine("[GEAR] ERROR: Value must be 0 or 1"); -#endif + Log::WriteLine(LOG_ERROR, "DMAN", "ERROR: Gear value must be 0 or 1"); return; } @@ -707,14 +656,11 @@ namespace XPC void DataManager::SetPosition(float pos[3], char aircraft) { -#if LOG_VERBOSITY > 3 - Log::FormatLine("[DMAN] Setting position (%f, %f, %f) for aircraft %i", pos[0], pos[1], pos[2], aircraft); -#endif + Log::FormatLine(LOG_INFO, "DMAN", "Setting position (%f, %f, %f) for aircraft %i", + pos[0], pos[1], pos[2], aircraft); if (isnan(pos[0] + pos[1] + pos[2])) { -#if LOG_VERBOSITY > 0 - Log::WriteLine("[DMAN] ERROR: Position must be a number (NaN received)"); -#endif + Log::WriteLine(LOG_ERROR, "DMAN", "ERROR: Position must be a number (NaN received)"); return; } @@ -746,15 +692,11 @@ namespace XPC void DataManager::SetOrientation(float orient[3], char aircraft) { -#if LOG_VERBOSITY > 3 - Log::FormatLine("[DMAN] Setting orientation (%f, %f, %f) for aircraft %i", + Log::FormatLine(LOG_INFO, "DMAN", "Setting orientation (%f, %f, %f) for aircraft %i", orient[0], orient[1], orient[2], aircraft); -#endif if (isnan(orient[0] + orient[1] + orient[2])) { -#if LOG_VERBOSITY > 0 - Log::WriteLine("[DMAN] ERROR: Orientation must be a number (NaN received)"); -#endif + Log::WriteLine(LOG_ERROR, "DMAN", "ERROR: Orientation must be a number (NaN received)"); return; } @@ -801,11 +743,11 @@ namespace XPC void DataManager::SetFlaps(float value) { + Log::FormatLine(LOG_INFO, "DMAN", "Setting flaps (value:%f)", value); + if (isnan(value)) { -#if LOG_VERBOSITY > 0 - Log::WriteLine("[DMAN] ERROR: Flap value must be a number (NaN received)"); -#endif + Log::WriteLine(LOG_ERROR, "DMAN", "ERROR: Flap value must be a number (NaN received)"); return; } if (value < -997.9 && value > -999.1) diff --git a/xpcPlugin/Log.cpp b/xpcPlugin/Log.cpp index 2266e99..8491402 100644 --- a/xpcPlugin/Log.cpp +++ b/xpcPlugin/Log.cpp @@ -12,7 +12,7 @@ #include #include -// Implementation note: I initial wrote this class using C++ iostreams, but I couldn't find any +// Implementation note: I initially wrote this class using C++ iostreams, but I couldn't find any // way to implement FormatLine without adding in a call to sprintf. It therefore seems more // efficient to me to just use C-style IO and call std::fprintf directly. namespace XPC @@ -38,8 +38,13 @@ namespace XPC std::fprintf(fd, ss.str().c_str()); } - void Log::Initialize(std::string version) + void Log::Initialize(const std::string& version) { + if (LOG_LEVEL == LOG_OFF) + { + return; + } + fd = std::fopen("XPCLog.txt", "w"); if (fd != NULL) { @@ -82,35 +87,31 @@ namespace XPC } } - void Log::WriteLine(const std::string& value) + void Log::WriteLine(int level, const std::string& tag, const std::string& value) { - Log::WriteLine(value.c_str()); - } - - void Log::WriteLine(const char* value) - { - if (!fd) + if (level > LOG_LEVEL || !fd) { return; } WriteTime(fd); - std::fprintf(fd, "%s\n", value); + std::fprintf(fd, "[%s] %s\n", tag.c_str(), value.c_str()); std::fflush(fd); } - void Log::FormatLine(const char* format, ...) + void Log::FormatLine(int level, const std::string& tag, const std::string& format, ...) { va_list args; - if (!fd) + if (level > LOG_LEVEL || !fd) { return; } va_start(args, format); WriteTime(fd); - std::vfprintf(fd, format, args); + std::fprintf(fd, "[%s] ", tag.c_str()); + std::vfprintf(fd, format.c_str(), args); std::fprintf(fd, "\n"); std::fflush(fd); diff --git a/xpcPlugin/Log.h b/xpcPlugin/Log.h index 6887eb9..094d996 100644 --- a/xpcPlugin/Log.h +++ b/xpcPlugin/Log.h @@ -5,18 +5,26 @@ #include // LOG_VERBOSITY determines the level of logging throughout the plugin. -// 0: Minimum logging. Only plugin manager events will be logged. -// 1: Critical errors. When an error that prevents correct operation of the -// plugin, attempt to write useful information to the log. Note that since -// XPC runs inside the X-Plane executable, we try very hard no to crash. -// As a result, these log messages may be the only indication of failure. -// 2: All errors. Any time something unexpected happens, log it. -// 3: Significant actions. Any time something happens outside of normal -// command processing, log it. Will also log when commands are received. -// 4: Detailed actions. Log aditional information as commands are processed. -// 5: Everything. Log nearly every single action the plugin takes. This may -// have a detrimental impact on X-Plane performance. -#define LOG_VERBOSITY 3 +// OFF: No logging at all will be performed. +// FATAL: Critical errors that would normally resulti in termination of the program. Because XPC +// operates in the X-Plane process, we try to never actually crash. As a result, we this +// level of logging may be the only indication of a problem. +// ERROR: All errors not covered by FATAL +// WARN: Potentially, but not definitely, incorrect behavior +// INFO: Information about normal actions taken by the plugin. +// DEBUG: More verbose information usefull for debugging. +// TRACE: Log all the things! +#define LOG_OFF 0 +#define LOG_FATAL 1 +#define LOG_ERROR 2 +#define LOG_WARN 3 +#define LOG_INFO 4 +#define LOG_DEBUG 5 +#define LOG_TRACE 6 + +#ifndef LOG_LEVEL +#define LOG_LEVEL LOG_ERROR +#endif namespace XPC { @@ -33,31 +41,25 @@ namespace XPC public: /// Initializes the logging component by deleting old log files, /// writing header information to the log file. - static void Initialize(std::string header); + static void Initialize(const std::string& header); /// Closes the log file. static void Close(); - /// Writes the C string pointed to by format, followed by a line + /// Writes the string pointed to by format, followed by a line /// terminator to the XPC log file. If format contains format /// specifiers, additional arguments following format will be formatted /// and inserted in the resulting string, replacing their respective /// specifiers. /// /// \param format The format string appropriate for consumption by sprintf. - static void FormatLine(const char* format, ...); + static void FormatLine(int level, const std::string& tag, const std::string& format, ...); /// Writes the specified string value, followed by a line terminator /// to the XPC log file. /// /// \param value The value to write. - static void WriteLine(const std::string& value); - - /// Writes the specified C string value, followed by a line terminator - /// to the XPC log file. - /// - /// \param value The value to write. - static void WriteLine(const char* value); + static void WriteLine(int level, const std::string& tag, const std::string& value); }; } #endif diff --git a/xpcPlugin/Message.cpp b/xpcPlugin/Message.cpp index 2f8f7e3..50d1246 100644 --- a/xpcPlugin/Message.cpp +++ b/xpcPlugin/Message.cpp @@ -58,9 +58,7 @@ namespace XPC void Message::PrintToLog() const { -#if LOG_VERBOSITY > 4 std::stringstream ss; - ss << "[DEBUG]"; // Dump raw bytes to string ss << std::hex << std::setfill('0'); @@ -68,18 +66,18 @@ namespace XPC { ss << ' ' << std::setw(2) << static_cast(buffer[i]); } - Log::WriteLine(ss.str()); + Log::WriteLine(LOG_DEBUG, "DBUG", ss.str()); ss << std::dec; ss.str(""); - ss << "[" << GetHead() << "-DEBUG] (" << GetSize() << ")"; + ss << "Head:" << GetHead() << " Size:" << GetSize(); switch (GetMagicNumber()) // Binary version of head { case 0x4E4EF443: // CONN case 0x54505957: // WYPT case 0x54584554: // TEXT { - Log::WriteLine(ss.str()); + Log::WriteLine(LOG_DEBUG, "DBUG", ss.str()); break; } case 0x4C525443: // CTRL @@ -98,7 +96,7 @@ namespace XPC } ss << " Attitude:(" << pitch << " " << roll << " " << yaw << ")"; ss << " Thr:" << thr << " Gear:" << (int)gear << " Flaps:" << flaps; - Log::WriteLine(ss.str()); + Log::WriteLine(LOG_DEBUG, "DBUG", ss.str()); break; } case 0x41544144: // DATA @@ -110,43 +108,44 @@ namespace XPC values[i][0] = buffer[5 + 36 * i]; std::memcpy(values[i] + 1, buffer + 9 + 36 * i, 9 * sizeof(float)); } - ss << " (" << numCols << " lines)"; - Log::WriteLine(ss.str()); + ss << " (" << numCols << " lines)"; + Log::WriteLine(LOG_DEBUG, "DBUG", ss.str()); for (int i = 0; i < numCols; ++i) { ss.str(""); - ss << "\t#" << values[i][0]; + ss << " #" << values[i][0]; for (int j = 1; j < 9; ++j) { ss << " " << values[i][j]; - } - Log::WriteLine(ss.str()); + } + Log::WriteLine(LOG_DEBUG, "DBUG", ss.str()); } break; } case 0x46455244: // DREF - { - Log::WriteLine(ss.str()); + { + Log::WriteLine(LOG_DEBUG, "DBUG", ss.str()); std::string dref((char*)buffer + 6, buffer[5]); - Log::FormatLine("-\tDREF (size %i) = %s", dref.length(), dref.c_str()); + Log::FormatLine(LOG_DEBUG, "DBUG", " DREF (size %i) = %s", dref.length(), dref); ss.str(""); int values = buffer[6 + buffer[5]]; - ss << "\tValues(size " << values << ") ="; + ss << " Values(size " << values << ") ="; for (int i = 0; i < values; ++i) { ss << " " << *((float*)(buffer + values + 1 + sizeof(float) * i)); - } - Log::WriteLine(ss.str()); + } + Log::WriteLine(LOG_DEBUG, "DBUG", ss.str()); break; } case 0x44544547: // GETD - { - Log::WriteLine(ss.str()); + { + Log::WriteLine(LOG_DEBUG, "DBUG", ss.str()); int cur = 6; for (int i = 0; i < buffer[5]; ++i) { std::string dref((char*)buffer + cur + 1, buffer[cur]); - Log::FormatLine("\t#%i/%i (size:%i) %s", i + 1, buffer[5], dref.length(), dref.c_str()); + Log::FormatLine(LOG_DEBUG, "DBUG", " #%i/%i (size:%i) %s", + i + 1, buffer[5], dref.length(), dref); cur += 1 + buffer[cur]; } break; @@ -162,23 +161,22 @@ namespace XPC ss << " AC:" << (int)aircraft; ss << " Pos:(" << pos[0] << ' ' << pos[1] << ' ' << pos[2] << ") Orient:("; ss << orient[3] << ' ' << orient[4] << ' ' << orient[5] << ") Gear:"; - ss << gear; - Log::WriteLine(ss.str()); + ss << gear; + Log::WriteLine(LOG_DEBUG, "DBUG", ss.str()); break; } case 0x554D4953: // SIMU { - ss << ' ' << (int)buffer[5]; - Log::WriteLine(ss.str()); + ss << ' ' << (int)buffer[5]; + Log::WriteLine(LOG_DEBUG, "DBUG", ss.str()); break; } default: { - ss << " UNKNOWN HEADER "; - Log::WriteLine(ss.str()); + ss << " UNKNOWN HEADER "; + Log::WriteLine(LOG_DEBUG, "DBUG", ss.str()); break; } } -#endif } } diff --git a/xpcPlugin/MessageHandlers.cpp b/xpcPlugin/MessageHandlers.cpp index b2dc625..f89c001 100644 --- a/xpcPlugin/MessageHandlers.cpp +++ b/xpcPlugin/MessageHandlers.cpp @@ -80,19 +80,14 @@ namespace XPC std::string head = msg.GetHead(); if (head == "") { -#if LOG_VERBOSITY > 1 - Log::WriteLine("[MSGH] Warning: HandleMessage called with empty message."); -#endif + Log::WriteLine(LOG_WARN, "MSGH", "Warning: HandleMessage called with empty message."); return; // No Message to handle } - msg.PrintToLog(); // Set current connection sockaddr sourceaddr = msg.GetSource(); connectionKey = UDPSocket::GetHost(&sourceaddr); -#if LOG_VERBOSITY > 4 - Log::FormatLine("[MSGH] Handling message from %s", connectionKey.c_str()); -#endif + Log::FormatLine(LOG_INFO, "MSGH", "Handling message from %s", connectionKey); std::map::iterator conn = connections.find(connectionKey); if (conn == connections.end()) // New connection { @@ -104,19 +99,17 @@ namespace XPC connection.addr = sourceaddr; connection.getdCount = 0; connections[connectionKey] = connection; -#if LOG_VERBOSITY > 2 - Log::FormatLine("[MSGH] New connection. ID=%u, Remote=%s", connection.id, connectionKey.c_str()); -#endif + Log::FormatLine(LOG_TRACE, "MSGH", "New connection. ID=%u, Remote=%s", + connection.id, connectionKey); } else { connection = (*conn).second; -#if LOG_VERBOSITY > 3 - Log::FormatLine("[MSGH] Existing connection. ID=%u, Remote=%s", - connection.id, connectionKey.c_str()); -#endif + Log::FormatLine(LOG_TRACE, "MSGH", "Existing connection. ID=%u, Remote=%s", + connection.id, connectionKey); } + msg.PrintToLog(); // Check if there is a handler for this message type. If so, execute // that handler. Otherwise, execute the unknown message handler. std::map::iterator iter = handlers.find(head); @@ -153,10 +146,8 @@ namespace XPC break; } default: -#if LOG_VERBOSITY > 0 - Log::WriteLine("[CONN] ERROR: Unknown address type."); + Log::WriteLine(LOG_ERROR, "CONN", "ERROR: Unknown address type."); return; -#endif } connections.erase(connectionKey); connectionKey = UDPSocket::GetHost(&connection.addr); @@ -167,10 +158,8 @@ namespace XPC response[5] = connection.id; // Update log -#if LOG_VERBOSITY > 1 - Log::FormatLine("[CONN] ID: %u New destination port: %u", + Log::FormatLine(LOG_TRACE, "CONN", "ID: %u New destination port: %u", connection.id, port); -#endif // Send response sock->SendTo(response, 6, &connection.addr); @@ -179,19 +168,16 @@ namespace XPC void MessageHandlers::HandleCtrl(const Message& msg) { // Update Log -#if LOG_VERBOSITY > 2 - Log::FormatLine("[CTRL] Message Received (Conn %i)", connection.id); -#endif + Log::FormatLine(LOG_TRACE, "CTRL", "Message Received (Conn %i)", connection.id); const unsigned char* buffer = msg.GetBuffer(); std::size_t size = msg.GetSize(); - //Legacy packets that don't specify an aircraft number should be 26 bytes long. - //Packets specifying an A/C num should be 27 bytes. + // Legacy packets that don't specify an aircraft number should be 26 bytes long. + // Packets specifying an A/C num should be 27 bytes. Packets specifying a speedbrake + // should be 31 bytes. if (size != 26 && size != 27 && size != 31) { -#if LOG_VERBOSITY > 0 - Log::FormatLine("[CTRL] ERROR: Unexpected message length (%i)", size); -#endif + Log::FormatLine(LOG_ERROR, "CTRL", "ERROR: Unexpected message length (%i)", size); return; } @@ -263,23 +249,17 @@ namespace XPC std::size_t numCols = (size - 5) / 36; if (numCols > 0) { -#if LOG_VERBOSITY > 1 - Log::FormatLine("[DATA] Message Received (Conn %i)", connection.id); -#endif + Log::FormatLine(LOG_TRACE, "DATA", "Message Received (Conn %i)", connection.id); } else { -#if LOG_VERBOSITY > 0 - Log::FormatLine("[DATA] WARNING: Empty data packet received (Conn %i)", connection.id); -#endif + Log::FormatLine(LOG_WARN, "DATA", "WARNING: Empty data packet received (Conn %i)", connection.id); return; } if (numCols > 134) // Error. Will overflow values { -#if LOG_VERBOSITY > 0 - Log::FormatLine("[DATA] ERROR: numCols to large."); -#endif + Log::FormatLine(LOG_ERROR, "DATA", "ERROR: numCols to large."); return; } float values[134][9]; @@ -299,9 +279,7 @@ namespace XPC unsigned char dataRef = (unsigned char)values[i][0]; if (dataRef >= 134) { -#if LOG_VERBOSITY > 0 - Log::FormatLine("[DATA] ERROR: DataRef # must be between 0 - 134 (Received: %hi)", (int)dataRef); -#endif + Log::FormatLine(LOG_ERROR, "DATA", "ERROR: DataRef # must be between 0 - 134 (Received: %hi)", (int)dataRef); continue; } @@ -315,9 +293,7 @@ namespace XPC float hpath = savedHPath != -998 ? savedHPath : DataManager::GetFloat(DREF_HPath); if (alpha != alpha || hpath != hpath) { -#if LOG_VERBOSITY > 0 - Log::WriteLine("[DATA] ERROR: Value must be a number (NaN received)"); -#endif + Log::WriteLine(LOG_ERROR, "DATA", "ERROR: Value must be a number (NaN received)"); break; } const float deg2rad = 0.0174532925F; @@ -347,9 +323,7 @@ namespace XPC { if (values[i][1] != values[i][1] || values[i][3] != values[i][3]) { -#if LOG_VERBOSITY > 0 - Log::WriteLine("[DATA] ERROR: Value must be a number (NaN received)"); -#endif + Log::WriteLine(LOG_ERROR, "DATA", "ERROR: Value must be a number (NaN received)"); break; } if (values[i][1] != -998) @@ -375,9 +349,7 @@ namespace XPC { if (values[i][1] != values[i][1]) { -#if LOG_VERBOSITY > 0 - Log::WriteLine("[DATA] ERROR: Value must be a number (NaN received)"); -#endif + Log::WriteLine(LOG_ERROR, "DATA", "ERROR: Value must be a number (NaN received)"); break; } float throttle[8]; @@ -394,9 +366,7 @@ namespace XPC memcpy(line, values[i] + 1, 8 * sizeof(float)); for (int j = 0; j < 8; ++j) { -#if LOG_VERBOSITY > 1 - Log::FormatLine("[DATA] Setting Dataref %i.%i to %f", dataRef, j, line[j]); -#endif + Log::FormatLine(LOG_ERROR, "DATA", "Setting Dataref %i.%i to %f", dataRef, j, line[j]); // TODO(jason-watkins): Why is this a special case? if (dataRef == 14 && j == 0) { @@ -465,24 +435,20 @@ namespace XPC unsigned char drefCount = buffer[5]; if (drefCount == 0) // Use last request { -#if LOG_VERBOSITY > 0 - Log::FormatLine("[GETD] DATA Requested: Repeat last request from connection %i (%i data refs)", + Log::FormatLine(LOG_TRACE, "GETD", + "DATA Requested: Repeat last request from connection %i (%i data refs)", connection.id, connection.getdCount); -#endif if (connection.getdCount == 0) // No previous request to use { -#if LOG_VERBOSITY > 1 - Log::FormatLine("[GETD] ERROR: No previous requests from connection %i.", connection.id); -#endif + Log::FormatLine(LOG_ERROR, "GETD", "ERROR: No previous requests from connection %i.", + connection.id); return; } } else // New request { -#if LOG_VERBOSITY > 0 - Log::FormatLine("[GETD] DATA Requested: New Request for connection %i (%i data refs)", + Log::FormatLine(LOG_TRACE, "GETD", "DATA Requested: New Request for connection %i (%i data refs)", connection.id, drefCount); -#endif std::size_t ptr = 6; for (int i = 0; i < drefCount; ++i) { @@ -512,17 +478,13 @@ namespace XPC void MessageHandlers::HandlePosi(const Message& msg) { // Update log -#if LOG_VERBOSITY > 0 - Log::FormatLine("[POSI] Message Received (Conn %i)", connection.id); -#endif + Log::FormatLine(LOG_TRACE, "POSI", "Message Received (Conn %i)", connection.id); const unsigned char* buffer = msg.GetBuffer(); const std::size_t size = msg.GetSize(); if (size < 34) { -#if LOG_VERBOSITY > 1 - Log::FormatLine("[POSI] ERROR: Unexpected size: %i (Expected at least 34)", size); -#endif + Log::FormatLine(LOG_ERROR, "POSI", "ERROR: Unexpected size: %i (Expected at least 34)", size); return; } @@ -556,17 +518,13 @@ namespace XPC void MessageHandlers::HandleSimu(const Message& msg) { // Update log -#if LOG_VERBOSITY > 1 - Log::FormatLine("[SIMU] Message Received (Conn %i)", connection.id); -#endif + Log::FormatLine(LOG_TRACE, "SIMU", "Message Received (Conn %i)", connection.id); char v = msg.GetBuffer()[5]; if (v < 0 || v > 2) { -#if LOG_VERBOSITY > 0 - Log::FormatLine("[SIMU] ERROR: Invalid argument: %i", v); + Log::FormatLine(LOG_ERROR, "SIMU", "ERROR: Invalid argument: %i", v); return; -#endif } int value[20]; @@ -589,28 +547,24 @@ namespace XPC // Set DREF DataManager::Set(DREF_Pause, value, 20); -#if LOG_VERBOSITY > 2 switch (v) { case 0: - Log::WriteLine("[SIMU] Simulation Resumed"); + Log::WriteLine(LOG_INFO, "SIMU", "Simulation resumed"); break; case 1: - Log::WriteLine("[SIMU] Simulation Paused"); + Log::WriteLine(LOG_INFO, "SIMU", "Simulation paused"); break; case 2: - Log::WriteLine("[SIMU] Simulation switched."); + Log::WriteLine(LOG_INFO, "SIMU", "Simulation switched"); break; } -#endif } void MessageHandlers::HandleText(const Message& msg) { // Update Log -#if LOG_VERBOSITY > 0 - Log::FormatLine("[TEXT] Message Received (Conn %i)", connection.id); -#endif + Log::FormatLine(LOG_TRACE, "TEXT", "Message Received (Conn %i)", connection.id); std::size_t len = msg.GetSize(); const unsigned char* buffer = msg.GetBuffer(); @@ -618,18 +572,14 @@ namespace XPC char text[256] = { 0 }; if (len < 14) { -#if LOG_VERBOSITY > 1 - Log::WriteLine("[TEXT] ERROR: Length less than 14 bytes"); -#endif + Log::WriteLine(LOG_ERROR, "TEXT", "ERROR: Length less than 14 bytes"); return; } size_t msgLen = (unsigned char)buffer[13]; if (msgLen == 0) { Drawing::ClearMessage(); -#if LOG_VERBOSITY > 2 - Log::WriteLine("[TEXT] Text cleared"); -#endif + Log::WriteLine(LOG_INFO, "TEXT", "[TEXT] Text cleared"); } else { @@ -637,25 +587,19 @@ namespace XPC int y = *((int*)(buffer + 9)); strncpy(text, (char*)buffer + 14, msgLen); Drawing::SetMessage(x, y, text); -#if LOG_VERBOSITY > 2 - Log::WriteLine("[TEXT] Text set"); -#endif + Log::WriteLine(LOG_INFO, "TEXT", "[TEXT] Text set"); } } void MessageHandlers::HandleView(const Message& msg) { // Update Log -#if LOG_VERBOSITY > 0 - Log::FormatLine("[VIEW] Message Received (Conn %i)", connection.id); -#endif + Log::FormatLine(LOG_TRACE, "VIEW", "Message Received(Conn %i)", connection.id); const std::size_t size = msg.GetSize(); if (size != 9) { -#if LOG_VERBOSITY > 1 - Log::FormatLine("[VIEW] Error: Unexpected length. Message was %d bytes, expected 9.", size); -#endif + Log::FormatLine(LOG_ERROR, "VIEW", "Error: Unexpected length. Message was %d bytes, expected 9.", size); return; } const unsigned char* buffer = msg.GetBuffer(); @@ -666,9 +610,7 @@ namespace XPC void MessageHandlers::HandleWypt(const Message& msg) { // Update Log -#if LOG_VERBOSITY > 0 - Log::FormatLine("[WYPT] Message Received (Conn %i)", connection.id); -#endif + Log::FormatLine(LOG_TRACE, "WYPT", "Message Received (Conn %i)", connection.id); // Parse data const unsigned char* buffer = msg.GetBuffer(); @@ -685,9 +627,7 @@ namespace XPC } // Perform operation -#if LOG_VERBOSITY > 2 - Log::FormatLine("[WYPT] Performing operation %i", op); -#endif + Log::FormatLine(LOG_INFO, "WYPT", "Performing operation %i", op); switch (op) { case 1: @@ -700,18 +640,14 @@ namespace XPC Drawing::ClearWaypoints(); break; default: -#if LOG_VERBOSITY > 1 - Log::FormatLine("[WYPT] ERROR: %i is not a valid operation.", op); -#endif + Log::FormatLine(LOG_ERROR, "WYPT", "ERROR: %i is not a valid operation.", op); break; } } void MessageHandlers::HandleXPlaneData(const Message& msg) { -#if LOG_VERBOSITY > 1 - Log::WriteLine("[MSGH] Sending raw data to X-Plane"); -#endif + Log::WriteLine(LOG_TRACE, "MSGH", "Sending raw data to X - Plane"); sockaddr_in loopback; loopback.sin_family = AF_INET; loopback.sin_addr.s_addr = htonl(INADDR_LOOPBACK); @@ -721,9 +657,6 @@ namespace XPC void MessageHandlers::HandleUnknown(const Message& msg) { - // UPDATE LOG -#if LOG_VERBOSITY > 0 - Log::FormatLine("[EXEC] ERROR: Unknown packet type %s", msg.GetHead().c_str()); -#endif + Log::FormatLine(LOG_ERROR, "MSGH", "ERROR: Unknown packet type %s", msg.GetHead().c_str()); } } diff --git a/xpcPlugin/UDPSocket.cpp b/xpcPlugin/UDPSocket.cpp index 802a06d..8b8dec9 100644 --- a/xpcPlugin/UDPSocket.cpp +++ b/xpcPlugin/UDPSocket.cpp @@ -8,11 +8,11 @@ namespace XPC { + const static std::string tag = "SOCK"; + UDPSocket::UDPSocket(unsigned short recvPort) { -#if LOG_VERBOSITY > 1 - Log::FormatLine("[SOCK] Opening socket (port:%d)", recvPort); -#endif + Log::FormatLine(LOG_TRACE, tag, "Opening socket (port:%d)", recvPort); // Setup Port struct sockaddr_in localAddr; localAddr.sin_family = AF_INET; @@ -25,18 +25,14 @@ namespace XPC int startResult = WSAStartup(MAKEWORD(2, 2), &wsa); if (startResult != 0) { -#if LOG_VERBOSITY > 0 - Log::FormatLine("[SOCK] ERROR: WSAStartup failed with error code %i.", startResult); -#endif + Log::FormatLine(LOG_FATAL, tag, "ERROR: WSAStartup failed with error code %i.", startResult); this->sock = ~0; return; } if ((this->sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == INVALID_SOCKET) { -#if LOG_VERBOSITY > 0 int err = WSAGetLastError(); - Log::FormatLine("[SOCK] ERROR: Failed to open socket. (Error code %i)", err); -#endif + Log::FormatLine(LOG_FATAL, tag, "ERROR: Failed to open socket. (Error code %i)", err); return; } #elif (__APPLE__ || __linux) @@ -52,10 +48,10 @@ namespace XPC if (bind(this->sock, (struct sockaddr*)&localAddr, sizeof(localAddr)) != 0) { #ifdef _WIN32 -#if LOG_VERBOSITY > 0 int err = WSAGetLastError(); - Log::FormatLine("[SOCK] ERROR: Failed to bind socket. (Error code %i)", err); -#endif + Log::FormatLine(LOG_FATAL, tag, "ERROR: Failed to bind socket. (Error code %i)", err); +#else + Log::WriteLine(LOG_FATAL, tag, "ERROR: Failed to bind socket."); #endif return; } @@ -67,10 +63,8 @@ namespace XPC DWORD msTimeOutWin = 1; // Minimum socket timeout in Windows is 1ms if(setsockopt(this->sock, SOL_SOCKET, SO_RCVTIMEO, (char *)&msTimeOutWin, sizeof(msTimeOutWin)) != 0) { -#if LOG_VERBOSITY > 1 int err = WSAGetLastError(); - Log::FormatLine("[SOCK] ERROR: Failed to set timeout. (Error code %i)", err); -#endif + Log::FormatLine(LOG_ERROR, tag, "ERROR: Failed to set timeout. (Error code %i)", err); } #else struct timeval tv; @@ -82,9 +76,7 @@ namespace XPC UDPSocket::~UDPSocket() { -#if LOG_VERBOSITY > 1 - Log::FormatLine("[SOCK] Closing socket (%d)", this->sock); -#endif + Log::FormatLine(LOG_TRACE, tag, "Closing socket (%d)", this->sock); #ifdef _WIN32 closesocket(this->sock); #elif (__APPLE__ || __linux) @@ -116,13 +108,11 @@ namespace XPC // Select Command int result = select(-1, &stReadFDS, (FD_SET *)0, &stExceptFDS, &tv); -#if LOG_VERBOSITY > 1 if (result == SOCKET_ERROR) { int err = WSAGetLastError(); - Log::FormatLine("[SOCK] ERROR: Select failed. (Error code %i)", err); + Log::FormatLine(LOG_ERROR, tag, "ERROR: Select failed. (Error code %i)", err); } -#endif if (result <= 0) // No Data or error { return -1; @@ -141,15 +131,11 @@ namespace XPC { if (sendto(sock, (char*)buffer, (int)len, 0, remote, sizeof(*remote)) < 0) { -#if LOG_VERBOSITY > 0 - Log::FormatLine("[SOCK] Send failed. (remote: %s)", GetHost(remote).c_str()); -#endif + Log::FormatLine(LOG_ERROR, tag, "Send failed. (remote: %s)", GetHost(remote).c_str()); } else { -#if LOG_VERBOSITY > 3 - Log::FormatLine("[SOCK] Datagram sent. (remote: %s)", GetHost(remote).c_str()); -#endif + Log::FormatLine(LOG_INFO, tag, "Send failed. (remote: %s)", GetHost(remote).c_str()); } } diff --git a/xpcPlugin/XPCPlugin.cpp b/xpcPlugin/XPCPlugin.cpp index 104e2d1..3b4f88f 100755 --- a/xpcPlugin/XPCPlugin.cpp +++ b/xpcPlugin/XPCPlugin.cpp @@ -106,7 +106,7 @@ PLUGIN_API int XPluginStart(char* outName, char* outSig, char* outDesc) } #endif XPC::Log::Initialize("1.0.1"); - XPC::Log::WriteLine("[EXEC] Plugin Start"); + XPC::Log::WriteLine(LOG_INFO, "EXEC", "Plugin Start"); XPC::DataManager::Initialize(); float interval = -1; // Call every frame @@ -119,7 +119,7 @@ PLUGIN_API int XPluginStart(char* outName, char* outSig, char* outDesc) PLUGIN_API void XPluginStop(void) { XPLMUnregisterFlightLoopCallback(XPCFlightLoopCallback, NULL); - XPC::Log::WriteLine("[EXEC] Plugin Shutdown"); + XPC::Log::WriteLine(LOG_INFO, "EXEC", "Plugin Shutdown"); XPC::Log::Close(); } @@ -135,7 +135,7 @@ PLUGIN_API void XPluginDisable(void) // Stop rendering waypoints to screen. XPC::Drawing::ClearWaypoints(); - XPC::Log::WriteLine("[EXEC] Plugin Disabled, sockets closed"); + XPC::Log::WriteLine(LOG_INFO, "EXEC", "Plugin Disabled, sockets closed"); } PLUGIN_API int XPluginEnable(void) @@ -144,14 +144,12 @@ PLUGIN_API int XPluginEnable(void) sock = new XPC::UDPSocket(RECVPORT); XPC::MessageHandlers::SetSocket(sock); - XPC::Log::WriteLine("[EXEC] Plugin Enabled, sockets opened"); + XPC::Log::WriteLine(LOG_INFO, "EXEC", "Plugin Enabled, sockets opened"); if (benchmarkingSwitch > 0) { - XPC::Log::FormatLine("[EXEC] Benchmarking Enabled (Verbosity: %i)", benchmarkingSwitch); + XPC::Log::FormatLine(LOG_INFO, "EXEC", "Benchmarking Enabled (Verbosity: %i)", benchmarkingSwitch); } -#if LOG_VERBOSITY > 0 - XPC::Log::FormatLine("[EXEC] Debug Logging Enabled (Verbosity: %i)", LOG_VERBOSITY); -#endif + XPC::Log::FormatLine(LOG_INFO, "EXEC", "Debug Logging Enabled (Verbosity: %i)", LOG_LEVEL); return 1; } @@ -174,7 +172,7 @@ float XPCFlightLoopCallback(float inElapsedSinceLastCall, counter++; if (benchmarkingSwitch > 1) { - XPC::Log::FormatLine("Cycle time %.6f", inElapsedSinceLastCall); + XPC::Log::FormatLine(LOG_DEBUG, "EXEC", "Cycle time %.6f", inElapsedSinceLastCall); } for (int i = 0; i < OPS_PER_CYCLE; i++) @@ -198,14 +196,14 @@ float XPCFlightLoopCallback(float inElapsedSinceLastCall, #if (__APPLE__) lap = (double)mach_absolute_time( ) * timeConvert; diff_t = lap - start; - XPC::Log::FormatLine("[BENCH] Runtime %.6f", diff_t); + XPC::Log::FormatLine(LOG_INFO, "EXEC", "Runtime %.6f", diff_t); #endif } } if (cyclesToClear != -1 && counter%cyclesToClear == 0) { - XPC::Log::WriteLine("[EXEC] Cleared UDP Buffer"); + XPC::Log::WriteLine(LOG_DEBUG, "EXEC", "Cleared UDP Buffer"); delete sock; sock = new XPC::UDPSocket(RECVPORT); } diff --git a/xpcPlugin/xpcPlugin/xpcPlugin.vcxproj b/xpcPlugin/xpcPlugin/xpcPlugin.vcxproj index 70c01f1..2f162aa 100644 --- a/xpcPlugin/xpcPlugin/xpcPlugin.vcxproj +++ b/xpcPlugin/xpcPlugin/xpcPlugin.vcxproj @@ -101,7 +101,7 @@ NotUsing Level3 Disabled - IBM=1;XPLM200;_DEBUG;_WINDOWS;_USRDLL;XPCPLUGIN_EXPORTS;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + IBM=1;XPLM200;_DEBUG;_WINDOWS;_USRDLL;XPCPLUGIN_EXPORTS;_CRT_SECURE_NO_WARNINGS;LOG_LEVEL=LOG_TRACE;%(PreprocessorDefinitions) OldStyle true