X-Git-Url: https://git.quassel-irc.org/?p=quassel.git;a=blobdiff_plain;f=src%2Fcommon%2Fquassel.cpp;h=9a922a6aee6724bfceed93b2a78af6808a96ac83;hp=86ab48b7449ff48719e56ab4d7b262d46b38ae97;hb=493043890c74e4679bb3fdaf512a0e1e52c426d3;hpb=8a86d4f72ff1d6f55c82b8f8fd5af3373d2e3f87 diff --git a/src/common/quassel.cpp b/src/common/quassel.cpp index 86ab48b7..9a922a6a 100644 --- a/src/common/quassel.cpp +++ b/src/common/quassel.cpp @@ -44,6 +44,7 @@ #include "bufferinfo.h" #include "identity.h" #include "logger.h" +#include "logmessage.h" #include "message.h" #include "network.h" #include "peer.h" @@ -53,15 +54,21 @@ #include "../../version.h" -Quassel *Quassel::instance() +Quassel::Quassel() + : Singleton{this} + , _logger{new Logger{this}} { - static Quassel instance; - return &instance; } -Quassel::Quassel() +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); @@ -70,13 +77,6 @@ Quassel::Quassel() // 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). @@ -88,12 +88,12 @@ bool Quassel::init() 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 */ } @@ -118,52 +118,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; } @@ -299,19 +261,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); } @@ -362,6 +318,8 @@ const Quassel::BuildInfo &Quassel::buildInfo() //! 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) { @@ -429,45 +387,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()) {