title: Qt5: How to use qDebug() to log in a file, multi-thread application
body: <p>I started using Qt5 few days ago. I needed a <em>logger</em> for my app and I decided to use <code>qDebug</code>, but it seems it has to be "redirected" in order to have the logs in a file.</p>
  
  <p>I used <code>qInstallMessageHandler</code> to do that and I wrote my own handler as presented below (inspired from other people here).</p>
  
  <p>It seems it works, but as I am not a guru, I have to ask:
  Is it ok to use this in a multi-thread application or not?</p>
  
  <p>Also, if it is ok/safe for using in a <em>multi-thread</em> app, can it be improved somehow?   </p>
  
  <pre><code>void MessageHandler(QtMsgType type, const QMessageLogContext &amp; context, const QString &amp; msg)
  {
      mutex.lock();
  
      QDateTime dateTime(QDateTime::currentDateTime());
  
      QString timeStr(dateTime.toString("dd-MM-yyyy HH:mm:ss:zzz"));
      QString contextString(QString("(%1, %2)").arg(context.file).arg(context.line));
  
      QFile outFile("file.log");
      outFile.open(QIODevice::WriteOnly | QIODevice::Append);
  
      QTextStream stream(&amp;outFile);
      stream &lt;&lt; timeStr &lt;&lt; " " &lt;&lt; contextString &lt;&lt; ": " &lt;&lt; msg &lt;&lt; endl;
  
      mutex.unlock();
  }
  </code></pre>