/***************************************************************************
- * Copyright (C) 2005-2016 by the Quassel Project *
+ * Copyright (C) 2005-2018 by the Quassel Project *
* devel@quassel-irc.org *
* *
* This program is free software; you can redistribute it and/or modify *
#include "quassel.h"
+#include <algorithm>
#include <iostream>
+
#include <signal.h>
#if !defined Q_OS_WIN && !defined Q_OS_MAC
# include <sys/types.h>
#include <QFileInfo>
#include <QHostAddress>
#include <QLibraryInfo>
+#include <QMetaEnum>
#include <QSettings>
#include <QTranslator>
#include <QUuid>
#include "bufferinfo.h"
#include "identity.h"
#include "logger.h"
+#include "logmessage.h"
#include "message.h"
#include "network.h"
#include "peer.h"
Quassel::Quassel()
+ : _logger{new Logger{this}}
{
+}
+
+
+bool Quassel::init()
+{
+ if (instance()->_initialized)
+ return true; // allow multiple invocations because of MonolithicApplication
+
+ // Setup signal handling
+ // TODO: Don't use unsafe methods, see handleSignal()
+
// We catch SIGTERM and SIGINT (caused by Ctrl+C) to graceful shutdown Quassel.
signal(SIGTERM, handleSignal);
signal(SIGINT, handleSignal);
// Windows does not support SIGHUP
signal(SIGHUP, handleSignal);
#endif
-}
-
-
-bool Quassel::init()
-{
- if (instance()->_initialized)
- return true; // allow multiple invocations because of MonolithicApplication
if (instance()->_handleCrashes) {
// we have crashhandler for win32 and unix (based on execinfo).
if (rc == -1 || !((long)limit->rlim_cur > 0 || limit->rlim_cur == RLIM_INFINITY)) {
# endif /* Q_OS_WIN */
- signal(SIGABRT, handleSignal);
- signal(SIGSEGV, handleSignal);
+ signal(SIGABRT, handleSignal);
+ signal(SIGSEGV, handleSignal);
# ifndef Q_OS_WIN
- signal(SIGBUS, handleSignal);
- }
- free(limit);
+ signal(SIGBUS, handleSignal);
+ }
+ free(limit);
# endif /* Q_OS_WIN */
#endif /* Q_OS_WIN || HAVE_EXECINFO */
}
instance()->setupEnvironment();
instance()->registerMetaTypes();
- Network::setDefaultCodecForServer("ISO-8859-1");
+ Network::setDefaultCodecForServer("UTF-8");
Network::setDefaultCodecForEncoding("UTF-8");
Network::setDefaultCodecForDecoding("ISO-8859-15");
return false;
}
- // set up logging
- if (Quassel::runMode() != Quassel::ClientOnly) {
- if (isOptionSet("loglevel")) {
- QString level = optionValue("loglevel");
-
- if (level == "Debug")
- setLogLevel(DebugLevel);
- else if (level == "Info")
- setLogLevel(InfoLevel);
- else if (level == "Warning")
- setLogLevel(WarningLevel);
- else if (level == "Error")
- setLogLevel(ErrorLevel);
- else {
- qWarning() << qPrintable(tr("Invalid log level %1; supported are Debug|Info|Warning|Error").arg(level));
- return false;
- }
- }
+ // Don't keep a debug log on the core
+ return instance()->logger()->setup(runMode() != RunMode::CoreOnly);
+}
- QString logfilename = optionValue("logfile");
- if (!logfilename.isEmpty()) {
- instance()->_logFile.reset(new QFile{logfilename});
- if (!logFile()->open(QIODevice::Append | QIODevice::Text)) {
- qWarning() << "Could not open log file" << logfilename << ":" << logFile()->errorString();
- instance()->_logFile.reset();
- }
- }
-#ifdef HAVE_SYSLOG
- instance()->_logToSyslog = isOptionSet("syslog");
-#endif
- }
- return true;
+void Quassel::destroy()
+{
}
-void Quassel::destroy()
+Logger *Quassel::logger() const
{
- if (logFile()) {
- logFile()->close();
- instance()->_logFile.reset();
- }
+ return _logger;
}
// Check if we got a commit hash
if (!QString(GIT_HEAD).isEmpty()) {
buildInfo.commitHash = GIT_HEAD;
- QDateTime date;
- date.setTime_t(GIT_COMMIT_DATE);
- buildInfo.commitDate = date.toString();
+ // Set to Unix epoch, wrapped as a string for backwards-compatibility
+ buildInfo.commitDate = QString::number(GIT_COMMIT_DATE);
}
else if (!QString(DIST_HASH).contains("Format")) {
buildInfo.commitHash = DIST_HASH;
+ // Leave as Unix epoch if set as Unix epoch, but don't force this for
+ // backwards-compatibility with existing packaging/release tools that might set strings.
buildInfo.commitDate = QString(DIST_DATE);
}
//! Signal handler for graceful shutdown.
+//! @todo: Ensure this doesn't use unsafe methods (it does currently)
+//! cf. QSocketNotifier, UnixSignalWatcher
void Quassel::handleSignal(int sig)
{
switch (sig) {
}
-Quassel::LogLevel Quassel::logLevel()
-{
- return instance()->_logLevel;
-}
-
-
-void Quassel::setLogLevel(LogLevel logLevel)
-{
- instance()->_logLevel = logLevel;
-}
-
-
-QFile *Quassel::logFile() {
- return instance()->_logFile.get();
-}
-
-
-bool Quassel::logToSyslog()
-{
- return instance()->_logToSyslog;
-}
-
-
-void Quassel::logFatalMessage(const char *msg)
-{
-#ifdef Q_OS_MAC
- Q_UNUSED(msg)
-#else
- QFile dumpFile(instance()->coreDumpFileName());
- dumpFile.open(QIODevice::Append);
- QTextStream dumpStream(&dumpFile);
-
- dumpStream << "Fatal: " << msg << '\n';
- dumpStream.flush();
- dumpFile.close();
-#endif
-}
-
-
-Quassel::Features Quassel::features()
-{
- Features feats = 0;
- for (int i = 1; i <= NumFeatures; i <<= 1)
- feats |= (Feature)i;
-
- // Disable DCC until it is ready
- feats &= ~Feature::DccFileTransfer;
-
- return feats;
-}
-
-
const QString &Quassel::coreDumpFileName()
{
if (_coreDumpFileName.isEmpty()) {
#endif /* Q_OS_MAC */
}
+ path = QFileInfo{path}.absoluteFilePath();
+
if (!path.endsWith(QDir::separator()) && !path.endsWith('/'))
path += QDir::separator();
}
instance()->_configDirPath = path;
-
return path;
}
quasselTranslator->load(QString("%1").arg(locale.name()), translationDirPath());
#endif
}
+
+
+// ---- Quassel::Features ---------------------------------------------------------------------------------------------
+
+Quassel::Features::Features()
+{
+ QStringList features;
+
+ // TODO Qt5: Use QMetaEnum::fromType()
+ auto featureEnum = Quassel::staticMetaObject.enumerator(Quassel::staticMetaObject.indexOfEnumerator("Feature"));
+ _features.resize(featureEnum.keyCount(), true); // enable all known features to true
+}
+
+
+Quassel::Features::Features(const QStringList &features, LegacyFeatures legacyFeatures)
+{
+ // TODO Qt5: Use QMetaEnum::fromType()
+ auto featureEnum = Quassel::staticMetaObject.enumerator(Quassel::staticMetaObject.indexOfEnumerator("Feature"));
+ _features.resize(featureEnum.keyCount(), false);
+
+ for (auto &&feature : features) {
+ int i = featureEnum.keyToValue(qPrintable(feature));
+ if (i >= 0) {
+ _features[i] = true;
+ }
+ else {
+ _unknownFeatures << feature;
+ }
+ }
+
+ if (legacyFeatures) {
+ // TODO Qt5: Use QMetaEnum::fromType()
+ auto legacyFeatureEnum = Quassel::staticMetaObject.enumerator(Quassel::staticMetaObject.indexOfEnumerator("LegacyFeature"));
+ for (quint32 mask = 0x0001; mask <= 0x8000; mask <<=1) {
+ if (static_cast<quint32>(legacyFeatures) & mask) {
+ int i = featureEnum.keyToValue(legacyFeatureEnum.valueToKey(mask));
+ if (i >= 0) {
+ _features[i] = true;
+ }
+ }
+ }
+ }
+}
+
+
+bool Quassel::Features::isEnabled(Feature feature) const
+{
+ size_t i = static_cast<size_t>(feature);
+ return i < _features.size() ? _features[i] : false;
+}
+
+
+QStringList Quassel::Features::toStringList(bool enabled) const
+{
+ // Check if any feature is enabled
+ if (!enabled && std::all_of(_features.cbegin(), _features.cend(), [](bool feature) { return !feature; })) {
+ return QStringList{} << "NoFeatures";
+ }
+
+ QStringList result;
+
+ // TODO Qt5: Use QMetaEnum::fromType()
+ auto featureEnum = Quassel::staticMetaObject.enumerator(Quassel::staticMetaObject.indexOfEnumerator("Feature"));
+ for (quint32 i = 0; i < _features.size(); ++i) {
+ if (_features[i] == enabled) {
+ result << featureEnum.key(i);
+ }
+ }
+ return result;
+}
+
+
+Quassel::LegacyFeatures Quassel::Features::toLegacyFeatures() const
+{
+ // TODO Qt5: Use LegacyFeatures (flag operators for enum classes not supported in Qt4)
+ quint32 result{0};
+ // TODO Qt5: Use QMetaEnum::fromType()
+ auto featureEnum = Quassel::staticMetaObject.enumerator(Quassel::staticMetaObject.indexOfEnumerator("Feature"));
+ auto legacyFeatureEnum = Quassel::staticMetaObject.enumerator(Quassel::staticMetaObject.indexOfEnumerator("LegacyFeature"));
+
+ for (quint32 i = 0; i < _features.size(); ++i) {
+ if (_features[i]) {
+ int v = legacyFeatureEnum.keyToValue(featureEnum.key(i));
+ if (v >= 0) {
+ result |= v;
+ }
+ }
+ }
+ return static_cast<LegacyFeatures>(result);
+}
+
+
+QStringList Quassel::Features::unknownFeatures() const
+{
+ return _unknownFeatures;
+}