[SailfishDevel] how to get qml debug output to file

Andrey Kozhevnikov coderusinbox at gmail.com
Tue Feb 4 21:14:10 UTC 2014


This is messages handler i'm using in my projects:

void writeLog(const QString &type, const QMessageLogContext &context, 
const QString &message)
{
     QString time = QDateTime::currentDateTime().toString("hh:mm:ss");
     QFile file(LOG_FILE);
     if (file.open(QIODevice::Append | QIODevice::Text))
     {
         QTextStream out(&file);
         out << type << time << "] ";
         out << context.file << ":" << context.line << " " << 
context.function << ": ";
         out << message << '\n';
         file.close();
     }
     //QTextStream(stdout) << context.file << ":" << context.line << " " 
<< context.function << "\n";
     QTextStream(stdout) << type << time <<  "] " << message << '\n';
}

void messageHandler(QtMsgType type, const QMessageLogContext &context, 
const QString &msg)
{
     switch (type) {
     case QtDebugMsg:
         writeLog("[DEBUG ", context, msg);
         break;
     case QtWarningMsg:
         writeLog("[WARNING ", context, msg);
         break;
     case QtCriticalMsg:
         writeLog("[CRITICAL ", context, msg);
         break;
     case QtFatalMsg:
         writeLog("[FATAL ", context, msg);
         abort();
     }
}

On 05.02.2014 03:08, Tero Siironen wrote:
> Hi,
>
> I’ve been using qInstallMsgHandler earlier with Harmattan to get debug prints from c++ and qml to a file. Now that I’m trying to do the same with Sailfish and Qt5 using qInstallMessageHandler, I get only c++ prints to file, qml prints are shown in console or qtcreator. Debug printer is installed as first line in the main:
>
>   Q_DECL_EXPORT int main(int argc, char * argv[])
> {
>          qInstallMessageHandler(DebugPrinter);
>>
> and the debug printer itself is like this:
>
>   void DebugPrinter(QtMsgType type, const QMessageLogContext &context, const QString &msg)
> {
>      QString txt;
>      switch (type) {
>      case QtDebugMsg:
>              txt = QString("Debug: %1").arg(msg);
>              break;
>      case QtWarningMsg:
>              txt = QString("Warning: %1").arg(msg);
>      break;
>      case QtCriticalMsg:
>              txt = QString("Critical: %1").arg(msg);
>      break;
>      case QtFatalMsg:
>              txt = QString("Fatal: %1").arg(msg);
>              abort();
>      }
>
>      QDir homePath = QDir::homePath();
>      QString dbFilePath = homePath.absolutePath();
>      dbFilePath.append( "/" + DEBUGLOG );
>      QFile outFile( dbFilePath );
>      outFile.open(QIODevice::WriteOnly | QIODevice::Append);
>      QTextStream ts(&outFile);
>      ts << txt << endl;
> }
>
> So what am I doing wrong here?
>
>
>> Tero
> _______________________________________________
> SailfishOS.org Devel mailing list



More information about the Devel mailing list