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)
40 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, &Logger::messageLogged, this, &Logger::onMessageLogged);
56 qInstallMessageHandler(Logger::messageHandler);
61 // If we're not initialized yet, output pending messages so they don't get lost
63 for (auto&& message : _messages) {
64 std::cerr << msgWithTime(message).constData();
69 std::vector<Logger::LogEntry> Logger::messages() const
74 void Logger::setup(bool keepMessages)
76 _keepMessages = keepMessages;
78 // Set maximum level for output (we still store/announce all messages for client-side filtering)
79 if (Quassel::isOptionSet("loglevel")) {
80 QString level = Quassel::optionValue("loglevel").toLower();
82 _outputLevel = LogLevel::Debug;
83 else if (level == "info")
84 _outputLevel = LogLevel::Info;
85 else if (level == "warning")
86 _outputLevel = LogLevel::Warning;
87 else if (level == "error")
88 _outputLevel = LogLevel::Error;
90 throw ExitException{EXIT_FAILURE, qPrintable(tr("Invalid log level %1; supported are Debug|Info|Warning|Error").arg(level))};
94 QString logfilename = Quassel::optionValue("logfile");
95 if (!logfilename.isEmpty()) {
96 _logFile.setFileName(logfilename);
97 if (!_logFile.open(QFile::Append | QFile::Unbuffered | QFile::Text)) {
98 qCritical() << qPrintable(tr("Could not open log file \"%1\": %2").arg(logfilename, _logFile.errorString()));
101 if (!_logFile.isOpen()) {
102 if (!_logFile.open(stderr, QFile::WriteOnly | QFile::Unbuffered | QFile::Text)) {
103 qCritical() << qPrintable(tr("Cannot write to stderr: %1").arg(_logFile.errorString()));
108 _syslogEnabled = Quassel::isOptionSet("syslog");
110 // set up options, program name, and facility for later calls to syslog(3)
112 openlog("quasselcore", LOG_PID, LOG_USER);
117 // Now that we've setup our logging backends, output pending messages
118 for (auto&& message : _messages) {
119 outputMessage(message);
121 if (!_keepMessages) {
126 void Logger::messageHandler(QtMsgType type, const QMessageLogContext&, const QString& message)
128 Quassel::instance()->logger()->handleMessage(type, message);
131 void Logger::handleMessage(QtMsgType type, const QString& msg)
135 handleMessage(LogLevel::Debug, msg);
138 handleMessage(LogLevel::Info, msg);
141 handleMessage(LogLevel::Warning, msg);
144 handleMessage(LogLevel::Error, msg);
147 handleMessage(LogLevel::Fatal, msg);
152 void Logger::handleMessage(LogLevel level, const QString& msg)
156 // Only add the log level to the message if we do not output to syslog
157 if (!_syslogEnabled) {
159 case LogLevel::Debug:
160 logString = "[Debug] ";
163 logString = "[Info ] ";
165 case LogLevel::Warning:
166 logString = "[Warn ] ";
168 case LogLevel::Error:
169 logString = "[Error] ";
171 case LogLevel::Fatal:
172 logString = "[FATAL] ";
177 // Use signal connection to make this method thread-safe
178 emit messageLogged({QDateTime::currentDateTime(), level, logString += msg});
181 void Logger::onMessageLogged(const LogEntry& message)
184 _messages.push_back(message);
187 // If setup() wasn't called yet, just store the message - will be output later
189 outputMessage(message);
193 void Logger::outputMessage(const LogEntry& message)
195 if (message.logLevel < _outputLevel) {
200 if (_syslogEnabled) {
202 switch (message.logLevel) {
203 case LogLevel::Debug:
209 case LogLevel::Warning:
212 case LogLevel::Error:
215 case LogLevel::Fatal:
218 syslog(prio, "%s", qPrintable(message.message));
222 if (!_logFile.fileName().isEmpty() || !_syslogEnabled) {
223 _logFile.write(msgWithTime(message));
227 // For fatal messages, write log to dump file
228 if (message.logLevel == LogLevel::Fatal) {
229 QFile dumpFile{Quassel::instance()->coreDumpFileName()};
230 if (dumpFile.open(QIODevice::Append)) {
231 dumpFile.write(msgWithTime(message));