migrate logging for effects and verbose options (#38)

* start step by step  migration to new logger

* fix linking for serialport

* migrate effectengine to new logger

* tune log messages

* add commandline options for hyperiond to control verbosity
--silent
--verbose
--debug
This commit is contained in:
redPanther 2016-06-23 13:48:49 +02:00 committed by brindosch
parent 34252b434d
commit d4142b4eb4
6 changed files with 54 additions and 19 deletions

View File

@ -12,6 +12,7 @@
// Effect engine includes // Effect engine includes
#include <effectengine/EffectDefinition.h> #include <effectengine/EffectDefinition.h>
#include <effectengine/ActiveEffectDefinition.h> #include <effectengine/ActiveEffectDefinition.h>
#include <utils/Logger.h>
// pre-declarioation // pre-declarioation
class Effect; class Effect;
@ -57,8 +58,10 @@ private:
std::list<EffectDefinition> _availableEffects; std::list<EffectDefinition> _availableEffects;
std::list<Effect *> _activeEffects; std::list<Effect *> _activeEffects;
std::list<ActiveEffectDefinition> _availableActiveEffects; std::list<ActiveEffectDefinition> _availableActiveEffects;
PyThreadState * _mainThreadState; PyThreadState * _mainThreadState;
Logger * _log;
}; };

View File

@ -25,7 +25,7 @@
class Logger class Logger
{ {
public: public:
enum LogLevel { UNSET=0,DEBUG=1, INFO=2,WARNING=3,ERROR=4 }; enum LogLevel { UNSET=0,DEBUG=1, INFO=2,WARNING=3,ERROR=4,OFF=5 };
static Logger* getInstance(std::string name="", LogLevel minLevel=Logger::INFO); static Logger* getInstance(std::string name="", LogLevel minLevel=Logger::INFO);
static void deleteInstance(std::string name=""); static void deleteInstance(std::string name="");

View File

@ -10,6 +10,7 @@
// effect engin eincludes // effect engin eincludes
#include "Effect.h" #include "Effect.h"
#include <utils/Logger.h>
// Python method table // Python method table
PyMethodDef Effect::effectMethods[] = { PyMethodDef Effect::effectMethods[] = {
@ -112,7 +113,7 @@ void Effect::run()
} }
else else
{ {
std::cerr << "EFFECTENGINE ERROR: Unable to open script file " << _script << std::endl; Error(Logger::getInstance("EFFECTENGINE"), "Unable to open script file %s", _script.c_str());
} }
fclose(file); fclose(file);
@ -356,7 +357,7 @@ Effect * Effect::getEffect()
{ {
// something is wrong // something is wrong
Py_XDECREF(module); Py_XDECREF(module);
std::cerr << "EFFECTENGINE ERROR: Unable to retrieve the effect object from the Python runtime" << std::endl; Error(Logger::getInstance("EFFECTENGINE"), "Unable to retrieve the effect object from the Python runtime");
return nullptr; return nullptr;
} }
@ -368,7 +369,7 @@ Effect * Effect::getEffect()
{ {
// something is wrong // something is wrong
Py_XDECREF(effectCapsule); Py_XDECREF(effectCapsule);
std::cerr << "EFFECTENGINE ERROR: Unable to retrieve the effect object from the Python runtime" << std::endl; Error(Logger::getInstance("EFFECTENGINE"), "Unable to retrieve the effect object from the Python runtime");
return nullptr; return nullptr;
} }

View File

@ -83,3 +83,4 @@ private:
/// Buffer for colorData /// Buffer for colorData
std::vector<ColorRgb> _colors; std::vector<ColorRgb> _colors;
}; };

View File

