From ebe6767a89fe4cd439a763f4549e0336bec3f6bc Mon Sep 17 00:00:00 2001 From: Chris Tallon Date: Sat, 4 Sep 2021 16:01:42 +0100 Subject: [PATCH] Fix argv/config daemonize and logging options --- config.cc | 23 +++++++++++------------ config.h | 1 + config.json.sample | 17 ++++++++++++----- imageomx.cc | 2 +- log.cc | 3 ++- main.cc | 42 ++++++++++++++++++++++++++++-------------- 6 files changed, 55 insertions(+), 33 deletions(-) diff --git a/config.cc b/config.cc index c420432..e76593f 100644 --- a/config.cc +++ b/config.cc @@ -33,10 +33,9 @@ void Config::applyDefaults() auto insertBool = [&] (const char* s, const char* k, bool v) { if (jconfig[s][k].isNull()) jconfig[s][k] = v; }; auto insertString = [&] (const char* s, const char* k, const char* v) { if (jconfig[s][k].isNull()) jconfig[s][k] = v; }; - insertBool("main", "debug", false); insertBool("main", "daemonize", true); - insertBool("log", "enabled", true); + insertBool("log", "enabled", false); insertString("log", "filename", "stdout"); insertString("log", "level", "debug"); @@ -62,18 +61,13 @@ bool Config::loadFile() std::string errs; bool ok = Json::parseFromStream(builder, configFile, &jconfig, &errs); - - std::cout << errs << std::endl; - - if (!ok) return false; -// std::cout << jconfig << std::endl; -// std::cout << errs << std::endl; - + if (!ok) + { + std::cout << errs << std::endl; + return false; + } applyDefaults(); - - dump(); - return true; } @@ -153,3 +147,8 @@ void Config::set(const std::string& section, const std::string& key, bool value) { jconfig[section][key] = value; } + +void Config::set(const std::string& section, const std::string& key, const char* value) +{ + jconfig[section][key] = value; +} diff --git a/config.h b/config.h index 1aa811f..0124b5b 100644 --- a/config.h +++ b/config.h @@ -42,6 +42,7 @@ class Config void set(const std::string& section, const std::string& key, const std::string& value); void set(const std::string& section, const std::string& key, bool value); + void set(const std::string& section, const std::string& key, const char* value); private: static Config* instance; diff --git a/config.json.sample b/config.json.sample index 3bae319..8bc0a98 100644 --- a/config.json.sample +++ b/config.json.sample @@ -1,8 +1,13 @@ /* Vomp local config file - To use, rename to config.json and place in the current working directory for vompclient - This file is optional. All fields are optional - Anything in here overrides program defaults - Example data is shown below + + Using a local config.json file is optional. + This file shows all config options and their defaults, + where applicable. + + To use a local config, create config.json and copy in just the + settings you want to change. Place the file in the current working + directory for vompclient. + */ { @@ -27,6 +32,8 @@ "input_lirc": { + // This is an example input_lirc section + "lirc_ip": "192.0.2.0", "lirc_port": 8765, @@ -37,7 +44,7 @@ "KEY_POWER": "POWER", "KEY_MUTE": "MUTE" /* etc. List all keys to be used by Vomp - On the left - the Lirc key name. On the right - the Vomp keyname. See input.h (for now) */ + On the left - the Lirc key name. On the right - the Vomp key name. See input.h (for now) */ }, "lirc-remote-name-2": diff --git a/imageomx.cc b/imageomx.cc index 84bab65..e5bb105 100644 --- a/imageomx.cc +++ b/imageomx.cc @@ -505,7 +505,7 @@ bool ImageOMX::intDecodePicture(LoadIndex index, unsigned char* /* buffer */, un return false; } LogNT::getInstance()->debug(TAG, - "getEGLPict {:#x}",pictInf.reference); + "getEGLPict {:#x}", (long) pictInf.reference); port_def_type.format.video.pNativeWindow = egl_display; error = OMX_SetParameter(omx_egl_render, OMX_IndexParamPortDefinition, diff --git a/log.cc b/log.cc index 8eccbb1..f5d51f7 100644 --- a/log.cc +++ b/log.cc @@ -1,4 +1,5 @@ #include +#include #include "log.h" @@ -33,7 +34,7 @@ bool LogNT::init(const std::string& tfileName, bool tenabled) } outstream = &logFile; - logFile.open(fileName); + logFile.open(fileName, std::ios_base::out | std::ios_base::app); if (!logFile.is_open()) return false; return true; diff --git a/main.cc b/main.cc index 65b126b..1ac7256 100644 --- a/main.cc +++ b/main.cc @@ -78,7 +78,6 @@ int main(int argc, char** argv) { bool crashed = false; - config = new Config(); if (!config->loadFile()) { @@ -86,18 +85,20 @@ int main(int argc, char** argv) shutdown(1); } - int c; - while ((c = getopt(argc, argv, "cdns:")) != -1) + while ((c = getopt(argc, argv, "clf:ns:")) != -1) { switch (c) { case 'c': crashed = true; break; - case 'd': - config->set("main", "debug", true); // and... - [[fallthrough]]; + case 'l': + config->set("log", "enabled", true); + break; + case 'f': + config->set("log", "filename", optarg); + break; case 'n': config->set("main", "daemonize", false); break; @@ -105,7 +106,12 @@ int main(int argc, char** argv) config->set("main", "argv_server", optarg); break; case '?': - printf("Unknown option\n"); + printf("Vompclient\n\n"); + printf("Usage:\n"); + printf(" -l Enable logging\n"); + printf(" -f F Log to file F instead of stdout\n"); + printf(" -n Disable daemonize\n"); + printf(" -s S Connect to server S\n"); return 1; default: printf("Option error\n"); @@ -122,23 +128,31 @@ int main(int argc, char** argv) shutdown(1); } - std::string logFileName("stdout"); + std::string logFileName; config->getString("log", "filename", logFileName); - bool debugEnabled; - config->getBool("main", "debug", debugEnabled); - if (!loggerNT->init(logFileName, debugEnabled ? 1 : 0)) // NCONFIG x2 + bool logEnabled; + config->getBool("log", "enabled", logEnabled); + bool daemonize; + config->getBool("main", "daemonize", daemonize); + + if (logEnabled && daemonize && !logFileName.compare("stdout")) + { + printf("Cannot daemonize and log to stdout\n"); + shutdown(1); + } + + if (!loggerNT->init(logFileName, logEnabled)) { printf("Could not initialise log object. Aborting.\n"); shutdown(1); } - //logger->log("Main", Log::INFO, "Starting up..."); loggerNT->info(TAG, "Starting up..."); // Daemonize -------------------------------------------------------------------------------------------------- - bool daemonize; - config->getBool("main", "daemonize", daemonize); + // Would read config for daemonize here, but actually it's just above in the log section + // to detect attempts to log to stdout while daemonized if (daemonize) { // Fork away -- 2.39.2