1 /***************************************************************************
2 * Copyright (C) 2005-2018 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 ***************************************************************************/
37 QByteArray msgWithTime(const Logger::LogEntry &msg)
39 return (msg.timeStamp.toString("yyyy-MM-dd hh:mm:ss ") + msg.message + "\n").toUtf8();
45 Logger::Logger(QObject *parent)
48 static bool registered = []() {
49 qRegisterMetaType<LogEntry>();
54 connect(this, SIGNAL(messageLogged(Logger::LogEntry)), this, SLOT(onMessageLogged(Logger::LogEntry)));
56 qInstallMessageHandler(Logger::messageHandler);
62 // If we're not initialized yet, output pending messages so they don't get lost
64 for (auto &&message : _messages) {
65 std::cerr << msgWithTime(message).constData();
71 std::vector<Logger::LogEntry> Logger::messages() const
77 bool Logger::setup(bool keepMessages)
79 _keepMessages = keepMessages;
81 // Set maximum level for output (we still store/announce all messages for client-side filtering)
82 if (Quassel::isOptionSet("loglevel")) {
83 QString level = Quassel::optionValue("loglevel").toLower();
85 _outputLevel = LogLevel::Debug;
86 else if (level == "info")
87 _outputLevel = LogLevel::Info;
88 else if (level == "warning")
89 _outputLevel = LogLevel::Warning;
90 else if (level == "error")
91 _outputLevel = LogLevel::Error;
93 qCritical() << qPrintable(tr("Invalid log level %1; supported are Debug|Info|Warning|Error").arg(level));
98 QString logfilename = Quassel::optionValue("logfile");
99 if (!logfilename.isEmpty()) {
100 _logFile.setFileName(logfilename);
101 if (!_logFile.open(QFile::Append|QFile::Unbuffered|QFile::Text)) {
102 qCritical() << qPrintable(tr("Could not open log file \"%1\": %2").arg(logfilename, _logFile.errorString()));
105 if (!_logFile.isOpen()) {
106 if (!_logFile.open(stderr, QFile::WriteOnly|QFile::Unbuffered|QFile::Text)) {
107 qCritical() << qPrintable(tr("Cannot write to stderr: %1").arg(_logFile.errorString()));
112 _syslogEnabled = Quassel::isOptionSet("syslog");
117 // Now that we've setup our logging backends, output pending messages
118 for (auto &&message : _messages) {
119 outputMessage(message);
121 if (!_keepMessages) {
129 void Logger::messageHandler(QtMsgType type, const QMessageLogContext &, const QString &message)
131 Quassel::instance()->logger()->handleMessage(type, message);
135 void Logger::handleMessage(QtMsgType type, const QString &msg)
139 handleMessage(LogLevel::Debug, msg);
142 handleMessage(LogLevel::Info, msg);
145 handleMessage(LogLevel::Warning, msg);
148 handleMessage(LogLevel::Error, msg);
151 handleMessage(LogLevel::Fatal, msg);
157 void Logger::handleMessage(LogLevel level, const QString &msg)
162 case LogLevel::Debug:
163 logString = "[Debug] ";
166 logString = "[Info ] ";
168 case LogLevel::Warning:
169 logString = "[Warn ] ";
171 case LogLevel::Error:
172 logString = "[Error] ";
174 case LogLevel::Fatal:
175 logString = "[FATAL] ";
179 // Use signal connection to make this method thread-safe
180 emit messageLogged({QDateTime::currentDateTime(), level, logString += msg});
184 void Logger::onMessageLogged(const LogEntry &message)
187 _messages.push_back(message);
190 // If setup() wasn't called yet, just store the message - will be output later
192 outputMessage(message);
197 void Logger::outputMessage(const LogEntry &message)
199 if (message.logLevel < _outputLevel) {
204 if (_syslogEnabled) {
206 switch (message.logLevel) {
207 case LogLevel::Debug:
213 case LogLevel::Warning:
216 case LogLevel::Error:
219 case LogLevel::Fatal:
222 syslog(prio|LOG_USER, "%s", qPrintable(message.message));
226 if (!_logFile.fileName().isEmpty() || !_syslogEnabled) {
227 _logFile.write(msgWithTime(message));
231 // For fatal messages, write log to dump file
232 if (message.logLevel == LogLevel::Fatal) {
233 QFile dumpFile{Quassel::instance()->coreDumpFileName()};
234 if (dumpFile.open(QIODevice::Append)) {
235 dumpFile.write(msgWithTime(message));