change logging output
This commit is contained in:
parent
10497313ea
commit
faed9f1f6d
@ -159,16 +159,33 @@ QString GuhSettings::logPath()
|
|||||||
QString organisationName = QCoreApplication::instance()->organizationName();
|
QString organisationName = QCoreApplication::instance()->organizationName();
|
||||||
|
|
||||||
if (organisationName == "guh-test") {
|
if (organisationName == "guh-test") {
|
||||||
logPath = "/tmp/" + organisationName + "/guhd-test.logs";
|
logPath = "/tmp/" + organisationName + "/guhd-test.sqlite";
|
||||||
} else if (GuhSettings::isRoot()) {
|
} else if (GuhSettings::isRoot()) {
|
||||||
logPath = "/var/log/guhd.log";
|
logPath = "/var/log/guh/guhd.sqlite";
|
||||||
} else {
|
} else {
|
||||||
logPath = QDir::homePath() + "/.config/" + organisationName + "/guhd.log";
|
logPath = QDir::homePath() + "/.config/" + organisationName + "/guhd.sqlite";
|
||||||
}
|
}
|
||||||
|
|
||||||
return logPath;
|
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.*/
|
/*! Return a list of all settings keys.*/
|
||||||
QStringList GuhSettings::allKeys() const
|
QStringList GuhSettings::allKeys() const
|
||||||
{
|
{
|
||||||
|
|||||||
@ -45,6 +45,7 @@ public:
|
|||||||
|
|
||||||
static bool isRoot();
|
static bool isRoot();
|
||||||
static QString logPath();
|
static QString logPath();
|
||||||
|
static QString consoleLogPath();
|
||||||
|
|
||||||
// forwarded QSettings methods
|
// forwarded QSettings methods
|
||||||
QStringList allKeys() const;
|
QStringList allKeys() const;
|
||||||
|
|||||||
@ -25,14 +25,19 @@
|
|||||||
#include <QCommandLineOption>
|
#include <QCommandLineOption>
|
||||||
#include <QMessageLogger>
|
#include <QMessageLogger>
|
||||||
#include <QStringList>
|
#include <QStringList>
|
||||||
|
#include <QTextStream>
|
||||||
|
#include <QDateTime>
|
||||||
#include <QtPlugin>
|
#include <QtPlugin>
|
||||||
|
#include <QtDebug>
|
||||||
|
#include <QFile>
|
||||||
|
|
||||||
|
#include "stdio.h"
|
||||||
#include "unistd.h"
|
#include "unistd.h"
|
||||||
#include "guhcore.h"
|
#include "guhcore.h"
|
||||||
#include "guhservice.h"
|
#include "guhservice.h"
|
||||||
|
#include "guhsettings.h"
|
||||||
#include "loggingcategories.h"
|
#include "loggingcategories.h"
|
||||||
|
|
||||||
|
|
||||||
QHash<QString, bool> s_loggingFilters;
|
QHash<QString, bool> s_loggingFilters;
|
||||||
|
|
||||||
using namespace guhserver;
|
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[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
|
qInstallMessageHandler(consoleLogHandler);
|
||||||
|
|
||||||
QCoreApplication application(argc, argv);
|
QCoreApplication application(argc, argv);
|
||||||
application.setOrganizationName("guh");
|
application.setOrganizationName("guh");
|
||||||
application.setApplicationName("guhd");
|
application.setApplicationName("guhd");
|
||||||
@ -80,10 +121,10 @@ int main(int argc, char *argv[])
|
|||||||
parser.addHelpOption();
|
parser.addHelpOption();
|
||||||
parser.addVersionOption();
|
parser.addVersionOption();
|
||||||
QString applicationDescription = QString("\nguh ( /[guːh]/ ) is an open source home automation server, which allows to\n"
|
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"
|
"control a lot of different devices from many different manufacturers.\n\n"
|
||||||
"guhd %1 (C) 2014-2015 guh\n"
|
"guhd %1 (C) 2014-2015 guh\n"
|
||||||
"Released under the GNU GENERAL PUBLIC LICENSE Version 2.\n\n"
|
"Released under the GNU GENERAL PUBLIC LICENSE Version 2.\n\n"
|
||||||
"API version: %2\n").arg(GUH_VERSION_STRING).arg(JSON_PROTOCOL_VERSION);
|
"API version: %2\n").arg(GUH_VERSION_STRING).arg(JSON_PROTOCOL_VERSION);
|
||||||
|
|
||||||
parser.setApplicationDescription(applicationDescription);
|
parser.setApplicationDescription(applicationDescription);
|
||||||
|
|
||||||
@ -133,7 +174,7 @@ int main(int argc, char *argv[])
|
|||||||
// inform about userid
|
// inform about userid
|
||||||
int userId = getuid();
|
int userId = getuid();
|
||||||
if (userId != 0) {
|
if (userId != 0) {
|
||||||
qCDebug(dcApplication) << "guhd started as user with ID" << userId;
|
qCDebug(dcApplication) << "guhd started with user ID" << userId;
|
||||||
} else {
|
} else {
|
||||||
qCDebug(dcApplication) << "guhd started as root.";
|
qCDebug(dcApplication) << "guhd started as root.";
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user