How to use qInstallMessageHandler in an in-Process Axserver
-
I want to direct all debug messages from a Axserver DLL that I have written to a file.
qInstallMessageHandler works for me when it is a QApplication. However, when I use that in the constructor of the Axserver DLL, it works only for the debug messages inside the constructor. The moment I call any other slot defined in the DLL, the DLL crashes. The DLL works very well without the qInstallMessageHandler code.
My Header file has the following.
static void myMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString& msg);My class constructor has the following.
static QTextStream output_ts; void MyClassSDK::myMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg) { switch (type) { case QtDebugMsg: output_ts << QString("Debug: %1").arg(msg) << endl; break; case QtWarningMsg: output_ts << QString("Warning: %1").arg(msg) << endl; break; case QtCriticalMsg: output_ts << QString("Critical: %1").arg(msg) << endl; break; case QtFatalMsg: output_ts << QString("Fatal: %1").arg(msg) << endl; abort(); } } MyClassSDK::MyClassSDK(QObject *parent) { QString logfilePath = "DLLDebugLogFile.txt";/ logfilePath.prepend(default_setting_path); QFile outFile(logfilePath); outFile.open(QIODevice::WriteOnly | QIODevice::Append); output_ts.setDevice(&outFile); qInstallMessageHandler(MyClassSDK::myMessageHandler); qDebug()<<"Test Message 1"; // followed by the rest of the class... // Somewhere another slot defined... void MyClassSDK::DebugTest() { qDebug()<< "Used for debug testing"; }
Here, the "Test Message 1" is output to the log file. However the DLL crashes at the call to DebugTest() function.
Is it even possible to use this in a Axserver especially the Library or In-Process one?
If Yes, then Unable to find any example or clue of how to do it. -
Hi @TheCrowKaka,
I don't know anything about Axserver, but in your
MyClassSDK
constructor, this line:output_ts.setDevice(&outFile);
Is taking a local pointer, and passing to a global object. That is
&outFile
is a pointer to youroutFile
variable, butoutFile
is a local variable, which will be automatically deleted once the constructor finishes. So when the laterqDebug()
runs, theoutput_ts
will try to access the non-longer-validoutFile
pointer.You should either allocate
outFile
on the heap, or provide some other way of maintaining the output stream.Cheers.
-
@Paul-Colby Hello, Thanks for this tip. I just made the QFile outFile also static and that solved my problem. This thing worked. Thanks again.
-
-
@TheCrowKaka said in How to use qInstallMessageHandler in an in-Process Axserver:
static
Static? Why?
Simply make it class member. -
@TheCrowKaka said in How to use qInstallMessageHandler in an in-Process Axserver:
Static works as both output_ts and outFile are required across multiple classes
Sounds like bad design