2011-04-27 10:05:43 +00:00
|
|
|
/****************************************************************************
|
|
|
|
**
|
2014-06-20 12:44:48 +00:00
|
|
|
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
|
2012-09-19 12:28:29 +00:00
|
|
|
** Contact: http://www.qt-project.org/legal
|
2011-04-27 10:05:43 +00:00
|
|
|
**
|
|
|
|
** This file is part of the QtCore module of the Qt Toolkit.
|
|
|
|
**
|
|
|
|
** $QT_BEGIN_LICENSE:LGPL$
|
2012-09-19 12:28:29 +00:00
|
|
|
** Commercial License Usage
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
|
|
|
** a written agreement between you and Digia. For licensing terms and
|
|
|
|
** conditions see http://qt.digia.com/licensing. For further information
|
|
|
|
** use the contact form at http://qt.digia.com/contact-us.
|
|
|
|
**
|
2011-04-27 10:05:43 +00:00
|
|
|
** GNU Lesser General Public License Usage
|
2012-09-19 12:28:29 +00:00
|
|
|
** Alternatively, this file may be used under the terms of the GNU Lesser
|
|
|
|
** General Public License version 2.1 as published by the Free Software
|
|
|
|
** Foundation and appearing in the file LICENSE.LGPL included in the
|
|
|
|
** packaging of this file. Please review the following information to
|
|
|
|
** ensure the GNU Lesser General Public License version 2.1 requirements
|
|
|
|
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|
|
|
**
|
|
|
|
** In addition, as a special exception, Digia gives you certain additional
|
|
|
|
** rights. These rights are described in the Digia Qt LGPL Exception
|
2011-04-27 10:05:43 +00:00
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
**
|
2011-05-24 09:34:08 +00:00
|
|
|
** GNU General Public License Usage
|
2012-09-19 12:28:29 +00:00
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
** General Public License version 3.0 as published by the Free Software
|
|
|
|
** Foundation and appearing in the file LICENSE.GPL included in the
|
|
|
|
** packaging of this file. Please review the following information to
|
|
|
|
** ensure the GNU General Public License version 3.0 requirements will be
|
|
|
|
** met: http://www.gnu.org/copyleft/gpl.html.
|
2011-04-27 10:05:43 +00:00
|
|
|
**
|
2012-01-24 06:17:24 +00:00
|
|
|
**
|
2011-04-27 10:05:43 +00:00
|
|
|
** $QT_END_LICENSE$
|
|
|
|
**
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef QDEBUG_H
|
|
|
|
#define QDEBUG_H
|
|
|
|
|
|
|
|
#include <QtCore/qalgorithms.h>
|
|
|
|
#include <QtCore/qhash.h>
|
|
|
|
#include <QtCore/qlist.h>
|
|
|
|
#include <QtCore/qmap.h>
|
|
|
|
#include <QtCore/qpair.h>
|
|
|
|
#include <QtCore/qtextstream.h>
|
|
|
|
#include <QtCore/qstring.h>
|
|
|
|
#include <QtCore/qvector.h>
|
|
|
|
#include <QtCore/qset.h>
|
|
|
|
#include <QtCore/qcontiguouscache.h>
|
|
|
|
|
|
|
|
QT_BEGIN_NAMESPACE
|
|
|
|
|
|
|
|
|
|
|
|
class Q_CORE_EXPORT QDebug
|
|
|
|
{
|
QtDebug: Include file, line, function information
Record the file, line, and function where a qDebug, qWarning, qCritical
or qFatal call happens, and make this information available in a custom
message handler.
The patch uses the C preprocessor to replace qDebug, qWarning, ... with
a line that also records the current file, line, and function. Custom
message handlers can access this information via a new QMessageLogContext
argument.
Change-Id: I0a9b89c1d137e41775932d3b1a35da4ebf12d18d
Reviewed-by: David Faure <faure@kde.org>
2012-01-17 15:20:45 +00:00
|
|
|
friend class QMessageLogger;
|
2012-07-27 12:17:38 +00:00
|
|
|
friend class QDebugStateSaverPrivate;
|
2011-04-27 10:05:43 +00:00
|
|
|
struct Stream {
|
2014-06-19 09:09:47 +00:00
|
|
|
Stream(QIODevice *device) : ts(device), ref(1), type(QtDebugMsg), space(true), message_output(false), flags(0) {}
|
|
|
|
Stream(QString *string) : ts(string, QIODevice::WriteOnly), ref(1), type(QtDebugMsg), space(true), message_output(false), flags(0) {}
|
|
|
|
Stream(QtMsgType t) : ts(&buffer, QIODevice::WriteOnly), ref(1), type(t), space(true), message_output(true), flags(0) {}
|
2011-04-27 10:05:43 +00:00
|
|
|
QTextStream ts;
|
|
|
|
QString buffer;
|
|
|
|
int ref;
|
|
|
|
QtMsgType type;
|
|
|
|
bool space;
|
|
|
|
bool message_output;
|
QtDebug: Include file, line, function information
Record the file, line, and function where a qDebug, qWarning, qCritical
or qFatal call happens, and make this information available in a custom
message handler.
The patch uses the C preprocessor to replace qDebug, qWarning, ... with
a line that also records the current file, line, and function. Custom
message handlers can access this information via a new QMessageLogContext
argument.
Change-Id: I0a9b89c1d137e41775932d3b1a35da4ebf12d18d
Reviewed-by: David Faure <faure@kde.org>
2012-01-17 15:20:45 +00:00
|
|
|
QMessageLogContext context;
|
2014-06-19 09:09:47 +00:00
|
|
|
|
|
|
|
enum FormatFlag {
|
|
|
|
NoQuotes = 0x1
|
|
|
|
};
|
|
|
|
|
|
|
|
// ### Qt 6: unify with space, introduce own version member
|
|
|
|
bool testFlag(FormatFlag flag) const { return (context.version > 1) ? (flags & flag) : false; }
|
|
|
|
void setFlag(FormatFlag flag) { if (context.version > 1) { flags |= flag; } }
|
|
|
|
void unsetFlag(FormatFlag flag) { if (context.version > 1) { flags &= ~flag; } }
|
|
|
|
|
|
|
|
// added in 5.4
|
|
|
|
int flags;
|
2011-04-27 10:05:43 +00:00
|
|
|
} *stream;
|
|
|
|
public:
|
|
|
|
inline QDebug(QIODevice *device) : stream(new Stream(device)) {}
|
|
|
|
inline QDebug(QString *string) : stream(new Stream(string)) {}
|
|
|
|
inline QDebug(QtMsgType t) : stream(new Stream(t)) {}
|
|
|
|
inline QDebug(const QDebug &o):stream(o.stream) { ++stream->ref; }
|
|
|
|
inline QDebug &operator=(const QDebug &other);
|
2014-04-29 10:39:31 +00:00
|
|
|
~QDebug();
|
2012-04-05 12:49:02 +00:00
|
|
|
inline void swap(QDebug &other) { qSwap(stream, other.stream); }
|
|
|
|
|
2014-06-20 12:44:48 +00:00
|
|
|
QDebug &resetFormat();
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
inline QDebug &space() { stream->space = true; stream->ts << ' '; return *this; }
|
|
|
|
inline QDebug &nospace() { stream->space = false; return *this; }
|
|
|
|
inline QDebug &maybeSpace() { if (stream->space) stream->ts << ' '; return *this; }
|
|
|
|
|
2012-07-27 11:53:16 +00:00
|
|
|
bool autoInsertSpaces() const { return stream->space; }
|
|
|
|
void setAutoInsertSpaces(bool b) { stream->space = b; }
|
|
|
|
|
2014-06-19 09:09:47 +00:00
|
|
|
inline QDebug "e() { stream->unsetFlag(Stream::NoQuotes); return *this; }
|
|
|
|
inline QDebug &noquote() { stream->setFlag(Stream::NoQuotes); return *this; }
|
|
|
|
inline QDebug &maybeQuote(char c = '"') { if (!(stream->testFlag(Stream::NoQuotes))) stream->ts << c; return *this; }
|
|
|
|
|
|
|
|
inline QDebug &operator<<(QChar t) { maybeQuote('\''); stream->ts << t; maybeQuote('\''); return maybeSpace(); }
|
2011-04-27 10:05:43 +00:00
|
|
|
inline QDebug &operator<<(bool t) { stream->ts << (t ? "true" : "false"); return maybeSpace(); }
|
|
|
|
inline QDebug &operator<<(char t) { stream->ts << t; return maybeSpace(); }
|
|
|
|
inline QDebug &operator<<(signed short t) { stream->ts << t; return maybeSpace(); }
|
|
|
|
inline QDebug &operator<<(unsigned short t) { stream->ts << t; return maybeSpace(); }
|
|
|
|
inline QDebug &operator<<(signed int t) { stream->ts << t; return maybeSpace(); }
|
|
|
|
inline QDebug &operator<<(unsigned int t) { stream->ts << t; return maybeSpace(); }
|
|
|
|
inline QDebug &operator<<(signed long t) { stream->ts << t; return maybeSpace(); }
|
|
|
|
inline QDebug &operator<<(unsigned long t) { stream->ts << t; return maybeSpace(); }
|
2014-02-13 13:44:38 +00:00
|
|
|
inline QDebug &operator<<(qint64 t) { stream->ts << t; return maybeSpace(); }
|
|
|
|
inline QDebug &operator<<(quint64 t) { stream->ts << t; return maybeSpace(); }
|
2011-04-27 10:05:43 +00:00
|
|
|
inline QDebug &operator<<(float t) { stream->ts << t; return maybeSpace(); }
|
|
|
|
inline QDebug &operator<<(double t) { stream->ts << t; return maybeSpace(); }
|
2012-05-02 11:29:54 +00:00
|
|
|
inline QDebug &operator<<(const char* t) { stream->ts << QString::fromUtf8(t); return maybeSpace(); }
|
2014-06-19 09:09:47 +00:00
|
|
|
inline QDebug &operator<<(const QString & t) { maybeQuote(); stream->ts << t; maybeQuote(); return maybeSpace(); }
|
2011-04-27 10:05:43 +00:00
|
|
|
inline QDebug &operator<<(const QStringRef & t) { return operator<<(t.toString()); }
|
2014-06-19 09:09:47 +00:00
|
|
|
inline QDebug &operator<<(QLatin1String t) { maybeQuote(); stream->ts << t; maybeQuote(); return maybeSpace(); }
|
|
|
|
inline QDebug &operator<<(const QByteArray & t) { maybeQuote(); stream->ts << t; maybeQuote(); return maybeSpace(); }
|
2011-04-27 10:05:43 +00:00
|
|
|
inline QDebug &operator<<(const void * t) { stream->ts << t; return maybeSpace(); }
|
|
|
|
inline QDebug &operator<<(QTextStreamFunction f) {
|
|
|
|
stream->ts << f;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline QDebug &operator<<(QTextStreamManipulator m)
|
|
|
|
{ stream->ts << m; return *this; }
|
|
|
|
};
|
|
|
|
|
2012-05-04 13:27:56 +00:00
|
|
|
Q_DECLARE_SHARED(QDebug)
|
|
|
|
|
2012-07-27 12:17:38 +00:00
|
|
|
class QDebugStateSaverPrivate;
|
|
|
|
class Q_CORE_EXPORT QDebugStateSaver
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
QDebugStateSaver(QDebug &dbg);
|
|
|
|
~QDebugStateSaver();
|
|
|
|
private:
|
|
|
|
Q_DISABLE_COPY(QDebugStateSaver)
|
|
|
|
QScopedPointer<QDebugStateSaverPrivate> d;
|
|
|
|
};
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
class QNoDebug
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
inline QNoDebug &operator<<(QTextStreamFunction) { return *this; }
|
|
|
|
inline QNoDebug &operator<<(QTextStreamManipulator) { return *this; }
|
|
|
|
inline QNoDebug &space() { return *this; }
|
|
|
|
inline QNoDebug &nospace() { return *this; }
|
|
|
|
inline QNoDebug &maybeSpace() { return *this; }
|
2014-06-19 09:09:47 +00:00
|
|
|
inline QNoDebug "e() { return *this; }
|
|
|
|
inline QNoDebug &noquote() { return *this; }
|
|
|
|
inline QNoDebug &maybeQuote(const char = '"') { return *this; }
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
inline QNoDebug &operator<<(const T &) { return *this; }
|
|
|
|
};
|
|
|
|
|
|
|
|
inline QDebug &QDebug::operator=(const QDebug &other)
|
|
|
|
{
|
|
|
|
if (this != &other) {
|
|
|
|
QDebug copy(other);
|
|
|
|
qSwap(stream, copy.stream);
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
inline QDebug operator<<(QDebug debug, const QList<T> &list)
|
|
|
|
{
|
2013-12-22 11:33:33 +00:00
|
|
|
const bool oldSetting = debug.autoInsertSpaces();
|
2011-04-27 10:05:43 +00:00
|
|
|
debug.nospace() << '(';
|
2012-01-06 06:33:17 +00:00
|
|
|
for (typename QList<T>::size_type i = 0; i < list.count(); ++i) {
|
2011-04-27 10:05:43 +00:00
|
|
|
if (i)
|
|
|
|
debug << ", ";
|
|
|
|
debug << list.at(i);
|
|
|
|
}
|
|
|
|
debug << ')';
|
2013-12-22 11:33:33 +00:00
|
|
|
debug.setAutoInsertSpaces(oldSetting);
|
|
|
|
return debug.maybeSpace();
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
inline QDebug operator<<(QDebug debug, const QVector<T> &vec)
|
|
|
|
{
|
2013-12-22 11:33:33 +00:00
|
|
|
const bool oldSetting = debug.autoInsertSpaces();
|
2011-04-27 10:05:43 +00:00
|
|
|
debug.nospace() << "QVector";
|
2013-12-22 11:33:33 +00:00
|
|
|
debug.setAutoInsertSpaces(oldSetting);
|
2011-04-27 10:05:43 +00:00
|
|
|
return operator<<(debug, vec.toList());
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class aKey, class aT>
|
|
|
|
inline QDebug operator<<(QDebug debug, const QMap<aKey, aT> &map)
|
|
|
|
{
|
2013-12-22 11:33:33 +00:00
|
|
|
const bool oldSetting = debug.autoInsertSpaces();
|
2011-04-27 10:05:43 +00:00
|
|
|
debug.nospace() << "QMap(";
|
|
|
|
for (typename QMap<aKey, aT>::const_iterator it = map.constBegin();
|
|
|
|
it != map.constEnd(); ++it) {
|
|
|
|
debug << '(' << it.key() << ", " << it.value() << ')';
|
|
|
|
}
|
|
|
|
debug << ')';
|
2013-12-22 11:33:33 +00:00
|
|
|
debug.setAutoInsertSpaces(oldSetting);
|
|
|
|
return debug.maybeSpace();
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class aKey, class aT>
|
|
|
|
inline QDebug operator<<(QDebug debug, const QHash<aKey, aT> &hash)
|
|
|
|
{
|
2013-12-22 11:33:33 +00:00
|
|
|
const bool oldSetting = debug.autoInsertSpaces();
|
2011-04-27 10:05:43 +00:00
|
|
|
debug.nospace() << "QHash(";
|
|
|
|
for (typename QHash<aKey, aT>::const_iterator it = hash.constBegin();
|
|
|
|
it != hash.constEnd(); ++it)
|
|
|
|
debug << '(' << it.key() << ", " << it.value() << ')';
|
|
|
|
debug << ')';
|
2013-12-22 11:33:33 +00:00
|
|
|
debug.setAutoInsertSpaces(oldSetting);
|
|
|
|
return debug.maybeSpace();
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class T1, class T2>
|
|
|
|
inline QDebug operator<<(QDebug debug, const QPair<T1, T2> &pair)
|
|
|
|
{
|
2013-12-22 11:33:33 +00:00
|
|
|
const bool oldSetting = debug.autoInsertSpaces();
|
2011-04-27 10:05:43 +00:00
|
|
|
debug.nospace() << "QPair(" << pair.first << ',' << pair.second << ')';
|
2013-12-22 11:33:33 +00:00
|
|
|
debug.setAutoInsertSpaces(oldSetting);
|
|
|
|
return debug.maybeSpace();
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
inline QDebug operator<<(QDebug debug, const QSet<T> &set)
|
|
|
|
{
|
2013-12-22 11:33:33 +00:00
|
|
|
const bool oldSetting = debug.autoInsertSpaces();
|
2011-04-27 10:05:43 +00:00
|
|
|
debug.nospace() << "QSet";
|
2013-12-22 11:33:33 +00:00
|
|
|
debug.setAutoInsertSpaces(oldSetting);
|
2011-04-27 10:05:43 +00:00
|
|
|
return operator<<(debug, set.toList());
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
inline QDebug operator<<(QDebug debug, const QContiguousCache<T> &cache)
|
|
|
|
{
|
2013-12-22 11:33:33 +00:00
|
|
|
const bool oldSetting = debug.autoInsertSpaces();
|
2011-04-27 10:05:43 +00:00
|
|
|
debug.nospace() << "QContiguousCache(";
|
|
|
|
for (int i = cache.firstIndex(); i <= cache.lastIndex(); ++i) {
|
|
|
|
debug << cache[i];
|
|
|
|
if (i != cache.lastIndex())
|
|
|
|
debug << ", ";
|
|
|
|
}
|
|
|
|
debug << ')';
|
2013-12-22 11:33:33 +00:00
|
|
|
debug.setAutoInsertSpaces(oldSetting);
|
|
|
|
return debug.maybeSpace();
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
inline QDebug operator<<(QDebug debug, const QFlags<T> &flags)
|
|
|
|
{
|
2013-12-22 11:33:33 +00:00
|
|
|
const bool oldSetting = debug.autoInsertSpaces();
|
2011-04-27 10:05:43 +00:00
|
|
|
debug.nospace() << "QFlags(";
|
|
|
|
bool needSeparator = false;
|
|
|
|
for (uint i = 0; i < sizeof(T) * 8; ++i) {
|
|
|
|
if (flags.testFlag(T(1 << i))) {
|
|
|
|
if (needSeparator)
|
|
|
|
debug.nospace() << '|';
|
|
|
|
else
|
|
|
|
needSeparator = true;
|
2013-05-08 16:02:02 +00:00
|
|
|
debug.nospace() << "0x" << QByteArray::number(typename QFlags<T>::Int(1) << i, 16).constData();
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
debug << ')';
|
2013-12-22 11:33:33 +00:00
|
|
|
debug.setAutoInsertSpaces(oldSetting);
|
|
|
|
return debug.maybeSpace();
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QT_END_NAMESPACE
|
|
|
|
|
|
|
|
#endif // QDEBUG_H
|