Refactored logging functionality into Log class.

This commit is contained in:
Jason Watkins
2015-04-09 17:44:28 -07:00
committed by jason-watkins
parent be9252ce25
commit a3a82f4a71
9 changed files with 224 additions and 163 deletions

71
xpcPlugin/Log.cpp Normal file
View File

@@ -0,0 +1,71 @@
//Copyright (c) 2013-2015 United States Government as represented by the Administrator of the
//National Aeronautics and Space Administration. All Rights Reserved.
#include "Log.h"
#include <cstdarg>
#include <stdio.h>
#include <time.h>
// Implementation note: I initial 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 fprintf directly.
namespace XPC
{
static void WriteTime(FILE* fd)
{
time_t rawtime;
struct tm* timeinfo;
time(&rawtime);
timeinfo = localtime(&rawtime);
char buffer[23] = { 0 };
// Format is equivalent to [%F %T], but neither of those specifiers is
// supported on Windows as of Visual Studio 13
strftime(buffer, 23, "[%Y-%m-%d %H:%M:%S] ", timeinfo);
fprintf(fd, buffer);
}
void Log::Initialize()
{
}
void Log::WriteLine(const std::string& value)
{
Log::WriteLine(value.c_str());
}
void Log::WriteLine(const char* value)
{
FILE* fd = fopen("xpcLog.txt", "a");
if (!fd)
{
return;
}
WriteTime(fd);
fprintf(fd, "%s\n", value);
fclose(fd);
}
void Log::FormatLine(const char* format, ...)
{
va_list args;
va_start(args, format);
FILE* fd = fopen("xpcLog.txt", "a");
if (!fd)
{
return;
}
WriteTime(fd);
vfprintf(fd, format, args);
fprintf(fd, "\n");
fclose(fd);
va_end(args);
}
}

48
xpcPlugin/Log.h Normal file
View File

@@ -0,0 +1,48 @@
//Copyright (c) 2013-2015 United States Government as represented by the Administrator of the
//National Aeronautics and Space Administration. All Rights Reserved.
#ifndef XPC_LOG_H
#define XPC_LOG_H
#include <string>
#define LOG_VERBOSITY 2
namespace XPC
{
/// Handles logging for the plugin.
///
/// \details Provides functions to write lines to the XPC log file.
/// \author Jason Watkins
/// \version 1.0
/// \since 1.0
/// \date Intial Version: 2015-04-09
/// \date Last Updated: 2015-04-09
class Log
{
public:
/// Initializes the logging component by deleting old log files,
/// writing header information to the log file.
static void Initialize();
/// Writes the C 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, ...);
/// 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);
};
}
#endif

View File

