Qt 5.4 - Sending qDebug() to a file (aka logging) - (SOLVED)
-
Also I would suggest to open file and keep it opened once instead of opening it for every string output, especially cause you current directory can be changed and you will and up with parts of the log in different locations.
-
By the way have you tried:
#include <QDebug>instead of
#include <QtCore/QDebug>
-
Alex_malyu
The only thing missing from the compiler message is the line number - which is the qDebug() statement.
main.cpp:49: undefined reference to `QDebug::~QDebug()'
I am using Qt 5.4 so the recommendation from the docs is to be specific by using <QtCore/QDebug> rather than the more general <QDebug> from earlier versions.
I do plan to open once and write many, but I have to get past this error first.
-
You might have problem with old libraries picked up.
At least people with similar issues solved it by cleaning and/or rebuilding.Check below link for example:
"https://bugreports.qt.io/browse/QTBUG-40458":https://bugreports.qt.io/browse/QTBUG-40458 -
Alex_Malyu
Well I rather doubt that is the case since Qt 5.4 is the only version I am referencing in the tool chain. However, I will check just to be sure.
BTW without the redirection, QDebug() works fine.
-
As suspected...no diff after a thorough clean and re-compile attempt.
-
i just grabbed your code in main.cpp and it worked.. output in log file:
@$ cat Debug.log
{Debug} Loading from Main()
@so it must be environmental
-
[quote author="ad5xj" date="1423516765"]As can be seen in the main.cpp there is a include for QDebug.
Actually this error occurs for any use of qDebug() anywhere.
Any help...what am I doing wrong?[/quote]
You have to #include <QDebug> (or, #include <QtCore/QDebug>) in every file you want to use qDebug() from.
-
The include does not clear the error.
An environmental error as suggested would not allow QDebug() to work normally as it does.
The only problem I have is the redirection as stated earlier. -
I think I have come up with at least a minimal solution:
main.cpp
@
#include <stdio.h>
#include <stdlib.h>#include <QtCore/QDebug>
#include <QtCore/QtGlobal>
#include <QtCore/QFile>
#include <QtCore/QDateTime>
#include <QtCore/QString>
#include <QtCore/QMessageLogger>
#include <QtCore/QMessageLogContext>
#include <QtCore/QtMessageHandler>
#include <QtWidgets/QApplication>#include "myGUI.hpp"
void debugMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
//Q_UNUSED(context);
QString timestr;
timestr = QDateTime::currentDateTime().toString("ddd d MMMM yyyy hh:mm:ss ");
QByteArray txt;
txt += timestr;
// Switch structure left to be converted to write into the file in the future
switch ( type )
{
case QtDebugMsg:
txt += " Debug ";
txt += context.file;
txt += ":";
txt += QString("%1").arg(context.line);
txt += QString(" - %1").arg(msg);
break;
case QtWarningMsg:
txt += context.function;
txt += ":";
txt += context.line;
txt += QString(" - Warning: \t %1").arg(msg);
break;
case QtCriticalMsg:
txt += context.function;
txt += ":";
txt += context.line;
txt += QString(" - Critical: \t %1").arg(msg);
break;
case QtFatalMsg:
txt += context.function;
txt += ":";
txt += context.line;
txt += QString(" - Fatal: \t %1").arg(msg);
abort(); // deliberately core dump
}
QFile outFile("Debug.log");
outFile.open(QIODevice::WriteOnly | QIODevice::Append);QTextStream textStream(&outFile); textStream << txt << endl;
}
int main(int argc, char *argv[])
{
qInstallMessageHandler(debugMessageOutput);QApplication a(argc, argv); QMessageLogger("MAIN",58,"main").debug("Loading from MAIN"); MyGui g; g.show(); return a.exec();
}
@then in any module you want to log from...
@include <QtCore/QDebug>
include <QtCore/QMessageLogger>
...
void myFunc()
{
#ifdef DEBUG_MYDEF
QString errmsg
errmsg += "This is my message - ";
errmsg += QString("%1").arg(errno);QMessageLogger("MyModule",int mylineno,"funcname").debug(errmsg.toLocal8Bit());
#endif
}
@It creates a Debug.log file with time stamped entries.
I have conditioned the logger with DEFINES I created but there are a number of other ways this could work just as well.