Skip to content

Instantly share code, notes, and snippets.

@OlivierLDff
Created July 1, 2020 13:45
Show Gist options
  • Save OlivierLDff/a820e07bb13e9a20c3c3ae5343f21b64 to your computer and use it in GitHub Desktop.
Save OlivierLDff/a820e07bb13e9a20c3c3ae5343f21b64 to your computer and use it in GitHub Desktop.
void loggingMessageHandler(QtMsgType type, const QMessageLogContext & context, const QString & msg)
{
QString timeStr(QDateTime::currentDateTime().toString("dd-MM-yy HH:mm:ss:zzz"));
QString contextString(QString("[%1 %2]").arg(context.file).arg(context.line));
mutex.lock();
QString level;
if(logFile.isOpen())
{
switch (type) {
case QtInfoMsg: level = QString("INF"); break;
case QtDebugMsg: level = QString("DEB"); break;
case QtWarningMsg: level = QString("WAR"); break;
case QtCriticalMsg: level = QString("CRT"); break;
case QtFatalMsg: level = QString("FTL"); break;
}
QTextStream stream(&logFile);
stream << timeStr << " " << contextString << "\t" << level << "\t" << msg << endl;
stream.flush();
}
#if defined(Q_OS_WIN)
OutputDebugString(reinterpret_cast<const wchar_t *>(level.append(' ' + msg + '\n').utf16()));
#elif defined(Q_OS_ANDROID)
android_default_message_handler(type, context, level.append(" " + msg));
#else // MACX || IOS || LINUX
fprintf(stderr, "%s\n", level.append(" " + msg).toLocal8Bit().constData());
#endif
mutex.unlock();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment