X-Git-Url: https://git.quassel-irc.org/?p=quassel.git;a=blobdiff_plain;f=src%2Fcommon%2Fquassel.cpp;h=3598fa394d5953d85535b0791ff0f830edef9e1c;hp=7ee7915e905a52608483faad752d02de1c0cb3b5;hb=ab7ef4d24f62b5848b628482b7762ebfc0b53e1a;hpb=f9e037ea6a0b946c84db62d324996d8f878c1efb diff --git a/src/common/quassel.cpp b/src/common/quassel.cpp index 7ee7915e..3598fa39 100644 --- a/src/common/quassel.cpp +++ b/src/common/quassel.cpp @@ -23,13 +23,6 @@ #include #include -#include -#if !defined Q_OS_WIN && !defined Q_OS_MAC -# include -# include -# include -#endif - #include #include #include @@ -44,6 +37,7 @@ #include "bufferinfo.h" #include "identity.h" #include "logger.h" +#include "logmessage.h" #include "message.h" #include "network.h" #include "peer.h" @@ -51,25 +45,18 @@ #include "syncableobject.h" #include "types.h" -#include "../../version.h" - -Quassel *Quassel::instance() -{ - static Quassel instance; - return &instance; -} +#ifndef Q_OS_WIN +# include "posixsignalwatcher.h" +#else +# include "windowssignalwatcher.h" +#endif +#include "../../version.h" Quassel::Quassel() + : Singleton{this} + , _logger{new Logger{this}} { - // We catch SIGTERM and SIGINT (caused by Ctrl+C) to graceful shutdown Quassel. - signal(SIGTERM, handleSignal); - signal(SIGINT, handleSignal); -#ifndef Q_OS_WIN - // SIGHUP is used to reload configuration (i.e. SSL certificates) - // Windows does not support SIGHUP - signal(SIGHUP, handleSignal); -#endif } @@ -78,33 +65,15 @@ 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 defined(Q_OS_WIN) || defined(HAVE_EXECINFO) -# ifndef Q_OS_WIN - // we only handle crashes ourselves if coredumps are disabled - struct rlimit *limit = (rlimit *)malloc(sizeof(struct rlimit)); - int rc = getrlimit(RLIMIT_CORE, limit); - - if (rc == -1 || !((long)limit->rlim_cur > 0 || limit->rlim_cur == RLIM_INFINITY)) { -# endif /* Q_OS_WIN */ - signal(SIGABRT, handleSignal); - signal(SIGSEGV, handleSignal); -# ifndef Q_OS_WIN - signal(SIGBUS, handleSignal); - } - free(limit); -# endif /* Q_OS_WIN */ -#endif /* Q_OS_WIN || HAVE_EXECINFO */ - } - - instance()->_initialized = true; qsrand(QTime(0, 0, 0).secsTo(QTime::currentTime())); + instance()->setupSignalHandling(); instance()->setupEnvironment(); instance()->registerMetaTypes(); - Network::setDefaultCodecForServer("ISO-8859-1"); + instance()->_initialized = true; + + Network::setDefaultCodecForServer("UTF-8"); Network::setDefaultCodecForEncoding("UTF-8"); Network::setDefaultCodecForDecoding("ISO-8859-15"); @@ -118,52 +87,14 @@ bool Quassel::init() return false; } - // Set up logging - if (isOptionSet("loglevel")) { - QString level = optionValue("loglevel").toLower(); - - 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; - } - } - - QString logfilename = optionValue("logfile"); - if (!logfilename.isEmpty()) { - instance()->_logFile.reset(new QFile{logfilename}); - if (!logFile()->open(QIODevice::Append | QIODevice::Text)) { - qWarning() << qPrintable(tr("Could not open log file \"%1\": %2").arg(logfilename, logFile()->errorString())); - instance()->_logFile.reset(); - } - } -#ifdef HAVE_SYSLOG - instance()->_logToSyslog = isOptionSet("syslog"); -#endif - -#if QT_VERSION < 0x050000 - qInstallMsgHandler(Logger::logMessage); -#else - qInstallMessageHandler(Logger::logMessage); -#endif - - return true; + // Don't keep a debug log on the core + return instance()->logger()->setup(runMode() != RunMode::CoreOnly); } -void Quassel::destroy() +Logger *Quassel::logger() const { - if (logFile()) { - logFile()->close(); - instance()->_logFile.reset(); - } + return _logger; } @@ -174,12 +105,18 @@ void Quassel::registerQuitHandler(QuitHandler handler) void Quassel::quit() { - if (_quitHandlers.empty()) { - QCoreApplication::quit(); - } - else { - for (auto &&handler : _quitHandlers) { - handler(); + // Protect against multiple invocations (e.g. triggered by MainWin::closeEvent()) + if (!_quitting) { + _quitting = true; + quInfo() << "Quitting..."; + if (_quitHandlers.empty()) { + QCoreApplication::quit(); + } + else { + // Note: We expect one of the registered handlers to call QCoreApplication::quit() + for (auto &&handler : _quitHandlers) { + handler(); + } } } } @@ -299,19 +236,13 @@ void Quassel::setupBuildInfo() // Check if we got a commit hash if (!QString(GIT_HEAD).isEmpty()) { buildInfo.commitHash = GIT_HEAD; - QDateTime date; -#if QT_VERSION >= 0x050800 - date.setSecsSinceEpoch(GIT_COMMIT_DATE); -#else - // toSecsSinceEpoch() was added in Qt 5.8. Manually downconvert to seconds for now. - // See https://doc.qt.io/qt-5/qdatetime.html#toMSecsSinceEpoch - // Warning generated if not converting the 1000 to a qint64 first. - date.setMSecsSinceEpoch(GIT_COMMIT_DATE * (qint64)1000); -#endif - 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); } @@ -361,42 +292,41 @@ const Quassel::BuildInfo &Quassel::buildInfo() } -//! Signal handler for graceful shutdown. -void Quassel::handleSignal(int sig) +void Quassel::setupSignalHandling() { - switch (sig) { - case SIGTERM: - case SIGINT: - qWarning("%s", qPrintable(QString("Caught signal %1 - exiting.").arg(sig))); - instance()->quit(); - break; #ifndef Q_OS_WIN -// Windows does not support SIGHUP - case SIGHUP: - // Most applications use this as the 'configuration reload' command, e.g. nginx uses it for - // graceful reloading of processes. - quInfo() << "Caught signal" << SIGHUP << "- reloading configuration"; - if (instance()->reloadConfig()) { - quInfo() << "Successfully reloaded configuration"; - } - break; -#endif - case SIGABRT: - case SIGSEGV: -#ifndef Q_OS_WIN - case SIGBUS: + _signalWatcher = new PosixSignalWatcher(this); +#else + _signalWatcher = new WindowsSignalWatcher(this); #endif - instance()->logBacktrace(instance()->coreDumpFileName()); - exit(EXIT_FAILURE); - default: - ; - } + connect(_signalWatcher, SIGNAL(handleSignal(AbstractSignalWatcher::Action)), this, SLOT(handleSignal(AbstractSignalWatcher::Action))); } -void Quassel::disableCrashHandler() +void Quassel::handleSignal(AbstractSignalWatcher::Action action) { - instance()->_handleCrashes = false; + switch (action) { + case AbstractSignalWatcher::Action::Reload: + // Most applications use this as the 'configuration reload' command, e.g. nginx uses it for graceful reloading of processes. + if (!_reloadHandlers.empty()) { + quInfo() << "Reloading configuration"; + if (reloadConfig()) { + quInfo() << "Successfully reloaded configuration"; + } + } + break; + case AbstractSignalWatcher::Action::Terminate: + if (!_quitting) { + quit(); + } + else { + quInfo() << "Already shutting down, ignoring signal"; + } + break; + case AbstractSignalWatcher::Action::HandleCrash: + logBacktrace(instance()->coreDumpFileName()); + exit(EXIT_FAILURE); + } } @@ -429,45 +359,6 @@ bool Quassel::isOptionSet(const QString &key) } -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 -} - - const QString &Quassel::coreDumpFileName() { if (_coreDumpFileName.isEmpty()) { @@ -516,6 +407,8 @@ QString Quassel::configDirPath() #endif /* Q_OS_MAC */ } + path = QFileInfo{path}.absoluteFilePath(); + if (!path.endsWith(QDir::separator()) && !path.endsWith('/')) path += QDir::separator(); @@ -528,7 +421,6 @@ QString Quassel::configDirPath() } instance()->_configDirPath = path; - return path; } @@ -667,7 +559,7 @@ void Quassel::loadTranslation(const QLocale &locale) quasselTranslator->setObjectName("QuasselTr"); qApp->installTranslator(quasselTranslator); -#if QT_VERSION >= 0x040800 && !defined Q_OS_MAC +#ifndef Q_OS_MAC bool success = qtTranslator->load(locale, QString("qt_"), translationDirPath()); if (!success) qtTranslator->load(locale, QString("qt_"), QLibraryInfo::location(QLibraryInfo::TranslationsPath));