2011-04-22 18:50:29 +02:00
|
|
|
/* This file is part of Clementine.
|
2011-04-25 21:16:26 +02:00
|
|
|
Copyright 2011, David Sansome <me@davidsansome.com>
|
2011-04-22 18:50:29 +02:00
|
|
|
|
2011-04-25 21:16:26 +02:00
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
2011-04-22 18:50:29 +02:00
|
|
|
|
2011-04-25 21:16:26 +02:00
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
2011-04-22 18:50:29 +02:00
|
|
|
|
2011-04-25 21:16:26 +02:00
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
2011-04-22 18:50:29 +02:00
|
|
|
*/
|
|
|
|
|
2011-04-26 15:43:13 +02:00
|
|
|
// Note: this file is licensed under the Apache License instead of GPL because
|
|
|
|
// it is used by the Spotify blob which links against libspotify and is not GPL
|
|
|
|
// compatible.
|
|
|
|
|
2012-01-18 06:29:20 +01:00
|
|
|
#include <QtGlobal>
|
|
|
|
|
2012-01-18 02:54:45 +01:00
|
|
|
#include <cxxabi.h>
|
2012-01-18 05:57:56 +01:00
|
|
|
#ifdef Q_OS_UNIX
|
2012-01-18 02:54:45 +01:00
|
|
|
#include <execinfo.h>
|
2012-01-18 05:57:56 +01:00
|
|
|
#endif
|
2011-04-26 15:43:13 +02:00
|
|
|
|
2016-02-11 16:11:31 +01:00
|
|
|
#include <iostream>
|
2020-03-02 08:10:58 +01:00
|
|
|
#include <memory>
|
2016-02-11 16:11:31 +01:00
|
|
|
|
2020-03-02 08:10:58 +01:00
|
|
|
#include <QBuffer>
|
2011-04-22 18:50:29 +02:00
|
|
|
#include <QCoreApplication>
|
|
|
|
#include <QDateTime>
|
|
|
|
#include <QStringList>
|
2015-04-11 22:52:31 +02:00
|
|
|
#include <QtMessageHandler>
|
2020-03-02 08:10:58 +01:00
|
|
|
#include <QTextStream>
|
2011-04-22 18:50:29 +02:00
|
|
|
|
|
|
|
#include <glib.h>
|
|
|
|
|
|
|
|
#include "logging.h"
|
|
|
|
|
|
|
|
namespace logging {
|
|
|
|
|
|
|
|
static Level sDefaultLevel = Level_Debug;
|
2014-02-06 16:49:49 +01:00
|
|
|
static QMap<QString, Level>* sClassLevels = nullptr;
|
|
|
|
static QIODevice* sNullDevice = nullptr;
|
2011-04-22 18:50:29 +02:00
|
|
|
|
2020-02-13 06:36:07 +01:00
|
|
|
const char* kDefaultLogLevels =
|
|
|
|
"GstEnginePipelineCallbacks:2,"
|
|
|
|
"NetworkRequests:2,"
|
|
|
|
"*:3";
|
2011-04-22 18:50:29 +02:00
|
|
|
|
|
|
|
static const char* kMessageHandlerMagic = "__logging_message__";
|
|
|
|
static const int kMessageHandlerMagicLength = strlen(kMessageHandlerMagic);
|
2015-04-11 22:52:31 +02:00
|
|
|
static QtMessageHandler sOriginalMessageHandler = nullptr;
|
2011-04-22 18:50:29 +02:00
|
|
|
|
2020-03-02 04:18:03 +01:00
|
|
|
template <class T>
|
|
|
|
static T CreateLogger(Level level, const QString& class_name, int line,
|
|
|
|
const char* category);
|
2020-03-02 04:11:25 +01:00
|
|
|
|
2011-04-25 21:16:26 +02:00
|
|
|
void GLog(const char* domain, int level, const char* message, void* user_data) {
|
2011-04-22 18:50:29 +02:00
|
|
|
switch (level) {
|
|
|
|
case G_LOG_FLAG_RECURSION:
|
|
|
|
case G_LOG_FLAG_FATAL:
|
|
|
|
case G_LOG_LEVEL_ERROR:
|
2014-02-07 16:34:20 +01:00
|
|
|
case G_LOG_LEVEL_CRITICAL:
|
2020-01-30 06:39:11 +01:00
|
|
|
qLogCat(Error, domain) << message;
|
2014-02-07 16:34:20 +01:00
|
|
|
break;
|
|
|
|
case G_LOG_LEVEL_WARNING:
|
2020-01-30 06:39:11 +01:00
|
|
|
qLogCat(Warning, domain) << message;
|
2014-02-07 16:34:20 +01:00
|
|
|
break;
|
2011-04-22 18:50:29 +02:00
|
|
|
case G_LOG_LEVEL_MESSAGE:
|
2014-02-07 16:34:20 +01:00
|
|
|
case G_LOG_LEVEL_INFO:
|
2020-01-30 06:39:11 +01:00
|
|
|
qLogCat(Info, domain) << message;
|
2014-02-07 16:34:20 +01:00
|
|
|
break;
|
2011-04-22 18:50:29 +02:00
|
|
|
case G_LOG_LEVEL_DEBUG:
|
2014-02-07 16:34:20 +01:00
|
|
|
default:
|
2020-01-30 06:39:11 +01:00
|
|
|
qLogCat(Debug, domain) << message;
|
2014-02-07 16:34:20 +01:00
|
|
|
break;
|
2011-04-22 18:50:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-02 08:10:58 +01:00
|
|
|
template <class T>
|
|
|
|
class DebugBase : public QDebug {
|
|
|
|
public:
|
|
|
|
DebugBase() : QDebug(sNullDevice) {}
|
|
|
|
DebugBase(QtMsgType t) : QDebug(t) {}
|
|
|
|
T& space() { return static_cast<T&>(QDebug::space()); }
|
|
|
|
T& noSpace() { return static_cast<T&>(QDebug::nospace()); }
|
|
|
|
};
|
|
|
|
|
|
|
|
// Debug message will be store in a buffer.
|
|
|
|
class BufferedDebug : public DebugBase<BufferedDebug> {
|
|
|
|
public:
|
|
|
|
BufferedDebug() : DebugBase() {}
|
|
|
|
BufferedDebug(QtMsgType t) : DebugBase(), buf_(new QBuffer, later_deleter) {
|
|
|
|
buf_->open(QIODevice::WriteOnly);
|
|
|
|
|
|
|
|
// QDebug doesn't have a method to set a new io device, but swap() allows
|
|
|
|
// the devices to be swapped between two instances.
|
|
|
|
QDebug other(buf_.get());
|
|
|
|
swap(other);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete function for the buffer. Since a base class is holding a reference
|
|
|
|
// to the raw pointer, it shouldn't be deleted until after the deletion of
|
|
|
|
// this object is complete.
|
|
|
|
static void later_deleter(QBuffer* b) { b->deleteLater(); }
|
|
|
|
|
|
|
|
std::shared_ptr<QBuffer> buf_;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Debug message will be logged immediately.
|
|
|
|
class LoggedDebug : public DebugBase<LoggedDebug> {
|
|
|
|
public:
|
|
|
|
LoggedDebug() : DebugBase() {}
|
|
|
|
LoggedDebug(QtMsgType t) : DebugBase(t) { nospace() << kMessageHandlerMagic; }
|
|
|
|
};
|
|
|
|
|
2015-04-11 22:52:31 +02:00
|
|
|
static void MessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &message) {
|
|
|
|
if (strncmp(kMessageHandlerMagic, message.toLocal8Bit().data(), kMessageHandlerMagicLength) == 0) {
|
|
|
|
fprintf(stderr, "%s\n", message.toLocal8Bit().data() + kMessageHandlerMagicLength);
|
2011-04-22 18:50:29 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Level level = Level_Debug;
|
|
|
|
switch (type) {
|
|
|
|
case QtFatalMsg:
|
2014-02-07 16:34:20 +01:00
|
|
|
case QtCriticalMsg:
|
|
|
|
level = Level_Error;
|
|
|
|
break;
|
|
|
|
case QtWarningMsg:
|
|
|
|
level = Level_Warning;
|
|
|
|
break;
|
2011-04-22 18:50:29 +02:00
|
|
|
case QtDebugMsg:
|
2014-02-07 16:34:20 +01:00
|
|
|
default:
|
|
|
|
level = Level_Debug;
|
|
|
|
break;
|
2011-04-22 18:50:29 +02:00
|
|
|
}
|
|
|
|
|
2015-04-11 22:52:31 +02:00
|
|
|
for (const QString& line : message.split('\n')) {
|
2020-03-02 08:10:58 +01:00
|
|
|
BufferedDebug d =
|
|
|
|
CreateLogger<BufferedDebug>(level, "unknown", -1, nullptr);
|
|
|
|
d << line.toLocal8Bit().constData();
|
|
|
|
if (d.buf_) {
|
|
|
|
d.buf_->close();
|
|
|
|
fprintf(stderr, "%s\n", d.buf_->buffer().data());
|
|
|
|
}
|
2011-04-22 18:50:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (type == QtFatalMsg) {
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Init() {
|
|
|
|
delete sClassLevels;
|
|
|
|
delete sNullDevice;
|
|
|
|
|
|
|
|
sClassLevels = new QMap<QString, Level>();
|
|
|
|
sNullDevice = new NullDevice;
|
2015-04-16 23:22:53 +02:00
|
|
|
sNullDevice->open(QIODevice::ReadWrite);
|
2011-04-22 18:50:29 +02:00
|
|
|
|
|
|
|
// Catch other messages from Qt
|
|
|
|
if (!sOriginalMessageHandler) {
|
2015-04-11 22:52:31 +02:00
|
|
|
sOriginalMessageHandler = qInstallMessageHandler(MessageHandler);
|
2011-04-22 18:50:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetLevels(const QString& levels) {
|
2014-02-07 16:34:20 +01:00
|
|
|
if (!sClassLevels) return;
|
2011-04-22 18:50:29 +02:00
|
|
|
|
2014-02-10 14:29:07 +01:00
|
|
|
for (const QString& item : levels.split(',')) {
|
2011-04-22 18:50:29 +02:00
|
|
|
const QStringList class_level = item.split(':');
|
|
|
|
|
|
|
|
QString class_name;
|
|
|
|
bool ok = false;
|
|
|
|
int level = Level_Error;
|
|
|
|
|
|
|
|
if (class_level.count() == 1) {
|
|
|
|
level = class_level.last().toInt(&ok);
|
|
|
|
} else if (class_level.count() == 2) {
|
|
|
|
class_name = class_level.first();
|
|
|
|
level = class_level.last().toInt(&ok);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ok || level < Level_Error || level > Level_Debug) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (class_name.isEmpty() || class_name == "*") {
|
2014-02-07 16:34:20 +01:00
|
|
|
sDefaultLevel = (Level)level;
|
2011-04-22 18:50:29 +02:00
|
|
|
} else {
|
2014-02-07 16:34:20 +01:00
|
|
|
sClassLevels->insert(class_name, (Level)level);
|
2011-04-22 18:50:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-02 04:11:25 +01:00
|
|
|
static QString ParsePrettyFunction(const char* pretty_function) {
|
2011-04-22 18:50:29 +02:00
|
|
|
// Get the class name out of the function name.
|
|
|
|
QString class_name = pretty_function;
|
|
|
|
const int paren = class_name.indexOf('(');
|
|
|
|
if (paren != -1) {
|
|
|
|
const int colons = class_name.lastIndexOf("::", paren);
|
|
|
|
if (colons != -1) {
|
|
|
|
class_name = class_name.left(colons);
|
|
|
|
} else {
|
|
|
|
class_name = class_name.left(paren);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const int space = class_name.lastIndexOf(' ');
|
|
|
|
if (space != -1) {
|
2014-02-07 16:34:20 +01:00
|
|
|
class_name = class_name.mid(space + 1);
|
2011-04-22 18:50:29 +02:00
|
|
|
}
|
|
|
|
|
2011-05-22 13:48:12 +02:00
|
|
|
return class_name;
|
|
|
|
}
|
|
|
|
|
2020-03-02 04:18:03 +01:00
|
|
|
template <class T>
|
|
|
|
static T CreateLogger(Level level, const QString& class_name, int line,
|
|
|
|
const char* category) {
|
2011-05-22 13:48:12 +02:00
|
|
|
// Map the level to a string
|
2014-02-06 16:49:49 +01:00
|
|
|
const char* level_name = nullptr;
|
2011-05-22 13:48:12 +02:00
|
|
|
switch (level) {
|
2014-02-07 16:34:20 +01:00
|
|
|
case Level_Debug:
|
|
|
|
level_name = " DEBUG ";
|
|
|
|
break;
|
|
|
|
case Level_Info:
|
|
|
|
level_name = " INFO ";
|
|
|
|
break;
|
|
|
|
case Level_Warning:
|
|
|
|
level_name = " WARN ";
|
|
|
|
break;
|
|
|
|
case Level_Error:
|
|
|
|
level_name = " ERROR ";
|
|
|
|
break;
|
|
|
|
case Level_Fatal:
|
|
|
|
level_name = " FATAL ";
|
|
|
|
break;
|
2011-05-22 13:48:12 +02:00
|
|
|
}
|
|
|
|
|
2020-01-30 06:32:09 +01:00
|
|
|
QString filter_category = (category != nullptr) ? category : class_name;
|
2011-04-22 18:50:29 +02:00
|
|
|
// Check the settings to see if we're meant to show or hide this message.
|
|
|
|
Level threshold_level = sDefaultLevel;
|
2020-01-30 06:32:09 +01:00
|
|
|
if (sClassLevels && sClassLevels->contains(filter_category)) {
|
|
|
|
threshold_level = sClassLevels->value(filter_category);
|
2011-04-22 18:50:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (level > threshold_level) {
|
2020-03-02 08:10:58 +01:00
|
|
|
return T();
|
2011-04-22 18:50:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QString function_line = class_name;
|
|
|
|
if (line != -1) {
|
|
|
|
function_line += ":" + QString::number(line);
|
|
|
|
}
|
2020-01-30 06:32:09 +01:00
|
|
|
if (category) {
|
|
|
|
function_line += "(" + QString(category) + ")";
|
|
|
|
}
|
2011-04-22 18:50:29 +02:00
|
|
|
|
2013-09-17 16:15:46 +02:00
|
|
|
QtMsgType type = QtDebugMsg;
|
|
|
|
if (level == Level_Fatal) {
|
|
|
|
type = QtFatalMsg;
|
|
|
|
}
|
|
|
|
|
2020-03-02 04:18:03 +01:00
|
|
|
T ret(type);
|
2020-03-02 08:10:58 +01:00
|
|
|
ret.nospace() << QDateTime::currentDateTime()
|
2016-02-11 16:11:31 +01:00
|
|
|
.toString("hh:mm:ss.zzz")
|
2016-02-29 18:03:02 +01:00
|
|
|
.toLatin1()
|
2020-03-02 08:10:58 +01:00
|
|
|
.constData()
|
|
|
|
<< level_name
|
2015-04-11 22:52:31 +02:00
|
|
|
<< function_line.leftJustified(32).toLatin1().constData();
|
2011-04-22 18:50:29 +02:00
|
|
|
|
|
|
|
return ret.space();
|
|
|
|
}
|
|
|
|
|
2012-02-02 15:50:42 +01:00
|
|
|
QString CXXDemangle(const QString& mangled_function) {
|
2012-01-18 02:54:45 +01:00
|
|
|
int status;
|
|
|
|
char* demangled_function = abi::__cxa_demangle(
|
2015-04-11 22:52:31 +02:00
|
|
|
mangled_function.toLatin1().constData(), nullptr, nullptr, &status);
|
2012-01-18 02:54:45 +01:00
|
|
|
if (status == 0) {
|
2015-04-11 22:52:31 +02:00
|
|
|
QString ret = QString::fromLatin1(demangled_function);
|
2012-01-18 02:54:45 +01:00
|
|
|
free(demangled_function);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
return mangled_function; // Probably not a C++ function.
|
|
|
|
}
|
|
|
|
|
2012-02-02 15:50:42 +01:00
|
|
|
QString DarwinDemangle(const QString& symbol) {
|
|
|
|
QStringList split = symbol.split(' ', QString::SkipEmptyParts);
|
|
|
|
QString mangled_function = split[3];
|
|
|
|
return CXXDemangle(mangled_function);
|
|
|
|
}
|
|
|
|
|
|
|
|
QString LinuxDemangle(const QString& symbol) {
|
|
|
|
QRegExp regex("\\(([^+]+)");
|
|
|
|
if (!symbol.contains(regex)) {
|
|
|
|
return symbol;
|
|
|
|
}
|
|
|
|
QString mangled_function = regex.cap(1);
|
|
|
|
return CXXDemangle(mangled_function);
|
|
|
|
}
|
|
|
|
|
|
|
|
QString DemangleSymbol(const QString& symbol) {
|
|
|
|
#ifdef Q_OS_DARWIN
|
|
|
|
return DarwinDemangle(symbol);
|
|
|
|
#elif defined(Q_OS_LINUX)
|
|
|
|
return LinuxDemangle(symbol);
|
|
|
|
#else
|
|
|
|
return symbol;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-01-18 02:54:45 +01:00
|
|
|
void DumpStackTrace() {
|
2012-01-18 05:45:33 +01:00
|
|
|
#ifdef Q_OS_UNIX
|
2012-01-18 02:54:45 +01:00
|
|
|
void* callstack[128];
|
2014-02-07 16:34:20 +01:00
|
|
|
int callstack_size =
|
|
|
|
backtrace(reinterpret_cast<void**>(&callstack), sizeof(callstack));
|
|
|
|
char** symbols =
|
|
|
|
backtrace_symbols(reinterpret_cast<void**>(&callstack), callstack_size);
|
2012-01-18 02:54:45 +01:00
|
|
|
// Start from 1 to skip ourself.
|
|
|
|
for (int i = 1; i < callstack_size; ++i) {
|
2016-02-29 18:03:02 +01:00
|
|
|
std::cerr << DemangleSymbol(QString::fromLatin1(symbols[i])).toStdString()
|
2016-02-11 16:11:31 +01:00
|
|
|
<< std::endl;
|
2012-01-18 02:54:45 +01:00
|
|
|
}
|
|
|
|
free(symbols);
|
2012-01-18 05:45:33 +01:00
|
|
|
#else
|
|
|
|
qLog(Debug) << "FIXME: Implement printing stack traces on this platform";
|
|
|
|
#endif
|
2012-01-18 02:54:45 +01:00
|
|
|
}
|
|
|
|
|
2020-03-02 08:10:58 +01:00
|
|
|
// These are the functions that create loggers for the rest of Clementine.
|
|
|
|
// It's okay that the LoggedDebug instance is copied to a QDebug in these. It
|
|
|
|
// doesn't override any behavior that should be needed after return.
|
|
|
|
#define qCreateLogger(line, pretty_function, category, level) \
|
|
|
|
logging::CreateLogger<LoggedDebug>( \
|
|
|
|
logging::Level_##level, logging::ParsePrettyFunction(pretty_function), \
|
|
|
|
line, category)
|
2020-03-02 04:11:25 +01:00
|
|
|
|
2020-01-30 06:32:09 +01:00
|
|
|
QDebug CreateLoggerFatal(int line, const char* pretty_function,
|
|
|
|
const char* category) {
|
|
|
|
return qCreateLogger(line, pretty_function, category, Fatal);
|
2020-01-30 04:56:54 +01:00
|
|
|
}
|
2020-01-30 06:32:09 +01:00
|
|
|
QDebug CreateLoggerError(int line, const char* pretty_function,
|
|
|
|
const char* category) {
|
|
|
|
return qCreateLogger(line, pretty_function, category, Error);
|
2020-01-30 04:56:54 +01:00
|
|
|
}
|
2015-06-13 20:51:18 +02:00
|
|
|
|
|
|
|
#ifdef QT_NO_WARNING_OUTPUT
|
2020-01-30 06:32:09 +01:00
|
|
|
QNoDebug CreateLoggerWarning(int, const char*, const char*) {
|
|
|
|
return QNoDebug();
|
|
|
|
}
|
2015-06-13 20:51:18 +02:00
|
|
|
#else
|
2020-01-30 06:32:09 +01:00
|
|
|
QDebug CreateLoggerWarning(int line, const char* pretty_function,
|
|
|
|
const char* category) {
|
|
|
|
return qCreateLogger(line, pretty_function, category, Warning);
|
2020-01-30 04:56:54 +01:00
|
|
|
}
|
2020-01-30 06:32:09 +01:00
|
|
|
#endif // QT_NO_WARNING_OUTPUT
|
2015-06-13 20:51:18 +02:00
|
|
|
|
|
|
|
#ifdef QT_NO_DEBUG_OUTPUT
|
2020-01-30 06:32:09 +01:00
|
|
|
QNoDebug CreateLoggerInfo(int, const char*, const char*) { return QNoDebug(); }
|
|
|
|
QNoDebug CreateLoggerDebug(int, const char*, const char*) { return QNoDebug(); }
|
2015-06-13 20:51:18 +02:00
|
|
|
#else
|
2020-01-30 06:32:09 +01:00
|
|
|
QDebug CreateLoggerInfo(int line, const char* pretty_function,
|
|
|
|
const char* category) {
|
|
|
|
return qCreateLogger(line, pretty_function, category, Info);
|
2020-01-30 04:56:54 +01:00
|
|
|
}
|
2020-01-30 06:32:09 +01:00
|
|
|
QDebug CreateLoggerDebug(int line, const char* pretty_function,
|
|
|
|
const char* category) {
|
|
|
|
return qCreateLogger(line, pretty_function, category, Debug);
|
2020-01-30 04:56:54 +01:00
|
|
|
}
|
2020-01-30 06:32:09 +01:00
|
|
|
#endif // QT_NO_DEBUG_OUTPUT
|
2015-06-13 20:51:18 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
} // namespace logging
|
2015-07-20 15:35:13 +02:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2016-02-11 16:11:31 +01:00
|
|
|
template <typename T>
|
2015-07-20 15:35:13 +02:00
|
|
|
QString print_duration(T duration, const std::string& unit) {
|
|
|
|
return QString("%1%2").arg(duration.count()).arg(unit.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
QDebug operator<<(QDebug dbg, std::chrono::seconds secs) {
|
|
|
|
dbg.nospace() << print_duration(secs, "s");
|
|
|
|
return dbg.space();
|
|
|
|
}
|