From c90eb1cca68ab89fd0b8a4d982089d356ff7decd Mon Sep 17 00:00:00 2001 From: Klaas Freitag Date: Fri, 15 Jun 2012 13:04:23 +0200 Subject: [PATCH] Log to file added, with flush option --- src/mirall/application.cpp | 13 ++++++++++++ src/mirall/logbrowser.cpp | 42 +++++++++++++++++++++++++++++++++----- src/mirall/logbrowser.h | 14 +++++++++++-- 3 files changed, 62 insertions(+), 7 deletions(-) diff --git a/src/mirall/application.cpp b/src/mirall/application.cpp index e968906eaa..cb839f8543 100644 --- a/src/mirall/application.cpp +++ b/src/mirall/application.cpp @@ -341,6 +341,19 @@ void Application::setupLogBrowser() 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()) .arg( QLocale::system().name() ) .arg(_theme->version()); diff --git a/src/mirall/logbrowser.cpp b/src/mirall/logbrowser.cpp index fb5f4ec8d4..effc8364c6 100644 --- a/src/mirall/logbrowser.cpp +++ b/src/mirall/logbrowser.cpp @@ -105,7 +105,7 @@ void Logger::setEnabled( bool state ) // ============================================================================== LogWidget::LogWidget(QWidget *parent) - :QTextEdit(parent) + :QTextBrowser(parent) { setReadOnly( true ); setLineWrapMode( QTextEdit::NoWrap ); @@ -124,7 +124,9 @@ LogWidget::LogWidget(QWidget *parent) LogBrowser::LogBrowser(QWidget *parent) : QDialog(parent), - _logWidget( new LogWidget(parent) ) + _logWidget( new LogWidget(parent) ), + _logstream(0), + _doFileFlush(false) { setWindowTitle(tr("Log Output")); setMinimumWidth(600); @@ -177,6 +179,13 @@ LogBrowser::LogBrowser(QWidget *parent) : connect(Logger::instance(), SIGNAL(newLog(QString)),this,SLOT(slotNewLog(QString)), Qt::QueuedConnection); } +LogBrowser::~LogBrowser() +{ + if( _logstream ) { + _logFile.close(); + } +} + void LogBrowser::show() { QDialog::show(); @@ -191,7 +200,31 @@ void LogBrowser::close() void LogBrowser::slotNewLog( const QString& msg ) { - _logWidget->append( msg ); + if( _logWidget->isVisible() ) { + _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("File '%1'
cannot be opened for writing.

" + "The log output can not be saved!
")) + .arg(name)); + return; + } + _doFileFlush = flush; + _logstream = new QTextStream( &_logFile ); } void LogBrowser::slotFind() @@ -236,10 +269,9 @@ void LogBrowser::slotSave() if( ! saveFile.isEmpty() ) { QFile file(saveFile); - if (file.open(QIODevice::ReadWrite)) { + if (file.open(QIODevice::WriteOnly)) { QTextStream stream(&file); stream << _logWidget->toPlainText(); - file.flush(); file.close(); } else { QMessageBox::critical(this, tr("Error"), tr("Could not write to log file ")+ saveFile); diff --git a/src/mirall/logbrowser.h b/src/mirall/logbrowser.h index 8239c603c1..1ef8902364 100644 --- a/src/mirall/logbrowser.h +++ b/src/mirall/logbrowser.h @@ -15,7 +15,9 @@ #ifndef LOGBROWSER_H #define LOGBROWSER_H -#include +#include +#include +#include #include #include #include @@ -65,7 +67,7 @@ protected: static Logger* _instance; }; -class LogWidget : public QTextEdit +class LogWidget : public QTextBrowser { Q_OBJECT public: @@ -80,6 +82,10 @@ class LogBrowser : public QDialog Q_OBJECT public: explicit LogBrowser(QWidget *parent = 0); + ~LogBrowser(); + + void setLogFile(const QString& , bool ); + signals: public slots: @@ -97,6 +103,10 @@ private: QLineEdit *_findTermEdit; QPushButton *_saveBtn; QLabel *_statusLabel; + + QFile _logFile; + bool _doFileFlush; + QTextStream *_logstream; }; } // namespace