@ -22,7 +22,8 @@ EffectEngine::EffectEngine(Hyperion * hyperion, const Json::Value & jsonEffectCo
_hyperion(hyperion), _hyperion(hyperion),
_availableEffects(), _availableEffects(),
_activeEffects(), _activeEffects(),
_mainThreadState(nullptr) _mainThreadState(nullptr),
_log(Logger::getInstance("EFFECTENGINE"))
{ {
qRegisterMetaType<std::vector<ColorRgb>>("std::vector<ColorRgb>"); qRegisterMetaType<std::vector<ColorRgb>>("std::vector<ColorRgb>");
@ -49,17 +50,17 @@ EffectEngine::EffectEngine(Hyperion * hyperion, const Json::Value & jsonEffectCo
efxCount++; efxCount++;
} }
} }
std::cerr << "EFFECTENGINE INFO: " << efxCount << " effects loaded from directory " << path << std::endl; Info(_log, "%d effects loaded from directory %s", efxCount, path);
} }
} }
if (_availableEffects.size() == 0) if (_availableEffects.size() == 0)
{ {
std::cerr << "EFFECTENGINE ERROR: no effects found, check your effect directories" << std::endl; Error(_log, "no effects found, check your effect directories");
} }
// initialize the python interpreter // initialize the python interpreter
std::cout << "EFFECTENGINE INFO: Initializing Python interpreter" << std::endl; Debug(_log,"Initializing Python interpreter");
Effect::registerHyperionExtensionModule(); Effect::registerHyperionExtensionModule();
Py_InitializeEx(0); Py_InitializeEx(0);
PyEval_InitThreads(); // Create the GIL PyEval_InitThreads(); // Create the GIL
@ -69,7 +70,7 @@ EffectEngine::EffectEngine(Hyperion * hyperion, const Json::Value & jsonEffectCo
EffectEngine::~EffectEngine() EffectEngine::~EffectEngine()
{ {
// clean up the Python interpreter // clean up the Python interpreter
std::cout << "EFFECTENGINE INFO: Cleaning up Python interpreter" << std::endl; Debug(_log, "Cleaning up Python interpreter");
PyEval_RestoreThread(_mainThreadState); PyEval_RestoreThread(_mainThreadState);
Py_Finalize(); Py_Finalize();
} }
@ -101,9 +102,10 @@ bool EffectEngine::loadEffectDefinition(const std::string &path, const std::stri
std::string fileName = path + QDir::separator().toLatin1() + effectConfigFile; std::string fileName = path + QDir::separator().toLatin1() + effectConfigFile;
std::ifstream file(fileName.c_str()); std::ifstream file(fileName.c_str());
Logger * log = Logger::getInstance("EFFECTENGINE");
if (!file.is_open()) if (!file.is_open())
{ {
std::cerr << "EFFECTENGINE ERROR: Effect file '" << fileName << "' could not be loaded" << std::endl; Error( log, "Effect file '%s' could not be loaded", fileName.c_str());
return false; return false;
} }
@ -112,7 +114,7 @@ bool EffectEngine::loadEffectDefinition(const std::string &path, const std::stri
Json::Value config; Json::Value config;
if (!jsonReader.parse(file, config, false)) if (!jsonReader.parse(file, config, false))
{ {
std::cerr << "EFFECTENGINE ERROR: Error while reading effect '" << fileName << "': " << jsonReader.getFormattedErrorMessages() << std::endl; Error( log, "Error while reading effect '%s': %s", fileName.c_str(), jsonReader.getFormattedErrorMessages().c_str());
return false; return false;
} }
@ -126,7 +128,7 @@ bool EffectEngine::loadEffectDefinition(const std::string &path, const std::stri
{ {
const std::list<std::string> & errors = schemaChecker.getMessages(); const std::list<std::string> & errors = schemaChecker.getMessages();
foreach (const std::string & error, errors) { foreach (const std::string & error, errors) {
std::cerr << "EFFECTENGINE ERROR: Error while checking '" << fileName << "':" << error << std::endl; Error( log, "Error while checking '%s':", fileName.c_str(), error.c_str());
} }
return false; return false;
} }
@ -148,7 +150,7 @@ int EffectEngine::runEffect(const std::string &effectName, int priority, int tim
int EffectEngine::runEffect(const std::string &effectName, const Json::Value &args, int priority, int timeout) int EffectEngine::runEffect(const std::string &effectName, const Json::Value &args, int priority, int timeout)
{ {
std::cout << "EFFECTENGINE INFO: run effect " << effectName << " on channel " << priority << std::endl; Info( _log, "run effect %s on channel %d", effectName.c_str(), priority);
const EffectDefinition * effectDefinition = nullptr; const EffectDefinition * effectDefinition = nullptr;
for (const EffectDefinition & e : _availableEffects) for (const EffectDefinition & e : _availableEffects)
@ -162,7 +164,7 @@ int EffectEngine::runEffect(const std::string &effectName, const Json::Value &ar
if (effectDefinition == nullptr) if (effectDefinition == nullptr)
{ {
// no such effect // no such effect
std::cerr << "EFFECTENGINE ERROR: effect " << effectName << " not found" << std::endl; Error(_log, "effect %s not found", effectName.c_str());
return -1; return -1;
} }
@ -213,7 +215,7 @@ void EffectEngine::effectFinished(Effect *effect)
_hyperion->clear(effect->getPriority()); _hyperion->clear(effect->getPriority());
} }
std::cout << "EFFECTENGINE INFO: effect finished" << std::endl; Info( _log, "effect finished");
for (auto effectIt = _activeEffects.begin(); effectIt != _activeEffects.end(); ++effectIt) for (auto effectIt = _activeEffects.begin(); effectIt != _activeEffects.end(); ++effectIt)
{ {
if (*effectIt == effect) if (*effectIt == effect)

View File

@ -41,7 +41,7 @@ int main(int argc, char** argv)
{ {
// initialize main logger and set global log level // initialize main logger and set global log level
Logger* log = Logger::getInstance("MAIN"); Logger* log = Logger::getInstance("MAIN");
Logger::setLogLevel(Logger::INFO); Logger::setLogLevel(Logger::WARNING);
// Initialising QCoreApplication // Initialising QCoreApplication
QCoreApplication app(argc, argv); QCoreApplication app(argc, argv);
@ -59,12 +59,40 @@ int main(int argc, char** argv)
SwitchParameter<> & argVersion = parameters.add<SwitchParameter<>> (0x0, "version", "Show version information"); SwitchParameter<> & argVersion = parameters.add<SwitchParameter<>> (0x0, "version", "Show version information");
IntParameter & argParentPid = parameters.add<IntParameter> (0x0, "parent", "pid of parent hyperiond"); IntParameter & argParentPid = parameters.add<IntParameter> (0x0, "parent", "pid of parent hyperiond");
SwitchParameter<> & argSilent = parameters.add<SwitchParameter<>> (0x0, "silent", "do not print any outputs");
SwitchParameter<> & argVerbose = parameters.add<SwitchParameter<>> (0x0, "verbose", "Increase verbosity");
SwitchParameter<> & argDebug = parameters.add<SwitchParameter<>> (0x0, "debug", "Show debug messages");
SwitchParameter<> & argHelp = parameters.add<SwitchParameter<>> ('h', "help", "Show this help message and exit"); SwitchParameter<> & argHelp = parameters.add<SwitchParameter<>> ('h', "help", "Show this help message and exit");
argParentPid.setDefault(0); argParentPid.setDefault(0);
optionParser.parse(argc, const_cast<const char **>(argv)); optionParser.parse(argc, const_cast<const char **>(argv));
const std::vector<std::string> configFiles = optionParser.getFiles(); const std::vector<std::string> configFiles = optionParser.getFiles();
int logLevelCheck = 0;
if (argSilent.isSet())
{
Logger::setLogLevel(Logger::OFF);
logLevelCheck++;
}
if (argVerbose.isSet())
{
Logger::setLogLevel(Logger::INFO);
logLevelCheck++;
}
if (argDebug.isSet())
{
Logger::setLogLevel(Logger::DEBUG);
logLevelCheck++;
}
if (logLevelCheck > 1)
{
Error(log, "aborting, because options --silent --verbose --debug can't used together");
return 0;
}
// check if we need to display the usage. exit if we do. // check if we need to display the usage. exit if we do.
if (argHelp.isSet()) if (argHelp.isSet())
{ {
@ -109,7 +137,7 @@ int main(int argc, char** argv)
Error(log, "No valid config found"); Error(log, "No valid config found");
return 1; return 1;
} }
HyperionDaemon* hyperiond = nullptr; HyperionDaemon* hyperiond = nullptr;
try try
{ {