1
0
Fork 0

Add optional logging for dashboards.

ycs232-kbc
Jonathan Naylor 9 years ago
parent 9380a36392
commit 353c5803d5

@ -21,13 +21,22 @@
#include "Network.h" #include "Network.h"
#include "Version.h" #include "Version.h"
#if defined(_WIN32) || defined(_WIN64)
#include <Windows.h>
#else
#include <sys/time.h>
#endif
#include <cstdio> #include <cstdio>
#include <cstdlib> #include <cstdlib>
#include <cstdarg>
#include <ctime>
#include <cstring>
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
if (argc == 1) { if (argc == 1) {
::fprintf(stderr, "Usage: YSFReflector <port>\n"); ::fprintf(stderr, "Usage: YSFReflector <port> [log file]\n");
return 1; return 1;
} }
@ -37,15 +46,28 @@ int main(int argc, char** argv)
return 1; return 1;
} }
CYSFReflector Reflector(port); FILE* fp = NULL;
if (argc > 2) {
fp = ::fopen(argv[2], "wt");
if (fp == NULL) {
::fprintf(stderr, "YSFReflector: cannot open the logging file - %s\n", argv[2]);
return 1;
}
}
CYSFReflector Reflector(port, fp);
Reflector.run(); Reflector.run();
if (fp != NULL)
::fclose(fp);
return 0; return 0;
} }
CYSFReflector::CYSFReflector(unsigned int port) : CYSFReflector::CYSFReflector(unsigned int port, FILE* fp) :
m_port(port), m_port(port),
m_repeaters() m_repeaters(),
m_fp(fp)
{ {
} }
@ -67,7 +89,7 @@ void CYSFReflector::run()
CTimer pollTimer(1000U, 5U); CTimer pollTimer(1000U, 5U);
pollTimer.start(); pollTimer.start();
::fprintf(stdout, "Starting YSFReflector-%s\n", VERSION); log("Starting YSFReflector-%s", VERSION);
CTimer watchdogTimer(1000U, 0U, 1500U); CTimer watchdogTimer(1000U, 0U, 1500U);
@ -93,7 +115,7 @@ void CYSFReflector::run()
else else
::memcpy(dst, "??????????", YSF_CALLSIGN_LENGTH); ::memcpy(dst, "??????????", YSF_CALLSIGN_LENGTH);
::fprintf(stdout, "Received data from %10.10s to %10.10s at %10.10s\n", src, dst, buffer + 4U); log("Received data from %10.10s to %10.10s at %10.10s", src, dst, buffer + 4U);
} else { } else {
if (::memcmp(tag, buffer + 4U, YSF_CALLSIGN_LENGTH) == 0) { if (::memcmp(tag, buffer + 4U, YSF_CALLSIGN_LENGTH) == 0) {
bool changed = false; bool changed = false;
@ -109,7 +131,7 @@ void CYSFReflector::run()
} }
if (changed) if (changed)
::fprintf(stdout, "Received data from %10.10s to %10.10s at %10.10s\n", src, dst, buffer + 4U); log("Received data from %10.10s to %10.10s at %10.10s", src, dst, buffer + 4U);
} }
} }
@ -127,7 +149,7 @@ void CYSFReflector::run()
} }
if (buffer[34U] == 0x01U) { if (buffer[34U] == 0x01U) {
::fprintf(stdout, "Received end of transmission\n"); log("Received end of transmission");
watchdogTimer.stop(); watchdogTimer.stop();
} }
} }
@ -141,7 +163,7 @@ void CYSFReflector::run()
if (ret) { if (ret) {
CYSFRepeater* rpt = findRepeater(callsign); CYSFRepeater* rpt = findRepeater(callsign);
if (rpt == NULL) { if (rpt == NULL) {
::fprintf(stdout, "Adding %s\n", callsign.c_str()); log("Adding %s", callsign.c_str());
rpt = new CYSFRepeater; rpt = new CYSFRepeater;
rpt->m_timer.start(); rpt->m_timer.start();
rpt->m_callsign = callsign; rpt->m_callsign = callsign;
@ -173,7 +195,7 @@ void CYSFReflector::run()
for (std::vector<CYSFRepeater*>::iterator it = m_repeaters.begin(); it != m_repeaters.end(); ++it) { for (std::vector<CYSFRepeater*>::iterator it = m_repeaters.begin(); it != m_repeaters.end(); ++it) {
if ((*it)->m_timer.hasExpired()) { if ((*it)->m_timer.hasExpired()) {
::fprintf(stdout, "Removing %s\n", (*it)->m_callsign.c_str()); log("Removing %s", (*it)->m_callsign.c_str());
m_repeaters.erase(it); m_repeaters.erase(it);
break; break;
} }
@ -181,7 +203,7 @@ void CYSFReflector::run()
watchdogTimer.clock(ms); watchdogTimer.clock(ms);
if (watchdogTimer.isRunning() && watchdogTimer.hasExpired()) { if (watchdogTimer.isRunning() && watchdogTimer.hasExpired()) {
::fprintf(stdout, "Watchdog has expired\n"); log("Network watchdog has expired");
watchdogTimer.stop(); watchdogTimer.stop();
} }
@ -206,3 +228,36 @@ CYSFRepeater* CYSFReflector::findRepeater(const std::string& callsign) const
return NULL; return NULL;
} }
void CYSFReflector::log(const char* text, ...)
{
char buffer[300U];
#if defined(_WIN32) || defined(_WIN64)
SYSTEMTIME st;
::GetSystemTime(&st);
::sprintf(buffer, "%04u-%02u-%02u %02u:%02u:%02u ", st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond);
#else
struct timeval now;
::gettimeofday(&now, NULL);
struct tm* tm = ::gmtime(&now.tv_sec);
::sprintf(buffer, "%04d-%02d-%02d %02d:%02d:%02d ", tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
#endif
va_list vl;
va_start(vl, text);
::vsprintf(buffer + ::strlen(buffer), text, vl);
va_end(vl);
if (m_fp != NULL) {
::fprintf(m_fp, "%s\n", buffer);
::fflush(m_fp);
}
::fprintf(stdout, "%s\n", buffer);
::fflush(stdout);
}

@ -21,6 +21,7 @@
#include "Timer.h" #include "Timer.h"
#include <cstdio>
#include <string> #include <string>
#include <vector> #include <vector>
@ -55,7 +56,7 @@ public:
class CYSFReflector class CYSFReflector
{ {
public: public:
CYSFReflector(unsigned int port); CYSFReflector(unsigned int port, FILE* fp);
~CYSFReflector(); ~CYSFReflector();
void run(); void run();
@ -63,8 +64,10 @@ public:
private: private:
unsigned int m_port; unsigned int m_port;
std::vector<CYSFRepeater*> m_repeaters; std::vector<CYSFRepeater*> m_repeaters;
FILE* m_fp;
CYSFRepeater* findRepeater(const std::string& callsign) const; CYSFRepeater* findRepeater(const std::string& callsign) const;
void log(const char* text, ...);
}; };
#endif #endif

@ -117,7 +117,7 @@
<Optimization>MaxSpeed</Optimization> <Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking> <FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions> <IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions> <PreprocessorDefinitions>_CRT_SECURE_NO_WARNINGS;WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile> </ClCompile>
<Link> <Link>
<SubSystem>Console</SubSystem> <SubSystem>Console</SubSystem>
@ -135,7 +135,7 @@
<Optimization>MaxSpeed</Optimization> <Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking> <FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions> <IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions> <PreprocessorDefinitions>_CRT_SECURE_NO_WARNINGS;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile> </ClCompile>
<Link> <Link>
<SubSystem>Console</SubSystem> <SubSystem>Console</SubSystem>

Loading…
Cancel
Save