@@ -60,6 +60,7 @@
#define _WINSOCKAPI_ #define _WINSOCKAPI_
#include "Log.h"
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
@@ -92,7 +93,6 @@ short current_connection = -1;
double start,lap; double start,lap;
static double timeConvert = 0.0; static double timeConvert = 0.0;
int benchmarkingSwitch = 0; // 1 = time for operations, 2 = time for op + cycle; int benchmarkingSwitch = 0; // 1 = time for operations, 2 = time for op + cycle;
int debugSwitch = 2;
int cyclesToClear = -1; // Clear message bus every n cycles. -1 == dont clear int cyclesToClear = -1; // Clear message bus every n cycles. -1 == dont clear
int counter = 0; int counter = 0;
@@ -150,8 +150,8 @@ PLUGIN_API int XPluginStart( char * outName,
fprintf(logFile,"\n"); fprintf(logFile,"\n");
fclose(logFile); fclose(logFile);
} }
updateLog("[EXEC] xpcPlugin Start", 22); XPC::Log::WriteLine("[EXEC] xpcPlugin Start");
#if (__APPLE__) #if (__APPLE__)
if ( abs(timeConvert) <= 1e-9 ) // is about 0 if ( abs(timeConvert) <= 1e-9 ) // is about 0
@@ -180,41 +180,40 @@ PLUGIN_API int XPluginStart( char * outName,
PLUGIN_API void XPluginStop(void) PLUGIN_API void XPluginStop(void)
{ {
char logmsg[100] = "[EXEC] xpcPlugin Shutdown";
XPLMUnregisterFlightLoopCallback(MyFlightLoopCallback, NULL); XPLMUnregisterFlightLoopCallback(MyFlightLoopCallback, NULL);
updateLog(logmsg,strlen(logmsg)); XPC::Log::WriteLine("[EXEC] xpcPlugin Shutdown");
} }
PLUGIN_API void XPluginDisable(void) PLUGIN_API void XPluginDisable(void)
{ {
char logmsg[100] = "[EXEC] xpcPlugin Disabled, sockets closed"; // Close sockets
closeUDP(recSocket); closeUDP(recSocket);
closeUDP(sendSocket); closeUDP(sendSocket);
updateLog(logmsg,strlen(logmsg));
// Stop rendering messages to screen.
XPCClearMessage(); XPCClearMessage();
// Stop rendering waypoints to screen.
XPCClearWaypoints();
XPC::Log::WriteLine("[EXEC] xpcPlugin Disabled, sockets closed");
} }
PLUGIN_API int XPluginEnable(void) PLUGIN_API int XPluginEnable(void)
{ {
char logmsg[100] = "[EXEC] xpcPlugin Enabled, sockets opened"; // Open sockets
char IP[16] = "127.0.0.1"; char IP[16] = "127.0.0.1";
recSocket = openUDP(RECVPORT, IP, 49009); recSocket = openUDP(RECVPORT, IP, 49009);
sendSocket = openUDP(SENDPORT, IP, 49099); sendSocket = openUDP(SENDPORT, IP, 49099);
updateLog(logmsg,strlen(logmsg));
memset(logmsg,0,strlen(logmsg)); XPC::Log::WriteLine("[EXEC] xpcPlugin Enabled, sockets opened");
if (benchmarkingSwitch > 0)
if (benchmarkingSwitch>0)
{ {
sprintf(logmsg,"[EXEC] Benchmarking Enabled (Verbosity: %i)",benchmarkingSwitch); XPC::Log::FormatLine("[EXEC] Benchmarking Enabled (Verbosity: %i)", benchmarkingSwitch);
updateLog(logmsg,strlen(logmsg)); }
memset(logmsg,0,strlen(logmsg)); if (LOG_VERBOSITY > 0)
}
if (debugSwitch>0)
{ {
sprintf(logmsg,"[EXEC] Debug Enabled (Verbosity: %i)",debugSwitch); XPC::Log::FormatLine("[EXEC] Debug Logging Enabled (Verbosity: %i)", LOG_VERBOSITY);
updateLog(logmsg,strlen(logmsg));
} }
return 1; return 1;
@@ -233,7 +232,6 @@ float MyFlightLoopCallback( float inElapsedSinceLastCall,
{ {
int i; int i;
short result; short result;
char logmsg[100] = {0};
#if (__APPLE__) #if (__APPLE__)
double diff_t; double diff_t;
#endif #endif
@@ -242,9 +240,7 @@ float MyFlightLoopCallback( float inElapsedSinceLastCall,
counter++; counter++;
if (benchmarkingSwitch > 1) if (benchmarkingSwitch > 1)
{ {
sprintf(logmsg,"Cycle time %.6f",inElapsedSinceLastCall); XPC::Log::FormatLine("Cycle time %.6f", inElapsedSinceLastCall);
updateLog(logmsg,strlen(logmsg));
memset(logmsg,0,strlen(logmsg));
} }
for (i=0;i<OPS_PER_CYCLE;i++) for (i=0;i<OPS_PER_CYCLE;i++)
@@ -263,9 +259,7 @@ float MyFlightLoopCallback( 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;
sprintf(logmsg,"Runtime %.6f",diff_t); XPC::Log::FormatLine("Runtime %.6f",diff_t);
updateLog(logmsg,strlen(logmsg));
memset(logmsg,0,strlen(logmsg));
#endif #endif
} }
@@ -276,8 +270,7 @@ float MyFlightLoopCallback( float inElapsedSinceLastCall,
{ {
if (counter%cyclesToClear==0) if (counter%cyclesToClear==0)
{ {
strcpy(logmsg,"[EXEC] Cleared UDP Buffer"); XPC::Log::WriteLine("[EXEC] Cleared UDP Buffer");
updateLog(logmsg,strlen(logmsg));
char IP[16] = "127.0.0.1"; char IP[16] = "127.0.0.1";
closeUDP(recSocket); closeUDP(recSocket);
recSocket = openUDP(RECVPORT, IP, 49009); recSocket = openUDP(RECVPORT, IP, 49009);
@@ -290,7 +283,6 @@ float MyFlightLoopCallback( float inElapsedSinceLastCall,
short handleInput(struct XPCMessage * theMessage) short handleInput(struct XPCMessage * theMessage)
{ {
int i; int i;
char logmsg[100] = {0};
char IP[16] = {0}; char IP[16] = {0};
unsigned short port = 0; unsigned short port = 0;
@@ -298,7 +290,7 @@ short handleInput(struct XPCMessage * theMessage)
if (theMessage->msglen > 0) // If message received if (theMessage->msglen > 0) // If message received
{ {
if (debugSwitch>0) if (LOG_VERBOSITY > 0)
{ {
#ifdef _WIN32 #ifdef _WIN32
#else #else
@@ -321,7 +313,7 @@ short handleInput(struct XPCMessage * theMessage)
{ {
if (number_of_connections>=MAXCONN) // Handle hitting MAXCONN (COME UP WITH A BETTER SOLUTION) if (number_of_connections>=MAXCONN) // Handle hitting MAXCONN (COME UP WITH A BETTER SOLUTION)
{ {
updateLog("[EXEC] Hit maximum number of connections-Removing old ones",58); XPC::Log::WriteLine("[EXEC] Hit maximum number of connections-Removing old ones");
number_of_connections = 0; number_of_connections = 0;
} }
@@ -332,9 +324,7 @@ short handleInput(struct XPCMessage * theMessage)
number_of_connections ++; number_of_connections ++;
// Log Connection // Log Connection
sprintf(logmsg,"[EXEC] New Connection [%i]. IP=%s, port=%i",number_of_connections,IP, port); XPC::Log::FormatLine("[EXEC] New Connection [%i]. IP=%s, port=%i", number_of_connections, IP, port);
updateLog(logmsg,strlen(logmsg));
memset(logmsg,0,strlen(logmsg));
} }
if (strncmp(theMessage->head,"CONN",4)==0) // Header = CONN (Connection) if (strncmp(theMessage->head,"CONN",4)==0) // Header = CONN (Connection)
@@ -423,8 +413,7 @@ short handleInput(struct XPCMessage * theMessage)
} }
else else
{ //unrecognized header { //unrecognized header
sprintf(logmsg,"[EXEC] ERROR: Command %s not recognized",theMessage->head); XPC::Log::FormatLine("[EXEC] ERROR: Command %s not recognized", theMessage->head);
updateLog(logmsg, strlen(logmsg));
} }
current_connection = -1; current_connection = -1;
} // end if (buflen > 0) } // end if (buflen > 0)
@@ -442,7 +431,6 @@ void sendBUF( char buf[], int buflen)
int handleCONN(char buf[]) int handleCONN(char buf[])
{ {
char logmsg[100] = {0};
char the_message[7]; char the_message[7];
char header[5]; char header[5];
@@ -456,13 +444,15 @@ int handleCONN(char buf[])
if (connectionList[current_connection].recPort <= 1) // Is not valid port if (connectionList[current_connection].recPort <= 1) // Is not valid port
{ {
connectionList[current_connection].recPort = 49008; connectionList[current_connection].recPort = 49008;
return updateLog("[CONN] ERROR: Port Number must be a number (NaN received)",51); XPC::Log::WriteLine("[CONN] ERROR: Port Number must be a number (NaN received)");
return 1;
} }
// UPDATE LOG // UPDATE LOG
sprintf(logmsg,"[CONN] Update Connection %i- Sending to port: %i",current_connection+1,connectionList[current_connection].recPort); XPC::Log::FormatLine("[CONN] Update Connection %i- Sending to port: %i",
updateLog(logmsg,strlen(logmsg)); current_connection + 1,
connectionList[current_connection].recPort);
// SEND CONFIRMATION // SEND CONFIRMATION
// TODO: Ivestigate why sending confirmation causes crashes on Windows 8 // TODO: Ivestigate why sending confirmation causes crashes on Windows 8
@@ -476,24 +466,22 @@ int handleCONN(char buf[])
int handleSIMU(char buf[]) int handleSIMU(char buf[])
{ {
int SIMUArray[1] = {buf[5]}; int SIMUArray[1] = {buf[5]};
char logmsg[100] = {0};
if (SIMUArray[0] != SIMUArray[0]) // Is NaN if (SIMUArray[0] != SIMUArray[0]) // Is NaN
{ {
return updateLog("[SIMU] ERROR: Value must be a number (NaN received)",51); XPC::Log::WriteLine("[SIMU] ERROR: Value must be a number (NaN received)");
return 1;
} }
XPLMSetDatavi(XPLMSwitch, SIMUArray, 0, 1); XPLMSetDatavi(XPLMSwitch, SIMUArray, 0, 1);
if (buf[5] == 0) if (buf[5] == 0)
{ {
sprintf(logmsg,"[SIMU] Simulation Resumed (Conn %i)", current_connection+1); XPC::Log::FormatLine("[SIMU] Simulation Resumed (Conn %i)", current_connection + 1);
updateLog(logmsg,strlen(logmsg));
} }
else else
{ {
sprintf(logmsg,"[SIMU] Simulation Paused (Conn %i)", current_connection+1); XPC::Log::FormatLine("[SIMU] Simulation Paused (Conn %i)", current_connection + 1);
updateLog(logmsg,strlen(logmsg));
} }
return 0; return 0;
@@ -504,14 +492,14 @@ int handleTEXT(char *buf, int len)
char msg[256] = { 0 }; char msg[256] = { 0 };
if (len < 14) if (len < 14)
{ {
updateLog("[TEXT] ERROR: Length less than 14 bytes", 39); XPC::Log::WriteLine("[TEXT] ERROR: Length less than 14 bytes");
return -1; return -1;
} }
size_t msgLen = (unsigned char)buf[13]; size_t msgLen = (unsigned char)buf[13];
if (msgLen == 0) if (msgLen == 0)
{ {
XPCClearMessage(); XPCClearMessage();
updateLog("[TEXT] Text cleared", 19); XPC::Log::WriteLine("[TEXT] Text cleared");
} }
else else
{ {
@@ -519,7 +507,7 @@ int handleTEXT(char *buf, int len)
int y = *((int*)(buf + 9)); int y = *((int*)(buf + 9));
strncpy(msg, buf + 14, msgLen); strncpy(msg, buf + 14, msgLen);
XPCSetMessage(x, y, msg); XPCSetMessage(x, y, msg);
updateLog("[TEXT] Text set", 15); XPC::Log::WriteLine("[TEXT] Text set");
} }
return 0; return 0;
} }
@@ -584,12 +572,15 @@ char setDREF(XPLMDataRef theDREF, float floatarray[], short arrayStart, short ar
} }
} }
else else
return updateLog("[DREF] ERROR: invalid DREF",26); {
XPC::Log::WriteLine("[DREF] ERROR: invalid DREF");
return 1;
}
return 0; return 0;
NANMessage: NANMessage:
return updateLog("[DREF] ERROR: Value must be a number (NaN received)",51); XPC::Log::WriteLine("[DREF] ERROR: Value must be a number (NaN received)");
return 1;
} }
char setGEAR(short aircraft, float gear, char posi) char setGEAR(short aircraft, float gear, char posi)
@@ -604,7 +595,8 @@ char setGEAR(short aircraft, float gear, char posi)
if ( ( gear != gear ) || ( gear < -1.f ) || ( gear > 1.f ) ) // NaN & Positive test if ( ( gear != gear ) || ( gear < -1.f ) || ( gear > 1.f ) ) // NaN & Positive test
{ {
return updateLog("[GEAR] ERROR: Value must be 0 or 1",51); XPC::Log::WriteLine("[GEAR] ERROR: Value must be 0 or 1");
return 1;
} }
for (i=0;i<8;i++) for (i=0;i<8;i++)
@@ -637,7 +629,8 @@ char setPOSI(short aircraft, float pos[3])
if (tPos != tPos) // Is NaN if (tPos != tPos) // Is NaN
{ {
return updateLog("[POSI] ERROR: Position must be a number (NaN received)",51); XPC::Log::WriteLine("[POSI] ERROR: Position must be a number (NaN received)");
return 1;
} }
XPLMWorldToLocal(pos[0],pos[1],pos[2],&local[0],&local[1],&local[2]); XPLMWorldToLocal(pos[0],pos[1],pos[2],&local[0],&local[1],&local[2]);
@@ -669,7 +662,8 @@ char setORIENT(short aircraft, float orient[3])
if ( tOrient != tOrient ) // Is NaN if ( tOrient != tOrient ) // Is NaN
{ {
return updateLog("[ORIENT] ERROR: Orientation must be a number (NaN received)",53); XPC::Log::WriteLine("[ORIENT] ERROR: Orientation must be a number (NaN received)");
return 1;
} }
if ( aircraft <= 0 ) // Main aircraft if ( aircraft <= 0 ) // Main aircraft
@@ -711,7 +705,6 @@ char setFLAP(float flap)
int handlePOSI(char buf[]) int handlePOSI(char buf[])
{ {
char logmsg[100];
float position[8] = {0.0}; float position[8] = {0.0};
float pos[3],orient[3]; float pos[3],orient[3];
short aircraft = 0; short aircraft = 0;
@@ -719,8 +712,7 @@ int handlePOSI(char buf[])
int autopilot[20] = {0}; int autopilot[20] = {0};
// UPDATE LOG // UPDATE LOG
sprintf(logmsg,"[POSI] Message Received (Conn %i)", current_connection+1); XPC::Log::FormatLine("[POSI] Message Received (Conn %i)", current_connection + 1);
updateLog(logmsg, strlen(logmsg));
aircraft = fmini(parsePOSI(buf,position,6, &gear),19); aircraft = fmini(parsePOSI(buf,position,6, &gear),19);
@@ -751,14 +743,12 @@ int handlePOSI(char buf[])
int handleCTRL(char buf[]) int handleCTRL(char buf[])
{ {
char logmsg[100];
xpcCtrl ctrl; xpcCtrl ctrl;
float thr[8] = { 0 }; float thr[8] = { 0 };
short i; short i;
// UPDATE LOG // UPDATE LOG
sprintf(logmsg,"[CTRL] Message Received (Conn %i)", current_connection+1); XPC::Log::FormatLine("[CTRL] Message Received (Conn %i)", current_connection + 1);
updateLog(logmsg, strlen(logmsg));
ctrl = parseCTRL(buf); ctrl = parseCTRL(buf);
if (ctrl.aircraft < 0) //parseCTRL failed if (ctrl.aircraft < 0) //parseCTRL failed
@@ -831,21 +821,17 @@ int handleCTRL(char buf[])
int handleWYPT(char buf[], int len) int handleWYPT(char buf[], int len)
{ {
// UPDATE LOG // UPDATE LOG
char logmsg[100]; XPC::Log::FormatLine("[WYPT] Message Received (Conn %i)", current_connection + 1);
sprintf(logmsg,"[WYPT] Message Received (Conn %i)", current_connection+1);
updateLog(logmsg, strlen(logmsg));
xpcWypt wypt = parseWYPT(buf); xpcWypt wypt = parseWYPT(buf);
if (wypt.op < 0) if (wypt.op < 0)
{ {
sprintf(logmsg, "[WYPT] Failed to parse command. ERR:%i", wypt.op); XPC::Log::FormatLine("[WYPT] Failed to parse command. ERR:%i", wypt.op);
updateLog(logmsg, strlen(logmsg));
return -1; return -1;
} }
else else
{ {
sprintf(logmsg, "[WYPT] Performing operation %i", wypt.op); XPC::Log::FormatLine("[WYPT] Performing operation %i", wypt.op);
updateLog(logmsg, strlen(logmsg));
} }
switch (wypt.op) switch (wypt.op)
@@ -869,7 +855,6 @@ int handleGETD(char buf[])
{ {
int length,i,k; int length,i,k;
XPLMDataTypeID dataType; XPLMDataTypeID dataType;
char logmsg[400] = {0};
int listLength = buf[5]; int listLength = buf[5];
char the_message[5000]; char the_message[5000];
char header[5] = {0}; char header[5] = {0};
@@ -884,14 +869,14 @@ int handleGETD(char buf[])
if (listLength == 0) // USE LAST REQUEST if (listLength == 0) // USE LAST REQUEST
{ {
sprintf(logmsg,"[GETD] DATA Requested- repeat last request from connection %i (%i data refs)",current_connection+1,connectionList[current_connection].requestLength); XPC::Log::FormatLine("[GETD] DATA Requested- repeat last request from connection %i (%i data refs)",
current_connection + 1,
connectionList[current_connection].requestLength);
if (connectionList[current_connection].requestLength < 0) // No past requests if (connectionList[current_connection].requestLength < 0) // No past requests
{ {
updateLog(logmsg, strlen(logmsg)); XPC::Log::FormatLine("[GETD] ERROR- No previous requests from connection %i.", current_connection + 1);
memset(logmsg,0,strlen(logmsg)); return 1;
sprintf(logmsg,"[GETD] ERROR- No previous requests from connection %i.",current_connection+1);
return updateLog(logmsg, strlen(logmsg));
} }
} }
else if (listLength > 0) // NEW REQUEST else if (listLength > 0) // NEW REQUEST
@@ -905,17 +890,15 @@ int handleGETD(char buf[])
} }
parseGETD(buf,DREFArray,DREFSizes); parseGETD(buf,DREFArray,DREFSizes);
sprintf(logmsg,"[GETD] DATA Requested- New Request for connection %i [%i]:",current_connection+1,listLength); XPC::Log::FormatLine("[GETD] DATA Requested- New Request for connection %i [%i]:",
current_connection + 1,
listLength);
} }
else else
{ {
return -1; return -1;
} }
// Update Log
updateLog(logmsg,strlen(logmsg));
memset(logmsg,0,strlen(logmsg));
for (i=0;i<connectionList[current_connection].requestLength;i++) for (i=0;i<connectionList[current_connection].requestLength;i++)
{ {
if (listLength > 0) if (listLength > 0)
@@ -962,8 +945,7 @@ int handleGETD(char buf[])
} }
else else
{ {
sprintf(logmsg,"%s-ERROR: invalid DREF",DREFArray[i]); XPC::Log::FormatLine("%s-ERROR: invalid DREF", DREFArray[i]);
updateLog(logmsg, strlen(logmsg));
} }
} }
the_message[5] = (char) connectionList[current_connection].requestLength; the_message[5] = (char) connectionList[current_connection].requestLength;
@@ -980,9 +962,7 @@ int handleGETD(char buf[])
} }
int handleDREF(char buf[]) int handleDREF(char buf[])
{ {
char logmsg[400]={0};
char DREF[100] = {0}; char DREF[100] = {0};
unsigned short lenDREF = 0; unsigned short lenDREF = 0;
unsigned short lenVALUE = 0; unsigned short lenVALUE = 0;
@@ -998,8 +978,7 @@ int handleDREF(char buf[])
} }
// Handle DREF // Handle DREF
sprintf(logmsg,"[DREF] Request to set DREF value received (Conn %i): %s",current_connection+1,DREF); XPC::Log::FormatLine("[DREF] Request to set DREF value received (Conn %i): %s", current_connection + 1, DREF);
updateLog(logmsg,strlen(logmsg));
theDREF = XPLMFindDataRef(DREF); theDREF = XPLMFindDataRef(DREF);
setDREF(theDREF, values, 0, lenVALUE); setDREF(theDREF, values, 0, lenVALUE);
@@ -1009,19 +988,14 @@ int handleDREF(char buf[])
int handleVIEW() int handleVIEW()
{ {
char logmsg[100]; XPC::Log::FormatLine("[VIEW] Message Received (Conn %i)- VIEW FEATURE UNDER CONSTRUCTION",
current_connection + 1);
// UPDATE LOG
sprintf(logmsg,"[VIEW] Message Received (Conn %i)- VIEW FEATURE UNDER CONSTRUCTION",current_connection+1);
updateLog(logmsg, strlen(logmsg));
return 0; return 0;
} }
int handleDATA(char buf[], int buflen) int handleDATA(char buf[], int buflen)
{ {
int i,j,lines; int i,j,lines;
char logmsg[100] = {0};
float floatArray[10] = {0}; float floatArray[10] = {0};
float recValues[20][9] = {0}; float recValues[20][9] = {0};
const float deg2rad = (float) 0.0174532925; const float deg2rad = (float) 0.0174532925;
@@ -1032,15 +1006,12 @@ int handleDATA(char buf[], int buflen)
if (lines > 0) if (lines > 0)
{ {
// UPDATE LOG // UPDATE LOG
sprintf(logmsg,"[DATA] Message Received (Conn %i)",current_connection+1); XPC::Log::FormatLine("[DATA] Message Received (Conn %i)", current_connection + 1);
updateLog(logmsg, strlen(logmsg));
memset(logmsg,0,strlen(logmsg));
} }
else else
{ {
// UPDATE LOG // UPDATE LOG
sprintf(logmsg,"[DATA] WARNING: Empty data packet received (Conn %i)",current_connection+1); XPC::Log::FormatLine("[DATA] WARNING: Empty data packet received (Conn %i)", current_connection + 1);
return updateLog(logmsg, strlen(logmsg));
} }
for (i = 0; i<lines; i++) //Execute changes one line at a time for (i = 0; i<lines; i++) //Execute changes one line at a time
@@ -1049,9 +1020,7 @@ int handleDATA(char buf[], int buflen)
if ( dataRef<0 || dataRef >134 ) if ( dataRef<0 || dataRef >134 )
{ // DREF Check 1: This ensures that the received dataRef is in the range of 0-134 { // DREF Check 1: This ensures that the received dataRef is in the range of 0-134
sprintf(logmsg,"[DATA] ERROR: DataRef # must be between 0-134 (Rec: %hi)",dataRef); XPC::Log::FormatLine("[DATA] ERROR: DataRef # must be between 0 - 134 (Rec: %hi)",dataRef);
updateLog(logmsg,strlen(logmsg));
memset(logmsg,0,strlen(logmsg));
continue; continue;
} }
@@ -1077,7 +1046,7 @@ int handleDATA(char buf[], int buflen)
if ( ( hpath != hpath ) && ( alpha != alpha ) && ( theta != theta ) ) // NaN Check if ( ( hpath != hpath ) && ( alpha != alpha ) && ( theta != theta ) ) // NaN Check
{ {
updateLog("[DATA] ERROR: Value must be a number (NaN received)",51); XPC::Log::WriteLine("[DATA] ERROR: Value must be a number (NaN received)");
break; break;
} }
@@ -1109,7 +1078,7 @@ int handleDATA(char buf[], int buflen)
{case 18: // Alpha, hpath etc. {case 18: // Alpha, hpath etc.
if ( ( recValues[i][1] != recValues[i][1] ) && ( recValues[i][3] != recValues[i][3] ) ) // NaN Check if ( ( recValues[i][1] != recValues[i][1] ) && ( recValues[i][3] != recValues[i][3] ) ) // NaN Check
{ {
updateLog("[DATA] ERROR: Value must be a number (NaN received)",51); XPC::Log::WriteLine("[DATA] ERROR: Value must be a number (NaN received)");
break; break;
} }
@@ -1136,7 +1105,7 @@ int handleDATA(char buf[], int buflen)
{case 25: // Throttle {case 25: // Throttle
if ( recValues[i][1] != recValues[i][1] ) if ( recValues[i][1] != recValues[i][1] )
{ {
updateLog("[DATA] ERROR: Value must be a number (NaN received)",51); XPC::Log::WriteLine("[DATA] ERROR: Value must be a number (NaN received)");
break; break;
} }
@@ -1150,11 +1119,9 @@ int handleDATA(char buf[], int buflen)
memcpy(floatArray,&recValues[i][1],8*sizeof(float)); memcpy(floatArray,&recValues[i][1],8*sizeof(float));
for (j=0; j<8; j++) for (j=0; j<8; j++)
{ {
if (debugSwitch > 0) if (LOG_VERBOSITY > 0)
{ {
char theString[100] = {0}; XPC::Log::FormatLine("Setting Dataref %i.%i to %f", dataRef, j, floatArray[j]);
sprintf(theString,"Setting Dataref %i.%i to %f",dataRef,j,floatArray[j]);
updateLog(theString, strlen(theString));
} }
if (dataRef==14 && j==0) if (dataRef==14 && j==0)

Binary file not shown.

Binary file not shown.

View File

@@ -66,7 +66,7 @@
<FunctionLevelLinking>false</FunctionLevelLinking> <FunctionLevelLinking>false</FunctionLevelLinking>
<DisableSpecificWarnings>4996</DisableSpecificWarnings> <DisableSpecificWarnings>4996</DisableSpecificWarnings>
<MinimalRebuild>false</MinimalRebuild> <MinimalRebuild>false</MinimalRebuild>
<RuntimeLibrary>MultiThreaded</RuntimeLibrary> <RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
</ClCompile> </ClCompile>
<Link> <Link>
<SubSystem>Windows</SubSystem> <SubSystem>Windows</SubSystem>
@@ -89,7 +89,7 @@
<FunctionLevelLinking>false</FunctionLevelLinking> <FunctionLevelLinking>false</FunctionLevelLinking>
<DisableSpecificWarnings>4996</DisableSpecificWarnings> <DisableSpecificWarnings>4996</DisableSpecificWarnings>
<MinimalRebuild>false</MinimalRebuild> <MinimalRebuild>false</MinimalRebuild>
<RuntimeLibrary>MultiThreaded</RuntimeLibrary> <RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
</ClCompile> </ClCompile>
<Link> <Link>
<SubSystem>Windows</SubSystem> <SubSystem>Windows</SubSystem>
@@ -99,11 +99,13 @@
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="..\..\C\src\xplaneConnect.h" /> <ClInclude Include="..\..\C\src\xplaneConnect.h" />
<ClInclude Include="..\Log.h" />
<ClInclude Include="..\xpcDrawing.h" /> <ClInclude Include="..\xpcDrawing.h" />
<ClInclude Include="..\xpcPluginTools.h" /> <ClInclude Include="..\xpcPluginTools.h" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="..\..\C\src\xplaneConnect.c" /> <ClCompile Include="..\..\C\src\xplaneConnect.c" />
<ClCompile Include="..\Log.cpp" />
<ClCompile Include="..\xpcDrawing.cpp" /> <ClCompile Include="..\xpcDrawing.cpp" />
<ClCompile Include="..\XPCPlugin.cpp" /> <ClCompile Include="..\XPCPlugin.cpp" />
<ClCompile Include="..\xpcPluginTools.cpp" /> <ClCompile Include="..\xpcPluginTools.cpp" />

View File

@@ -24,6 +24,9 @@
<ClInclude Include="..\xpcDrawing.h"> <ClInclude Include="..\xpcDrawing.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="..\Log.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="..\..\C\src\xplaneConnect.c"> <ClCompile Include="..\..\C\src\xplaneConnect.c">
@@ -38,6 +41,9 @@
<ClCompile Include="..\xpcDrawing.cpp"> <ClCompile Include="..\xpcDrawing.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="..\Log.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Library Include="..\SDK\Libraries\Win\XPLM.lib"> <Library Include="..\SDK\Libraries\Win\XPLM.lib">

View File

@@ -24,6 +24,7 @@
// //
// BEGIN CODE // BEGIN CODE
#include "Log.h"
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
@@ -286,33 +287,6 @@ int almostequal(float arg1, float arg2, float tol)
return (abs(arg1-arg2)<tol); return (abs(arg1-arg2)<tol);
} }
int updateLog(const char *buffer, int length)
{
// Writes buffer to logfile (xpcLog.txt)
time_t rawtime;
struct tm * timeinfo;
char logBuffer[523] = { 0 };
FILE * logFile;
logFile = fopen("xpcLog.txt","a");
time(&rawtime);
timeinfo = localtime(&rawtime);
// Format is equivalent to [%F %T], but neither of those specifiers is
// supported on Windows as of Visual Studio 13
strftime(logBuffer, 523, "[%Y-%m-%d %H:%M:%S] ", timeinfo);
length = length < 500 ? length : 500;
memcpy(&(logBuffer[22]), buffer, length);
fprintf(logFile,"%s\n",logBuffer);
fclose(logFile);
return 1;
}
unsigned short getIP(struct sockaddr recvaddr, char *IP) unsigned short getIP(struct sockaddr recvaddr, char *IP)
{ {
// Gets the IP Address from sockaddr // Gets the IP Address from sockaddr
@@ -337,7 +311,7 @@ int printBufferToLog(struct XPCMessage & msg)
sprintf(logmsg,"%s %hhu",logmsg,msg.msg[i]); sprintf(logmsg,"%s %hhu",logmsg,msg.msg[i]);
} }
updateLog(logmsg,strlen(logmsg)); XPC::Log::WriteLine(logmsg);
memset(logmsg,0,strlen(logmsg)); memset(logmsg,0,strlen(logmsg));
sprintf(logmsg,"[%s-DEBUG](%i)",msg.head,msg.msg[4]); sprintf(logmsg,"[%s-DEBUG](%i)",msg.head,msg.msg[4]);
@@ -345,13 +319,13 @@ int printBufferToLog(struct XPCMessage & msg)
//Switch for header //Switch for header
if (strncmp(msg.head,"CONN",4)==0) if (strncmp(msg.head,"CONN",4)==0)
{// Header = CONN (Connection) {// Header = CONN (Connection)
updateLog(logmsg,strlen(logmsg)); XPC::Log::WriteLine(logmsg);
} }
else if (strncmp(msg.head,"SIMU",4)==0) else if (strncmp(msg.head,"SIMU",4)==0)
{// Header = SIMU {// Header = SIMU
sprintf(logmsg,"%s %i",logmsg,msg.msg[5]); sprintf(logmsg,"%s %i",logmsg,msg.msg[5]);
updateLog(logmsg,strlen(logmsg)); XPC::Log::WriteLine(logmsg);
} }
else if (strncmp(msg.head,"POSI",4)==0) else if (strncmp(msg.head,"POSI",4)==0)
@@ -374,7 +348,7 @@ int printBufferToLog(struct XPCMessage & msg)
sprintf(logmsg,"%s %i (%f %f %f) (%f %f %f) %f",logmsg,aircraft,pos[0],pos[1],pos[2],orient[0],orient[1],orient[2],gear); sprintf(logmsg,"%s %i (%f %f %f) (%f %f %f) %f",logmsg,aircraft,pos[0],pos[1],pos[2],orient[0],orient[1],orient[2],gear);
updateLog(logmsg,strlen(logmsg)); XPC::Log::WriteLine(logmsg);
} }
else if (strncmp(msg.head,"CTRL",4)==0) else if (strncmp(msg.head,"CTRL",4)==0)
{// Header = CTRL (Control) {// Header = CTRL (Control)
@@ -382,13 +356,13 @@ int printBufferToLog(struct XPCMessage & msg)
sprintf(logmsg,"%s (%f %f %f) %f %hhi %f",logmsg, ctrl.pitch, ctrl.roll, ctrl.yaw, ctrl.throttle, ctrl.gear, ctrl.flaps); sprintf(logmsg,"%s (%f %f %f) %f %hhi %f",logmsg, ctrl.pitch, ctrl.roll, ctrl.yaw, ctrl.throttle, ctrl.gear, ctrl.flaps);
updateLog(logmsg,strlen(logmsg)); XPC::Log::WriteLine(logmsg);
} }
else if (strncmp(msg.head,"WYPT",4)==0) else if (strncmp(msg.head,"WYPT",4)==0)
{// Header = WYPT (Waypoint Draw) {// Header = WYPT (Waypoint Draw)
updateLog(logmsg,strlen(logmsg)); XPC::Log::WriteLine(logmsg);
} }
else if (strncmp(msg.head,"GETD",4)==0) else if (strncmp(msg.head,"GETD",4)==0)
@@ -396,7 +370,7 @@ int printBufferToLog(struct XPCMessage & msg)
char DREF[100]; char DREF[100];
int counter = 6; int counter = 6;
updateLog(logmsg,strlen(logmsg)); XPC::Log::WriteLine(logmsg);
memset(logmsg,0,strlen(logmsg)); memset(logmsg,0,strlen(logmsg));
for (i=0;i<msg.msg[5];i++) for (i=0;i<msg.msg[5];i++)
@@ -404,7 +378,7 @@ int printBufferToLog(struct XPCMessage & msg)
memcpy(DREF,&msg.msg[counter+1],msg.msg[counter]); memcpy(DREF,&msg.msg[counter+1],msg.msg[counter]);
sprintf(logmsg,"\t#%i/%i (size:%i) %s",i+1,msg.msg[5],msg.msg[counter],DREF); sprintf(logmsg,"\t#%i/%i (size:%i) %s",i+1,msg.msg[5],msg.msg[counter],DREF);
updateLog(logmsg,strlen(logmsg)); XPC::Log::WriteLine(logmsg);
memset(logmsg,0,strlen(logmsg)); memset(logmsg,0,strlen(logmsg));
memset(DREF,0,sizeof(DREF)); memset(DREF,0,sizeof(DREF));
@@ -415,12 +389,12 @@ int printBufferToLog(struct XPCMessage & msg)
{// Header = DREF (By Data Ref) (this is slower than DATA) {// Header = DREF (By Data Ref) (this is slower than DATA)
char DREF[100]={0}; char DREF[100]={0};
float tmp; float tmp;
updateLog(logmsg,strlen(logmsg)); XPC::Log::WriteLine(logmsg);
memset(logmsg,0,strlen(logmsg)); memset(logmsg,0,strlen(logmsg));
memcpy(DREF,&msg.msg[6],msg.msg[5]); memcpy(DREF,&msg.msg[6],msg.msg[5]);
sprintf(logmsg,"-\tDREF (size %i)= %s",msg.msg[5],DREF); sprintf(logmsg,"-\tDREF (size %i)= %s",msg.msg[5],DREF);
updateLog(logmsg,strlen(logmsg)); XPC::Log::WriteLine(logmsg);
memset(logmsg,0,strlen(logmsg)); memset(logmsg,0,strlen(logmsg));
sprintf(logmsg,"-\tValues(Size %i)=",msg.msg[6+msg.msg[5]]); sprintf(logmsg,"-\tValues(Size %i)=",msg.msg[6+msg.msg[5]]);
for (i=0;i<msg.msg[6+msg.msg[5]];i++) for (i=0;i<msg.msg[6+msg.msg[5]];i++)
@@ -428,11 +402,11 @@ int printBufferToLog(struct XPCMessage & msg)
memcpy(&tmp,&msg.msg[7+msg.msg[5]+sizeof(float)*i],sizeof(float)); memcpy(&tmp,&msg.msg[7+msg.msg[5]+sizeof(float)*i],sizeof(float));
sprintf(logmsg,"%s %f",logmsg,tmp); sprintf(logmsg,"%s %f",logmsg,tmp);
} }
updateLog(logmsg,strlen(logmsg)); XPC::Log::WriteLine(logmsg);
} }
else if (strncmp(msg.head,"VIEW",4)==0) else if (strncmp(msg.head,"VIEW",4)==0)
{// Header = VIEW (Change View) {// Header = VIEW (Change View)
updateLog(logmsg,strlen(logmsg)); XPC::Log::WriteLine(logmsg);
} }
else if (strncmp(msg.head,"DATA",4)==0) else if (strncmp(msg.head,"DATA",4)==0)
{// Header = DATA (UDP Data) {// Header = DATA (UDP Data)
@@ -441,7 +415,7 @@ int printBufferToLog(struct XPCMessage & msg)
float dataRef[30][9]; float dataRef[30][9];
sprintf(logmsg,"%s (%i lines)",logmsg,totalColumns); sprintf(logmsg,"%s (%i lines)",logmsg,totalColumns);
updateLog(logmsg,strlen(logmsg)); XPC::Log::WriteLine(logmsg);
memset(logmsg,0,strlen(logmsg)); memset(logmsg,0,strlen(logmsg));
totalColumns = parseDATA(msg.msg, msg.msg[4], dataRef); totalColumns = parseDATA(msg.msg, msg.msg[4], dataRef);
@@ -455,7 +429,7 @@ int printBufferToLog(struct XPCMessage & msg)
sprintf(logmsg,"%s %f",logmsg,dataRef[i][j]); sprintf(logmsg,"%s %f",logmsg,dataRef[i][j]);
if (dataRef[i][j]!=dataRef[i][j]) sprintf(logmsg,"%s (not a number)",logmsg); if (dataRef[i][j]!=dataRef[i][j]) sprintf(logmsg,"%s (not a number)",logmsg);
} }
updateLog(logmsg,strlen(logmsg)); XPC::Log::WriteLine(logmsg);
memset(logmsg,0,strlen(logmsg)); memset(logmsg,0,strlen(logmsg));
} }
} }
@@ -470,12 +444,10 @@ int printBufferToLog(struct XPCMessage & msg)
int test(const char *buffer) int test(const char *buffer)
{ {
// Prints "test buffer" to log (for debugging) // Prints "test buffer" to log (for debugging)
char logmsg[100] = {0};
char buffer2[95] = {0}; char buffer2[95] = {0};
strncpy(buffer2, buffer, 95); strncpy(buffer2, buffer, 95);
sprintf(logmsg,"[TEST] %s",buffer2);
updateLog(logmsg,strlen(logmsg)); XPC::Log::FormatLine("[TEST] %s", buffer2);
return 0; return 0;
} }
@@ -483,10 +455,7 @@ int test(const char *buffer)
int test(int buffer) int test(int buffer)
{ {
// Prints "test #[buffer]" to log (for debugging) // Prints "test #[buffer]" to log (for debugging)
char logmsg[100] = {0}; XPC::Log::FormatLine("[TEST] #%i", buffer);
sprintf(logmsg,"[TEST] #%i",buffer);
updateLog(logmsg,strlen(logmsg));
return 0; return 0;
} }

View File

@@ -36,8 +36,6 @@
int test(int buffer); int test(int buffer);
int updateLog(const char *buffer, int length);
unsigned short getIP(struct sockaddr recvaddr, char *IP); unsigned short getIP(struct sockaddr recvaddr, char *IP);
int printBufferToLog(struct XPCMessage & msg); int printBufferToLog(struct XPCMessage & msg);