From 98748e800d2476ae9eedd9a748ba152206465398 Mon Sep 17 00:00:00 2001 From: Michael Zanetti Date: Fri, 30 Jun 2017 13:30:07 +0200 Subject: [PATCH] make logging optional --- libguh/guhsettings.cpp | 21 ------------------- libguh/guhsettings.h | 1 - server/main.cpp | 42 +++++++++++++++++++++++++++++--------- translations/guhd-de_DE.ts | 15 +++++++++----- translations/guhd-en_US.ts | 15 +++++++++----- 5 files changed, 52 insertions(+), 42 deletions(-) diff --git a/libguh/guhsettings.cpp b/libguh/guhsettings.cpp index e268f9a8..1e72c29e 100644 --- a/libguh/guhsettings.cpp +++ b/libguh/guhsettings.cpp @@ -233,27 +233,6 @@ QString GuhSettings::translationsPath() #endif // SNAPPY } -/*! Returns the path where the log file (console log) will be stored. */ -QString GuhSettings::consoleLogPath() -{ - QString consoleLogPath; -#ifdef SNAPPY - consoleLogPath = QString(qgetenv("SNAP_DATA")) + "/guhd.log"; -#else - QString organisationName = QCoreApplication::instance()->organizationName(); - - if (organisationName == "guh-test") { - consoleLogPath = "/tmp/" + organisationName + "/guhd-test.log"; - } else if (GuhSettings::isRoot()) { - consoleLogPath = "/var/log/guhd.log"; - } else { - consoleLogPath = QDir::homePath() + "/.config/" + organisationName + "/guhd.log"; - } -#endif // SNAPPY - - return consoleLogPath; -} - /*! Return a list of all settings keys.*/ QStringList GuhSettings::allKeys() const { diff --git a/libguh/guhsettings.h b/libguh/guhsettings.h index 5de2b70c..8e078de4 100644 --- a/libguh/guhsettings.h +++ b/libguh/guhsettings.h @@ -51,7 +51,6 @@ public: static QString logPath(); static QString settingsPath(); static QString translationsPath(); - static QString consoleLogPath(); // forwarded QSettings methods QStringList allKeys() const; diff --git a/server/main.cpp b/server/main.cpp index 8ac54696..3d973fed 100644 --- a/server/main.cpp +++ b/server/main.cpp @@ -42,6 +42,7 @@ #include "loggingcategories.h" static QHash s_loggingFilters; +static QFile s_logFile; static const char *const normal = "\033[0m"; static const char *const warning = "\e[33m"; @@ -91,15 +92,10 @@ static void consoleLogHandler(QtMsgType type, const QMessageLogContext& context, } fflush(stdout); - QFile logFile(GuhSettings::consoleLogPath()); - if (!logFile.open(QIODevice::WriteOnly | QIODevice::Append)) { - fprintf(stdout, " W | Application: Could not open logfile.\n"); - return; + if (s_logFile.isOpen()) { + QTextStream textStream(&s_logFile); + textStream << messageString << endl; } - - QTextStream textStream(&logFile); - textStream << messageString << endl; - logFile.close(); } int main(int argc, char *argv[]) @@ -187,8 +183,26 @@ int main(int argc, char *argv[]) QCommandLineOption debugOption(QStringList() << "d" << "debug-category", debugDescription, "[No]DebugCategory"); parser.addOption(debugOption); + QCommandLineOption logOption({"l", "log"}, QCoreApplication::translate("main", "Specify a log file to write to, If this option is not specified, logs will be printed to the standard output."), "logfile", "/var/log/guhd.log"); + parser.addOption(logOption); + parser.process(application); + // Open the logfile, if any specified + if (parser.isSet(logOption)) { + QFileInfo fi(parser.value(logOption)); + QDir dir(fi.absolutePath()); + if (!dir.exists() && !dir.mkpath(dir.absolutePath())) { + qWarning() << "Error opening log file" << parser.value(logOption); + return 1; + } + s_logFile.setFileName(parser.value(logOption)); + if (!s_logFile.open(QFile::WriteOnly | QFile::Append)) { + qWarning() << "Error opening log file" << parser.value(logOption); + return 1; + } + } + // add plugin metadata to the static hash foreach (const QString &category, loggingFiltersPlugins.keys()) s_loggingFilters.insert(category, false); @@ -242,9 +256,17 @@ int main(int argc, char *argv[]) // create core instance GuhCore::instance(); - return application.exec(); + int ret = application.exec(); + if (s_logFile.isOpen()) { + s_logFile.close(); + } + return ret; } GuhService service(argc, argv); - return service.exec(); + int ret = service.exec(); + if (s_logFile.isOpen()) { + s_logFile.close(); + } + return ret; } diff --git a/translations/guhd-de_DE.ts b/translations/guhd-de_DE.ts index 8e410178..32340b28 100644 --- a/translations/guhd-de_DE.ts +++ b/translations/guhd-de_DE.ts @@ -4,7 +4,7 @@ main - + guh ( /[guːh]/ ) is an open source IoT (Internet of Things) server, which allows to control a lot of different devices from many different @@ -23,12 +23,12 @@ Szenen undVerhaltensweisen des Systems festzulegen. - + Run guhd in the foreground, not as daemon. Starte guhd im Vordergrund, nicht als Service. - + Debug categories to enable. Prefix with "No" to disable. Warnings from all categories will be printed unless explicitly muted with "NoWarnings". Categories are: @@ -36,12 +36,17 @@ Categories are: Es gibt folgende Kategorien: - + Enables all debug categories. This parameter overrides all debug category parameters. Aktiviere alle Debug-Kategorien. Dieser Parameter überschreibt alle anderen Debug-Kategorien Parameter. - + + Specify a log file to write to, If this option is not specified, logs will be printed to the standard output. + + + + No such debug category: Diese Debug-Kategorie existiert nicht: diff --git a/translations/guhd-en_US.ts b/translations/guhd-en_US.ts index 13da9aa1..971be8cd 100644 --- a/translations/guhd-en_US.ts +++ b/translations/guhd-en_US.ts @@ -4,7 +4,7 @@ main - + guh ( /[guːh]/ ) is an open source IoT (Internet of Things) server, which allows to control a lot of different devices from many different @@ -23,12 +23,12 @@ for your environment. - + Run guhd in the foreground, not as daemon. Run guhd in the foreground, not as daemon. - + Debug categories to enable. Prefix with "No" to disable. Warnings from all categories will be printed unless explicitly muted with "NoWarnings". Categories are: @@ -37,12 +37,17 @@ Categories are: Categories are: - + Enables all debug categories. This parameter overrides all debug category parameters. Enables all debug categories. This parameter overrides all debug category parameters. - + + Specify a log file to write to, If this option is not specified, logs will be printed to the standard output. + + + + No such debug category: No such debug category: