I have this code:
void myMessageHandler(QtMsgType type, const QMessageLogContext &, const QString & msg)
{
QTime time = QTime::currentTime();
QString formattedTime = time.toString("hh:mm:ss");
QByteArray formattedTimeMsg = formattedTime.toLocal8Bit();
QString txt;
switch (type) {
case QtDebugMsg:
txt = QString("%1: Debug: %2").arg(formattedTime, msg);
break;
case QtWarningMsg:
txt = QString("%1: Warning: %2").arg(formattedTime, msg);
break;
case QtCriticalMsg:
txt = QString("%1: Critical: %2").arg(formattedTime, msg);
break;
case QtFatalMsg:
txt = QString("%1: Fatal: %2").arg(formattedTime, msg);
break;
}
QFile outFile("debug.log");
outFile.open(QIODevice::WriteOnly | QIODevice::Append);
QTextStream ts(&outFile);
ts << txt << endl;
}
which works very well as saving all console logs into the external file debug.log. However I don't know how to set it to format the file as UTF8 because some of my local special characters cant be read properly when open the file. And also how do I make new line after each record? It's all in one line depends on what application I open the log file - notepad won't show me new lines, notepad++ does, wordpad also don't...)
Thank you very much for your help guys!