Log to file added, with flush option

This commit is contained in:
Klaas Freitag 2012-06-15 13:04:23 +02:00
parent 4d1fed3d00
commit c90eb1cca6
3 changed files with 62 additions and 7 deletions

View file

@ -341,6 +341,19 @@ void Application::setupLogBrowser()
slotOpenLogBrowser(); slotOpenLogBrowser();
} }
// check for command line option for a log file.
int lf = arguments().indexOf("--logfile");
if( lf > -1 && lf+1 < arguments().count() ) {
QString logfile = arguments().at( lf+1 );
bool flush = false;
if( arguments().contains("--logflush")) flush = true;
qDebug() << "Logging into logfile: " << logfile << " with flush " << flush;
_logBrowser->setLogFile( logfile, flush );
}
qDebug() << QString( "################## %1 %2 %3 ").arg(_theme->appName()) qDebug() << QString( "################## %1 %2 %3 ").arg(_theme->appName())
.arg( QLocale::system().name() ) .arg( QLocale::system().name() )
.arg(_theme->version()); .arg(_theme->version());

View file

@ -105,7 +105,7 @@ void Logger::setEnabled( bool state )
// ============================================================================== // ==============================================================================
LogWidget::LogWidget(QWidget *parent) LogWidget::LogWidget(QWidget *parent)
:QTextEdit(parent) :QTextBrowser(parent)
{ {
setReadOnly( true ); setReadOnly( true );
setLineWrapMode( QTextEdit::NoWrap ); setLineWrapMode( QTextEdit::NoWrap );
@ -124,7 +124,9 @@ LogWidget::LogWidget(QWidget *parent)
LogBrowser::LogBrowser(QWidget *parent) : LogBrowser::LogBrowser(QWidget *parent) :
QDialog(parent), QDialog(parent),
_logWidget( new LogWidget(parent) ) _logWidget( new LogWidget(parent) ),
_logstream(0),
_doFileFlush(false)
{ {
setWindowTitle(tr("Log Output")); setWindowTitle(tr("Log Output"));
setMinimumWidth(600); setMinimumWidth(600);
@ -177,6 +179,13 @@ LogBrowser::LogBrowser(QWidget *parent) :
connect(Logger::instance(), SIGNAL(newLog(QString)),this,SLOT(slotNewLog(QString)), Qt::QueuedConnection); connect(Logger::instance(), SIGNAL(newLog(QString)),this,SLOT(slotNewLog(QString)), Qt::QueuedConnection);
} }
LogBrowser::~LogBrowser()
{
if( _logstream ) {
_logFile.close();
}
}
void LogBrowser::show() void LogBrowser::show()
{ {
QDialog::show(); QDialog::show();
@ -191,9 +200,33 @@ void LogBrowser::close()
void LogBrowser::slotNewLog( const QString& msg ) void LogBrowser::slotNewLog( const QString& msg )
{ {
if( _logWidget->isVisible() ) {
_logWidget->append( msg ); _logWidget->append( msg );
} }
if( _logstream ) {
(*_logstream) << msg << endl;
if( _doFileFlush ) _logstream->flush();
}
}
void LogBrowser::setLogFile( const QString & name, bool flush )
{
_logFile.setFileName( name );
if(!_logFile.open(QIODevice::WriteOnly)) {
QMessageBox::warning(
this,
tr("Error"),
QString(tr("<nobr>File '%1'<br/>cannot be opened for writing.<br/><br/>"
"The log output can <b>not</b> be saved!</nobr>"))
.arg(name));
return;
}
_doFileFlush = flush;
_logstream = new QTextStream( &_logFile );
}
void LogBrowser::slotFind() void LogBrowser::slotFind()
{ {
QString searchText = _findTermEdit->text(); QString searchText = _findTermEdit->text();
@ -236,10 +269,9 @@ void LogBrowser::slotSave()
if( ! saveFile.isEmpty() ) { if( ! saveFile.isEmpty() ) {
QFile file(saveFile); QFile file(saveFile);
if (file.open(QIODevice::ReadWrite)) { if (file.open(QIODevice::WriteOnly)) {
QTextStream stream(&file); QTextStream stream(&file);
stream << _logWidget->toPlainText(); stream << _logWidget->toPlainText();
file.flush();
file.close(); file.close();
} else { } else {
QMessageBox::critical(this, tr("Error"), tr("Could not write to log file ")+ saveFile); QMessageBox::critical(this, tr("Error"), tr("Could not write to log file ")+ saveFile);

View file

@ -15,7 +15,9 @@
#ifndef LOGBROWSER_H #ifndef LOGBROWSER_H
#define LOGBROWSER_H #define LOGBROWSER_H
#include <QTextEdit> #include <QTextBrowser>
#include <QTextStream>
#include <QFile>
#include <QObject> #include <QObject>
#include <QList> #include <QList>
#include <QDateTime> #include <QDateTime>
@ -65,7 +67,7 @@ protected:
static Logger* _instance; static Logger* _instance;
}; };
class LogWidget : public QTextEdit class LogWidget : public QTextBrowser
{ {
Q_OBJECT Q_OBJECT
public: public:
@ -80,6 +82,10 @@ class LogBrowser : public QDialog
Q_OBJECT Q_OBJECT
public: public:
explicit LogBrowser(QWidget *parent = 0); explicit LogBrowser(QWidget *parent = 0);
~LogBrowser();
void setLogFile(const QString& , bool );
signals: signals:
public slots: public slots:
@ -97,6 +103,10 @@ private:
QLineEdit *_findTermEdit; QLineEdit *_findTermEdit;
QPushButton *_saveBtn; QPushButton *_saveBtn;
QLabel *_statusLabel; QLabel *_statusLabel;
QFile _logFile;
bool _doFileFlush;
QTextStream *_logstream;
}; };
} // namespace } // namespace