QDebug not showing __FILE __, __ LINE__

According to qlogging.h

#define qDebug QMessageLogger(__FILE__, __LINE__, Q_FUNC_INFO).debug

but when I use it, the file name, line, function is not displayed.

qDebug()<< "abc"; // only show abc;
qDebug()<< "";    // show nothing;

I searched for a while, it seems I did not have such a problem as described above.

I am using ubuntu14.04, g ++ version 4.8.2, qt5.3 build from git.

+5
source share
8 answers

If you dig out the Qt history, you can find out that __FILE__they __FUNCTION__are logged only in debug builds from October 1, 2014. The git commit hash is d78fb442d750b33afe2e41f31588ec94cf4023ad. Commit message:

Logging: Disable tracking of source debug information for release versions

, , , , . , & "". .

+8

. Qt 5.0.

, - "% {if-category}% {category}:% {endif}% {message}". , , .

% cat logtest.pro

TEMPLATE = app
TARGET = logtest
mac:CONFIG-=app_bundle
SOURCES += main.cpp

% cat main.cpp

#include <QCoreApplication>
#include <QDebug>

int main(int argc, char *argv[])
{
    qSetMessagePattern("%{file}(%{line}): %{message}");
    QCoreApplication a(argc, argv);
    qDebug() << "my output";
    return 0;
}

% qmake &&

%./logtest

main.cpp(8): my output

QT_MESSAGE_PATTERN qSetMessagePattern().

. . http://qt-project.org/doc/qt-5/qtglobal.html#qSetMessagePattern

+15

, QMessageLogContext , qInstallMessageHandler. category version, . .

#include <QDebug>
#include <QString>
#include <QDateTime>
#include <iostream>

void verboseMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
    static const char* typeStr[] = {"[   Debug]", "[ Warning]", "[Critical]", "[   Fatal]" };

    if(type <= QtFatalMsg)
    {
        QByteArray localMsg = msg.toLocal8Bit();
        QString contextString(QStringLiteral("(%1, %2, %3)")
                              .arg(context.file)
                              .arg(context.function)
                              .arg(context.line));

        QString timeStr(QDateTime::currentDateTime().toString("dd-MM-yy HH:mm:ss:zzz"));

        std::cerr << timeStr.toLocal8Bit().constData() << " - " 
                  << typeStr[type] << " "
                  << contextString.toLocal8Bit().constData() << " " 
                  << localMsg.constData() << std::endl;

        if(type == QtFatalMsg)
        {
            abort();
        }
    }
}

int main()
{
    //Use default handler
    qDebug() << "default handler";
    qWarning() << "default handler";
    qCritical() << "default handler";

    //Install verbose handler
    qInstallMessageHandler(verboseMessageHandler);

    qDebug() << "verbose handler";
    qWarning() << "verbose handler";
    qCritical() << "verbose handler";

    //Restore default handler
    qInstallMessageHandler(0);

    qDebug() << "default handler";
    qWarning() << "default handler";
    qCritical() << "default handler";

    return 0;
}
+4

, :

#define qDebug() QMessageLogger(__FILE__, __LINE__, Q_FUNC_INFO).debug()

qDebug() << "abc";

#define qDebug QMessageLogger(__FILE__, __LINE__, Q_FUNC_INFO).debug()

:

qDebug << "abc";
+1

++ __LINE__ __FILE__. , __PRETTY_FUNCTION__, __FUNCTION__, __func__ SO. GCC, __PRETTY_FUNCTION__, , . , .

, :

#include <QApplication>
#include <QDebug>
#include <iostream>

// Qt-way
#define MyDBG (qDebug()<<__FILE__<<__LINE__<<__PRETTY_FUNCTION__)
// GCC
#define MyStdDBG (std::cout<< __FILE__<<":"<<__LINE__<<" in "<<__PRETTY_FUNCTION__<<std::endl)

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    // Qt-way
    MyDBG;
    MyDBG << "Something happened!";

    // GCC
    MyStdDBG;

    return a.exec();
}

:

../path/main.cpp 14 int main(int, char**)
../path/main.cpp 15 int main(int, char**) Something happened!
../path/main.cpp:18 in int main(int, char**)

UPD: ++ - .

+1

qDebug() QMessageLogger(). stderr. , qInstallMessageHandler() ,

Edit:

, . Qt4 , Qt5+. , , . .

+1

Qt, , Qt Qt .

Qt 5.4, "qlogging.cpp":

QMessageLogger is used to generate messages for the Qt logging framework. Usually one uses
it through qDebug(), qWarning(), qCritical, or qFatal() functions,
which are actually macros: For example qDebug() expands to
QMessageLogger(__FILE__, __LINE__, Q_FUNC_INFO).debug()
for debug builds, and QMessageLogger(0, 0, 0).debug() for release builds.

, , , , Qt .

, Qt , , .

+1
0
source

Source: https://habr.com/ru/post/1664974/


All Articles