Refactored logging.

- Logging levels are now named preprocessor defines
 - Logging level is now a parameter for WriteLine/FormatLine
This commit is contained in:
Jason Watkins
2015-05-11 14:51:08 -07:00
parent 044a7498f7
commit a2c1f83629
8 changed files with 239 additions and 379 deletions

View File

@@ -29,8 +29,9 @@ namespace XPC
{ {
using namespace std; using namespace std;
const size_t PLANE_COUNT = 20;
static map<DREF, XPLMDataRef> drefs; static map<DREF, XPLMDataRef> drefs;
static map<DREF, XPLMDataRef> mdrefs[20]; static map<DREF, XPLMDataRef> mdrefs[PLANE_COUNT];
static map<string, XPLMDataRef> sdrefs; static map<string, XPLMDataRef> sdrefs;
DREF XPData[134][8] = { DREF_None }; DREF XPData[134][8] = { DREF_None };
@@ -143,7 +144,7 @@ namespace XPC
drefs.insert(make_pair(DREF_MP7Alt, XPLMFindDataRef("sim/multiplayer/position/plane7_el"))); drefs.insert(make_pair(DREF_MP7Alt, XPLMFindDataRef("sim/multiplayer/position/plane7_el")));
char multi[256]; 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); sprintf(multi, "sim/multiplayer/position/plane%i_x", i);
mdrefs[i][DREF_LocalX] = XPLMFindDataRef(multi); mdrefs[i][DREF_LocalX] = XPLMFindDataRef(multi);
@@ -302,6 +303,7 @@ namespace XPC
int DataManager::Get(const string& dref, float values[], int size) int DataManager::Get(const string& dref, float values[], int size)
{ {
Log::WriteLine(LOG_TRACE, "DMAN", "Entered Get(string, float*, int)");
XPLMDataRef& xdref = sdrefs[dref]; XPLMDataRef& xdref = sdrefs[dref];
if (xdref == NULL) if (xdref == NULL)
{ {
@@ -309,24 +311,18 @@ namespace XPC
} }
if (!xdref) // DREF does not exist if (!xdref) // DREF does not exist
{ {
#if LOG_VERBOSITY > 0 Log::FormatLine(LOG_ERROR, "DMAN", "ERROR: invalid DREF %s", dref);
Log::FormatLine("[DMAN] ERROR: invalid DREF %s", dref.c_str());
#endif
return 0; return 0;
} }
XPLMDataTypeID dataType = XPLMGetDataRefTypes(xdref); XPLMDataTypeID dataType = XPLMGetDataRefTypes(xdref);
#if LOG_VERBOSITY > 4 Log::FormatLine(LOG_INFO, "DMAN", "Get DREF %s (x:%X) Type: %i", dref, xdref, dataType);
Log::FormatLine("[DMAN] Get DREF %s (x:%X) Type: %i", dref.c_str(), xdref, dataType);
#endif
// XPLMDataTypeID is a bit flag, so it may contain more than one of the // 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. // following types. We prefer types as close to float as possible.
if ((dataType & 2) == 2) // Float if ((dataType & 2) == 2) // Float
{ {
values[0] = XPLMGetDataf(xdref); values[0] = XPLMGetDataf(xdref);
#if LOG_VERBOSITY > 4 Log::FormatLine(LOG_INFO, "DMAN", " -- value was %f", values[0]);
Log::FormatLine("[DMAN] -- value was %f", values[0]);
#endif
return 1; return 1;
} }
if ((dataType & 8) == 8) // Float array if ((dataType & 8) == 8) // Float array
@@ -334,99 +330,80 @@ namespace XPC
int drefSize = XPLMGetDatavf(xdref, NULL, 0, 0); int drefSize = XPLMGetDatavf(xdref, NULL, 0, 0);
if (drefSize > size) if (drefSize > size)
{ {
#if LOG_VERBOSITY > 1 Log::WriteLine(LOG_WARN, "DMAN", "Warning: dref size is larger than available space");
Log::WriteLine("[DMAN] WARN: dref size is larger than available space"); Log::FormatLine(LOG_DEBUG, "DMAN", "Actual dref size : %i, Available size : %i", drefSize, size);
Log::FormatLine(" Actual dref size: %i, Available size: %i", drefSize, size);
#endif
drefSize = size; drefSize = size;
} }
XPLMGetDatavf(xdref, values, 0, drefSize); XPLMGetDatavf(xdref, values, 0, drefSize);
#if LOG_VERBOSITY > 4 Log::FormatLine(LOG_INFO, "DMAN", " -- value count was %i", drefSize);
Log::FormatLine("[DMAN] -- value count was %i", drefSize);
#endif
return drefSize; return drefSize;
} }
if ((dataType & 4) == 4) // Double if ((dataType & 4) == 4) // Double
{ {
values[0] = (float)XPLMGetDatad(xdref); values[0] = (float)XPLMGetDatad(xdref);
#if LOG_VERBOSITY > 4 Log::FormatLine(LOG_INFO, "DMAN", " -- value was %f", values[0]);
Log::FormatLine("[DMAN] -- value was %f", values[0]);
#endif
return 1; return 1;
} }
if ((dataType & 1) == 1) // Integer if ((dataType & 1) == 1) // Integer
{ {
values[0] = (float)XPLMGetDatai(xdref); int iValue = XPLMGetDatai(xdref);
#if LOG_VERBOSITY > 4 values[0] = (float)iValue;
Log::FormatLine("[DMAN] -- value was %i", (int)values[0]); Log::FormatLine(LOG_INFO, "DMAN", " -- Real value was %i, cast to %f", iValue, values[0]);
#endif
return 1; return 1;
} }
if ((dataType & 16) == 16) // Integer array 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); int drefSize = XPLMGetDatavi(xdref, NULL, 0, 0);
if (drefSize > size) if (drefSize > size)
{ {
#if LOG_VERBOSITY > 1 Log::WriteLine(LOG_WARN, "DMAN", "Warning: dref size is larger than available space");
Log::WriteLine("[DMAN] WARN: dref size is larger than available space"); Log::FormatLine(LOG_DEBUG, "DMAN", "Actual dref size : %i, Available size : %i", drefSize, size);
Log::FormatLine(" Actual dref size: %i, Available size: %i", drefSize, size);
#endif
drefSize = size; drefSize = size;
} }
if (drefSize > 200) if (drefSize > TMP_SIZE)
{ {
#if LOG_VERBOSITY > 1 Log::WriteLine(LOG_WARN, "DMAN", "Warning: dref size is larger than temp buffer");
Log::WriteLine("[DMAN] WARN: dref size is larger than temp buffer"); Log::FormatLine(LOG_DEBUG, "DMAN", "Actual dref size : %i, Temp buffer size: %u", drefSize, TMP_SIZE);
Log::FormatLine(" Actual dref size: %i, Temp buffer size: 200", drefSize); drefSize = TMP_SIZE;
#endif
drefSize = 200;
} }
XPLMGetDatavi(xdref, iValues, 0, drefSize); XPLMGetDatavi(xdref, iValues, 0, drefSize);
for (int i = 0; i < drefSize; ++i) for (int i = 0; i < drefSize; ++i)
{ {
values[i] = (float)iValues[i]; values[i] = (float)iValues[i];
} }
#if LOG_VERBOSITY > 4 Log::FormatLine(LOG_INFO, "DMAN", " -- value count was %i", drefSize);
Log::FormatLine("[DMAN] -- value count was %i", drefSize);
#endif
return drefSize; return drefSize;
} }
if ((dataType & 32) == 32) // Byte array 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); int drefSize = XPLMGetDatab(xdref, NULL, 0, 0);
if (drefSize > size) if (drefSize > size)
{ {
#if LOG_VERBOSITY > 1 Log::WriteLine(LOG_WARN, "DMAN", "Warning: dref size is larger than available space");
Log::WriteLine("[DMAN] WARN: dref size is larger than available space"); Log::FormatLine(LOG_DEBUG, "DMAN", "Actual dref size : %i, Available size : %i", drefSize, size);
Log::FormatLine(" Actual dref size: %i, Available size: %i", drefSize, size);
#endif
drefSize = size; drefSize = size;
} }
if (drefSize > 1024) if (drefSize > TMP_SIZE)
{ {
#if LOG_VERBOSITY > 1 Log::WriteLine(LOG_WARN, "DMAN", "Warning: dref size is larger than temp buffer");
Log::WriteLine("[DMAN] WARN: dref size is larger than temp buffer"); Log::FormatLine(LOG_DEBUG, "DMAN", "Actual dref size : %i, Temp buffer size: %u", drefSize, TMP_SIZE);
Log::FormatLine(" Actual dref size: %i, Temp buffer size: 1024", drefSize); drefSize = TMP_SIZE;
#endif
drefSize = 1024;
} }
XPLMGetDatab(xdref, bValues, 0, drefSize); XPLMGetDatab(xdref, bValues, 0, drefSize);
for (int i = 0; i < drefSize; ++i) for (int i = 0; i < drefSize; ++i)
{ {
values[i] = (float)bValues[i]; values[i] = (float)bValues[i];
} }
#if LOG_VERBOSITY > 4 Log::FormatLine(LOG_INFO, "DMAN", " -- value count was %i", drefSize);
Log::FormatLine("[DMAN] -- value count was %i", drefSize);
#endif
return drefSize; return drefSize;
} }
// No match // No match
#if LOG_VERBOSITY > 0 Log::WriteLine(LOG_ERROR, "DMAN", "ERROR: Unrecognized data type.");
Log::WriteLine("[DMAN] ERROR: Unrecognized data type.");
#endif
return 0; return 0;
} }
@@ -434,9 +411,8 @@ namespace XPC
{ {
const XPLMDataRef& xdref = aircraft == 0 ? drefs[dref] : mdrefs[aircraft][dref]; const XPLMDataRef& xdref = aircraft == 0 ? drefs[dref] : mdrefs[aircraft][dref];
double value = XPLMGetDatad(xdref); double value = XPLMGetDatad(xdref);
#if LOG_VERBOSITY > 4 Log::FormatLine(LOG_INFO, "DMAN", "Get DREF %i (x:%X) result %f for a/c %i",
Log::FormatLine("[DMAN] Get DREF %i (x:%X) result %f for a/c %i", dref, xdref, value, aircraft); dref, xdref, value, aircraft);
#endif
return value; return value;
} }
@@ -444,9 +420,8 @@ namespace XPC
{ {
const XPLMDataRef& xdref = aircraft == 0 ? drefs[dref] : mdrefs[aircraft][dref]; const XPLMDataRef& xdref = aircraft == 0 ? drefs[dref] : mdrefs[aircraft][dref];
float value = XPLMGetDataf(xdref); float value = XPLMGetDataf(xdref);
#if LOG_VERBOSITY > 4 Log::FormatLine(LOG_INFO, "DMAN", "Get DREF %i (x:%X) result %f for a/c %i",
Log::FormatLine("[DMAN] Get DREF %i (x:%X) result %f for a/c %i", dref, xdref, value, aircraft); dref, xdref, value, aircraft);
#endif
return value; return value;
} }
@@ -454,9 +429,8 @@ namespace XPC
{ {
const XPLMDataRef& xdref = aircraft == 0 ? drefs[dref] : mdrefs[aircraft][dref]; const XPLMDataRef& xdref = aircraft == 0 ? drefs[dref] : mdrefs[aircraft][dref];
int value = XPLMGetDatai(xdref); int value = XPLMGetDatai(xdref);
#if LOG_VERBOSITY > 4 Log::FormatLine(LOG_INFO, "DMAN", "Get DREF %i (x:%X) result %i for a/c %i",
Log::FormatLine("[DMAN] Get DREF %i (x:%X) result %i for a/c %i", dref, xdref, value, aircraft); dref, xdref, value, aircraft);
#endif
return value; return value;
} }
@@ -464,9 +438,8 @@ namespace XPC
{ {
const XPLMDataRef& xdref = aircraft == 0 ? drefs[dref] : mdrefs[aircraft][dref]; const XPLMDataRef& xdref = aircraft == 0 ? drefs[dref] : mdrefs[aircraft][dref];
int resultSize = XPLMGetDatavf(xdref, values, 0, size); int resultSize = XPLMGetDatavf(xdref, values, 0, size);
#if LOG_VERBOSITY > 4 Log::FormatLine(LOG_INFO, "DMAN", "Get DREF %i (x:%X) result size %i for a/c %i",
Log::FormatLine("[DMAN] Get DREF %i (x:%X) result size %i for a/c %i", dref, xdref, resultSize, aircraft); dref, xdref, resultSize, aircraft);
#endif
return resultSize; return resultSize;
} }
@@ -474,46 +447,46 @@ namespace XPC
{ {
const XPLMDataRef& xdref = aircraft == 0 ? drefs[dref] : mdrefs[aircraft][dref]; const XPLMDataRef& xdref = aircraft == 0 ? drefs[dref] : mdrefs[aircraft][dref];
int resultSize = XPLMGetDatavi(xdref, values, 0, size); int resultSize = XPLMGetDatavi(xdref, values, 0, size);
#if LOG_VERBOSITY > 4 Log::FormatLine(LOG_INFO, "DMAN", "Get DREF %i (x:%X) result size %i for a/c %i",
Log::FormatLine("[DMAN] Get DREF %i (x:%X) result size %i for a/c %i", dref, xdref, resultSize, aircraft); dref, xdref, resultSize, aircraft);
#endif
return resultSize; return resultSize;
} }
void DataManager::Set(DREF dref, double value, char aircraft) void DataManager::Set(DREF dref, double value, char aircraft)
{ {
const XPLMDataRef& xdref = aircraft == 0 ? drefs[dref] : mdrefs[aircraft][dref]; const XPLMDataRef& xdref = aircraft == 0 ? drefs[dref] : mdrefs[aircraft][dref];
#if LOG_VERBOSITY > 4 Log::FormatLine(LOG_INFO, "DMAN", "Setting DREF %i (x:%X) to %f for a/c %i",
Log::FormatLine("[DMAN] Setting DREF %i (x:%X) to %f for a/c %i", dref, xdref, value, aircraft); dref, xdref, value, aircraft);
#endif
XPLMSetDatad(xdref, value); XPLMSetDatad(xdref, value);
} }
void DataManager::Set(DREF dref, float value, char aircraft) void DataManager::Set(DREF dref, float value, char aircraft)
{ {
const XPLMDataRef& xdref = aircraft == 0 ? drefs[dref] : mdrefs[aircraft][dref]; const XPLMDataRef& xdref = aircraft == 0 ? drefs[dref] : mdrefs[aircraft][dref];
#if LOG_VERBOSITY > 4 Log::FormatLine(LOG_INFO, "DMAN", "Setting DREF %i (x:%X) to %f for a/c %i",
Log::FormatLine("[DMAN] Set DREF %i (x:%X) to %f for a/c %i", dref, xdref, value, aircraft); dref, xdref, value, aircraft);
#endif
XPLMSetDataf(xdref, value); XPLMSetDataf(xdref, value);
} }
void DataManager::Set(DREF dref, int value, char aircraft) void DataManager::Set(DREF dref, int value, char aircraft)
{ {
const XPLMDataRef& xdref = aircraft == 0 ? drefs[dref] : mdrefs[aircraft][dref]; const XPLMDataRef& xdref = aircraft == 0 ? drefs[dref] : mdrefs[aircraft][dref];
#if LOG_VERBOSITY > 4 Log::FormatLine(LOG_INFO, "DMAN", "Setting DREF %i (x:%X) to %i for a/c %i",
Log::FormatLine("[DMAN] Set DREF %i (x:%X) to %i for a/c %i", dref, xdref, value, aircraft); dref, xdref, value, aircraft);
#endif
XPLMSetDatai(xdref, value); XPLMSetDatai(xdref, value);
} }
void DataManager::Set(DREF dref, float values[], int size, char aircraft) void DataManager::Set(DREF dref, float values[], int size, char aircraft)
{ {
const XPLMDataRef& xdref = aircraft == 0 ? drefs[dref] : mdrefs[aircraft][dref]; const XPLMDataRef& xdref = aircraft == 0 ? drefs[dref] : mdrefs[aircraft][dref];
#if LOG_VERBOSITY > 4 Log::FormatLine(LOG_INFO, "DMAN", "Setting DREF %i (x:%X) (%i values) for a/c %i",
Log::FormatLine("[DMAN] Set DREF %i (x:%X) (%i values) for a/c %i", dref, xdref, size, aircraft); dref, xdref, size, aircraft);
#endif
int drefSize = XPLMGetDatavf(xdref, NULL, 0, 0); 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); drefSize = min(drefSize, size);
XPLMSetDatavf(xdref, values, 0, drefSize); XPLMSetDatavf(xdref, values, 0, drefSize);
} }
@@ -521,10 +494,14 @@ namespace XPC
void DataManager::Set(DREF dref, int values[], int size, char aircraft) void DataManager::Set(DREF dref, int values[], int size, char aircraft)
{ {
const XPLMDataRef& xdref = aircraft == 0 ? drefs[dref] : mdrefs[aircraft][dref]; const XPLMDataRef& xdref = aircraft == 0 ? drefs[dref] : mdrefs[aircraft][dref];
#if LOG_VERBOSITY > 4 Log::FormatLine(LOG_INFO, "DMAN", "Setting DREF %i (x:%X) (%i values) for a/c %i",
Log::FormatLine("[DMAN] Setting DREF %i (x:%X) (%i values) for a/c %i", dref, xdref, size, aircraft); dref, xdref, size, aircraft);
#endif
int drefSize = XPLMGetDatavi(xdref, NULL, 0, 0); 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); drefSize = min(drefSize, size);
XPLMSetDatavi(xdref, values, 0, drefSize); XPLMSetDatavi(xdref, values, 0, drefSize);
} }
@@ -539,139 +516,113 @@ namespace XPC
if (!xdref) if (!xdref)
{ {
// DREF does not exist // DREF does not exist
#if LOG_VERBOSITY > 0 Log::FormatLine(LOG_ERROR, "DMAN", "ERROR: invalid DREF %s", dref);
Log::FormatLine("[DMAN] ERROR: invalid DREF %s", dref.c_str());
#endif
return; return;
} }
if (isnan(values[0])) if (isnan(values[0]))
{ {
#if LOG_VERBOSITY > 0 Log::WriteLine(LOG_ERROR, "DMAN", "ERROR: Value must be a number (NaN received)");
Log::WriteLine("[DMAN] ERROR: Value must be a number (NaN received)");
#endif
return; return;
} }
XPLMDataTypeID dataType = XPLMGetDataRefTypes(xdref); XPLMDataTypeID dataType = XPLMGetDataRefTypes(xdref);
#if LOG_VERBOSITY > 4 Log::FormatLine(LOG_INFO, "DMAN", "Setting DREF %s (x:%X) Type: %i", xdref, dataType);
Log::FormatLine("[DMAN] Setting DREF %s (x:%X) Type: %i", dref.c_str(), xdref, dataType);
#endif
if ((dataType & 2) == 2) // Float if ((dataType & 2) == 2) // Float
{ {
XPLMSetDataf(xdref, values[0]); XPLMSetDataf(xdref, values[0]);
#if LOG_VERBOSITY > 4 Log::FormatLine(LOG_INFO, "DMAN", " -- value was %f", values[0]);
Log::FormatLine("[DMAN] -- value was %f", values[0]);
#endif
} }
else if ((dataType & 8) == 8) // Float Array else if ((dataType & 8) == 8) // Float Array
{ {
int drefSize = XPLMGetDatavf(xdref, NULL, 0, 0); int drefSize = XPLMGetDatavf(xdref, NULL, 0, 0);
#if LOG_VERBOSITY > 1
if (size > drefSize) if (size > drefSize)
{ {
Log::WriteLine("[DMAN] WARN: Provided size is larger than actual dref size"); Log::WriteLine(LOG_WARN, "DMAN", "Warning: dref size is larger than actual dref size");
Log::FormatLine(" Actual dref size: %i, Provided buffer size: %i", drefSize, size); Log::FormatLine(LOG_DEBUG, "DMAN", "Actual dref size : %i, Provided buffer size : %i",
drefSize, size);
} }
#endif
drefSize = min(drefSize, size); drefSize = min(drefSize, size);
XPLMSetDatavf(xdref, values, 0, drefSize); XPLMSetDatavf(xdref, values, 0, drefSize);
#if LOG_VERBOSITY > 4 Log::FormatLine(LOG_INFO, "DMAN", " -- value count was %i", drefSize);
Log::FormatLine("[DMAN] -- value count was %i", drefSize);
#endif
} }
else if ((dataType & 4) == 4) // Double else if ((dataType & 4) == 4) // Double
{ {
XPLMSetDatad(xdref, values[0]); XPLMSetDatad(xdref, values[0]);
#if LOG_VERBOSITY > 4 Log::FormatLine(LOG_INFO, "DMAN", " -- value was %f", values[0]);
Log::FormatLine("[DMAN] -- value was %f", values[0]);
#endif
} }
else if ((dataType & 1) == 1) // Integer else if ((dataType & 1) == 1) // Integer
{ {
XPLMSetDatai(xdref, (int)values[0]); XPLMSetDatai(xdref, (int)values[0]);
#if LOG_VERBOSITY > 4 Log::FormatLine(LOG_INFO, "DMAN", " -- value was %i", (int)values[0]);
Log::FormatLine("[DMAN] -- value was %i", (int)values[0]);
#endif
} }
else if ((dataType & 16) == 16) // Integer Array 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); int drefSize = XPLMGetDatavi(xdref, NULL, 0, 0);
#if LOG_VERBOSITY > 1
if (size > drefSize) if (size > drefSize)
{ {
Log::WriteLine("[DMAN] WARN: Provided size is larger than actual dref size"); Log::WriteLine(LOG_WARN, "DMAN", "Warning: dref size is larger than actual dref size");
Log::FormatLine(" Actual dref size: %i, Provided buffer size: %i", drefSize, size); Log::FormatLine(LOG_DEBUG, "DMAN", "Actual dref size : %i, Provided buffer size : %i",
drefSize, size);
} }
#endif
drefSize = min(drefSize, size); drefSize = min(drefSize, size);
if (drefSize > 200) if (drefSize > TMP_SIZE)
{ {
#if LOG_VERBOSITY > 1 Log::WriteLine(LOG_WARN, "DMAN", "Warning: dref size is larger temp buffer size");
Log::WriteLine("[DMAN] WARN: drefSize larger than temp buffer size."); Log::FormatLine(LOG_DEBUG, "DMAN", "Actual dref size : %i, Temp buffer size: %i",
Log::FormatLine(" Actual dref size: %i, Temp buffer size: 200", drefSize); drefSize, TMP_SIZE);
#endif drefSize = TMP_SIZE;
drefSize = 200;
} }
for (int i = 0; i < drefSize; ++i) for (int i = 0; i < drefSize; ++i)
{ {
iValues[i] = (int)values[i]; iValues[i] = (int)values[i];
} }
XPLMSetDatavi(xdref, iValues, 0, drefSize); XPLMSetDatavi(xdref, iValues, 0, drefSize);
#if LOG_VERBOSITY > 4 Log::FormatLine(LOG_INFO, "DMAN", " -- value count was %i", drefSize);
Log::FormatLine("[DMAN] -- value count was %i", drefSize);
#endif
} }
else if ((dataType & 32) == 32) // Byte Array 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); int drefSize = XPLMGetDatab(xdref, NULL, 0, 0);
#if LOG_VERBOSITY > 1
if (size > drefSize) if (size > drefSize)
{ {
Log::WriteLine("[DMAN] WARN: Provided size is larger than actual dref size"); Log::WriteLine(LOG_WARN, "DMAN", "Warning: dref size is larger than actual dref size");
Log::FormatLine(" Actual dref size: %i, Provided buffer size: %i", drefSize, size); Log::FormatLine(LOG_DEBUG, "DMAN", "Actual dref size : %i, Provided buffer size : %i",
drefSize, size);
} }
#endif
drefSize = min(drefSize, size); drefSize = min(drefSize, size);
if (drefSize > 1024) if (drefSize > TMP_SIZE)
{ {
#if LOG_VERBOSITY > 1 Log::WriteLine(LOG_WARN, "DMAN", "Warning: dref size is larger temp buffer size");
Log::WriteLine("[DMAN] WARN: drefSize larger than temp buffer size."); Log::FormatLine(LOG_DEBUG, "DMAN", "Actual dref size : %i, Temp buffer size: %i",
Log::FormatLine(" Actual dref size: %i, Temp buffer size: 1024", drefSize); drefSize, TMP_SIZE);
#endif drefSize = TMP_SIZE;
drefSize = 1024;
} }
for (int i = 0; i < drefSize; ++i) for (int i = 0; i < drefSize; ++i)
{ {
bValues[i] = (char)values[i]; bValues[i] = (char)values[i];
} }
XPLMSetDatab(xdref, bValues, 0, drefSize); XPLMSetDatab(xdref, bValues, 0, drefSize);
#if LOG_VERBOSITY > 4 Log::FormatLine(LOG_INFO, "DMAN", " -- value count was %i", drefSize);
Log::FormatLine("[DMAN] -- value count was %i", drefSize);
#endif
} }
else else
{ {
#if LOG_VERBOSITY > 0 Log::WriteLine(LOG_ERROR, "DMAN", "ERROR: Unknown type.");
Log::WriteLine("[DMAN] ERROR: Unknown type.");
#endif
} }
#if LOG_VERBOSITY > 1
if (!XPLMCanWriteDataRef(xdref)) 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) void DataManager::SetGear(float gear, bool immediate, char aircraft)
{ {
#if LOG_VERBOSITY > 3 Log::FormatLine(LOG_INFO, "DMAN", "Setting gear (value:%f, immediate:%i) for aircraft %i",
Log::FormatLine("[DMAN] Setting gear (value:%f, immediate:%i) for aircraft %i", gear, immediate, aircraft); gear, immediate, aircraft);
#endif
if ((gear < -8.5 && gear > -9.5) || (gear < -997.9 && gear > -999.1)) 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 (isnan(gear) || gear < 0 || gear > 1)
{ {
#if LOG_VERBOSITY > 1 Log::WriteLine(LOG_ERROR, "DMAN", "ERROR: Gear value must be 0 or 1");
Log::WriteLine("[GEAR] ERROR: Value must be 0 or 1");
#endif
return; return;
} }
@@ -707,14 +656,11 @@ namespace XPC
void DataManager::SetPosition(float pos[3], char aircraft) void DataManager::SetPosition(float pos[3], char aircraft)
{ {
#if LOG_VERBOSITY > 3 Log::FormatLine(LOG_INFO, "DMAN", "Setting position (%f, %f, %f) for aircraft %i",
Log::FormatLine("[DMAN] Setting position (%f, %f, %f) for aircraft %i", pos[0], pos[1], pos[2], aircraft); pos[0], pos[1], pos[2], aircraft);
#endif
if (isnan(pos[0] + pos[1] + pos[2])) if (isnan(pos[0] + pos[1] + pos[2]))
{ {
#if LOG_VERBOSITY > 0 Log::WriteLine(LOG_ERROR, "DMAN", "ERROR: Position must be a number (NaN received)");
Log::WriteLine("[DMAN] ERROR: Position must be a number (NaN received)");
#endif
return; return;
} }
@@ -746,15 +692,11 @@ namespace XPC
void DataManager::SetOrientation(float orient[3], char aircraft) void DataManager::SetOrientation(float orient[3], char aircraft)
{ {
#if LOG_VERBOSITY > 3 Log::FormatLine(LOG_INFO, "DMAN", "Setting orientation (%f, %f, %f) for aircraft %i",
Log::FormatLine("[DMAN] Setting orientation (%f, %f, %f) for aircraft %i",
orient[0], orient[1], orient[2], aircraft); orient[0], orient[1], orient[2], aircraft);
#endif
if (isnan(orient[0] + orient[1] + orient[2])) if (isnan(orient[0] + orient[1] + orient[2]))
{ {
#if LOG_VERBOSITY > 0 Log::WriteLine(LOG_ERROR, "DMAN", "ERROR: Orientation must be a number (NaN received)");
Log::WriteLine("[DMAN] ERROR: Orientation must be a number (NaN received)");
#endif
return; return;
} }
@@ -801,11 +743,11 @@ namespace XPC
void DataManager::SetFlaps(float value) void DataManager::SetFlaps(float value)
{ {
Log::FormatLine(LOG_INFO, "DMAN", "Setting flaps (value:%f)", value);
if (isnan(value)) if (isnan(value))
{ {
#if LOG_VERBOSITY > 0 Log::WriteLine(LOG_ERROR, "DMAN", "ERROR: Flap value must be a number (NaN received)");
Log::WriteLine("[DMAN] ERROR: Flap value must be a number (NaN received)");
#endif
return; return;
} }
if (value < -997.9 && value > -999.1) if (value < -997.9 && value > -999.1)

View File

@@ -12,7 +12,7 @@
#include <iomanip> #include <iomanip>
#include <sstream> #include <sstream>
// 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 // 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. // efficient to me to just use C-style IO and call std::fprintf directly.
namespace XPC namespace XPC
@@ -38,8 +38,13 @@ namespace XPC
std::fprintf(fd, ss.str().c_str()); 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"); fd = std::fopen("XPCLog.txt", "w");
if (fd != NULL) 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()); if (level > LOG_LEVEL || !fd)
}
void Log::WriteLine(const char* value)
{
if (!fd)
{ {
return; return;
} }
WriteTime(fd); WriteTime(fd);
std::fprintf(fd, "%s\n", value); std::fprintf(fd, "[%s] %s\n", tag.c_str(), value.c_str());
std::fflush(fd); 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; va_list args;
if (!fd) if (level > LOG_LEVEL || !fd)
{ {
return; return;
} }
va_start(args, format); va_start(args, format);
WriteTime(fd); 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::fprintf(fd, "\n");
std::fflush(fd); std::fflush(fd);

View File

@@ -5,18 +5,26 @@
#include <string> #include <string>
// LOG_VERBOSITY determines the level of logging throughout the plugin. // LOG_VERBOSITY determines the level of logging throughout the plugin.
// 0: Minimum logging. Only plugin manager events will be logged. // OFF: No logging at all will be performed.
// 1: Critical errors. When an error that prevents correct operation of the // FATAL: Critical errors that would normally resulti in termination of the program. Because XPC
// plugin, attempt to write useful information to the log. Note that since // operates in the X-Plane process, we try to never actually crash. As a result, we this
// XPC runs inside the X-Plane executable, we try very hard no to crash. // level of logging may be the only indication of a problem.
// As a result, these log messages may be the only indication of failure. // ERROR: All errors not covered by FATAL
// 2: All errors. Any time something unexpected happens, log it. // WARN: Potentially, but not definitely, incorrect behavior
// 3: Significant actions. Any time something happens outside of normal // INFO: Information about normal actions taken by the plugin.
// command processing, log it. Will also log when commands are received. // DEBUG: More verbose information usefull for debugging.
// 4: Detailed actions. Log aditional information as commands are processed. // TRACE: Log all the things!
// 5: Everything. Log nearly every single action the plugin takes. This may #define LOG_OFF 0
// have a detrimental impact on X-Plane performance. #define LOG_FATAL 1
#define LOG_VERBOSITY 3 #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 namespace XPC
{ {
@@ -33,31 +41,25 @@ namespace XPC
public: public:
/// Initializes the logging component by deleting old log files, /// Initializes the logging component by deleting old log files,
/// writing header information to the log file. /// writing header information to the log file.
static void Initialize(std::string header); static void Initialize(const std::string& header);
/// Closes the log file. /// Closes the log file.
static void Close(); 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 /// terminator to the XPC log file. If format contains format
/// specifiers, additional arguments following format will be formatted /// specifiers, additional arguments following format will be formatted
/// and inserted in the resulting string, replacing their respective /// and inserted in the resulting string, replacing their respective
/// specifiers. /// specifiers.
/// ///
/// \param format The format string appropriate for consumption by sprintf. /// \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 /// Writes the specified string value, followed by a line terminator
/// to the XPC log file. /// to the XPC log file.
/// ///
/// \param value The value to write. /// \param value The value to write.
static void WriteLine(const std::string& value); static void WriteLine(int level, const std::string& tag, 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);
}; };
} }
#endif #endif

View File

@@ -58,9 +58,7 @@ namespace XPC
void Message::PrintToLog() const void Message::PrintToLog() const
{ {
#if LOG_VERBOSITY > 4
std::stringstream ss; std::stringstream ss;
ss << "[DEBUG]";
// Dump raw bytes to string // Dump raw bytes to string
ss << std::hex << std::setfill('0'); ss << std::hex << std::setfill('0');
@@ -68,18 +66,18 @@ namespace XPC
{ {
ss << ' ' << std::setw(2) << static_cast<unsigned>(buffer[i]); ss << ' ' << std::setw(2) << static_cast<unsigned>(buffer[i]);
} }
Log::WriteLine(ss.str()); Log::WriteLine(LOG_DEBUG, "DBUG", ss.str());
ss << std::dec; ss << std::dec;
ss.str(""); ss.str("");
ss << "[" << GetHead() << "-DEBUG] (" << GetSize() << ")"; ss << "Head:" << GetHead() << " Size:" << GetSize();
switch (GetMagicNumber()) // Binary version of head switch (GetMagicNumber()) // Binary version of head
{ {
case 0x4E4EF443: // CONN case 0x4E4EF443: // CONN
case 0x54505957: // WYPT case 0x54505957: // WYPT
case 0x54584554: // TEXT case 0x54584554: // TEXT
{ {
Log::WriteLine(ss.str()); Log::WriteLine(LOG_DEBUG, "DBUG", ss.str());
break; break;
} }
case 0x4C525443: // CTRL case 0x4C525443: // CTRL
@@ -98,7 +96,7 @@ namespace XPC
} }
ss << " Attitude:(" << pitch << " " << roll << " " << yaw << ")"; ss << " Attitude:(" << pitch << " " << roll << " " << yaw << ")";
ss << " Thr:" << thr << " Gear:" << (int)gear << " Flaps:" << flaps; ss << " Thr:" << thr << " Gear:" << (int)gear << " Flaps:" << flaps;
Log::WriteLine(ss.str()); Log::WriteLine(LOG_DEBUG, "DBUG", ss.str());
break; break;
} }
case 0x41544144: // DATA case 0x41544144: // DATA
@@ -110,43 +108,44 @@ namespace XPC
values[i][0] = buffer[5 + 36 * i]; values[i][0] = buffer[5 + 36 * i];
std::memcpy(values[i] + 1, buffer + 9 + 36 * i, 9 * sizeof(float)); std::memcpy(values[i] + 1, buffer + 9 + 36 * i, 9 * sizeof(float));
} }
ss << " (" << numCols << " lines)"; ss << " (" << numCols << " lines)";
Log::WriteLine(ss.str()); Log::WriteLine(LOG_DEBUG, "DBUG", ss.str());
for (int i = 0; i < numCols; ++i) for (int i = 0; i < numCols; ++i)
{ {
ss.str(""); ss.str("");
ss << "\t#" << values[i][0]; ss << " #" << values[i][0];
for (int j = 1; j < 9; ++j) for (int j = 1; j < 9; ++j)
{ {
ss << " " << values[i][j]; ss << " " << values[i][j];
} }
Log::WriteLine(ss.str()); Log::WriteLine(LOG_DEBUG, "DBUG", ss.str());
} }
break; break;
} }
case 0x46455244: // DREF case 0x46455244: // DREF
{ {
Log::WriteLine(ss.str()); Log::WriteLine(LOG_DEBUG, "DBUG", ss.str());
std::string dref((char*)buffer + 6, buffer[5]); 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(""); ss.str("");
int values = buffer[6 + buffer[5]]; int values = buffer[6 + buffer[5]];
ss << "\tValues(size " << values << ") ="; ss << " Values(size " << values << ") =";
for (int i = 0; i < values; ++i) for (int i = 0; i < values; ++i)
{ {
ss << " " << *((float*)(buffer + values + 1 + sizeof(float) * i)); ss << " " << *((float*)(buffer + values + 1 + sizeof(float) * i));
} }
Log::WriteLine(ss.str()); Log::WriteLine(LOG_DEBUG, "DBUG", ss.str());
break; break;
} }
case 0x44544547: // GETD case 0x44544547: // GETD
{ {
Log::WriteLine(ss.str()); Log::WriteLine(LOG_DEBUG, "DBUG", ss.str());
int cur = 6; int cur = 6;
for (int i = 0; i < buffer[5]; ++i) for (int i = 0; i < buffer[5]; ++i)
{ {
std::string dref((char*)buffer + cur + 1, buffer[cur]); 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]; cur += 1 + buffer[cur];
} }
break; break;
@@ -162,23 +161,22 @@ namespace XPC
ss << " AC:" << (int)aircraft; ss << " AC:" << (int)aircraft;
ss << " Pos:(" << pos[0] << ' ' << pos[1] << ' ' << pos[2] << ") Orient:("; ss << " Pos:(" << pos[0] << ' ' << pos[1] << ' ' << pos[2] << ") Orient:(";
ss << orient[3] << ' ' << orient[4] << ' ' << orient[5] << ") Gear:"; ss << orient[3] << ' ' << orient[4] << ' ' << orient[5] << ") Gear:";
ss << gear; ss << gear;
Log::WriteLine(ss.str()); Log::WriteLine(LOG_DEBUG, "DBUG", ss.str());
break; break;
} }
case 0x554D4953: // SIMU case 0x554D4953: // SIMU
{ {
ss << ' ' << (int)buffer[5]; ss << ' ' << (int)buffer[5];
Log::WriteLine(ss.str()); Log::WriteLine(LOG_DEBUG, "DBUG", ss.str());
break; break;
} }
default: default:
{ {
ss << " UNKNOWN HEADER "; ss << " UNKNOWN HEADER ";
Log::WriteLine(ss.str()); Log::WriteLine(LOG_DEBUG, "DBUG", ss.str());
break; break;
} }
} }
#endif
} }
} }

