Skip to content

Commit

Permalink
Fix #3773 Add -x option to xschedule which exits silently and immedia…
Browse files Browse the repository at this point in the history
…tely if another instance is already running.
  • Loading branch information
keithsw1111 committed Apr 25, 2023
1 parent bb8ea3c commit ba125f9
Showing 1 changed file with 66 additions and 76 deletions.
142 changes: 66 additions & 76 deletions xSchedule/xScheduleApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ xScheduleApp::xScheduleApp() :
{
}

std::string DecodeOS(wxOperatingSystemId o) {
std::string DecodeOS(wxOperatingSystemId o)
{
switch (o) {
case wxOS_UNKNOWN:
return "Call get get operating system failed.";
Expand Down Expand Up @@ -151,60 +152,48 @@ std::string DecodeOS(wxOperatingSystemId o) {

void DumpConfig()
{
static log4cpp::Category &logger_base = log4cpp::Category::getInstance(std::string("log_base"));
static log4cpp::Category& logger_base = log4cpp::Category::getInstance(std::string("log_base"));
logger_base.info("Version: " + std::string(xlights_version_string.c_str()));
logger_base.info("Bits: " + std::string(GetBitness().c_str()));
logger_base.info("Build Date: " + std::string(xlights_build_date.c_str()));
logger_base.info("Machine configuration:");
wxMemorySize s = wxGetFreeMemory();
if (s != -1)
{
if (s != -1) {
#if wxUSE_LONGLONG
wxString msg = wxString::Format(_T(" Free Memory: %" wxLongLongFmtSpec "d."), s);
#else
wxString msg = wxString::Format(_T(" Free Memory: %ld."), s);
#endif
logger_base.info("%s", (const char *)msg.c_str());
logger_base.info("%s", (const char*)msg.c_str());
}
logger_base.info(" Current directory: " + std::string(wxGetCwd().c_str()));
logger_base.info(" Machine name: " + std::string(wxGetHostName().c_str()));
logger_base.info(" OS: " + std::string(wxGetOsDescription().c_str()));
int verMaj = -1;
int verMin = -1;
wxOperatingSystemId o = wxGetOsVersion(&verMaj, &verMin);
logger_base.info(" OS: %s %d.%d", (const char *)DecodeOS(o).c_str(), verMaj, verMin);
if (wxIsPlatform64Bit())
{
logger_base.info(" OS: %s %d.%d", (const char*)DecodeOS(o).c_str(), verMaj, verMin);
if (wxIsPlatform64Bit()) {
logger_base.info(" 64 bit");
}
else
{
} else {
logger_base.info(" NOT 64 bit");
}
if (wxIsPlatformLittleEndian())
{
if (wxIsPlatformLittleEndian()) {
logger_base.info(" Little Endian");
}
else
{
} else {
logger_base.info(" Big Endian");
}
#ifdef LINUX
wxLinuxDistributionInfo l = wxGetLinuxDistributionInfo();
logger_base.info(" " + std::string(l.Id.c_str()) \
+ " " + std::string(l.Release.c_str()) \
+ " " + std::string(l.CodeName.c_str()) \
+ " " + std::string(l.Description.c_str()));
logger_base.info(" " + std::string(l.Id.c_str()) + " " + std::string(l.Release.c_str()) + " " + std::string(l.CodeName.c_str()) + " " + std::string(l.Description.c_str()));
#endif
}

void InitialiseLogging(bool fromMain)
{
static bool loggingInitialised = false;

if (!loggingInitialised)
{

if (!loggingInitialised) {
#ifdef __WXMSW__
std::string initFileName = "xschedule.windows.properties";
#endif
Expand All @@ -215,17 +204,13 @@ void InitialiseLogging(bool fromMain)
}
#endif

if (!wxFile::Exists(initFileName))
{
if (!wxFile::Exists(initFileName)) {
#ifdef _MSC_VER
// the app is not initialized so GUI is not available and no event loop.
wxMessageBox(initFileName + " not found in " + wxGetCwd() + ". Logging disabled.");
#endif
}
else
{
try
{
} else {
try {
log4cpp::PropertyConfigurator::configure(initFileName);
static log4cpp::Category& logger_base = log4cpp::Category::getInstance(std::string("log_base"));

Expand All @@ -238,39 +223,44 @@ void InitialiseLogging(bool fromMain)
logger_base.info("Log4cpp config read from %s.", (const char*)initFileName.c_str());

auto categories = log4cpp::Category::getCurrentCategories();
for (const auto& it : *categories)
{
for (const auto& it : *categories) {
std::string apps = "";
auto appenders = it->getAllAppenders();
for (const auto& it2 : appenders)
{
if (apps != "") apps += ", ";
for (const auto& it2 : appenders) {
if (apps != "")
apps += ", ";
apps += it2->getName();
}

std::string levels = "";
if (it->isAlertEnabled()) levels += "ALERT ";
if (it->isCritEnabled()) levels += "CRIT ";
if (it->isDebugEnabled()) levels += "DEBUG ";
if (it->isEmergEnabled()) levels += "EMERG ";
if (it->isErrorEnabled()) levels += "ERROR ";
if (it->isFatalEnabled()) levels += "FATAL ";
if (it->isInfoEnabled()) levels += "INFO ";
if (it->isNoticeEnabled()) levels += "NOTICE ";
if (it->isWarnEnabled()) levels += "WARN ";
if (it->isAlertEnabled())
levels += "ALERT ";
if (it->isCritEnabled())
levels += "CRIT ";
if (it->isDebugEnabled())
levels += "DEBUG ";
if (it->isEmergEnabled())
levels += "EMERG ";
if (it->isErrorEnabled())
levels += "ERROR ";
if (it->isFatalEnabled())
levels += "FATAL ";
if (it->isInfoEnabled())
levels += "INFO ";
if (it->isNoticeEnabled())
levels += "NOTICE ";
if (it->isWarnEnabled())
levels += "WARN ";

logger_base.info(" %s : %s", it->getName().c_str(), levels.c_str());
if (apps != "")
{
if (apps != "") {
logger_base.info(" " + apps);
}
}
}
catch (log4cpp::ConfigureFailure& e) {
} catch (log4cpp::ConfigureFailure& e) {
// ignore config failure ... but logging wont work
printf("Log issue: %s\n", e.what());
}
catch (const std::exception& ex) {
} catch (const std::exception& ex) {
printf("Log issue: %s\n", ex.what());
}
}
Expand All @@ -279,7 +269,7 @@ void InitialiseLogging(bool fromMain)

void xScheduleApp::WipeSettings()
{
static log4cpp::Category &logger_base = log4cpp::Category::getInstance(std::string("log_base"));
static log4cpp::Category& logger_base = log4cpp::Category::getInstance(std::string("log_base"));
logger_base.warn("------ Wiping settings ------");

wxConfigBase* config = wxConfigBase::Get();
Expand All @@ -288,13 +278,12 @@ void xScheduleApp::WipeSettings()

int xScheduleApp::OnExit()
{
if (_checker != nullptr)
{
if (_checker != nullptr) {
delete _checker;
_checker = nullptr;
}

static log4cpp::Category &logger_base = log4cpp::Category::getInstance(std::string("log_base"));
static log4cpp::Category& logger_base = log4cpp::Category::getInstance(std::string("log_base"));
logger_base.info("xSchedule exiting.");

return 0;
Expand All @@ -309,7 +298,7 @@ bool xScheduleApp::OnInit()

wxLog::SetLogLevel(wxLOG_FatalError);

//curl_global_init(CURL_GLOBAL_SSL);
// curl_global_init(CURL_GLOBAL_SSL);

InitialiseLogging(false);
static log4cpp::Category& logger_base = log4cpp::Category::getInstance(std::string("log_base"));
Expand All @@ -322,17 +311,18 @@ bool xScheduleApp::OnInit()
#endif
DumpConfig();

static const wxCmdLineEntryDesc cmdLineDesc[] =
{
static const wxCmdLineEntryDesc cmdLineDesc[] = {
{ wxCMD_LINE_SWITCH, "h", "help", "displays help on the command line parameters", wxCMD_LINE_VAL_NONE, wxCMD_LINE_OPTION_HELP },
{ wxCMD_LINE_OPTION, "s", "show", "specify show directory" },
{ wxCMD_LINE_OPTION, "p", "playlist", "specify the playlist to play" },
{ wxCMD_LINE_SWITCH, "w", "wipe", "wipe settings clean" },
{ wxCMD_LINE_SWITCH, "x", "exit", "exit silently if another instance is runnning" },
{ wxCMD_LINE_NONE }
};

bool parmfound = false;
bool wipeSettings = false;
bool exitIfRunning = false;
wxString showDir;
wxString playlist;
wxCmdLineParser parser(cmdLineDesc, argc, argv);
Expand All @@ -341,13 +331,16 @@ bool xScheduleApp::OnInit()
// help was given
return false;
case 0:
if (parser.Found("w"))
{
if (parser.Found("w")) {
parmfound = true;
logger_base.info("-w: Wiping settings");
logger_base.info("-w: Wiping settings.");
WipeSettings();
wipeSettings = true;
}
if (parser.Found("x")) {
logger_base.info("-x: Exit silently if another instance is running.");
exitIfRunning = true;
}
if (parser.Found("s", &showDir)) {
parmfound = true;
logger_base.info("-s: Show directory set to %s.", (const char*)showDir.c_str());
Expand All @@ -356,10 +349,9 @@ bool xScheduleApp::OnInit()
parmfound = true;
logger_base.info("-p: Playlist to play %s.", (const char*)playlist.c_str());
}
if (!parmfound && parser.GetParamCount() > 0)
{
if (!parmfound && parser.GetParamCount() > 0) {
logger_base.info("Unrecognised command line parameter found.");
wxMessageBox("Unrecognised command line parameter found.", _("Command Line Options")); //give positive feedback*/
wxMessageBox("Unrecognised command line parameter found.", _("Command Line Options")); // give positive feedback*/
}
break;
default:
Expand All @@ -368,36 +360,34 @@ bool xScheduleApp::OnInit()
}

_checker = new wxSingleInstanceChecker();
if (showDir == "")
{
if (_checker->IsAnotherRunning())
{
logger_base.info("Another instance of xSchedule is running.");
if (showDir == "") {
if (_checker->IsAnotherRunning()) {
logger_base.info("Another instance of xSchedule is running. Exiting.");
delete _checker; // OnExit() won't be called if we return false
_checker = nullptr;

// WOuld be nice to switch focuse here to the existing instance ... but that doesnt work ... this only sees windows in this process
//wxWindow* x = FindWindowByLabel(_("xLights Scheduler"));
// wxWindow* x = FindWindowByLabel(_("xLights Scheduler"));

wxMessageBox("Another instance of xSchedule is already running. A second instance not allowed. Exiting.");
if (!exitIfRunning) {
wxMessageBox("Another instance of xSchedule is already running. A second instance not allowed. Exiting.");
}

return false;
}
}
else
{
} else {
_checker->CreateDefault();
}

//(*AppInitialize
bool wxsOK = true;
wxInitAllImageHandlers();
if (wxsOK)
{
if (wxsOK) {
xScheduleFrame* Frame = new xScheduleFrame(0, showDir, playlist);
Frame->Show();
SetTopWindow(Frame);
if (wipeSettings) Frame->GetPluginManager().WipeSettings();
if (wipeSettings)
Frame->GetPluginManager().WipeSettings();
}
//*)
return wxsOK;
Expand Down

0 comments on commit ba125f9

Please sign in to comment.