make logging optional

This commit is contained in:
Michael Zanetti 2017-06-30 13:30:07 +02:00
parent 3577f37e1e
commit 98748e800d
5 changed files with 52 additions and 42 deletions

View File

@ -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
{

View File

@ -51,7 +51,6 @@ public:
static QString logPath();
static QString settingsPath();
static QString translationsPath();
static QString consoleLogPath();
// forwarded QSettings methods
QStringList allKeys() const;

View File

@ -42,6 +42,7 @@
#include "loggingcategories.h"
static QHash<QString, bool> 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;
}

View File

@ -4,7 +4,7 @@
<context>
<name>main</name>
<message>
<location filename="../server/main.cpp" line="153"/>
<location filename="../server/main.cpp" line="149"/>
<source>
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.
</translation>
</message>
<message>
<location filename="../server/main.cpp" line="165"/>
<location filename="../server/main.cpp" line="161"/>
<source>Run guhd in the foreground, not as daemon.</source>
<translation>Starte guhd im Vordergrund, nicht als Service.</translation>
</message>
<message>
<location filename="../server/main.cpp" line="168"/>
<location filename="../server/main.cpp" line="164"/>
<source>Debug categories to enable. Prefix with &quot;No&quot; to disable. Warnings from all categories will be printed unless explicitly muted with &quot;NoWarnings&quot;.
Categories are:</source>
@ -36,12 +36,17 @@ Categories are:</source>
Es gibt folgende Kategorien:</translation>
</message>
<message>
<location filename="../server/main.cpp" line="185"/>
<location filename="../server/main.cpp" line="181"/>
<source>Enables all debug categories. This parameter overrides all debug category parameters.</source>
<translation>Aktiviere alle Debug-Kategorien. Dieser Parameter überschreibt alle anderen Debug-Kategorien Parameter.</translation>
</message>
<message>
<location filename="../server/main.cpp" line="204"/>
<location filename="../server/main.cpp" line="186"/>
<source>Specify a log file to write to, If this option is not specified, logs will be printed to the standard output.</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../server/main.cpp" line="220"/>
<source>No such debug category:</source>
<translation>Diese Debug-Kategorie existiert nicht:</translation>
</message>

View File

@ -4,7 +4,7 @@
<context>
<name>main</name>
<message>
<location filename="../server/main.cpp" line="153"/>
<location filename="../server/main.cpp" line="149"/>
<source>
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.
</translation>
</message>
<message>
<location filename="../server/main.cpp" line="165"/>
<location filename="../server/main.cpp" line="161"/>
<source>Run guhd in the foreground, not as daemon.</source>
<translation>Run guhd in the foreground, not as daemon.</translation>
</message>
<message>
<location filename="../server/main.cpp" line="168"/>
<location filename="../server/main.cpp" line="164"/>
<source>Debug categories to enable. Prefix with &quot;No&quot; to disable. Warnings from all categories will be printed unless explicitly muted with &quot;NoWarnings&quot;.
Categories are:</source>
@ -37,12 +37,17 @@ Categories are:</source>
Categories are:</translation>
</message>
<message>
<location filename="../server/main.cpp" line="185"/>
<location filename="../server/main.cpp" line="181"/>
<source>Enables all debug categories. This parameter overrides all debug category parameters.</source>
<translation>Enables all debug categories. This parameter overrides all debug category parameters.</translation>
</message>
<message>
<location filename="../server/main.cpp" line="204"/>
<location filename="../server/main.cpp" line="186"/>
<source>Specify a log file to write to, If this option is not specified, logs will be printed to the standard output.</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../server/main.cpp" line="220"/>
<source>No such debug category:</source>
<translation>No such debug category:</translation>
</message>