Debug messages to GUI terminal

This commit is contained in:
santi 2014-07-10 17:37:52 +02:00
parent cccd987bdd
commit a6909f8c16
7 changed files with 131 additions and 80 deletions

View file

@ -19,36 +19,22 @@
*/
#include <QApplication>
#include <QMutex>
#include <QMutexLocker>
#include "libremediaserver-audio.h"
// Handler for pipe the stderr to a log file
// Handler for pipe the stderr to a log file and texEdit
bool initMessageHandler = false;
QFile outFile;
QMutex mutex;
//QMutexLocker mutexLocker(mutex);
void MessageHandler(QtMsgType type, const QMessageLogContext &logcontext, const QString &msg)
void MessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
Q_UNUSED(logcontext);
QString txt;
switch (type) {
case QtDebugMsg:
txt.append("Debug: ");
txt.append(msg);
break;
case QtWarningMsg:
txt.append("Warning: ");
txt.append(msg);
break;
case QtCriticalMsg:
txt.append("Critical: ");
txt.append(msg);
break;
case QtFatalMsg:
txt.append("Fatal: ");
txt.append(msg);
abort();
}
Q_UNUSED(context);
// mutex.lock();
// Create the log dir and log file
if (!initMessageHandler) {
/* if (!initMessageHandler) {
QDir dir;
if (!dir.exists("log")) {
if (!dir.mkdir("log")) {
@ -69,13 +55,74 @@ void MessageHandler(QtMsgType type, const QMessageLogContext &logcontext, const
}
initMessageHandler = true;
}
QTextStream ts(&outFile);
ts << txt << endl;
QTextStream ts(&outFile);*/
if (libreMediaServerAudio::textEdit == 0)
{
QByteArray localMsg = msg.toLocal8Bit();
switch (type) {
case QtDebugMsg:
fprintf(stderr, "Debug: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function);
break;
case QtWarningMsg:
fprintf(stderr, "Warning: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function);
break;
case QtCriticalMsg:
fprintf(stderr, "Critical: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function);
break;
case QtFatalMsg:
fprintf(stderr, "Fatal: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function);
abort();
}
}
else
{
QString txt;
switch (type) {
case QtDebugMsg:
txt.append("Debug: ");
txt.append(context.file);
txt.append(context.line);
txt.append(context.function);
txt.append(msg);
// libreMediaServerAudio::textEdit->append(msg);, context.file, context.line, context.function);
libreMediaServerAudio::textEdit->append(txt);
break;
case QtWarningMsg:
// txt.append("Warning: ");
// txt.append(msg);
libreMediaServerAudio::textEdit->append(msg);
break;
case QtCriticalMsg:
// txt.append("Critical: ");
// txt.append(msg);
libreMediaServerAudio::textEdit->append(msg);
break;
case QtFatalMsg:
// txt.append("Fatal: ");
// txt.append(msg);
libreMediaServerAudio::textEdit->append(msg);
// ts << txt << endl;
abort();
}
// outFile.write(txt.toLatin1().constData(), txt.size());
// ts << txt << endl;
// libreMediaServerAudio::textEdit->append(txt);
}
// mutex.unlock();
}
int main(int argc, char *argv[])
{
// ToDo: discriminar niveles de log y log a fichero segúna argumentos
/*
if (args.contains("-log"))
{
qInstallMessageHandler(MessageHandler);
}*/
QApplication app(argc, argv);
qInstallMessageHandler(MessageHandler);
QStringList args = app.arguments();
// parse the command line
if (args.size() > 1)
@ -98,10 +145,6 @@ int main(int argc, char *argv[])
qDebug() << "-h this help";
return 0;
}
if (args.contains("-log"))
{
qInstallMessageHandler(MessageHandler);
}
}
libreMediaServerAudio libreMediaServerAudio(args);
libreMediaServerAudio.show();