Merge pull request #51 from sq7lrx/container-logging

Adds console logging option to ircddbgatewayd
This commit is contained in:
Jonathan Naylor 2020-05-27 23:00:37 +01:00 committed by GitHub
commit 52c9c48140
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 138 additions and 20 deletions

View file

@ -53,6 +53,9 @@
<ClCompile Include="ConnectData.cpp"> <ClCompile Include="ConnectData.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="ConsoleLogger.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="DCSHandler.cpp"> <ClCompile Include="DCSHandler.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
@ -259,6 +262,9 @@
<ClInclude Include="ConnectData.h"> <ClInclude Include="ConnectData.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="ConsoleLogger.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="DCSHandler.h"> <ClInclude Include="DCSHandler.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>

48
Common/ConsoleLogger.cpp Normal file
View file

@ -0,0 +1,48 @@
/*
* Copyright (C) 2002,2003,2009,2011,2012,2019 by Jonathan Naylor G4KLX
* Copyright (C) 2020 by Adam Kolakowski SQ7LRX
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "ConsoleLogger.h"
const char CConsoleLogger::S_LEVELS[] = "FEWMMIDTPU";
CConsoleLogger::CConsoleLogger() :
wxLog(),
m_stdout(new wxMessageOutputStderr(stdout)),
m_stderr(new wxMessageOutputStderr(stderr))
{
}
CConsoleLogger::~CConsoleLogger()
{
}
void CConsoleLogger::DoLogRecord(wxLogLevel level, const wxString& msg, const wxLogRecordInfo& info)
{
if (level > 9)
level = 9;
if (level <= wxLOG_Error) {
m_stderr->Printf(wxT("%c: %s\n"), CConsoleLogger::S_LEVELS[level], msg.c_str());
} else {
m_stdout->Printf(wxT("%c: %s\n"), CConsoleLogger::S_LEVELS[level], msg.c_str());
}
if (level == wxLOG_FatalError)
::abort();
}

41
Common/ConsoleLogger.h Normal file
View file

@ -0,0 +1,41 @@
/*
* Copyright (C) 2002,2003,2009,2011,2012,2019 by Jonathan Naylor G4KLX
* Copyright (C) 2020 by Adam Kolakowski SQ7LRX
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#ifndef ConsoleLogger_H
#define ConsoleLogger_H
#include <wx/wx.h>
class CConsoleLogger : public wxLog {
public:
CConsoleLogger();
virtual ~CConsoleLogger();
virtual void DoLogRecord(wxLogLevel level, const wxString& msg, const wxLogRecordInfo& info);
private:
const static char S_LEVELS[];
wxMessageOutput *m_stdout;
wxMessageOutput *m_stderr;
};
#endif

View file

@ -1,5 +1,5 @@
OBJECTS = AMBEData.o AnnouncementUnit.o APRSCollector.o APRSWriter.o APRSWriterThread.o AudioUnit.o CacheManager.o CallsignList.o \ OBJECTS = AMBEData.o AnnouncementUnit.o APRSCollector.o APRSWriter.o APRSWriterThread.o AudioUnit.o CacheManager.o CallsignList.o \
CallsignServer.o CCITTChecksum.o CCSData.o CCSHandler.o CCSProtocolHandler.o ConnectData.o DCSHandler.o DCSProtocolHandler.o \ CallsignServer.o CCITTChecksum.o CCSData.o CCSHandler.o CCSProtocolHandler.o ConnectData.o ConsoleLogger.o DCSHandler.o DCSProtocolHandler.o \
DCSProtocolHandlerPool.o DDData.o DDHandler.o DExtraHandler.o DExtraProtocolHandler.o DExtraProtocolHandlerPool.o \ DCSProtocolHandlerPool.o DDData.o DDHandler.o DExtraHandler.o DExtraProtocolHandler.o DExtraProtocolHandlerPool.o \
DPlusAuthenticator.o DPlusHandler.o DPlusProtocolHandler.o DPlusProtocolHandlerPool.o DRATSServer.o DTMF.o \ DPlusAuthenticator.o DPlusHandler.o DPlusProtocolHandler.o DPlusProtocolHandlerPool.o DRATSServer.o DTMF.o \
DummyRepeaterProtocolHandler.o DVTOOLFileReader.o EchoUnit.o G2Handler.o G2ProtocolHandler.o GatewayCache.o \ DummyRepeaterProtocolHandler.o DVTOOLFileReader.o EchoUnit.o G2Handler.o G2ProtocolHandler.o GatewayCache.o \

View file

@ -26,6 +26,7 @@
#include "APRSWriter.h" #include "APRSWriter.h"
#include "Version.h" #include "Version.h"
#include "Logger.h" #include "Logger.h"
#include "ConsoleLogger.h"
#include "IRCDDB.h" #include "IRCDDB.h"
#include "IRCDDBClient.h" #include "IRCDDBClient.h"
#include "IRCDDBMultiClient.h" #include "IRCDDBMultiClient.h"
@ -48,6 +49,7 @@ const wxChar* DEBUG_SWITCH = wxT("debug");
const wxChar* LOGDIR_OPTION = wxT("logdir"); const wxChar* LOGDIR_OPTION = wxT("logdir");
const wxChar* CONFDIR_OPTION = wxT("confdir"); const wxChar* CONFDIR_OPTION = wxT("confdir");
const wxChar* DAEMON_SWITCH = wxT("daemon"); const wxChar* DAEMON_SWITCH = wxT("daemon");
const wxChar* FGROUND_SWITCH = wxT("foreground");
const wxString LOG_BASE_NAME = wxT("ircDDBGateway"); const wxString LOG_BASE_NAME = wxT("ircDDBGateway");
@ -70,6 +72,7 @@ int main(int argc, char** argv)
parser.AddSwitch(NOLOGGING_SWITCH, wxEmptyString, wxEmptyString, wxCMD_LINE_PARAM_OPTIONAL); parser.AddSwitch(NOLOGGING_SWITCH, wxEmptyString, wxEmptyString, wxCMD_LINE_PARAM_OPTIONAL);
parser.AddSwitch(DEBUG_SWITCH, wxEmptyString, wxEmptyString, wxCMD_LINE_PARAM_OPTIONAL); parser.AddSwitch(DEBUG_SWITCH, wxEmptyString, wxEmptyString, wxCMD_LINE_PARAM_OPTIONAL);
parser.AddSwitch(DAEMON_SWITCH, wxEmptyString, wxEmptyString, wxCMD_LINE_PARAM_OPTIONAL); parser.AddSwitch(DAEMON_SWITCH, wxEmptyString, wxEmptyString, wxCMD_LINE_PARAM_OPTIONAL);
parser.AddSwitch(FGROUND_SWITCH, wxEmptyString, wxEmptyString, wxCMD_LINE_PARAM_OPTIONAL);
parser.AddOption(LOGDIR_OPTION, wxEmptyString, wxEmptyString, wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL); parser.AddOption(LOGDIR_OPTION, wxEmptyString, wxEmptyString, wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL);
parser.AddOption(CONFDIR_OPTION, wxEmptyString, wxEmptyString, wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL); parser.AddOption(CONFDIR_OPTION, wxEmptyString, wxEmptyString, wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL);
parser.AddParam(NAME_PARAM, wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL); parser.AddParam(NAME_PARAM, wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL);
@ -80,9 +83,10 @@ int main(int argc, char** argv)
return 0; return 0;
} }
bool nolog = parser.Found(NOLOGGING_SWITCH); bool nolog = parser.Found(NOLOGGING_SWITCH);
bool debug = parser.Found(DEBUG_SWITCH); bool debug = parser.Found(DEBUG_SWITCH);
bool daemon = parser.Found(DAEMON_SWITCH); bool daemon = parser.Found(DAEMON_SWITCH);
bool foreground = parser.Found(FGROUND_SWITCH);
wxString logDir; wxString logDir;
bool found = parser.Found(LOGDIR_OPTION, &logDir); bool found = parser.Found(LOGDIR_OPTION, &logDir);
@ -98,6 +102,12 @@ int main(int argc, char** argv)
if (parser.GetParamCount() > 0U) if (parser.GetParamCount() > 0U)
name = parser.GetParam(0U); name = parser.GetParam(0U);
if (daemon && foreground) {
::fprintf(stderr, "ircddbgatewayd: -daemon and -foreground are mutually exclusive, exiting\n");
::wxUninitialize();
return 1;
}
if (daemon) { if (daemon) {
pid_t pid = ::fork(); pid_t pid = ::fork();
@ -138,7 +148,7 @@ int main(int argc, char** argv)
::fclose(fp); ::fclose(fp);
} }
m_gateway = new CIRCDDBGatewayAppD(nolog, debug, logDir, confDir, name); m_gateway = new CIRCDDBGatewayAppD(nolog, debug, foreground, logDir, confDir, name);
if (!m_gateway->init()) { if (!m_gateway->init()) {
::wxUninitialize(); ::wxUninitialize();
return 1; return 1;
@ -157,10 +167,11 @@ int main(int argc, char** argv)
return 0; return 0;
} }
CIRCDDBGatewayAppD::CIRCDDBGatewayAppD(bool nolog, bool debug, const wxString& logDir, const wxString& confDir, const wxString& name) : CIRCDDBGatewayAppD::CIRCDDBGatewayAppD(bool nolog, bool debug, bool foreground, const wxString& logDir, const wxString& confDir, const wxString& name) :
m_name(name), m_name(name),
m_nolog(nolog), m_nolog(nolog),
m_debug(debug), m_debug(debug),
m_foreground(foreground),
m_logDir(logDir), m_logDir(logDir),
m_confDir(confDir), m_confDir(confDir),
m_thread(NULL), m_thread(NULL),
@ -174,7 +185,9 @@ CIRCDDBGatewayAppD::~CIRCDDBGatewayAppD()
bool CIRCDDBGatewayAppD::init() bool CIRCDDBGatewayAppD::init()
{ {
if (!m_nolog) { if (m_foreground) {
initLogging(new CConsoleLogger());
} else if (!m_nolog) {
wxString logBaseName = LOG_BASE_NAME; wxString logBaseName = LOG_BASE_NAME;
if (!m_name.IsEmpty()) { if (!m_name.IsEmpty()) {
logBaseName.Append(wxT("_")); logBaseName.Append(wxT("_"));
@ -184,16 +197,7 @@ bool CIRCDDBGatewayAppD::init()
if (m_logDir.IsEmpty()) if (m_logDir.IsEmpty())
m_logDir = wxT(LOG_DIR); m_logDir = wxT(LOG_DIR);
wxLog* log = new CLogger(m_logDir, logBaseName); initLogging(new CLogger(m_logDir, logBaseName));
wxLog::SetActiveTarget(log);
if (m_debug) {
wxLog::SetVerbose(true);
wxLog::SetLogLevel(wxLOG_Debug);
} else {
wxLog::SetVerbose(false);
wxLog::SetLogLevel(wxLOG_Message);
}
} else { } else {
new wxLogNull; new wxLogNull;
} }
@ -212,14 +216,31 @@ bool CIRCDDBGatewayAppD::init()
return false; return false;
} }
wxLogInfo(wxT("Starting ") + APPLICATION_NAME + wxT(" daemon - ") + VERSION); wxLogMessage(wxT("Starting ") + APPLICATION_NAME + wxT(" daemon - ") + VERSION);
// Log the version of wxWidgets and the Operating System // Log the version of wxWidgets and the Operating System
wxLogInfo(wxT("Using wxWidgets %d.%d.%d on %s"), wxMAJOR_VERSION, wxMINOR_VERSION, wxRELEASE_NUMBER, ::wxGetOsDescription().c_str()); wxLogInfo(wxT("Using wxWidgets %d.%d.%d on %s"), wxMAJOR_VERSION, wxMINOR_VERSION, wxRELEASE_NUMBER, ::wxGetOsDescription().c_str());
if (!m_nolog && m_foreground) {
wxLogWarning(wxT("Running in foreground, logging to file disabled. Use -nolog when running this application in foreground to suppress this "));
}
return createThread(); return createThread();
} }
void CIRCDDBGatewayAppD::initLogging(wxLog *logger)
{
wxLog::SetActiveTarget(logger);
if (m_debug) {
wxLog::SetVerbose(true);
wxLog::SetLogLevel(wxLOG_Debug);
} else {
wxLog::SetVerbose(false);
wxLog::SetLogLevel(wxLOG_Message);
}
}
void CIRCDDBGatewayAppD::run() void CIRCDDBGatewayAppD::run()
{ {
m_thread->run(); m_thread->run();
@ -878,7 +899,7 @@ bool CIRCDDBGatewayAppD::createThread()
bool xlxEnabled; bool xlxEnabled;
wxString xlxHostsFileUrl; wxString xlxHostsFileUrl;
config.getXLX(xlxEnabled, xlxHostsFileUrl); config.getXLX(xlxEnabled, xlxHostsFileUrl);
wxLogInfo(wxT("XLX enabled: %d, Override Local %d, Hosts file url: %s"), int(xlxEnabled), xlxHostsFileUrl.c_str()); wxLogInfo(wxT("XLX enabled: %d, Hosts file url: %s"), int(xlxEnabled), xlxHostsFileUrl.c_str());
if (repeaterBand1.Len() > 1U || repeaterBand2.Len() > 1U || if (repeaterBand1.Len() > 1U || repeaterBand2.Len() > 1U ||
repeaterBand3.Len() > 1U || repeaterBand4.Len() > 1U) { repeaterBand3.Len() > 1U || repeaterBand4.Len() > 1U) {

View file

@ -28,7 +28,7 @@
class CIRCDDBGatewayAppD { class CIRCDDBGatewayAppD {
public: public:
CIRCDDBGatewayAppD(bool nolog, bool debug, const wxString& logDir, const wxString& confDir, const wxString& name); CIRCDDBGatewayAppD(bool nolog, bool debug, bool foreground, const wxString& logDir, const wxString& confDir, const wxString& name);
~CIRCDDBGatewayAppD(); ~CIRCDDBGatewayAppD();
bool init(); bool init();
@ -41,12 +41,14 @@ private:
wxString m_name; wxString m_name;
bool m_nolog; bool m_nolog;
bool m_debug; bool m_debug;
bool m_foreground;
wxString m_logDir; wxString m_logDir;
wxString m_confDir; wxString m_confDir;
CIRCDDBGatewayThread* m_thread; CIRCDDBGatewayThread* m_thread;
wxSingleInstanceChecker* m_checker; wxSingleInstanceChecker* m_checker;
bool createThread(); bool createThread();
void initLogging(wxLog *logger);
}; };
#endif #endif