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 ***************************************************************************/
37 QByteArray msgWithTime(const Logger::LogEntry &msg)
41 switch (msg.logLevel) {
42 case Logger::LogLevel::Debug:
43 levelString = "[Debug] ";
45 case Logger::LogLevel::Info:
46 levelString = "[Info ] ";
48 case Logger::LogLevel::Warning:
49 levelString = "[Warn ] ";
51 case Logger::LogLevel::Error:
52 levelString = "[Error] ";
54 case Logger::LogLevel::Fatal:
55 levelString = "[FATAL] ";
59 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, SIGNAL(messageLogged(Logger::LogEntry)), this, SLOT(onMessageLogged(Logger::LogEntry)));
76 #if QT_VERSION < 0x050000
77 qInstallMsgHandler(Logger::messageHandler);
79 qInstallMessageHandler(Logger::messageHandler);
86 // If we're not initialized yet, output pending messages so they don't get lost
88 for (auto &&message : _messages) {
89 std::cerr << msgWithTime(message).constData();
95 std::vector<Logger::LogEntry> Logger::messages() const
101 bool Logger::setup(bool keepMessages)
103 _keepMessages = keepMessages;
105 // Set maximum level for output (we still store/announce all messages for client-side filtering)
106 if (Quassel::isOptionSet("loglevel")) {
107 QString level = Quassel::optionValue("loglevel").toLower();
108 if (level == "debug")
109 _outputLevel = LogLevel::Debug;
110 else if (level == "info")
111 _outputLevel = LogLevel::Info;
112 else if (level == "warning")
113 _outputLevel = LogLevel::Warning;
114 else if (level == "error")
115 _outputLevel = LogLevel::Error;
117 qCritical() << qPrintable(tr("Invalid log level %1; supported are Debug|Info|Warning|Error").arg(level));
122 QString logfilename = Quassel::optionValue("logfile");
123 if (!logfilename.isEmpty()) {
124 _logFile.setFileName(logfilename);
125 if (!_logFile.open(QFile::Append|QFile::Unbuffered|QFile::Text)) {
126 qCritical() << qPrintable(tr("Could not open log file \"%1\": %2").arg(logfilename, _logFile.errorString()));
129 if (!_logFile.isOpen()) {
130 if (!_logFile.open(stderr, QFile::WriteOnly|QFile::Unbuffered|QFile::Text)) {
131 qCritical() << qPrintable(tr("Cannot write to stderr: %1").arg(_logFile.errorString()));
136 _syslogEnabled = Quassel::isOptionSet("syslog");
138 Quassel::RunMode mode = Quassel::runMode();
139 Quassel::BuildInfo info = Quassel::buildInfo();
140 QString prgname = info.applicationName;
142 if (mode == Quassel::RunMode::ClientOnly) {
143 prgname = info.clientApplicationName;
144 } else if (mode == Quassel::RunMode::CoreOnly) {
145 prgname = info.coreApplicationName;
148 _prgname = prgname.toLocal8Bit();
150 // set up options, program name, and facility for later calls to syslog(3)
151 if (_syslogEnabled) {
152 openlog(_prgname.constData(), LOG_PID, LOG_USER);
158 // Now that we've setup our logging backends, output pending messages
159 for (auto &&message : _messages) {
160 outputMessage(message);
162 if (!_keepMessages) {
170 #if QT_VERSION < 0x050000
171 void Logger::messageHandler(QtMsgType type, const char *message)
173 void Logger::messageHandler(QtMsgType type, const QMessageLogContext &, const QString &message)
176 Quassel::instance()->logger()->handleMessage(type, message);
180 void Logger::handleMessage(QtMsgType type, const QString &msg)
184 handleMessage(LogLevel::Debug, msg);
186 #if QT_VERSION >= 0x050500
188 handleMessage(LogLevel::Info, msg);
192 handleMessage(LogLevel::Warning, msg);
195 handleMessage(LogLevel::Error, msg);
198 handleMessage(LogLevel::Fatal, msg);
204 void Logger::handleMessage(LogLevel level, const QString &msg)
206 // Use signal connection to make this method thread-safe
207 emit messageLogged({QDateTime::currentDateTime(), level, msg});
211 void Logger::onMessageLogged(const LogEntry &message)
214 _messages.push_back(message);
217 // If setup() wasn't called yet, just store the message - will be output later
219 outputMessage(message);
224 void Logger::outputMessage(const LogEntry &message)
226 if (message.logLevel < _outputLevel) {
231 if (_syslogEnabled) {
233 switch (message.logLevel) {
234 case LogLevel::Debug:
240 case LogLevel::Warning:
243 case LogLevel::Error:
246 case LogLevel::Fatal:
249 syslog(prio, "%s", qPrintable(message.message));
253 if (!_logFile.fileName().isEmpty() || !_syslogEnabled) {
254 _logFile.write(msgWithTime(message));
258 // For fatal messages, write log to dump file
259 if (message.logLevel == LogLevel::Fatal) {
260 QFile dumpFile{Quassel::instance()->coreDumpFileName()};
261 if (dumpFile.open(QIODevice::Append)) {
262 dumpFile.write(msgWithTime(message));