Commit 3146ed2e authored by Hugo Beauzee-Luyssen's avatar Hugo Beauzee-Luyssen

Added message handler.

Depending on the log message, debug is written or not on the console.
This use Qt's log level. By default, qDebug is not printed, qWarning is.
In every case, messages are written in a log file (if it has been
specified in the config)
parent 46c8161b
......@@ -51,6 +51,9 @@ void VLMCSettingsDefault::loadVLMCDefaults( const QString& part )
settingsMan->setValue( part, "AutomaticBackup", false );
settingsMan->setValue( part, "AutomaticBackupInterval", 5 );
settingsMan->setValue( "private", "LogLevel", QtWarningMsg );
settingsMan->setValue( "private", "LogFile", "vlmc.log" );
return ;
}
......
......@@ -58,6 +58,7 @@
#include "KeyboardShortcut.h"
#include "SettingValue.h"
#include "SettingsManager.h"
#include "VlmcDebug.h"
MainWindow::MainWindow( QWidget *parent ) :
QMainWindow( parent ), m_renderer( NULL )
......@@ -72,6 +73,8 @@ MainWindow::MainWindow( QWidget *parent ) :
VLMCSettingsDefault::load( "default" );
VLMCSettingsDefault::load( "VLMC" );
VLMCSettingsDefault::loadKeyboardShortcutDefaults();
//We only install message handler here cause it uses configuration.
qInstallMsgHandler( VlmcDebug::vlmcMessageHandler );
// GUI
DockWidgetManager::instance( this )->setMainWindow( this );
......
HEADERS += Pool.hpp \
QSingleton.hpp \
Singleton.hpp \
Toggleable.hpp \
WaitCondition.hpp
HEADERS += Pool.hpp \
QSingleton.hpp \
Singleton.hpp \
Toggleable.hpp \
WaitCondition.hpp \
VlmcDebug.h
SOURCES += VlmcDebug.cpp
/*****************************************************************************
* VlmcDebug.cpp: Debug tools for VLMC
*****************************************************************************
* Copyright (C) 2008-2009 the VLMC team
*
* Authors: Hugo Beauzee-Luyssen <hugo@vlmc.org>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
#include <QCoreApplication>
#include "SettingsManager.h"
#include "VlmcDebug.h"
VlmcDebug::VlmcDebug() : m_logFile( NULL )
{
const SettingValue* setVal = SettingsManager::getInstance()->getValue( "private", "LogFile" );
connect( setVal, SIGNAL( changed( QVariant ) ), this, SLOT( logFileChanged( const QVariant& ) ) );
QObject::connect( qApp,
SIGNAL( aboutToQuit() ),
this,
SLOT( deleteLater() ) );
QString logFile = setVal->get().toString();
if ( logFile.isEmpty() == false )
{
m_logFile = new QFile( logFile );
m_logFile->open( QFile::WriteOnly | QFile::Truncate );
}
}
VlmcDebug::~VlmcDebug()
{
delete m_logFile;
}
void VlmcDebug::logFileChanged( const QVariant& logFileV )
{
QString logFile = logFileV.toString();
if ( logFile.isEmpty() == true )
{
m_logFile->close();
return ;
}
if ( logFile == m_logFile->fileName() )
{
fprintf( stderr, "Trying to use the same log file. Nothing will be done" );
return ;
}
m_logFile->close();
m_logFile->setFileName( logFile );
m_logFile->open( QFile::Append | QFile::Truncate );
}
void VlmcDebug::vlmcMessageHandler( QtMsgType type, const char* msg )
{
if ( VlmcDebug::getInstance()->m_logFile != NULL )
{
VlmcDebug::getInstance()->m_logFile->write( msg );
VlmcDebug::getInstance()->m_logFile->write( "\n" );
}
if ( type != QtFatalMsg
&& type < SettingsManager::getInstance()->getValue( "private", "LogLevel" )->get().toInt() )
return ;
switch ( type )
{
case QtDebugMsg:
fprintf(stderr, "%s\n", msg);
break;
case QtWarningMsg:
fprintf(stderr, "%s\n", msg);
break;
case QtCriticalMsg:
fprintf(stderr, "%s\n", msg);
break;
case QtFatalMsg:
fprintf(stderr, "%s\n", msg);
abort();
}
}
/*****************************************************************************
* VlmcDebug.h: Debug tools for VLMC
*****************************************************************************
* Copyright (C) 2008-2009 the VLMC team
*
* Authors: Hugo Beauzee-Luyssen <hugo@vlmc.org>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
#ifndef VLMCDEBUG_H
#define VLMCDEBUG_H
#include <QObject>
#include <QDebug>
#include <QFile>
#include "Singleton.hpp"
/**
* \warning Do not use qDebug() qWarning() etc... from here, unless you know exactly what you're doing
* Chances are very high that you end up with a stack overflow !!
*/
class VlmcDebug : public QObject, public Singleton<VlmcDebug>
{
Q_OBJECT
public:
static void vlmcMessageHandler( QtMsgType type, const char* msg );
private:
VlmcDebug();
virtual ~VlmcDebug();
QFile* m_logFile;
private slots:
void logFileChanged( const QVariant& logFile );
friend class Singleton<VlmcDebug>;
};
#endif // VLMCDEBUG_H
......@@ -15,7 +15,6 @@ QT += gui \
network \
svg \
xml
HEADERS += vlmc.h
SOURCES += main.cpp
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment