#include #include #include #include #include "core.h" #include "loggingcategory.h" static const char *const normal = "\033[0m"; static const char *const warning = "\e[33m"; static const char *const error = "\e[31m"; static QHash s_loggingFilters; static void loggingCategoryFilter(QLoggingCategory *category) { // If this is a known category if (s_loggingFilters.contains(category->categoryName())) { category->setEnabled(QtDebugMsg, s_loggingFilters.value(category->categoryName())); category->setEnabled(QtWarningMsg, true); category->setEnabled(QtCriticalMsg, true); category->setEnabled(QtFatalMsg, true); } else { //Disable default debug messages, print only >= warnings category->setEnabled(QtDebugMsg, false); category->setEnabled(QtWarningMsg, true); category->setEnabled(QtCriticalMsg, true); category->setEnabled(QtFatalMsg, true); } } static void consoleLogHandler(QtMsgType type, const QMessageLogContext& context, const QString& message) { switch (type) { case QtInfoMsg: fprintf(stdout, " I | %s: %s\n", context.category, message.toUtf8().data()); break; case QtDebugMsg: fprintf(stdout, " I | %s: %s\n", context.category, message.toUtf8().data()); break; case QtWarningMsg: fprintf(stdout, "%s W | %s: %s%s\n", warning, context.category, message.toUtf8().data(), normal); break; case QtCriticalMsg: fprintf(stdout, "%s C | %s: %s%s\n", error, context.category, message.toUtf8().data(), normal); break; case QtFatalMsg: fprintf(stdout, "%s F | %s: %s%s\n", error, context.category, message.toUtf8().data(), normal); break; } fflush(stdout); } int main(int argc, char *argv[]) { qInstallMessageHandler(consoleLogHandler); QCoreApplication application(argc, argv); application.setOrganizationName("guh"); application.setApplicationName("qt-zigbee"); // Command line parser QCommandLineParser parser; parser.addHelpOption(); parser.addVersionOption(); parser.setApplicationDescription(QString("\nDaemon for the zigbee NXP uart bridge.\n\nCopyright %1 2016 Simon Stürz \nAll rights reserved.").arg(QChar(0xA9))); // Debug level QCommandLineOption debugLevelOption(QStringList() << "d" << "debug-level", "Set debug level [1-4]."); debugLevelOption.setDefaultValue("1"); debugLevelOption.setValueName("level"); parser.addOption(debugLevelOption); // Channel QCommandLineOption channelOption(QStringList() << "c" << "channel", "Set channel for the zigbee network. Channel between [11-26] are allowed. If not specified, the quitest channel will be choosen automatically."); channelOption.setDefaultValue(0); channelOption.setValueName("channel"); parser.addOption(channelOption); parser.process(application); // Check debug level bool debugLevelValueOk = false; int debugLevel = parser.value(debugLevelOption).toInt(&debugLevelValueOk); if (debugLevel < 1 || debugLevel > 4 || !debugLevelValueOk) { qCritical() << "Invalid debug level passed:" << parser.value(debugLevelOption) << "Reset to default debug level 1."; debugLevel = 1; } s_loggingFilters.insert("Zigbee", true); s_loggingFilters.insert("ZigbeeController", (debugLevel > 1)); s_loggingFilters.insert("ZigbeeInterface", (debugLevel > 2)); s_loggingFilters.insert("ZigbeeInterfaceTraffic", (debugLevel > 3)); QLoggingCategory::installFilter(loggingCategoryFilter); // Check channel bool channelValueOk = false; int channel = parser.value(channelOption).toInt(&channelValueOk); if (channel != 0) { if (channel < 11 || channel > 26 || !channelValueOk) { qCritical() << "Invalid channel value passed:" << parser.value(channelOption) << "Selecting automatically quitest channel."; channel = 0; } } Core core(channel); return application.exec(); }