View File

@@ -80,19 +80,14 @@ namespace XPC
std::string head = msg.GetHead(); std::string head = msg.GetHead();
if (head == "") if (head == "")
{ {
#if LOG_VERBOSITY > 1 Log::WriteLine(LOG_WARN, "MSGH", "Warning: HandleMessage called with empty message.");
Log::WriteLine("[MSGH] Warning: HandleMessage called with empty message.");
#endif
return; // No Message to handle return; // No Message to handle
} }
msg.PrintToLog();
// Set current connection // Set current connection
sockaddr sourceaddr = msg.GetSource(); sockaddr sourceaddr = msg.GetSource();
connectionKey = UDPSocket::GetHost(&sourceaddr); connectionKey = UDPSocket::GetHost(&sourceaddr);
#if LOG_VERBOSITY > 4 Log::FormatLine(LOG_INFO, "MSGH", "Handling message from %s", connectionKey);
Log::FormatLine("[MSGH] Handling message from %s", connectionKey.c_str());
#endif
std::map<std::string, ConnectionInfo>::iterator conn = connections.find(connectionKey); std::map<std::string, ConnectionInfo>::iterator conn = connections.find(connectionKey);
if (conn == connections.end()) // New connection if (conn == connections.end()) // New connection
{ {
@@ -104,19 +99,17 @@ namespace XPC
connection.addr = sourceaddr; connection.addr = sourceaddr;
connection.getdCount = 0; connection.getdCount = 0;
connections[connectionKey] = connection; connections[connectionKey] = connection;
#if LOG_VERBOSITY > 2 Log::FormatLine(LOG_TRACE, "MSGH", "New connection. ID=%u, Remote=%s",
Log::FormatLine("[MSGH] New connection. ID=%u, Remote=%s", connection.id, connectionKey.c_str()); connection.id, connectionKey);
#endif
} }
else else
{ {
connection = (*conn).second; connection = (*conn).second;
#if LOG_VERBOSITY > 3 Log::FormatLine(LOG_TRACE, "MSGH", "Existing connection. ID=%u, Remote=%s",
Log::FormatLine("[MSGH] Existing connection. ID=%u, Remote=%s", connection.id, connectionKey);
connection.id, connectionKey.c_str());
#endif
} }
msg.PrintToLog();
// Check if there is a handler for this message type. If so, execute // Check if there is a handler for this message type. If so, execute
// that handler. Otherwise, execute the unknown message handler. // that handler. Otherwise, execute the unknown message handler.
std::map<std::string, MessageHandler>::iterator iter = handlers.find(head); std::map<std::string, MessageHandler>::iterator iter = handlers.find(head);
@@ -153,10 +146,8 @@ namespace XPC
break; break;
} }
default: default:
#if LOG_VERBOSITY > 0 Log::WriteLine(LOG_ERROR, "CONN", "ERROR: Unknown address type.");
Log::WriteLine("[CONN] ERROR: Unknown address type.");
return; return;
#endif
} }
connections.erase(connectionKey); connections.erase(connectionKey);
connectionKey = UDPSocket::GetHost(&connection.addr); connectionKey = UDPSocket::GetHost(&connection.addr);
@@ -167,10 +158,8 @@ namespace XPC
response[5] = connection.id; response[5] = connection.id;
// Update log // Update log
#if LOG_VERBOSITY > 1 Log::FormatLine(LOG_TRACE, "CONN", "ID: %u New destination port: %u",
Log::FormatLine("[CONN] ID: %u New destination port: %u",
connection.id, port); connection.id, port);
#endif
// Send response // Send response
sock->SendTo(response, 6, &connection.addr); sock->SendTo(response, 6, &connection.addr);
@@ -179,19 +168,16 @@ namespace XPC
void MessageHandlers::HandleCtrl(const Message& msg) void MessageHandlers::HandleCtrl(const Message& msg)
{ {
// Update Log // Update Log
#if LOG_VERBOSITY > 2 Log::FormatLine(LOG_TRACE, "CTRL", "Message Received (Conn %i)", connection.id);
Log::FormatLine("[CTRL] Message Received (Conn %i)", connection.id);
#endif
const unsigned char* buffer = msg.GetBuffer(); const unsigned char* buffer = msg.GetBuffer();
std::size_t size = msg.GetSize(); std::size_t size = msg.GetSize();
//Legacy packets that don't specify an aircraft number should be 26 bytes long. // 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 an A/C num should be 27 bytes. Packets specifying a speedbrake
// should be 31 bytes.
if (size != 26 && size != 27 && size != 31) if (size != 26 && size != 27 && size != 31)
{ {
#if LOG_VERBOSITY > 0 Log::FormatLine(LOG_ERROR, "CTRL", "ERROR: Unexpected message length (%i)", size);
Log::FormatLine("[CTRL] ERROR: Unexpected message length (%i)", size);
#endif
return; return;
} }
@@ -263,23 +249,17 @@ namespace XPC
std::size_t numCols = (size - 5) / 36; std::size_t numCols = (size - 5) / 36;
if (numCols > 0) if (numCols > 0)
{ {
#if LOG_VERBOSITY > 1 Log::FormatLine(LOG_TRACE, "DATA", "Message Received (Conn %i)", connection.id);
Log::FormatLine("[DATA] Message Received (Conn %i)", connection.id);
#endif
} }
else else
{ {
#if LOG_VERBOSITY > 0 Log::FormatLine(LOG_WARN, "DATA", "WARNING: Empty data packet received (Conn %i)", connection.id);
Log::FormatLine("[DATA] WARNING: Empty data packet received (Conn %i)", connection.id);
#endif
return; return;
} }
if (numCols > 134) // Error. Will overflow values if (numCols > 134) // Error. Will overflow values
{ {
#if LOG_VERBOSITY > 0 Log::FormatLine(LOG_ERROR, "DATA", "ERROR: numCols to large.");
Log::FormatLine("[DATA] ERROR: numCols to large.");
#endif
return; return;
} }
float values[134][9]; float values[134][9];
@@ -299,9 +279,7 @@ namespace XPC
unsigned char dataRef = (unsigned char)values[i][0]; unsigned char dataRef = (unsigned char)values[i][0];
if (dataRef >= 134) if (dataRef >= 134)
{ {
#if LOG_VERBOSITY > 0 Log::FormatLine(LOG_ERROR, "DATA", "ERROR: DataRef # must be between 0 - 134 (Received: %hi)", (int)dataRef);
Log::FormatLine("[DATA] ERROR: DataRef # must be between 0 - 134 (Received: %hi)", (int)dataRef);
#endif
continue; continue;
} }
@@ -315,9 +293,7 @@ namespace XPC
float hpath = savedHPath != -998 ? savedHPath : DataManager::GetFloat(DREF_HPath); float hpath = savedHPath != -998 ? savedHPath : DataManager::GetFloat(DREF_HPath);
if (alpha != alpha || hpath != hpath) if (alpha != alpha || hpath != hpath)
{ {
#if LOG_VERBOSITY > 0 Log::WriteLine(LOG_ERROR, "DATA", "ERROR: Value must be a number (NaN received)");
Log::WriteLine("[DATA] ERROR: Value must be a number (NaN received)");
#endif
break; break;
} }
const float deg2rad = 0.0174532925F; 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 (values[i][1] != values[i][1] || values[i][3] != values[i][3])
{ {
#if LOG_VERBOSITY > 0 Log::WriteLine(LOG_ERROR, "DATA", "ERROR: Value must be a number (NaN received)");
Log::WriteLine("[DATA] ERROR: Value must be a number (NaN received)");
#endif
break; break;
} }
if (values[i][1] != -998) if (values[i][1] != -998)
@@ -375,9 +349,7 @@ namespace XPC
{ {
if (values[i][1] != values[i][1]) if (values[i][1] != values[i][1])
{ {
#if LOG_VERBOSITY > 0 Log::WriteLine(LOG_ERROR, "DATA", "ERROR: Value must be a number (NaN received)");
Log::WriteLine("[DATA] ERROR: Value must be a number (NaN received)");
#endif
break; break;
} }
float throttle[8]; float throttle[8];
@@ -394,9 +366,7 @@ namespace XPC
memcpy(line, values[i] + 1, 8 * sizeof(float)); memcpy(line, values[i] + 1, 8 * sizeof(float));
for (int j = 0; j < 8; ++j) for (int j = 0; j < 8; ++j)
{ {
#if LOG_VERBOSITY > 1 Log::FormatLine(LOG_ERROR, "DATA", "Setting Dataref %i.%i to %f", dataRef, j, line[j]);
Log::FormatLine("[DATA] Setting Dataref %i.%i to %f", dataRef, j, line[j]);
#endif
// TODO(jason-watkins): Why is this a special case? // TODO(jason-watkins): Why is this a special case?
if (dataRef == 14 && j == 0) if (dataRef == 14 && j == 0)
{ {
@@ -465,24 +435,20 @@ namespace XPC
unsigned char drefCount = buffer[5]; unsigned char drefCount = buffer[5];
if (drefCount == 0) // Use last request if (drefCount == 0) // Use last request
{ {
#if LOG_VERBOSITY > 0 Log::FormatLine(LOG_TRACE, "GETD",
Log::FormatLine("[GETD] DATA Requested: Repeat last request from connection %i (%i data refs)", "DATA Requested: Repeat last request from connection %i (%i data refs)",
connection.id, connection.getdCount); connection.id, connection.getdCount);
#endif
if (connection.getdCount == 0) // No previous request to use if (connection.getdCount == 0) // No previous request to use
{ {
#if LOG_VERBOSITY > 1 Log::FormatLine(LOG_ERROR, "GETD", "ERROR: No previous requests from connection %i.",
Log::FormatLine("[GETD] ERROR: No previous requests from connection %i.", connection.id); connection.id);
#endif
return; return;
} }
} }
else // New request else // New request
{ {
#if LOG_VERBOSITY > 0 Log::FormatLine(LOG_TRACE, "GETD", "DATA Requested: New Request for connection %i (%i data refs)",
Log::FormatLine("[GETD] DATA Requested: New Request for connection %i (%i data refs)",
connection.id, drefCount); connection.id, drefCount);
#endif
std::size_t ptr = 6; std::size_t ptr = 6;
for (int i = 0; i < drefCount; ++i) for (int i = 0; i < drefCount; ++i)
{ {
@@ -512,17 +478,13 @@ namespace XPC
void MessageHandlers::HandlePosi(const Message& msg) void MessageHandlers::HandlePosi(const Message& msg)
{ {
// Update log // Update log
#if LOG_VERBOSITY > 0 Log::FormatLine(LOG_TRACE, "POSI", "Message Received (Conn %i)", connection.id);
Log::FormatLine("[POSI] Message Received (Conn %i)", connection.id);
#endif
const unsigned char* buffer = msg.GetBuffer(); const unsigned char* buffer = msg.GetBuffer();
const std::size_t size = msg.GetSize(); const std::size_t size = msg.GetSize();
if (size < 34) if (size < 34)
{ {
#if LOG_VERBOSITY > 1 Log::FormatLine(LOG_ERROR, "POSI", "ERROR: Unexpected size: %i (Expected at least 34)", size);
Log::FormatLine("[POSI] ERROR: Unexpected size: %i (Expected at least 34)", size);
#endif
return; return;
} }
@@ -556,17 +518,13 @@ namespace XPC
void MessageHandlers::HandleSimu(const Message& msg) void MessageHandlers::HandleSimu(const Message& msg)
{ {
// Update log // Update log
#if LOG_VERBOSITY > 1 Log::FormatLine(LOG_TRACE, "SIMU", "Message Received (Conn %i)", connection.id);
Log::FormatLine("[SIMU] Message Received (Conn %i)", connection.id);
#endif
char v = msg.GetBuffer()[5]; char v = msg.GetBuffer()[5];
if (v < 0 || v > 2) if (v < 0 || v > 2)
{ {
#if LOG_VERBOSITY > 0 Log::FormatLine(LOG_ERROR, "SIMU", "ERROR: Invalid argument: %i", v);
Log::FormatLine("[SIMU] ERROR: Invalid argument: %i", v);
return; return;
#endif
} }
int value[20]; int value[20];
@@ -589,28 +547,24 @@ namespace XPC
// Set DREF // Set DREF
DataManager::Set(DREF_Pause, value, 20); DataManager::Set(DREF_Pause, value, 20);
#if LOG_VERBOSITY > 2
switch (v) switch (v)
{ {
case 0: case 0:
Log::WriteLine("[SIMU] Simulation Resumed"); Log::WriteLine(LOG_INFO, "SIMU", "Simulation resumed");
break; break;
case 1: case 1:
Log::WriteLine("[SIMU] Simulation Paused"); Log::WriteLine(LOG_INFO, "SIMU", "Simulation paused");
break; break;
case 2: case 2:
Log::WriteLine("[SIMU] Simulation switched."); Log::WriteLine(LOG_INFO, "SIMU", "Simulation switched");
break; break;
} }
#endif
} }
void MessageHandlers::HandleText(const Message& msg) void MessageHandlers::HandleText(const Message& msg)
{ {
// Update Log // Update Log
#if LOG_VERBOSITY > 0 Log::FormatLine(LOG_TRACE, "TEXT", "Message Received (Conn %i)", connection.id);
Log::FormatLine("[TEXT] Message Received (Conn %i)", connection.id);
#endif
std::size_t len = msg.GetSize(); std::size_t len = msg.GetSize();
const unsigned char* buffer = msg.GetBuffer(); const unsigned char* buffer = msg.GetBuffer();
@@ -618,18 +572,14 @@ namespace XPC
char text[256] = { 0 }; char text[256] = { 0 };
if (len < 14) if (len < 14)
{ {
#if LOG_VERBOSITY > 1 Log::WriteLine(LOG_ERROR, "TEXT", "ERROR: Length less than 14 bytes");
Log::WriteLine("[TEXT] ERROR: Length less than 14 bytes");
#endif
return; return;
} }
size_t msgLen = (unsigned char)buffer[13]; size_t msgLen = (unsigned char)buffer[13];
if (msgLen == 0) if (msgLen == 0)
{ {
Drawing::ClearMessage(); Drawing::ClearMessage();
#if LOG_VERBOSITY > 2 Log::WriteLine(LOG_INFO, "TEXT", "[TEXT] Text cleared");
Log::WriteLine("[TEXT] Text cleared");
#endif
} }
else else
{ {
@@ -637,25 +587,19 @@ namespace XPC
int y = *((int*)(buffer + 9)); int y = *((int*)(buffer + 9));
strncpy(text, (char*)buffer + 14, msgLen); strncpy(text, (char*)buffer + 14, msgLen);
Drawing::SetMessage(x, y, text); Drawing::SetMessage(x, y, text);
#if LOG_VERBOSITY > 2 Log::WriteLine(LOG_INFO, "TEXT", "[TEXT] Text set");
Log::WriteLine("[TEXT] Text set");
#endif
} }
} }
void MessageHandlers::HandleView(const Message& msg) void MessageHandlers::HandleView(const Message& msg)
{ {
// Update Log // Update Log
#if LOG_VERBOSITY > 0 Log::FormatLine(LOG_TRACE, "VIEW", "Message Received(Conn %i)", connection.id);
Log::FormatLine("[VIEW] Message Received (Conn %i)", connection.id);
#endif
const std::size_t size = msg.GetSize(); const std::size_t size = msg.GetSize();
if (size != 9) if (size != 9)
{ {
#if LOG_VERBOSITY > 1 Log::FormatLine(LOG_ERROR, "VIEW", "Error: Unexpected length. Message was %d bytes, expected 9.", size);
Log::FormatLine("[VIEW] Error: Unexpected length. Message was %d bytes, expected 9.", size);
#endif
return; return;
} }
const unsigned char* buffer = msg.GetBuffer(); const unsigned char* buffer = msg.GetBuffer();
@@ -666,9 +610,7 @@ namespace XPC
void MessageHandlers::HandleWypt(const Message& msg) void MessageHandlers::HandleWypt(const Message& msg)
{ {
// Update Log // Update Log
#if LOG_VERBOSITY > 0 Log::FormatLine(LOG_TRACE, "WYPT", "Message Received (Conn %i)", connection.id);
Log::FormatLine("[WYPT] Message Received (Conn %i)", connection.id);
#endif
// Parse data // Parse data
const unsigned char* buffer = msg.GetBuffer(); const unsigned char* buffer = msg.GetBuffer();
@@ -685,9 +627,7 @@ namespace XPC
} }
// Perform operation // Perform operation
#if LOG_VERBOSITY > 2 Log::FormatLine(LOG_INFO, "WYPT", "Performing operation %i", op);
Log::FormatLine("[WYPT] Performing operation %i", op);
#endif
switch (op) switch (op)
{ {
case 1: case 1:
@@ -700,18 +640,14 @@ namespace XPC
Drawing::ClearWaypoints(); Drawing::ClearWaypoints();
break; break;
default: default:
#if LOG_VERBOSITY > 1 Log::FormatLine(LOG_ERROR, "WYPT", "ERROR: %i is not a valid operation.", op);
Log::FormatLine("[WYPT] ERROR: %i is not a valid operation.", op);
#endif
break; break;
} }
} }
void MessageHandlers::HandleXPlaneData(const Message& msg) void MessageHandlers::HandleXPlaneData(const Message& msg)
{ {
#if LOG_VERBOSITY > 1 Log::WriteLine(LOG_TRACE, "MSGH", "Sending raw data to X - Plane");
Log::WriteLine("[MSGH] Sending raw data to X-Plane");
#endif
sockaddr_in loopback; sockaddr_in loopback;
loopback.sin_family = AF_INET; loopback.sin_family = AF_INET;
loopback.sin_addr.s_addr = htonl(INADDR_LOOPBACK); loopback.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
@@ -721,9 +657,6 @@ namespace XPC
void MessageHandlers::HandleUnknown(const Message& msg) void MessageHandlers::HandleUnknown(const Message& msg)
{ {
// UPDATE LOG Log::FormatLine(LOG_ERROR, "MSGH", "ERROR: Unknown packet type %s", msg.GetHead().c_str());
#if LOG_VERBOSITY > 0
Log::FormatLine("[EXEC] ERROR: Unknown packet type %s", msg.GetHead().c_str());
#endif
} }
} }

View File

@@ -8,11 +8,11 @@
namespace XPC namespace XPC
{ {
const static std::string tag = "SOCK";
UDPSocket::UDPSocket(unsigned short recvPort) UDPSocket::UDPSocket(unsigned short recvPort)
{ {
#if LOG_VERBOSITY > 1 Log::FormatLine(LOG_TRACE, tag, "Opening socket (port:%d)", recvPort);
Log::FormatLine("[SOCK] Opening socket (port:%d)", recvPort);
#endif
// Setup Port // Setup Port
struct sockaddr_in localAddr; struct sockaddr_in localAddr;
localAddr.sin_family = AF_INET; localAddr.sin_family = AF_INET;
@@ -25,18 +25,14 @@ namespace XPC
int startResult = WSAStartup(MAKEWORD(2, 2), &wsa); int startResult = WSAStartup(MAKEWORD(2, 2), &wsa);
if (startResult != 0) if (startResult != 0)
{ {
#if LOG_VERBOSITY > 0 Log::FormatLine(LOG_FATAL, tag, "ERROR: WSAStartup failed with error code %i.", startResult);
Log::FormatLine("[SOCK] ERROR: WSAStartup failed with error code %i.", startResult);
#endif
this->sock = ~0; this->sock = ~0;
return; return;
} }
if ((this->sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == INVALID_SOCKET) if ((this->sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == INVALID_SOCKET)
{ {
#if LOG_VERBOSITY > 0
int err = WSAGetLastError(); int err = WSAGetLastError();
Log::FormatLine("[SOCK] ERROR: Failed to open socket. (Error code %i)", err); Log::FormatLine(LOG_FATAL, tag, "ERROR: Failed to open socket. (Error code %i)", err);
#endif
return; return;
} }
#elif (__APPLE__ || __linux) #elif (__APPLE__ || __linux)
@@ -52,10 +48,10 @@ namespace XPC
if (bind(this->sock, (struct sockaddr*)&localAddr, sizeof(localAddr)) != 0) if (bind(this->sock, (struct sockaddr*)&localAddr, sizeof(localAddr)) != 0)
{ {
#ifdef _WIN32 #ifdef _WIN32
#if LOG_VERBOSITY > 0
int err = WSAGetLastError(); int err = WSAGetLastError();
Log::FormatLine("[SOCK] ERROR: Failed to bind socket. (Error code %i)", err); Log::FormatLine(LOG_FATAL, tag, "ERROR: Failed to bind socket. (Error code %i)", err);
#endif #else
Log::WriteLine(LOG_FATAL, tag, "ERROR: Failed to bind socket.");
#endif #endif
return; return;
} }
@@ -67,10 +63,8 @@ namespace XPC
DWORD msTimeOutWin = 1; // Minimum socket timeout in Windows is 1ms DWORD msTimeOutWin = 1; // Minimum socket timeout in Windows is 1ms
if(setsockopt(this->sock, SOL_SOCKET, SO_RCVTIMEO, (char *)&msTimeOutWin, sizeof(msTimeOutWin)) != 0) if(setsockopt(this->sock, SOL_SOCKET, SO_RCVTIMEO, (char *)&msTimeOutWin, sizeof(msTimeOutWin)) != 0)
{ {
#if LOG_VERBOSITY > 1
int err = WSAGetLastError(); int err = WSAGetLastError();
Log::FormatLine("[SOCK] ERROR: Failed to set timeout. (Error code %i)", err); Log::FormatLine(LOG_ERROR, tag, "ERROR: Failed to set timeout. (Error code %i)", err);
#endif
} }
#else #else
struct timeval tv; struct timeval tv;
@@ -82,9 +76,7 @@ namespace XPC
UDPSocket::~UDPSocket() UDPSocket::~UDPSocket()
{ {
#if LOG_VERBOSITY > 1 Log::FormatLine(LOG_TRACE, tag, "Closing socket (%d)", this->sock);
Log::FormatLine("[SOCK] Closing socket (%d)", this->sock);
#endif
#ifdef _WIN32 #ifdef _WIN32
closesocket(this->sock); closesocket(this->sock);
#elif (__APPLE__ || __linux) #elif (__APPLE__ || __linux)
@@ -116,13 +108,11 @@ namespace XPC
// Select Command // Select Command
int result = select(-1, &stReadFDS, (FD_SET *)0, &stExceptFDS, &tv); int result = select(-1, &stReadFDS, (FD_SET *)0, &stExceptFDS, &tv);
#if LOG_VERBOSITY > 1
if (result == SOCKET_ERROR) if (result == SOCKET_ERROR)
{ {
int err = WSAGetLastError(); 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 if (result <= 0) // No Data or error
{ {
return -1; return -1;
@@ -141,15 +131,11 @@ namespace XPC
{ {
if (sendto(sock, (char*)buffer, (int)len, 0, remote, sizeof(*remote)) < 0) if (sendto(sock, (char*)buffer, (int)len, 0, remote, sizeof(*remote)) < 0)
{ {
#if LOG_VERBOSITY > 0 Log::FormatLine(LOG_ERROR, tag, "Send failed. (remote: %s)", GetHost(remote).c_str());
Log::FormatLine("[SOCK] Send failed. (remote: %s)", GetHost(remote).c_str());
#endif
} }
else else
{ {
#if LOG_VERBOSITY > 3 Log::FormatLine(LOG_INFO, tag, "Send failed. (remote: %s)", GetHost(remote).c_str());
Log::FormatLine("[SOCK] Datagram sent. (remote: %s)", GetHost(remote).c_str());
#endif
} }
} }

View File

@@ -106,7 +106,7 @@ PLUGIN_API int XPluginStart(char* outName, char* outSig, char* outDesc)
} }
#endif #endif
XPC::Log::Initialize("1.0.1"); XPC::Log::Initialize("1.0.1");
XPC::Log::WriteLine("[EXEC] Plugin Start"); XPC::Log::WriteLine(LOG_INFO, "EXEC", "Plugin Start");
XPC::DataManager::Initialize(); XPC::DataManager::Initialize();
float interval = -1; // Call every frame 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) PLUGIN_API void XPluginStop(void)
{ {
XPLMUnregisterFlightLoopCallback(XPCFlightLoopCallback, NULL); XPLMUnregisterFlightLoopCallback(XPCFlightLoopCallback, NULL);
XPC::Log::WriteLine("[EXEC] Plugin Shutdown"); XPC::Log::WriteLine(LOG_INFO, "EXEC", "Plugin Shutdown");
XPC::Log::Close(); XPC::Log::Close();
} }
@@ -135,7 +135,7 @@ PLUGIN_API void XPluginDisable(void)
// Stop rendering waypoints to screen. // Stop rendering waypoints to screen.
XPC::Drawing::ClearWaypoints(); 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) PLUGIN_API int XPluginEnable(void)
@@ -144,14 +144,12 @@ PLUGIN_API int XPluginEnable(void)
sock = new XPC::UDPSocket(RECVPORT); sock = new XPC::UDPSocket(RECVPORT);
XPC::MessageHandlers::SetSocket(sock); 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) 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(LOG_INFO, "EXEC", "Debug Logging Enabled (Verbosity: %i)", LOG_LEVEL);
XPC::Log::FormatLine("[EXEC] Debug Logging Enabled (Verbosity: %i)", LOG_VERBOSITY);
#endif
return 1; return 1;
} }
@@ -174,7 +172,7 @@ float XPCFlightLoopCallback(float inElapsedSinceLastCall,
counter++; counter++;
if (benchmarkingSwitch > 1) 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++) for (int i = 0; i < OPS_PER_CYCLE; i++)
@@ -198,14 +196,14 @@ float XPCFlightLoopCallback(float inElapsedSinceLastCall,
#if (__APPLE__) #if (__APPLE__)
lap = (double)mach_absolute_time( ) * timeConvert; lap = (double)mach_absolute_time( ) * timeConvert;
diff_t = lap - start; diff_t = lap - start;
XPC::Log::FormatLine("[BENCH] Runtime %.6f", diff_t); XPC::Log::FormatLine(LOG_INFO, "EXEC", "Runtime %.6f", diff_t);
#endif #endif
} }
} }
if (cyclesToClear != -1 && counter%cyclesToClear == 0) if (cyclesToClear != -1 && counter%cyclesToClear == 0)
{ {
XPC::Log::WriteLine("[EXEC] Cleared UDP Buffer"); XPC::Log::WriteLine(LOG_DEBUG, "EXEC", "Cleared UDP Buffer");
delete sock; delete sock;
sock = new XPC::UDPSocket(RECVPORT); sock = new XPC::UDPSocket(RECVPORT);
} }

View File

@@ -101,7 +101,7 @@
<PrecompiledHeader>NotUsing</PrecompiledHeader> <PrecompiledHeader>NotUsing</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel> <WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization> <Optimization>Disabled</Optimization>
<PreprocessorDefinitions>IBM=1;XPLM200;_DEBUG;_WINDOWS;_USRDLL;XPCPLUGIN_EXPORTS;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions)</PreprocessorDefinitions> <PreprocessorDefinitions>IBM=1;XPLM200;_DEBUG;_WINDOWS;_USRDLL;XPCPLUGIN_EXPORTS;_CRT_SECURE_NO_WARNINGS;LOG_LEVEL=LOG_TRACE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<DebugInformationFormat>OldStyle</DebugInformationFormat> <DebugInformationFormat>OldStyle</DebugInformationFormat>
<MultiProcessorCompilation>true</MultiProcessorCompilation> <MultiProcessorCompilation>true</MultiProcessorCompilation>
<OmitFramePointers /> <OmitFramePointers />