From faed9f1f6d068d10f2b38f2e8a7221e089a54499 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20St=C3=BCrz?= Date: Fri, 4 Dec 2015 16:21:01 +0100 Subject: [PATCH] change logging output --- libguh/guhsettings.cpp | 23 +++++++++++++++--- libguh/guhsettings.h | 1 + server/main.cpp | 53 +++++++++++++++++++++++++++++++++++++----- 3 files changed, 68 insertions(+), 9 deletions(-) diff --git a/libguh/guhsettings.cpp b/libguh/guhsettings.cpp index e955cc5c..acb4856b 100644 --- a/libguh/guhsettings.cpp +++ b/libguh/guhsettings.cpp @@ -159,16 +159,33 @@ QString GuhSettings::logPath() QString organisationName = QCoreApplication::instance()->organizationName(); if (organisationName == "guh-test") { - logPath = "/tmp/" + organisationName + "/guhd-test.logs"; + logPath = "/tmp/" + organisationName + "/guhd-test.sqlite"; } else if (GuhSettings::isRoot()) { - logPath = "/var/log/guhd.log"; + logPath = "/var/log/guh/guhd.sqlite"; } else { - logPath = QDir::homePath() + "/.config/" + organisationName + "/guhd.log"; + logPath = QDir::homePath() + "/.config/" + organisationName + "/guhd.sqlite"; } return logPath; } +/*! Returns the path where the log file (console log) will be stored. */ +QString GuhSettings::consoleLogPath() +{ + QString consoleLogPath; + QString organisationName = QCoreApplication::instance()->organizationName(); + + if (organisationName == "guh-test") { + consoleLogPath = "/tmp/" + organisationName + "/guhd-test.logs"; + } else if (GuhSettings::isRoot()) { + consoleLogPath = "/var/log/guh/guhd.log"; + } else { + consoleLogPath = QDir::homePath() + "/.config/" + organisationName + "/guhd.log"; + } + + return consoleLogPath; +} + /*! Return a list of all settings keys.*/ QStringList GuhSettings::allKeys() const { diff --git a/libguh/guhsettings.h b/libguh/guhsettings.h index 5f3a1a35..9828573f 100644 --- a/libguh/guhsettings.h +++ b/libguh/guhsettings.h @@ -45,6 +45,7 @@ public: static bool isRoot(); static QString logPath(); + static QString consoleLogPath(); // forwarded QSettings methods QStringList allKeys() const; diff --git a/server/main.cpp b/server/main.cpp index 75ed308f..5ef96049 100644 --- a/server/main.cpp +++ b/server/main.cpp @@ -25,14 +25,19 @@ #include #include #include +#include +#include #include +#include +#include +#include "stdio.h" #include "unistd.h" #include "guhcore.h" #include "guhservice.h" +#include "guhsettings.h" #include "loggingcategories.h" - QHash s_loggingFilters; using namespace guhserver; @@ -49,8 +54,44 @@ void loggingCategoryFilter(QLoggingCategory *category) } } +void consoleLogHandler(QtMsgType type, const QMessageLogContext& context, const QString& message) +{ + QString messageString; + QString timeString = QDateTime::currentDateTime().toString("yyyy.MM.dd hh:mm:ss.zzz"); + switch (type) { + case QtDebugMsg: + messageString = QString("I %1 | %2: %3").arg(timeString).arg(context.category).arg(message); + fprintf(stdout, "I | %s: %s\n", context.category, message.toUtf8().data()); + break; + case QtWarningMsg: + messageString = QString("W %1 | %2: %3").arg(timeString).arg(context.category).arg(message); + fprintf(stdout, "W | %s: %s\n", context.category, message.toUtf8().data()); + break; + case QtCriticalMsg: + messageString = QString("C %1 | %2: %3").arg(timeString).arg(context.category).arg(message); + fprintf(stdout, "C | %s: %s\n", context.category, message.toUtf8().data()); + break; + case QtFatalMsg: + messageString = QString("F %1 | %2: %3").arg(timeString).arg(context.category).arg(message); + fprintf(stdout, "F | %s: %s\n", context.category, message.toUtf8().data()); + break; + } + + QFile logFile(GuhSettings::consoleLogPath()); + if (!logFile.open(QIODevice::WriteOnly | QIODevice::Append)) { + fprintf(stdout, "W | Application: Could not open logfile.\n"); + return; + } + QTextStream textStream(&logFile); + textStream << messageString << endl; + logFile.close(); +} + + int main(int argc, char *argv[]) { + qInstallMessageHandler(consoleLogHandler); + QCoreApplication application(argc, argv); application.setOrganizationName("guh"); application.setApplicationName("guhd"); @@ -80,10 +121,10 @@ int main(int argc, char *argv[]) parser.addHelpOption(); parser.addVersionOption(); QString applicationDescription = QString("\nguh ( /[guːh]/ ) is an open source home automation server, which allows to\n" - "control a lot of different devices from many different manufacturers.\n\n" - "guhd %1 (C) 2014-2015 guh\n" - "Released under the GNU GENERAL PUBLIC LICENSE Version 2.\n\n" - "API version: %2\n").arg(GUH_VERSION_STRING).arg(JSON_PROTOCOL_VERSION); + "control a lot of different devices from many different manufacturers.\n\n" + "guhd %1 (C) 2014-2015 guh\n" + "Released under the GNU GENERAL PUBLIC LICENSE Version 2.\n\n" + "API version: %2\n").arg(GUH_VERSION_STRING).arg(JSON_PROTOCOL_VERSION); parser.setApplicationDescription(applicationDescription); @@ -133,7 +174,7 @@ int main(int argc, char *argv[]) // inform about userid int userId = getuid(); if (userId != 0) { - qCDebug(dcApplication) << "guhd started as user with ID" << userId; + qCDebug(dcApplication) << "guhd started with user ID" << userId; } else { qCDebug(dcApplication) << "guhd started as root."; }