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
#include <effectengine/EffectDefinition.h>
#include <effectengine/ActiveEffectDefinition.h>
#include <utils/Logger.h>
// pre-declarioation
class Effect;
@ -61,4 +62,6 @@ private:
std::list<ActiveEffectDefinition> _availableActiveEffects;
PyThreadState * _mainThreadState;
Logger * _log;
};

View File

@ -25,7 +25,7 @@
class Logger
{
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 void deleteInstance(std::string name="");

View File

@ -10,6 +10,7 @@
// effect engin eincludes
#include "Effect.h"
#include <utils/Logger.h>
// Python method table
PyMethodDef Effect::effectMethods[] = {
@ -112,7 +113,7 @@ void Effect::run()
}
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);
@ -356,7 +357,7 @@ Effect * Effect::getEffect()
{
// something is wrong
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;
}
@ -368,7 +369,7 @@ Effect * Effect::getEffect()
{
// something is wrong
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;
}

View File

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

View File

@ -22,7 +22,8 @@ EffectEngine::EffectEngine(Hyperion * hyperion, const Json::Value & jsonEffectCo
_hyperion(hyperion),
_availableEffects(),
_activeEffects(),
_mainThreadState(nullptr)
_mainThreadState(nullptr),
_log(Logger::getInstance("EFFECTENGINE"))
{
qRegisterMetaType<std::vector<ColorRgb>>("std::vector<ColorRgb>");
@ -49,17 +50,17 @@ EffectEngine::EffectEngine(Hyperion * hyperion, const Json::Value & jsonEffectCo
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)
{
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
std::cout << "EFFECTENGINE INFO: Initializing Python interpreter" << std::endl;
Debug(_log,"Initializing Python interpreter");
Effect::registerHyperionExtensionModule();
Py_InitializeEx(0);
PyEval_InitThreads(); // Create the GIL
@ -69,7 +70,7 @@ EffectEngine::EffectEngine(Hyperion * hyperion, const Json::Value & jsonEffectCo
EffectEngine::~EffectEngine()
{
// clean up the Python interpreter
std::cout << "EFFECTENGINE INFO: Cleaning up Python interpreter" << std::endl;
Debug(_log, "Cleaning up Python interpreter");
PyEval_RestoreThread(_mainThreadState);
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::ifstream file(fileName.c_str());
Logger * log = Logger::getInstance("EFFECTENGINE");
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;
}
@ -112,7 +114,7 @@ bool EffectEngine::loadEffectDefinition(const std::string &path, const std::stri
Json::Value config;
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;
}
@ -126,7 +128,7 @@ bool EffectEngine::loadEffectDefinition(const std::string &path, const std::stri
{
const std::list<std::string> & errors = schemaChecker.getMessages();
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;
}
@ -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)
{
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;
for (const EffectDefinition & e : _availableEffects)
@ -162,7 +164,7 @@ int EffectEngine::runEffect(const std::string &effectName, const Json::Value &ar
if (effectDefinition == nullptr)
{
// no such effect
std::cerr << "EFFECTENGINE ERROR: effect " << effectName << " not found" << std::endl;
Error(_log, "effect %s not found", effectName.c_str());
return -1;
}
@ -213,7 +215,7 @@ void EffectEngine::effectFinished(Effect *effect)
_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)
{
if (*effectIt == effect)

View File

@ -41,7 +41,7 @@ int main(int argc, char** argv)
{
// initialize main logger and set global log level
Logger* log = Logger::getInstance("MAIN");
Logger::setLogLevel(Logger::INFO);
Logger::setLogLevel(Logger::WARNING);
// Initialising QCoreApplication
QCoreApplication app(argc, argv);
@ -59,12 +59,40 @@ int main(int argc, char** argv)
SwitchParameter<> & argVersion = parameters.add<SwitchParameter<>> (0x0, "version", "Show version information");
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");
argParentPid.setDefault(0);
optionParser.parse(argc, const_cast<const char **>(argv));
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.
if (argHelp.isSet())
{