1 /***************************************************************************
2 * Copyright (C) 2005-2019 by the Quassel Project *
3 * devel@quassel-irc.org *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) version 3. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
38 QByteArray msgWithTime(const Logger::LogEntry& msg)
42 switch (msg.logLevel) {
43 case Logger::LogLevel::Debug:
44 levelString = "[Debug] ";
46 case Logger::LogLevel::Info:
47 levelString = "[Info ] ";
49 case Logger::LogLevel::Warning:
50 levelString = "[Warn ] ";
52 case Logger::LogLevel::Error:
53 levelString = "[Error] ";
55 case Logger::LogLevel::Fatal:
56 levelString = "[FATAL] ";
60 return (msg.timeStamp.toString("yyyy-MM-dd hh:mm:ss ") + levelString + msg.message + "\n").toUtf8();
65 Logger::Logger(QObject* parent)
68 static bool registered = []() {
69 qRegisterMetaType<LogEntry>();
74 connect(this, &Logger::messageLogged, this, &Logger::onMessageLogged);
76 qInstallMessageHandler(Logger::messageHandler);
81 // If we're not initialized yet, output pending messages so they don't get lost
83 for (auto&& message : _messages) {
84 std::cerr << msgWithTime(message).constData();
89 std::vector<Logger::LogEntry> Logger::messages() const
94 void Logger::setup(bool keepMessages)
96 _keepMessages = keepMessages;
98 // Set maximum level for output (we still store/announce all messages for client-side filtering)
99 if (Quassel::isOptionSet("loglevel")) {
100 QString level = Quassel::optionValue("loglevel").toLower();
101 if (level == "debug")
102 _outputLevel = LogLevel::Debug;
103 else if (level == "info")
104 _outputLevel = LogLevel::Info;
105 else if (level == "warning")
106 _outputLevel = LogLevel::Warning;
107 else if (level == "error")
108 _outputLevel = LogLevel::Error;
110 throw ExitException{EXIT_FAILURE, qPrintable(tr("Invalid log level %1; supported are Debug|Info|Warning|Error").arg(level))};
114 QString logfilename = Quassel::optionValue("logfile");
115 if (!logfilename.isEmpty()) {
116 _logFile.setFileName(logfilename);
117 if (!_logFile.open(QFile::Append | QFile::Unbuffered | QFile::Text)) {
118 qCritical() << qPrintable(tr("Could not open log file \"%1\": %2").arg(logfilename, _logFile.errorString()));
121 if (!_logFile.isOpen()) {
122 if (!_logFile.open(stderr, QFile::WriteOnly | QFile::Unbuffered | QFile::Text)) {
123 qCritical() << qPrintable(tr("Cannot write to stderr: %1").arg(_logFile.errorString()));
128 _syslogEnabled = Quassel::isOptionSet("syslog");
130 Quassel::RunMode mode = Quassel::runMode();
131 Quassel::BuildInfo info = Quassel::buildInfo();
132 QString prgname = info.applicationName;
134 if (mode == Quassel::RunMode::ClientOnly) {
135 prgname = info.clientApplicationName;
136 } else if (mode == Quassel::RunMode::CoreOnly) {
137 prgname = info.coreApplicationName;
140 _prgname = prgname.toLocal8Bit();
142 // set up options, program name, and facility for later calls to syslog(3)
143 if (_syslogEnabled) {
144 openlog(_prgname.constData(), LOG_PID, LOG_USER);
150 // Now that we've setup our logging backends, output pending messages
151 for (auto&& message : _messages) {
152 outputMessage(message);
154 if (!_keepMessages) {
159 void Logger::messageHandler(QtMsgType type, const QMessageLogContext&, const QString& message)
161 Quassel::instance()->logger()->handleMessage(type, message);
164 void Logger::handleMessage(QtMsgType type, const QString& msg)
168 handleMessage(LogLevel::Debug, msg);
171 handleMessage(LogLevel::Info, msg);
174 handleMessage(LogLevel::Warning, msg);
177 handleMessage(LogLevel::Error, msg);
180 handleMessage(LogLevel::Fatal, msg);
185 void Logger::handleMessage(LogLevel level, const QString& msg)
187 // Use signal connection to make this method thread-safe
188 emit messageLogged({QDateTime::currentDateTime(), level, msg});
191 void Logger::onMessageLogged(const LogEntry& message)
194 _messages.push_back(message);
197 // If setup() wasn't called yet, just store the message - will be output later
199 outputMessage(message);
203 void Logger::outputMessage(const LogEntry& message)
205 if (message.logLevel < _outputLevel) {
210 if (_syslogEnabled) {
212 switch (message.logLevel) {
213 case LogLevel::Debug:
219 case LogLevel::Warning:
222 case LogLevel::Error:
225 case LogLevel::Fatal:
228 syslog(prio, "%s", qPrintable(message.message));
232 if (!_logFile.fileName().isEmpty() || !_syslogEnabled) {
233 _logFile.write(msgWithTime(message));
237 // For fatal messages, write log to dump file
238 if (message.logLevel == LogLevel::Fatal) {
239 QFile dumpFile{Quassel::instance()->coreDumpFileName()};
240 if (dumpFile.open(QIODevice::Append)) {
241 dumpFile.write(msgWithTime(message));